|
@@ -1,3 +1,8 @@
|
|
|
+import { FloatType, RGBAFormat } from '../../constants.js';
|
|
|
+import { DataTexture2DArray } from '../../textures/DataTexture2DArray.js';
|
|
|
+import { Vector3 } from '../../math/Vector3.js';
|
|
|
+import { Vector2 } from '../../math/Vector2.js';
|
|
|
+
|
|
|
function numericalSort( a, b ) {
|
|
|
|
|
|
return a[ 0 ] - b[ 0 ];
|
|
@@ -10,10 +15,12 @@ function absNumericalSort( a, b ) {
|
|
|
|
|
|
}
|
|
|
|
|
|
-function WebGLMorphtargets( gl ) {
|
|
|
+function WebGLMorphtargets( gl, capabilities, textures ) {
|
|
|
|
|
|
const influencesList = {};
|
|
|
const morphInfluences = new Float32Array( 8 );
|
|
|
+ const morphTextures = new WeakMap();
|
|
|
+ const morph = new Vector3();
|
|
|
|
|
|
const workInfluences = [];
|
|
|
|
|
@@ -27,115 +34,220 @@ function WebGLMorphtargets( gl ) {
|
|
|
|
|
|
const objectInfluences = object.morphTargetInfluences;
|
|
|
|
|
|
- // When object doesn't have morph target influences defined, we treat it as a 0-length array
|
|
|
- // This is important to make sure we set up morphTargetBaseInfluence / morphTargetInfluences
|
|
|
+ if ( capabilities.isWebGL2 === true ) {
|
|
|
|
|
|
- const length = objectInfluences === undefined ? 0 : objectInfluences.length;
|
|
|
+ // instead of using attributes, the WebGL 2 code path encodes morph targets
|
|
|
+ // into an array of data textures. Each layer represents a single morph target.
|
|
|
|
|
|
- let influences = influencesList[ geometry.id ];
|
|
|
+ const numberOfMorphTargets = geometry.morphAttributes.position.length;
|
|
|
|
|
|
- if ( influences === undefined || influences.length !== length ) {
|
|
|
+ let entry = morphTextures.get( geometry );
|
|
|
|
|
|
- // initialise list
|
|
|
+ if ( entry === undefined || entry.count !== numberOfMorphTargets ) {
|
|
|
|
|
|
- influences = [];
|
|
|
+ if ( entry !== undefined ) entry.texture.dispose();
|
|
|
|
|
|
- for ( let i = 0; i < length; i ++ ) {
|
|
|
+ const hasMorphNormals = geometry.morphAttributes.normal !== undefined;
|
|
|
|
|
|
- influences[ i ] = [ i, 0 ];
|
|
|
+ const morphTargets = geometry.morphAttributes.position;
|
|
|
+ const morphNormals = geometry.morphAttributes.normal || [];
|
|
|
|
|
|
- }
|
|
|
+ const numberOfVertices = geometry.attributes.position.count;
|
|
|
+ const numberOfVertexData = ( hasMorphNormals === true ) ? 2 : 1; // (v,n) vs. (v)
|
|
|
|
|
|
- influencesList[ geometry.id ] = influences;
|
|
|
+ let width = numberOfVertices * numberOfVertexData;
|
|
|
+ let height = 1;
|
|
|
|
|
|
- }
|
|
|
+ if ( width > capabilities.maxTextureSize ) {
|
|
|
|
|
|
- // Collect influences
|
|
|
+ height = Math.ceil( width / capabilities.maxTextureSize );
|
|
|
+ width = capabilities.maxTextureSize;
|
|
|
|
|
|
- for ( let i = 0; i < length; i ++ ) {
|
|
|
+ }
|
|
|
|
|
|
- const influence = influences[ i ];
|
|
|
+ const buffer = new Float32Array( width * height * 4 * numberOfMorphTargets );
|
|
|
|
|
|
- influence[ 0 ] = i;
|
|
|
- influence[ 1 ] = objectInfluences[ i ];
|
|
|
+ const texture = new DataTexture2DArray( buffer, width, height, numberOfMorphTargets );
|
|
|
+ texture.format = RGBAFormat; // using RGBA since RGB might be emulated (and is thus slower)
|
|
|
+ texture.type = FloatType;
|
|
|
|
|
|
- }
|
|
|
+ // fill buffer
|
|
|
+
|
|
|
+ const vertexDataStride = numberOfVertexData * 4;
|
|
|
+
|
|
|
+ for ( let i = 0; i < numberOfMorphTargets; i ++ ) {
|
|
|
|
|
|
- influences.sort( absNumericalSort );
|
|
|
+ const morphTarget = morphTargets[ i ];
|
|
|
+ const morphNormal = morphNormals[ i ];
|
|
|
|
|
|
- for ( let i = 0; i < 8; i ++ ) {
|
|
|
+ const offset = width * height * 4 * i;
|
|
|
|
|
|
- if ( i < length && influences[ i ][ 1 ] ) {
|
|
|
+ for ( let j = 0; j < morphTarget.count; j ++ ) {
|
|
|
|
|
|
- workInfluences[ i ][ 0 ] = influences[ i ][ 0 ];
|
|
|
- workInfluences[ i ][ 1 ] = influences[ i ][ 1 ];
|
|
|
+ morph.fromBufferAttribute( morphTarget, j );
|
|
|
|
|
|
- } else {
|
|
|
+ const stride = j * vertexDataStride;
|
|
|
+
|
|
|
+ buffer[ offset + stride + 0 ] = morph.x;
|
|
|
+ buffer[ offset + stride + 1 ] = morph.y;
|
|
|
+ buffer[ offset + stride + 2 ] = morph.z;
|
|
|
+
|
|
|
+ if ( hasMorphNormals === true ) {
|
|
|
+
|
|
|
+ morph.fromBufferAttribute( morphNormal, j );
|
|
|
+
|
|
|
+ buffer[ offset + stride + 3 ] = morph.x;
|
|
|
+ buffer[ offset + stride + 4 ] = morph.y;
|
|
|
+ buffer[ offset + stride + 5 ] = morph.z;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- workInfluences[ i ][ 0 ] = Number.MAX_SAFE_INTEGER;
|
|
|
- workInfluences[ i ][ 1 ] = 0;
|
|
|
+ entry = {
|
|
|
+ count: numberOfMorphTargets,
|
|
|
+ texture: texture,
|
|
|
+ size: new Vector2( width, height )
|
|
|
+ };
|
|
|
+
|
|
|
+ morphTextures.set( geometry, entry );
|
|
|
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
+ //
|
|
|
|
|
|
- workInfluences.sort( numericalSort );
|
|
|
+ let morphInfluencesSum = 0;
|
|
|
|
|
|
- const morphTargets = geometry.morphAttributes.position;
|
|
|
- const morphNormals = geometry.morphAttributes.normal;
|
|
|
+ for ( let i = 0; i < objectInfluences.length; i ++ ) {
|
|
|
|
|
|
- let morphInfluencesSum = 0;
|
|
|
+ morphInfluencesSum += objectInfluences[ i ];
|
|
|
|
|
|
- for ( let i = 0; i < 8; i ++ ) {
|
|
|
+ }
|
|
|
|
|
|
- const influence = workInfluences[ i ];
|
|
|
- const index = influence[ 0 ];
|
|
|
- const value = influence[ 1 ];
|
|
|
+ const morphBaseInfluence = geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum;
|
|
|
|
|
|
- if ( index !== Number.MAX_SAFE_INTEGER && value ) {
|
|
|
+ program.getUniforms().setValue( gl, 'morphTargetBaseInfluence', morphBaseInfluence );
|
|
|
+ program.getUniforms().setValue( gl, 'morphTargetInfluences', objectInfluences );
|
|
|
|
|
|
- if ( morphTargets && geometry.getAttribute( 'morphTarget' + i ) !== morphTargets[ index ] ) {
|
|
|
+ program.getUniforms().setValue( gl, 'morphTargetsTexture', entry.texture, textures );
|
|
|
+ program.getUniforms().setValue( gl, 'morphTargetsTextureSize', entry.size );
|
|
|
|
|
|
- geometry.setAttribute( 'morphTarget' + i, morphTargets[ index ] );
|
|
|
|
|
|
- }
|
|
|
+ } else {
|
|
|
|
|
|
- if ( morphNormals && geometry.getAttribute( 'morphNormal' + i ) !== morphNormals[ index ] ) {
|
|
|
+ // When object doesn't have morph target influences defined, we treat it as a 0-length array
|
|
|
+ // This is important to make sure we set up morphTargetBaseInfluence / morphTargetInfluences
|
|
|
|
|
|
- geometry.setAttribute( 'morphNormal' + i, morphNormals[ index ] );
|
|
|
+ const length = objectInfluences === undefined ? 0 : objectInfluences.length;
|
|
|
|
|
|
- }
|
|
|
+ let influences = influencesList[ geometry.id ];
|
|
|
+
|
|
|
+ if ( influences === undefined || influences.length !== length ) {
|
|
|
|
|
|
- morphInfluences[ i ] = value;
|
|
|
- morphInfluencesSum += value;
|
|
|
+ // initialise list
|
|
|
|
|
|
- } else {
|
|
|
+ influences = [];
|
|
|
|
|
|
- if ( morphTargets && geometry.hasAttribute( 'morphTarget' + i ) === true ) {
|
|
|
+ for ( let i = 0; i < length; i ++ ) {
|
|
|
|
|
|
- geometry.deleteAttribute( 'morphTarget' + i );
|
|
|
+ influences[ i ] = [ i, 0 ];
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( morphNormals && geometry.hasAttribute( 'morphNormal' + i ) === true ) {
|
|
|
+ influencesList[ geometry.id ] = influences;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // Collect influences
|
|
|
+
|
|
|
+ for ( let i = 0; i < length; i ++ ) {
|
|
|
+
|
|
|
+ const influence = influences[ i ];
|
|
|
+
|
|
|
+ influence[ 0 ] = i;
|
|
|
+ influence[ 1 ] = objectInfluences[ i ];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ influences.sort( absNumericalSort );
|
|
|
+
|
|
|
+ for ( let i = 0; i < 8; i ++ ) {
|
|
|
+
|
|
|
+ if ( i < length && influences[ i ][ 1 ] ) {
|
|
|
+
|
|
|
+ workInfluences[ i ][ 0 ] = influences[ i ][ 0 ];
|
|
|
+ workInfluences[ i ][ 1 ] = influences[ i ][ 1 ];
|
|
|
|
|
|
- geometry.deleteAttribute( 'morphNormal' + i );
|
|
|
+ } else {
|
|
|
+
|
|
|
+ workInfluences[ i ][ 0 ] = Number.MAX_SAFE_INTEGER;
|
|
|
+ workInfluences[ i ][ 1 ] = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
- morphInfluences[ i ] = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ workInfluences.sort( numericalSort );
|
|
|
+
|
|
|
+ const morphTargets = geometry.morphAttributes.position;
|
|
|
+ const morphNormals = geometry.morphAttributes.normal;
|
|
|
+
|
|
|
+ let morphInfluencesSum = 0;
|
|
|
+
|
|
|
+ for ( let i = 0; i < 8; i ++ ) {
|
|
|
+
|
|
|
+ const influence = workInfluences[ i ];
|
|
|
+ const index = influence[ 0 ];
|
|
|
+ const value = influence[ 1 ];
|
|
|
+
|
|
|
+ if ( index !== Number.MAX_SAFE_INTEGER && value ) {
|
|
|
+
|
|
|
+ if ( morphTargets && geometry.getAttribute( 'morphTarget' + i ) !== morphTargets[ index ] ) {
|
|
|
+
|
|
|
+ geometry.setAttribute( 'morphTarget' + i, morphTargets[ index ] );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if ( morphNormals && geometry.getAttribute( 'morphNormal' + i ) !== morphNormals[ index ] ) {
|
|
|
+
|
|
|
+ geometry.setAttribute( 'morphNormal' + i, morphNormals[ index ] );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ morphInfluences[ i ] = value;
|
|
|
+ morphInfluencesSum += value;
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ if ( morphTargets && geometry.hasAttribute( 'morphTarget' + i ) === true ) {
|
|
|
+
|
|
|
+ geometry.deleteAttribute( 'morphTarget' + i );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if ( morphNormals && geometry.hasAttribute( 'morphNormal' + i ) === true ) {
|
|
|
+
|
|
|
+ geometry.deleteAttribute( 'morphNormal' + i );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ morphInfluences[ i ] = 0;
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
+ // GLSL shader uses formula baseinfluence * base + sum(target * influence)
|
|
|
+ // This allows us to switch between absolute morphs and relative morphs without changing shader code
|
|
|
+ // When baseinfluence = 1 - sum(influence), the above is equivalent to sum((target - base) * influence)
|
|
|
+ const morphBaseInfluence = geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum;
|
|
|
|
|
|
- // GLSL shader uses formula baseinfluence * base + sum(target * influence)
|
|
|
- // This allows us to switch between absolute morphs and relative morphs without changing shader code
|
|
|
- // When baseinfluence = 1 - sum(influence), the above is equivalent to sum((target - base) * influence)
|
|
|
- const morphBaseInfluence = geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum;
|
|
|
+ program.getUniforms().setValue( gl, 'morphTargetBaseInfluence', morphBaseInfluence );
|
|
|
+ program.getUniforms().setValue( gl, 'morphTargetInfluences', morphInfluences );
|
|
|
|
|
|
- program.getUniforms().setValue( gl, 'morphTargetBaseInfluence', morphBaseInfluence );
|
|
|
- program.getUniforms().setValue( gl, 'morphTargetInfluences', morphInfluences );
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|