/// Uniform for shadow-map vertex transform (one directional CSM cascade per pass).
struct ShadowVsUniform {
light_view_proj: mat4x4<f32>,
};
@group(0) @binding(0)
var<uniform> shadow_vs: ShadowVsUniform;
struct SkeletonData {
joints: array<mat4x4<f32>, 128>,
};
@group(1) @binding(0)
var<uniform> skeleton: SkeletonData;
struct InstanceData {
model_matrix_0: vec4<f32>,
model_matrix_1: vec4<f32>,
model_matrix_2: vec4<f32>,
model_matrix_3: vec4<f32>,
albedo_color: vec4<f32>,
pbr: vec4<f32>,
};
@group(2) @binding(0)
var<storage, read> instances: array<InstanceData>;
struct VertexInput {
@location(0) position: vec3<f32>,
@location(1) color: vec3<f32>,
@location(2) normal: vec3<f32>,
@location(3) tex_coords: vec2<f32>,
@location(4) joint_indices: vec4<u32>,
@location(5) joint_weights: vec4<f32>,
};
@vertex
fn vs_main(@builtin(instance_index) instance_idx: u32, input: VertexInput) -> @builtin(position) vec4<f32> {
let inst = instances[instance_idx];
let model = mat4x4<f32>(
inst.model_matrix_0,
inst.model_matrix_1,
inst.model_matrix_2,
inst.model_matrix_3,
);
var skin_mat = mat4x4<f32>(
vec4<f32>(1.0, 0.0, 0.0, 0.0),
vec4<f32>(0.0, 1.0, 0.0, 0.0),
vec4<f32>(0.0, 0.0, 1.0, 0.0),
vec4<f32>(0.0, 0.0, 0.0, 1.0)
);
if (input.joint_weights.x + input.joint_weights.y + input.joint_weights.z + input.joint_weights.w > 0.0) {
skin_mat =
input.joint_weights.x * skeleton.joints[input.joint_indices.x] +
input.joint_weights.y * skeleton.joints[input.joint_indices.y] +
input.joint_weights.z * skeleton.joints[input.joint_indices.z] +
input.joint_weights.w * skeleton.joints[input.joint_indices.w];
}
let skinned_pos = skin_mat * vec4<f32>(input.position, 1.0);
let world_pos = model * vec4<f32>(skinned_pos.xyz, 1.0);
return shadow_vs.light_view_proj * world_pos;
}