playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
33 lines (32 loc) • 1.55 kB
JavaScript
var compose_cas_default = `
#ifdef CAS
uniform sharpness: f32;
#ifdef CAS_HDR
fn maxComponent(x: f32, y: f32, z: f32) -> f32 { return max(x, max(y, z)); }
fn toSDR(c: vec3f) -> vec3f { return c / (1.0 + maxComponent(c.r, c.g, c.b)); }
fn toHDR(c: vec3f) -> vec3f { return c / max(1.0 - maxComponent(c.r, c.g, c.b), 1e-4); }
#else
fn toSDR(c: vec3f) -> vec3f { return c; }
fn toHDR(c: vec3f) -> vec3f { return c; }
#endif
fn applyCas(color: vec3f, uv: vec2f, sharpness: f32) -> vec3f {
let x = uniform.sceneTextureInvRes.x;
let y = uniform.sceneTextureInvRes.y;
let a: half3 = half3(toSDR(textureSampleLevel(sceneTexture, sceneTextureSampler, uv + vec2f(0.0, -y), 0.0).rgb));
let b: half3 = half3(toSDR(textureSampleLevel(sceneTexture, sceneTextureSampler, uv + vec2f(-x, 0.0), 0.0).rgb));
let c: half3 = half3(toSDR(color.rgb));
let d: half3 = half3(toSDR(textureSampleLevel(sceneTexture, sceneTextureSampler, uv + vec2f(x, 0.0), 0.0).rgb));
let e: half3 = half3(toSDR(textureSampleLevel(sceneTexture, sceneTextureSampler, uv + vec2f(0.0, y), 0.0).rgb));
let min_g = min(a.g, min(b.g, min(c.g, min(d.g, e.g))));
let max_g = max(a.g, max(b.g, max(c.g, max(d.g, e.g))));
let sharpening_amount = sqrt(min(half(1.0) - max_g, min_g) / max(max_g, half(1e-4)));
let w = sharpening_amount * half(sharpness);
var res = (w * (a + b + d + e) + c) / (half(4.0) * w + half(1.0));
res = max(res, half3(0.0));
return toHDR(vec3f(res));
}
#endif
`;
export {
compose_cas_default as default
};