Skip to content

Instantly share code, notes, and snippets.

View manthrax's full-sized avatar

Michael Schlachter manthrax

View GitHub Profile
class vec3 {
set(x=0, y=0, z=0) {
this.x = x;
this.y = y;
this.z = z;
return this;
}
copy(v) {
return this.set(v.x, v.y, v.z)
}
@manthrax
manthrax / wichmann_hill_random.js
Created June 15, 2020 08:08
js wichmann hill rng.. untested.. drop in replacement for Math.random.. uses zelda ww seed by default
let wichmann_hill_rng = (s1=100,s2=100,s3=100)=>()=>((s1 = (171 * s1) % 30269) / 30269 + (s2 = (172 * s1) % 30307) / 30307 + (s3 = (170 * s1) % 30323) / 30323) % 1
let rng = wichmann_hill_rng()
for (let i = 0; i < 1000; i++) console.log(rng())
/******** CANVAS RECORDER
Invoke with:
import CanvasRecorder from "./canvasrecorder.js"
CanvasRecorder( yourCanvas )
*/
class Easing {
}
Easing.fns = function() {
var x = Math.pow
, C = Math.sqrt
, T = Math.sin
, q = Math.cos
, B = Math.PI
@manthrax
manthrax / gist:8e9d71ed5efe2f865846d3516d029fc0
Created February 9, 2021 20:07
Save/Load camera/controls target
/**
* Use the Web Storage API to save camera position and target between page refreshes.
*
* @param {Object} options
* @param {*} options.camera - The camera you want to store the position of.
* @param {*} [options.controls] - A controls object with a `.target` property.
* @param {String} [options.name="main"] - An optional label. Useful if you want to store multiple cameras.
* @param {Boolean} [options.session=true] - Indicates if you want to use local or session storage.
* See https://developer.mozilla.org/en-US/docs/Web/API/Storage
*/
@manthrax
manthrax / gist:3724dac17379782794bd0e602637d7c5
Last active March 16, 2021 19:52
InstanceGroup abstraction for THREEJS.
import*as THREE from "https://threejs.org/build/three.module.js"
class InstanceCache {
constructor(geometry, material, startingCount) {
const mesh = (this.mesh = new THREE.InstancedMesh(
geometry.clone(),
material.clone(),
startingCount
));
mesh.userData.max = startingCount;
// **** This is for testing each number in the 32 bit uint random state individually. ****
//#define BIT_BY_BIT_DEBUG
#define saturate(a) clamp(a, 0.0, 1.0)
// ---- Random functions use one 32 bit state var to change things up ----
// This is the single state variable for the random number generator.
uint randomState = 4056649889u;
// 0xffffff is biggest 2^n-1 that 32 bit float does exactly.
// Check with Math.fround(0xffffff) in javascript.
@manthrax
manthrax / gist:ce3639911ddeafd173dd1a03b7ecf749
Created May 26, 2021 14:03
Load json model in point/normal/uv format and export as .GLB
import * as THREE from 'https:/threejs.org/build/three.module.js'
import {GLTFExporter} from 'https:/threejs.org/examples/jsm/exporters/GLTFExporter.js'
fetch('./map.json').then(data=>data.json().then((js)=>{
console.log(js)
let g = new THREE.PlaneGeometry(1,1)
g.attributes.normal.array=new Float32Array(js.normal.length)
g.attributes.normal.array.set(js.normal)
@manthrax
manthrax / gist:ac96ac0a41ddd012204d0181bbbded81
Created November 13, 2021 13:48
Take a screenshot of canvas and download as jpg
let takeScreenShot=()=>{
let d=document;
let e = d.createElement("a");
e.setAttribute("href", renderer.domElement.toDataURL("image/jpeg"));
e.setAttribute("download", "Screenshot.jpeg");
e.style.display = "none";
d.body.appendChild(e);
d.click();
d.body.removeChild(e);
@manthrax
manthrax / gist:aa0012cd870421465a4902d0006e88b4
Created November 29, 2021 12:58
threejs update horizontal fov projectionmatrix
const DEG2RAD = Math.PI / 180;
let updateProjectionMatrixHorizontalFOV = (camera)=>{
const near = camera.near;
/*
let top = near * Math.tan( DEG2RAD * 0.5 * camera.fov ) / camera.zoom;
let height = 2 * top;
let width = camera.aspect * height;