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.
51 lines (43 loc) • 1.72 kB
JavaScript
export let fragmentWGSL = `override shadowDepthTextureSize: f32 = 1024.0;
struct Scene {
lightViewProjMatrix : mat4x4f,
cameraViewProjMatrix : mat4x4f,
lightPos : vec3f,
}
var<uniform> scene : Scene;
var shadowMap: texture_depth_2d;
var shadowSampler: sampler_comparison;
var meshTexture: texture_2d<f32>;
var meshSampler: sampler;
struct FragmentInput {
shadowPos : vec3f,
fragPos : vec3f,
fragNorm : vec3f,
uv : vec2f,
}
const albedo = vec3f(0.9);
const ambientFactor = 0.7;
fn main(input : FragmentInput) -> vec4f {
// Percentage-closer filtering. Sample texels in the region
// to smooth the result.
var visibility = 0.0;
let oneOverShadowDepthTextureSize = 1.0 / shadowDepthTextureSize;
for (var y = -1; y <= 1; y++) {
for (var x = -1; x <= 1; x++) {
let offset = vec2f(vec2(x, y)) * oneOverShadowDepthTextureSize;
visibility += textureSampleCompare(
shadowMap, shadowSampler,
input.shadowPos.xy + offset, input.shadowPos.z - 0.007
);
}
}
visibility /= 9.0;
let lambertFactor = max(dot(normalize(scene.lightPos - input.fragPos), normalize(input.fragNorm)), 0.0);
let lightingFactor = min(ambientFactor + visibility * lambertFactor, 1.0);
let textureColor = textureSample(meshTexture, meshSampler, input.uv);
return vec4(textureColor.rgb * lightingFactor * albedo, 1.0);
// return vec4f(input.fragNorm * 0.5 + 0.5, 1)
// return vec4f(input.uv, 0, 1)
// return vec4(textureColor.rgb , 0.5);
}`