1 line
9.1 KiB
Plaintext
1 line
9.1 KiB
Plaintext
|
|
{"version":3,"file":"GodRaysShader.cjs","sources":["../../src/shaders/GodRaysShader.ts"],"sourcesContent":["import { Color, Vector3 } from 'three'\n\n/**\n * God-rays (crepuscular rays)\n *\n * Similar implementation to the one used by Crytek for CryEngine 2 [Sousa2008].\n * Blurs a mask generated from the depth map along radial lines emanating from the light\n * source. The blur repeatedly applies a blur filter of increasing support but constant\n * sample count to produce a blur filter with large support.\n *\n * My implementation performs 3 passes, similar to the implementation from Sousa. I found\n * just 6 samples per pass produced acceptible results. The blur is applied three times,\n * with decreasing filter support. The result is equivalent to a single pass with\n * 6*6*6 = 216 samples.\n *\n * References:\n *\n * Sousa2008 - Crysis Next Gen Effects, GDC2008, http://www.crytek.com/sites/default/files/GDC08_SousaT_CrysisEffects.ppt\n */\n\nexport const GodRaysDepthMaskShader = {\n uniforms: {\n tInput: {\n value: null,\n },\n },\n\n vertexShader: /* glsl */ `\n varying vec2 vUv;\n\n void main() {\n\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\n }\n `,\n\n fragmentShader: /* glsl */ `\n varying vec2 vUv;\n\n uniform sampler2D tInput;\n\n void main() {\n\n \tgl_FragColor = vec4( 1.0 ) - texture2D( tInput, vUv );\n\n }\n `,\n}\n\n/**\n * The god-ray generation shader.\n *\n * First pass:\n *\n * The depth map is blurred along radial lines towards the \"sun\". The\n * output is written to a temporary render target (I used a 1/4 sized\n * target).\n *\n * Pass two & three:\n *\n * The results of the previous pass are re-blurred, each time with a\n * decreased distance between samples.\n */\n\nexport const GodRaysGenerateShader = {\n uniforms: {\n tInput: {\n value: null,\n },\n fStepSize: {\n value: 1.0,\n },\n vSunPositionScreenSpace: {\n value: /* @__PURE__ */ new Vector3(),\n },\n },\n\n vertexShader: /* glsl */ `\n varying vec2 vUv;\n\n void main() {\n\n vUv = uv;\n gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\n }\n `,\n\n fragmentShader: /* glsl */ `\n #define TAPS_PER_PASS 6.0\n\n varying vec2 vUv;\n\n uniform sampler2D tInput;\n\n uniform vec3 vSunPositionScreenSpace;\n uniform float fStepSize; // filter step size\n\n void main() {\n\n // delta from current pixel to \"sun\" position\n\n \tvec2 delta = vSunPositionScreenSpace.xy - vUv;\n \tfloat dist = length( delta );\n\n // Step vector (uv space)\n\n \tvec2 stepv = fStepSize * delta / dist;\n\n // Number of iterations between pixel and sun\n\n \tfloat iters = dist/fStepSize;\n\n \tvec2 uv = vUv.xy;\n \tfloat col = 0.0;\n\n // This breaks ANGLE in Chrome 22\n //\t- see http://code.google.com/p/chromium/issues/detail?id=153105\n\n /*\n\t\t// Unrolling didnt do much on my hardware (ATI Mobility Radeon 3450),\n\t\t// so ive just left the loop\n\n\t\tfor ( float i = 0.0; i < TAPS_PER_PASS; i += 1.0 ) {\n\n\t\t// Accumulate samples, making sure we dont walk past the light source.\n\n\t\t// The check for uv.y < 1 would not be necessary with \"border\" UV wrap\n\t\t// mode, with a black border color. I dont think this is currently\n\t\t// exposed by three.js. As a result there might be artifacts when the\n\t\t// sun is to the left, right or bottom of screen as these cases are\n\t\t// not specifically handled.\n\n\t\tcol += ( i <= iters && uv.y < 1.0 ? texture2D( tInput, uv ).r : 0.0 );\n\t\tuv += stepv;\n\n\t\t}\n\t\t*/\n\n // Unrolling loop manually makes it work in ANGLE\n\n \tfloat f = min( 1.0, max( vSunPositionScreenSpace.z / 1000.0, 0.0 ) ); // used to fade out godrays\n\n \tif ( 0.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r * f;\n \tuv += stepv;\n\n \tif ( 1.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r * f;\n \tuv += stepv;\n\n \tif ( 2.0 <= iters && uv.y < 1.0 ) col += texture2D(
|