From 451c693101b9ca32e3b0bfa654b8fc2dedb73c87 Mon Sep 17 00:00:00 2001 From: Rinnegatamante Date: Fri, 23 Oct 2020 01:50:16 +0200 Subject: [PATCH] Optimized skinning vertex shader. --- src/gl/shaders/skin.vert | 15 ++++++++------- src/gl/shaders/skin_gl3.inc | 15 ++++++++------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/gl/shaders/skin.vert b/src/gl/shaders/skin.vert index 982a9e1..4d2ec42 100644 --- a/src/gl/shaders/skin.vert +++ b/src/gl/shaders/skin.vert @@ -4,7 +4,7 @@ void main( fixed4 in_color, half2 in_tex0, float4 in_weights, - fixed4 in_indices, + float4 in_indices, uniform float4x4 u_wvp, uniform float4x4 u_world, uniform half4 u_ambLight, @@ -20,12 +20,13 @@ void main( fixed out v_fog : FOG, float4 out gl_Position : POSITION ) { - float3 SkinVertex = float3(0.0, 0.0, 0.0); - float3 SkinNormal = float3(0.0, 0.0, 0.0); - for(int i = 0; i < 4; i++){ - SkinVertex += (mul(float4(in_pos, 1.0), u_boneMatrices[int(in_indices[i])])).xyz * in_weights[i]; - SkinNormal += mul(in_normal, float3x3(u_boneMatrices[int(in_indices[i])])) * in_weights[i]; - } + float4x4 Skin = u_boneMatrices[(int)in_indices.x] * in_weights.x + + u_boneMatrices[(int)in_indices.y] * in_weights.y + + u_boneMatrices[(int)in_indices.z] * in_weights.z + + u_boneMatrices[(int)in_indices.w] * in_weights.w; + + float3 SkinVertex = (mul(float4(in_pos, 1.f), Skin)).xyz; + float3 SkinNormal = (mul(float4(in_normal, 0.f), Skin)).xyz; gl_Position = mul(float4(SkinVertex, 1.0), u_wvp); float3 Normal = mul(SkinNormal, float3x3(u_world)); diff --git a/src/gl/shaders/skin_gl3.inc b/src/gl/shaders/skin_gl3.inc index d41d848..c8a2038 100644 --- a/src/gl/shaders/skin_gl3.inc +++ b/src/gl/shaders/skin_gl3.inc @@ -5,7 +5,7 @@ const char *skin_vert_src = " fixed4 in_color,\n" " half2 in_tex0,\n" " float4 in_weights,\n" -" fixed4 in_indices,\n" +" float4 in_indices,\n" " uniform float4x4 u_wvp,\n" " uniform float4x4 u_world,\n" " uniform half4 u_ambLight,\n" @@ -21,12 +21,13 @@ const char *skin_vert_src = " fixed out v_fog : FOG,\n" " float4 out gl_Position : POSITION\n" ") {\n" -" float3 SkinVertex = float3(0.0, 0.0, 0.0);\n" -" float3 SkinNormal = float3(0.0, 0.0, 0.0);\n" -" for(int i = 0; i < 4; i++){\n" -" SkinVertex += (mul(float4(in_pos, 1.0), u_boneMatrices[int(in_indices[i])])).xyz * in_weights[i];\n" -" SkinNormal += mul(in_normal, float3x3(u_boneMatrices[int(in_indices[i])])) * in_weights[i];\n" -" }\n" +" float4x4 Skin = u_boneMatrices[(int)in_indices.x] * in_weights.x\n" +" + u_boneMatrices[(int)in_indices.y] * in_weights.y\n" +" + u_boneMatrices[(int)in_indices.z] * in_weights.z\n" +" + u_boneMatrices[(int)in_indices.w] * in_weights.w;\n" +" \n" +" float3 SkinVertex = (mul(float4(in_pos, 1.f), Skin)).xyz;\n" +" float3 SkinNormal = (mul(float4(in_normal, 0.f), Skin)).xyz;\n" " \n" " gl_Position = mul(float4(SkinVertex, 1.0), u_wvp);\n" " float3 Normal = mul(SkinNormal, float3x3(u_world));\n"