matrix-engine-wgpu
Version:
obj sequence anim +HOTFIX raycast, webGPU powered pwa application. Crazy fast rendering with AmmoJS physics support. Simple raycaster hit object added.
52 lines (41 loc) • 1.42 kB
JavaScript
/**
* @description
* UNIT Texures -
* Good for performance
*/
export const UNLIT_SHADER = `struct Uniforms {
viewProjectionMatrix : mat4x4f
}
var<uniform> uniforms : Uniforms;
var<uniform> modelMatrix : mat4x4f;
struct VertexInput {
position : vec4f,
normal : vec3f,
uv : vec2f
}
struct VertexOutput {
position : vec4f,
normal: vec3f,
uv : vec2f,
}
fn vertexMain(input: VertexInput) -> VertexOutput {
var output : VertexOutput;
output.position = uniforms.viewProjectionMatrix * modelMatrix * input.position;
output.normal = normalize((modelMatrix * vec4(input.normal, 0)).xyz);
output.uv = input.uv;
return output;
}
var meshSampler: sampler;
var meshTexture: texture_2d<f32>;
// Static directional lighting
const lightDir = vec3f(0, 1, 0);
const dirColor = vec3(1);
const ambientColor = vec3f(0.05);
fn fragmentMain(input: VertexOutput) -> vec4f {
let textureColor = textureSample(meshTexture, meshSampler, input.uv);
// Very simplified lighting algorithm.
let lightColor = saturate(ambientColor + max(dot(input.normal, lightDir), 0.0) * dirColor);
return vec4f(textureColor.rgb * lightColor, textureColor.a);
}`;