123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- const BITS = 3;
- const TEXTURE_WIDTH = 1024;
- const TEXTURE_HEIGHT = 4;
- import {
- DataTexture,
- RGBFormat,
- FloatType,
- RepeatWrapping,
- Mesh,
- InstancedMesh,
- NearestFilter,
- DynamicDrawUsage,
- Matrix4
- } from '../../../build/three.module.js';
- export function initSplineTexture( numberOfCurves = 1 ) {
- const dataArray = new Float32Array( TEXTURE_WIDTH * TEXTURE_HEIGHT * numberOfCurves * BITS );
- const dataTexture = new DataTexture(
- dataArray,
- TEXTURE_WIDTH,
- TEXTURE_HEIGHT * numberOfCurves,
- RGBFormat,
- FloatType
- );
- dataTexture.wrapS = RepeatWrapping;
- dataTexture.wrapY = RepeatWrapping;
- dataTexture.magFilter = NearestFilter;
- dataTexture.needsUpdate = true;
- return dataTexture;
- }
- export function updateSplineTexture( texture, splineCurve, offset = 0 ) {
- const numberOfPoints = Math.floor( TEXTURE_WIDTH * ( TEXTURE_HEIGHT / 4 ) );
- splineCurve.arcLengthDivisions = numberOfPoints / 2;
- splineCurve.updateArcLengths();
- const points = splineCurve.getSpacedPoints( numberOfPoints );
- const frenetFrames = splineCurve.computeFrenetFrames( numberOfPoints, true );
- for ( let i = 0; i < numberOfPoints; i ++ ) {
- const rowOffset = Math.floor( i / TEXTURE_WIDTH );
- const rowIndex = i % TEXTURE_WIDTH;
- let pt = points[ i ];
- setTextureValue( texture, rowIndex, pt.x, pt.y, pt.z, 0 + rowOffset + ( TEXTURE_HEIGHT * offset ) );
- pt = frenetFrames.tangents[ i ];
- setTextureValue( texture, rowIndex, pt.x, pt.y, pt.z, 1 + rowOffset + ( TEXTURE_HEIGHT * offset ) );
- pt = frenetFrames.normals[ i ];
- setTextureValue( texture, rowIndex, pt.x, pt.y, pt.z, 2 + rowOffset + ( TEXTURE_HEIGHT * offset ) );
- pt = frenetFrames.binormals[ i ];
- setTextureValue( texture, rowIndex, pt.x, pt.y, pt.z, 3 + rowOffset + ( TEXTURE_HEIGHT * offset ) );
- }
- texture.needsUpdate = true;
- }
- function setTextureValue( texture, index, x, y, z, o ) {
- const image = texture.image;
- const { data } = image;
- const i = BITS * TEXTURE_WIDTH * o;
- data[ index * BITS + i + 0 ] = x;
- data[ index * BITS + i + 1 ] = y;
- data[ index * BITS + i + 2 ] = z;
- }
- export function getUniforms( splineTexture ) {
- const uniforms = {
- spineTexture: { value: splineTexture },
- pathOffset: { type: 'f', value: 0 },
- pathSegment: { type: 'f', value: 1 },
- spineOffset: { type: 'f', value: 161 },
- spineLength: { type: 'f', value: 400 },
- flow: { type: 'i', value: 1 },
- };
- return uniforms;
- }
- export function modifyShader( material, uniforms, numberOfCurves = 1 ) {
- if ( material.__ok ) return;
- material.__ok = true;
- material.onBeforeCompile = ( shader ) => {
- if ( shader.__modified ) return;
- shader.__modified = true;
- Object.assign( shader.uniforms, uniforms );
- const vertexShader = `
- uniform sampler2D spineTexture;
- uniform float pathOffset;
- uniform float pathSegment;
- uniform float spineOffset;
- uniform float spineLength;
- uniform int flow;
- float textureLayers = ${TEXTURE_HEIGHT * numberOfCurves}.;
- float textureStacks = ${TEXTURE_HEIGHT / 4}.;
- ${shader.vertexShader}
- `
-
- .replace( '#include <beginnormal_vertex>', '' )
-
- .replace( '#include <defaultnormal_vertex>', '' )
-
- .replace( '#include <begin_vertex>', '' )
-
- .replace(
- /void\s*main\s*\(\)\s*\{/,
- `
- void main() {
- #include <beginnormal_vertex>
- vec4 worldPos = modelMatrix * vec4(position, 1.);
- bool bend = flow > 0;
- float xWeight = bend ? 0. : 1.;
- #ifdef USE_INSTANCING
- float pathOffsetFromInstanceMatrix = instanceMatrix[3][2];
- float spineLengthFromInstanceMatrix = instanceMatrix[3][0];
- float spinePortion = bend ? (worldPos.x + spineOffset) / spineLengthFromInstanceMatrix : 0.;
- float mt = (spinePortion * pathSegment + pathOffset + pathOffsetFromInstanceMatrix)*textureStacks;
- #else
- float spinePortion = bend ? (worldPos.x + spineOffset) / spineLength : 0.;
- float mt = (spinePortion * pathSegment + pathOffset)*textureStacks;
- #endif
- mt = mod(mt, textureStacks);
- float rowOffset = floor(mt);
- #ifdef USE_INSTANCING
- rowOffset += instanceMatrix[3][1] * ${TEXTURE_HEIGHT}.;
- #endif
- vec3 spinePos = texture2D(spineTexture, vec2(mt, (0. + rowOffset + 0.5) / textureLayers)).xyz;
- vec3 a = texture2D(spineTexture, vec2(mt, (1. + rowOffset + 0.5) / textureLayers)).xyz;
- vec3 b = texture2D(spineTexture, vec2(mt, (2. + rowOffset + 0.5) / textureLayers)).xyz;
- vec3 c = texture2D(spineTexture, vec2(mt, (3. + rowOffset + 0.5) / textureLayers)).xyz;
- mat3 basis = mat3(a, b, c);
- vec3 transformed = basis
- * vec3(worldPos.x * xWeight, worldPos.y * 1., worldPos.z * 1.)
- + spinePos;
- vec3 transformedNormal = normalMatrix * (basis * objectNormal);
- ` ).replace(
- '#include <project_vertex>',
- `vec4 mvPosition = modelViewMatrix * vec4( transformed, 1.0 );
- gl_Position = projectionMatrix * mvPosition;`
- );
- shader.vertexShader = vertexShader;
- };
- }
- export class Flow {
-
- constructor( mesh, numberOfCurves = 1 ) {
- const obj3D = mesh.clone();
- const splineTexure = initSplineTexture( numberOfCurves );
- const uniforms = getUniforms( splineTexure );
- obj3D.traverse( function ( child ) {
- if (
- child instanceof Mesh ||
- child instanceof InstancedMesh
- ) {
- child.material = child.material.clone();
- modifyShader( child.material, uniforms, numberOfCurves );
- }
- } );
- this.curveArray = new Array( numberOfCurves );
- this.curveLengthArray = new Array( numberOfCurves );
- this.object3D = obj3D;
- this.splineTexure = splineTexure;
- this.uniforms = uniforms;
- }
- updateCurve( index, curve ) {
- if ( index >= this.curveArray.length ) throw Error( 'Index out of range for Flow' );
- const curveLength = curve.getLength();
- this.uniforms.spineLength.value = curveLength;
- this.curveLengthArray[ index ] = curveLength;
- this.curveArray[ index ] = curve;
- updateSplineTexture( this.splineTexure, curve, index );
- }
- moveAlongCurve( amount ) {
- this.uniforms.pathOffset.value += amount;
- }
- }
- const matrix = new Matrix4();
- export class InstancedFlow extends Flow {
-
- constructor( count, curveCount, geometry, material ) {
- const mesh = new InstancedMesh(
- geometry,
- material,
- count
- );
- mesh.instanceMatrix.setUsage( DynamicDrawUsage );
- super( mesh, curveCount );
- this.offsets = new Array( count ).fill( 0 );
- this.whichCurve = new Array( count ).fill( 0 );
- }
-
- writeChanges( index ) {
- matrix.makeTranslation(
- this.curveLengthArray[ this.whichCurve[ index ] ],
- this.whichCurve[ index ],
- this.offsets[ index ]
- );
- this.object3D.setMatrixAt( index, matrix );
- this.object3D.instanceMatrix.needsUpdate = true;
- }
-
- moveIndividualAlongCurve( index, offset ) {
- this.offsets[ index ] += offset;
- this.writeChanges( index );
- }
-
- setCurve( index, curveNo ) {
- if ( isNaN( curveNo ) ) throw Error( 'curve index being set is Not a Number (NaN)' );
- this.whichCurve[ index ] = curveNo;
- this.writeChanges( index );
- }
- }
|