Skip to content

Instantly share code, notes, and snippets.

View manthrax's full-sized avatar

Michael Schlachter manthrax

View GitHub Profile
@manthrax
manthrax / gist:df155eb9a63c84413f165582cc80bddf
Created March 4, 2024 07:25
Warp 3d plane to 2d screen space GLSL:
uniform sampler2D uTexture;
uniform float uAspect; //Aspect ratio of the screen
uniform float uProgress;
...
at the end of the vertex shader:
vec4 worldPosition = projectionMatrix * modelViewMatrix * vec4(pos, 1.);
vec4 screenPosition = vec4((uv-.5)*2.,0.,1.);
ivec2 texDim = textureSize(uTexture,0);
@manthrax
manthrax / gist:c2cc117b98c00f539a054699b8eb14e4
Created August 8, 2023 08:07
Combine skinnedmeshes in threejs
import * as SkeletonUtils from 'threeModules/utils/SkeletonUtils.js';
import * as BufferGeometryUtils from 'threeModules/utils/BufferGeometryUtils.js';
function CrushAnimatedObject(root){
//Make a clone of the root.. to work with..
let ud = root.userData;
root.userData={}
let binsert = (items,item)=>{
let low = 0;
let high = items.length;
while (low < high) {
const mid = (low + high) >> 1;
((item.cost - items[mid].cost) > 0) ? (high = mid) : (low = mid + 1);
}
items.splice(low, 0, item);
}
uint32 EncodeMorton2(uint32 x, uint32 y)
{
return (Part1By1(y) << 1) + Part1By1(x);
}
uint32 EncodeMorton3(uint32 x, uint32 y, uint32 z)
{
return (Part1By2(z) << 2) + (Part1By2(y) << 1) + Part1By2(x);
}
@manthrax
manthrax / gist:96f5edadabe2da3ec39d94e807eaf096
Created November 29, 2021 15:42
Uint8Array to hex string.. and back... and some tests....
let b2h = []
for(let i=0;i<256;i++)b2h[i]=i.toString(16).padStart(2,"0")
let arrayToHex=(a)=>{
let hex = new Array(a.length)
for(let i=0,l=a.length;i<l;i++)hex[i]=b2h[a[i]]
return hex.join('')
}
let h2b = {}
for(let i=0;i<256;i++)h2b[i.toString(16).padStart(2,"0")]=i;
@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;
@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: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)
// **** 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: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;