From a8c54abdd06c8931f47479bdf25a82d8af132827 Mon Sep 17 00:00:00 2001 From: SirLynix Date: Thu, 8 Sep 2022 08:57:37 +0200 Subject: [PATCH] Graphics: Add skinning support to DepthMaterial --- .../Resources/Shaders/DepthMaterial.nzsl | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/Nazara/Graphics/Resources/Shaders/DepthMaterial.nzsl b/src/Nazara/Graphics/Resources/Shaders/DepthMaterial.nzsl index af1f62bb5..0294f1887 100644 --- a/src/Nazara/Graphics/Resources/Shaders/DepthMaterial.nzsl +++ b/src/Nazara/Graphics/Resources/Shaders/DepthMaterial.nzsl @@ -2,13 +2,20 @@ module DepthMaterial; import InstanceData from Engine.InstanceData; +import SkeletalData from Engine.SkeletalData; import ViewerData from Engine.ViewerData; option HasBaseColorTexture: bool = false; option HasAlphaTexture: bool = false; option AlphaTest: bool = false; +option PosLocation: i32; +option UvLocation: i32 = -1; +option JointIndicesLocation: i32 = -1; +option JointWeightsLocation: i32 = -1; + const HasUV = AlphaTest && (HasBaseColorTexture || HasAlphaTexture); +const HasSkinning = (JointIndicesLocation >= 0 && JointWeightsLocation >= 0); [layout(std140)] struct BasicSettings @@ -25,6 +32,7 @@ external [binding(3)] TextureOverlay: sampler2D[f32], [binding(4)] instanceData: uniform[InstanceData], [binding(5)] viewerData: uniform[ViewerData], + [binding(6)] skeletalData: uniform[SkeletalData] } // Fragment stage @@ -58,8 +66,16 @@ fn main() {} // Vertex stage struct VertIn { - [location(0)] pos: vec3[f32], - [location(1), cond(HasUV)] uv: vec2[f32] + [location(PosLocation)] pos: vec3[f32], + + [cond(HasUV), location(UvLocation)] + uv: vec2[f32], + + [cond(HasSkinning), location(JointIndicesLocation)] + jointIndices: vec4[i32], + + [cond(HasSkinning), location(JointWeightsLocation)] + jointWeights: vec4[f32], } struct VertOut @@ -71,7 +87,25 @@ struct VertOut [entry(vert)] fn main(input: VertIn) -> VertOut { - let worldPosition = instanceData.worldMatrix * vec4[f32](input.pos, 1.0); + let pos: vec3[f32]; + const if (HasSkinning) + { + pos = vec3[f32](0.0, 0.0, 0.0); + + [unroll] + for i in 0 -> 4 + { + let jointIndex = input.jointIndices[i]; + let jointWeight = input.jointWeights[i]; + + let jointMatrix = skeletalData.JointMatrices[jointIndex]; + pos += (jointMatrix * vec4[f32](input.pos, 1.0)).xyz * jointWeight; + } + } + else + pos = input.pos; + + let worldPosition = instanceData.worldMatrix * vec4[f32](pos, 1.0); let output: VertOut; output.position = viewerData.viewProjMatrix * worldPosition;