Skip to content

Commit 75fb49a

Browse files
committed
WebGLMorphtargets: Fix texture sampling.
1 parent df7ed79 commit 75fb49a

File tree

4 files changed

+16
-20
lines changed

4 files changed

+16
-20
lines changed

src/renderers/shaders/ShaderChunk/morphnormal_vertex.glsl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default /* glsl */`
1010
1111
for ( int i = 0; i < MORPHTARGETS_COUNT; i ++ ) {
1212
13-
if ( morphTargetInfluences[ i ] > 0.0 ) objectNormal += getMorph( gl_VertexID, i, 1.0, 2.0 ) * morphTargetInfluences[ i ];
13+
if ( morphTargetInfluences[ i ] > 0.0 ) objectNormal += getMorph( gl_VertexID, i, 1, 2 ) * morphTargetInfluences[ i ];
1414
1515
}
1616

src/renderers/shaders/ShaderChunk/morphtarget_pars_vertex.glsl.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,17 @@ export default /* glsl */`
66
#ifdef MORPHTARGETS_TEXTURE
77
88
uniform float morphTargetInfluences[ MORPHTARGETS_COUNT ];
9-
uniform sampler2DArray morphTargets;
10-
uniform int morphTargetsWidth;
9+
uniform sampler2DArray morphTargetsTexture;
10+
uniform vec2 morphTargetsTextureSize;
1111
12-
vec3 getMorph( const in int vertexIndex, const in int morphTargetIndex, const in float offset, const in float stride ) {
12+
vec3 getMorph( const in int vertexIndex, const in int morphTargetIndex, const in int offset, const in int stride ) {
1313
14-
float j = float( vertexIndex ) * stride + offset;
15-
float x = mod( j, float( morphTargetsWidth ) );
16-
float y = floor( j / float( morphTargetsWidth ) );
14+
float texelIndex = float( vertexIndex * stride + offset );
15+
float y = floor( texelIndex / morphTargetsTextureSize.x );
16+
float x = texelIndex - y * morphTargetsTextureSize.x;
1717
18-
float dx = 1.0 / float( morphTargetsWidth );
19-
float dy = 1.0 / float( morphTargetsWidth );
20-
21-
x = dx * ( x + 0.5 );
22-
y = dy * ( y + 0.5 );
23-
24-
vec3 morphUV = vec3( x, y, morphTargetIndex );
25-
return texture( morphTargets, morphUV ).xyz;
18+
vec3 morphUV = vec3( ( x + 0.5 ) / morphTargetsTextureSize.x, ( y + 0.5 ) / morphTargetsTextureSize.y, morphTargetIndex );
19+
return texture( morphTargetsTexture, morphUV ).xyz;
2620
2721
}
2822

src/renderers/shaders/ShaderChunk/morphtarget_vertex.glsl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ export default /* glsl */`
1212
1313
#ifndef USE_MORPHNORMALS
1414
15-
if ( morphTargetInfluences[ i ] > 0.0 ) transformed += getMorph( gl_VertexID, i, 0.0, 1.0 ) * morphTargetInfluences[ i ];
15+
if ( morphTargetInfluences[ i ] > 0.0 ) transformed += getMorph( gl_VertexID, i, 0, 1 ) * morphTargetInfluences[ i ];
1616
1717
#else
1818
19-
if ( morphTargetInfluences[ i ] > 0.0 ) transformed += getMorph( gl_VertexID, i, 0.0, 2.0 ) * morphTargetInfluences[ i ];
19+
if ( morphTargetInfluences[ i ] > 0.0 ) transformed += getMorph( gl_VertexID, i, 0, 2 ) * morphTargetInfluences[ i ];
2020
2121
#endif
2222

src/renderers/webgl/WebGLMorphtargets.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { FloatType, RGBAFormat } from '../../constants.js';
22
import { DataTexture2DArray } from '../../textures/DataTexture2DArray.js';
33
import { Vector3 } from '../../math/Vector3.js';
4+
import { Vector2 } from '../../math/Vector2.js';
45

56
function numericalSort( a, b ) {
67

@@ -107,7 +108,8 @@ function WebGLMorphtargets( gl, capabilities, textures ) {
107108

108109
entry = {
109110
count: numberOfMorphTargets,
110-
texture: texture
111+
texture: texture,
112+
size: new Vector2( width, height )
111113
};
112114

113115
morphTextures.set( geometry, entry );
@@ -129,8 +131,8 @@ function WebGLMorphtargets( gl, capabilities, textures ) {
129131
program.getUniforms().setValue( gl, 'morphTargetBaseInfluence', morphBaseInfluence );
130132
program.getUniforms().setValue( gl, 'morphTargetInfluences', objectInfluences );
131133

132-
program.getUniforms().setValue( gl, 'morphTargets', entry.texture, textures );
133-
program.getUniforms().setValue( gl, 'morphTargetsWidth', entry.texture.image.width );
134+
program.getUniforms().setValue( gl, 'morphTargetsTexture', entry.texture, textures );
135+
program.getUniforms().setValue( gl, 'morphTargetsTextureSize', entry.size );
134136

135137

136138
} else {

0 commit comments

Comments
 (0)