Three Blocks

Skinned fox / source

Open preview
webgpu_gaussiansplat_skinned/foxSplats.jsPlain text ↗
import { Float32BufferAttribute, MathUtils, Matrix3, Quaternion, Vector2, Vector3 } from 'three/webgpu';
import { MeshSurfaceSampler } from 'three/addons/math/MeshSurfaceSampler.js';

// The source rig has three skeletal clips. Add an independent ear fold to show
// how SplatSkin combines a shape key with any of those clips.
export function addEarFold( mesh ) {

	const geometry = mesh.geometry;
	const positions = geometry.getAttribute( 'position' );
	const offsets = new Float32BufferAttribute( new Float32Array( positions.count * 3 ), 3 );
	for ( let i = 0; i < positions.count; i ++ ) {

		if ( positions.getZ( i ) < 30 ) continue;
		const tip = MathUtils.smoothstep( positions.getY( i ), 64, 79 );
		offsets.setXYZ( i, Math.sign( positions.getX( i ) ) * 5 * tip, - 8 * tip, - 8 * tip );

	}
	offsets.name = 'Fold ears';
	geometry.morphAttributes.position = [ offsets ];
	geometry.morphTargetsRelative = true;
	mesh.updateMorphTargets();

}

// Write ordinary 32-byte SPLAT records from the fox's textured rest surface.
export function sampleFoxSplats( mesh, count ) {

	const source = mesh.material.map.image;
	const canvas = document.createElement( 'canvas' );
	canvas.width = source.width;
	canvas.height = source.height;
	const context = canvas.getContext( '2d', { willReadFrequently: true } );
	context.drawImage( source, 0, 0 );
	const pixels = context.getImageData( 0, 0, canvas.width, canvas.height ).data;

	let seed = 914367;
	const random = () => {

		seed = ( Math.imul( 1664525, seed ) + 1013904223 ) >>> 0;
		return seed / 4294967296;

	};
	const sampler = new MeshSurfaceSampler( mesh ).setRandomGenerator( random ).build();
	const normalMatrix = new Matrix3().getNormalMatrix( mesh.matrixWorld );
	const position = new Vector3(), normal = new Vector3(), uv = new Vector2();
	const axis = new Vector3( 0, 0, 1 ), rotation = new Quaternion();
	const light = new Vector3( - 0.5, 0.9, 0.5 ).normalize();
	const buffer = new ArrayBuffer( count * 32 );
	const floats = new Float32Array( buffer ), bytes = new Uint8ClampedArray( buffer );
	for ( let i = 0; i < count; i ++ ) {

		sampler.sample( position, normal, undefined, uv );
		// Reinforce the tiny eye and nose markings so they survive splat filtering.
		const eye = Math.abs( position.x ) > 4 ? 1 - MathUtils.smoothstep(
			Math.hypot( ( position.y - 61.2 ) / 0.95, ( position.z - 54.9 ) / 1.35 ), 0.71, 1
		) : 0;
		const nose = MathUtils.smoothstep( position.z, 64.7, 66.0 );
		const marking = Math.max( eye, nose );
		position.applyMatrix4( mesh.matrixWorld );
		normal.applyNormalMatrix( normalMatrix );
		const x = Math.min( canvas.width - 1, Math.floor( MathUtils.euclideanModulo( uv.x, 1 ) * canvas.width ) );
		const y = Math.min( canvas.height - 1, Math.floor( MathUtils.euclideanModulo( uv.y, 1 ) * canvas.height ) );
		const texel = ( y * canvas.width + x ) * 4;
		const shade = ( 0.72 + 0.28 * Math.max( 0, normal.dot( light ) ) ) * ( 0.95 + 0.1 * random() );
		const radius = 0.0065 + 0.003 * random();
		const f = i * 8, b = i * 32;
		// Three.js uses +Y up. The SPLAT interchange axes have Y and Z reversed.
		floats[ f ] = position.x;
		floats[ f + 1 ] = - position.y;
		floats[ f + 2 ] = - position.z;
		floats[ f + 3 ] = radius;
		floats[ f + 4 ] = radius * 0.8;
		floats[ f + 5 ] = radius * 0.2;
		bytes[ b + 24 ] = MathUtils.lerp( pixels[ texel ] * shade, 35, marking );
		bytes[ b + 25 ] = MathUtils.lerp( pixels[ texel + 1 ] * shade, 27, marking );
		bytes[ b + 26 ] = MathUtils.lerp( pixels[ texel + 2 ] * shade, 24, marking );
		bytes[ b + 27 ] = 235;
		normal.set( normal.x, - normal.y, - normal.z );
		rotation.setFromUnitVectors( axis, normal );
		bytes[ b + 28 ] = 128 + 127 * rotation.w;
		bytes[ b + 29 ] = 128 + 127 * rotation.x;
		bytes[ b + 30 ] = 128 + 127 * rotation.y;
		bytes[ b + 31 ] = 128 + 127 * rotation.z;

	}
	return buffer;

}