dustParticlesFragmentShader
strata-game-library/shaders / dustParticlesFragmentShader
Variable: dustParticlesFragmentShader
constdustParticlesFragmentShader: "\n uniform sampler2D tDiffuse;\n uniform float uTime;\n uniform float uDensity;\n uniform float uParticleSize;\n uniform vec3 uLightDirection;\n uniform vec3 uCameraPosition;\n uniform vec2 uResolution;\n \n varying vec2 vUv;\n \n \n// Hash function\nfloat hash(vec3 p) {\n p = fract(p * vec3(443.897, 441.423, 437.195));\n p += dot(p, p.yxz + 19.19);\n return fract((p.x + p.y) * p.z);\n}\n\n// 3D Value noise\nfloat noise(vec3 p) {\n vec3 i = floor(p);\n vec3 f = fract(p);\n f = f * f * (3.0 - 2.0 * f);\n \n float n = mix(\n mix(\n mix(hash(i + vec3(0,0,0)), hash(i + vec3(1,0,0)), f.x),\n mix(hash(i + vec3(0,1,0)), hash(i + vec3(1,1,0)), f.x),\n f.y\n ),\n mix(\n mix(hash(i + vec3(0,0,1)), hash(i + vec3(1,0,1)), f.x),\n mix(hash(i + vec3(0,1,1)), hash(i + vec3(1,1,1)), f.x),\n f.y\n ),\n f.z\n );\n return n;\n}\n\n// FBM (Fractal Brownian Motion)\nfloat fbm(vec3 p, int octaves) {\n float value = 0.0;\n float amplitude = 0.5;\n float frequency = 1.0;\n \n for (int i = 0; i < 6; i++) {\n if (i >= octaves) break;\n value += amplitude * noise(p * frequency);\n amplitude *= 0.5;\n frequency *= 2.0;\n }\n \n return value;\n}\n\n \n float dustParticle(vec3 pos, float size) {\n // Create discrete particles using 3D grid\n vec3 grid = fract(pos / size) - 0.5;\n float particle = length(grid) - 0.3;\n \n // Randomize per-cell\n vec3 cellId = floor(pos / size);\n float random = hash(cellId);\n \n // Only show some particles\n if (random > uDensity) return 0.0;\n \n // Particle brightness based on light direction\n vec3 lightDir = normalize(uLightDirection);\n float brightness = max(0.2, dot(normalize(grid), lightDir));\n \n return smoothstep(0.1, 0.0, particle) * brightness;\n }\n \n void main() {\n vec4 sceneColor = texture2D(tDiffuse, vUv);\n \n // Raymarch for dust particles\n vec3 rayOrigin = uCameraPosition;\n vec3 rayDir = normalize(vec3(\n (vUv.x - 0.5) * uResolution.x / uResolution.y,\n vUv.y - 0.5,\n 1.0\n ));\n \n float dustAccum = 0.0;\n #define MAX_DUST_STEPS 16\n float stepSize = 2.0;\n \n vec3 pos = rayOrigin;\n for (int i = 0; i < MAX_DUST_STEPS; i++) {\n pos += rayDir * stepSize;\n \n // Animate dust floating\n vec3 animPos = pos + vec3(\n sin(uTime * 0.5 + pos.y) * 0.5,\n uTime * 0.2,\n cos(uTime * 0.3 + pos.x) * 0.5\n );\n \n dustAccum += dustParticle(animPos, uParticleSize * 20.0);\n }\n \n // Add dust glow to scene\n vec3 dustColor = vec3(1.0, 0.95, 0.85) * dustAccum * 0.02;\n \n gl_FragColor = vec4(sceneColor.rgb + dustColor, 1.0);\n }\n"
Defined in: volumetrics.ts:480
