From 6280256f0c2ae61521183c08584a9a4664451af4 Mon Sep 17 00:00:00 2001 From: Jim Page Date: Sun, 30 Jun 2024 10:34:53 +0200 Subject: [PATCH] Revert "Skinning Shaders: Remove need skinning texture size uniform by using textureSize function (#27117)" This reverts commit 3e0f80faa748a52ec469c838053fe8a748eba27b. --- src/objects/Skeleton.js | 2 ++ src/renderers/WebGLRenderer.js | 1 + .../ShaderChunk/skinning_pars_vertex.glsl.js | 26 ++++++++++++------- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/objects/Skeleton.js b/src/objects/Skeleton.js index 23abe42b09be41..d256e98bb458c4 100644 --- a/src/objects/Skeleton.js +++ b/src/objects/Skeleton.js @@ -21,6 +21,7 @@ class Skeleton { this.boneMatrices = null; this.boneTexture = null; + this.boneTextureSize = 0; this.init(); @@ -179,6 +180,7 @@ class Skeleton { this.boneMatrices = boneMatrices; this.boneTexture = boneTexture; + this.boneTextureSize = size; return this; diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index aae548d8df6e46..44571ffc9b9ddd 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -1988,6 +1988,7 @@ class WebGLRenderer { if ( skeleton.boneTexture === null ) skeleton.computeBoneTexture(); p_uniforms.setValue( _gl, 'boneTexture', skeleton.boneTexture, textures ); + p_uniforms.setValue( _gl, 'boneTextureSize', skeleton.boneTextureSize ); } else { diff --git a/src/renderers/shaders/ShaderChunk/skinning_pars_vertex.glsl.js b/src/renderers/shaders/ShaderChunk/skinning_pars_vertex.glsl.js index 10b4dc5222ff53..a17c93391afa8a 100644 --- a/src/renderers/shaders/ShaderChunk/skinning_pars_vertex.glsl.js +++ b/src/renderers/shaders/ShaderChunk/skinning_pars_vertex.glsl.js @@ -5,19 +5,27 @@ export default /* glsl */` uniform mat4 bindMatrixInverse; uniform highp sampler2D boneTexture; + uniform int boneTextureSize; mat4 getBoneMatrix( const in float i ) { - int size = textureSize( boneTexture, 0 ).x; - int j = int( i ) * 4; - int x = j % size; - int y = j / size; - vec4 v1 = texelFetch( boneTexture, ivec2( x, y ), 0 ); - vec4 v2 = texelFetch( boneTexture, ivec2( x + 1, y ), 0 ); - vec4 v3 = texelFetch( boneTexture, ivec2( x + 2, y ), 0 ); - vec4 v4 = texelFetch( boneTexture, ivec2( x + 3, y ), 0 ); + float j = i * 4.0; + float x = mod( j, float( boneTextureSize ) ); + float y = floor( j / float( boneTextureSize ) ); - return mat4( v1, v2, v3, v4 ); + float dx = 1.0 / float( boneTextureSize ); + float dy = 1.0 / float( boneTextureSize ); + + y = dy * ( y + 0.5 ); + + vec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) ); + vec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) ); + vec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) ); + vec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) ); + + mat4 bone = mat4( v1, v2, v3, v4 ); + + return bone; }