matrix-engine-wgpu
Version:
Networking implemented - based on kurento openvidu server. fix arcball camera,instanced draws added also effect pipeline blend with instancing option.Normalmap added, Fixed shadows casting vs camera/video texture, webGPU powered pwa application. Crazy fas
103 lines (90 loc) • 2.86 kB
JavaScript
export let vertexWGSL_NM = `const MAX_BONES = 100u;
struct Scene {
lightViewProjMatrix: mat4x4f,
cameraViewProjMatrix: mat4x4f,
lightPos: vec3f,
}
struct Model {
modelMatrix: mat4x4f,
}
struct Bones {
boneMatrices : array<mat4x4f, MAX_BONES>
}
struct SkinResult {
position : vec4f,
normal : vec3f,
};
var<uniform> scene : Scene;
var<uniform> model : Model;
var<uniform> bones : Bones;
struct VertexOutput {
shadowPos: vec4f,
fragPos: vec3f,
fragNorm: vec3f,
uv: vec2f,
tangent: vec4f, // NEW
Position: vec4f,
}
fn skinVertex(pos: vec4f, nrm: vec3f, joints: vec4<u32>, weights: vec4f) -> SkinResult {
var skinnedPos = vec4f(0.0);
var skinnedNorm = vec3f(0.0);
for (var i: u32 = 0u; i < 4u; i = i + 1u) {
let jointIndex = joints[i];
let w = weights[i];
if (w > 0.0) {
let boneMat = bones.boneMatrices[jointIndex];
skinnedPos += (boneMat * pos) * w;
let boneMat3 = mat3x3f(
boneMat[0].xyz,
boneMat[1].xyz,
boneMat[2].xyz
);
skinnedNorm += (boneMat3 * nrm) * w;
}
}
return SkinResult(skinnedPos, skinnedNorm);
}
fn main(
position: vec3f,
normal: vec3f,
uv: vec2f,
joints: vec4<u32>,
weights: vec4<f32>,
tangent: vec4f // NEW
) -> VertexOutput {
var output : VertexOutput;
// Skin positions & normals
var pos = vec4(position, 1.0);
var nrm = normal;
let skinned = skinVertex(pos, nrm, joints, weights);
// Skin tangent
var skinnedTangent = vec3f(tangent.xyz);
for (var i: u32 = 0u; i < 4u; i = i + 1u) {
let jointIndex = joints[i];
let w = weights[i];
if (w > 0.0) {
let boneMat = bones.boneMatrices[jointIndex];
let boneMat3 = mat3x3f(
boneMat[0].xyz,
boneMat[1].xyz,
boneMat[2].xyz
);
skinnedTangent += (boneMat3 * tangent.xyz) * w;
}
}
// Transform to world space
let worldPos = model.modelMatrix * skinned.position;
let normalMatrix = mat3x3f(
model.modelMatrix[0].xyz,
model.modelMatrix[1].xyz,
model.modelMatrix[2].xyz
);
output.Position = scene.cameraViewProjMatrix * worldPos;
output.fragPos = worldPos.xyz;
output.shadowPos = scene.lightViewProjMatrix * worldPos;
output.fragNorm = normalize(normalMatrix * skinned.normal);
output.uv = uv;
output.tangent = vec4(normalize(skinnedTangent), tangent.w); // OUTPUT tangent
return output;
}`;