|
1 |
| -import { Color, Vector2, Vector3, REVISION, type Texture } from 'three' |
| 1 | +import { Color, Vector2, Vector3, type Texture } from 'three' |
2 | 2 | import { shaderMaterial } from '../core/shaderMaterial'
|
| 3 | +import { version } from '../helpers/constants' |
3 | 4 |
|
4 | 5 | type SpotLightMaterialProps = {
|
5 | 6 | depth: Texture | null
|
@@ -31,67 +32,73 @@ export const SpotLightMaterial = shaderMaterial<SpotLightMaterialProps>(
|
31 | 32 | },
|
32 | 33 | /* glsl */ `
|
33 | 34 | varying vec3 vNormal;
|
34 |
| - varying vec3 vWorldPosition; |
35 | 35 | varying float vViewZ;
|
36 | 36 | varying float vIntensity;
|
37 | 37 | uniform vec3 spotPosition;
|
38 | 38 | uniform float attenuation;
|
39 | 39 |
|
| 40 | + #include <common> |
| 41 | + #include <logdepthbuf_pars_vertex> |
| 42 | +
|
40 | 43 | void main() {
|
41 | 44 | // compute intensity
|
42 |
| - vNormal = normalize( normalMatrix * normal ); |
43 |
| - vec4 worldPosition = modelMatrix * vec4( position, 1.0 ); |
44 |
| - vWorldPosition = worldPosition.xyz; |
| 45 | + vNormal = normalize(normalMatrix * normal); |
| 46 | + vec4 worldPosition = modelMatrix * vec4(position, 1); |
45 | 47 | vec4 viewPosition = viewMatrix * worldPosition;
|
46 | 48 | vViewZ = viewPosition.z;
|
47 |
| - float intensity = distance(worldPosition.xyz, spotPosition) / attenuation; |
48 |
| - intensity = 1.0 - clamp(intensity, 0.0, 1.0); |
49 |
| - vIntensity = intensity; |
50 |
| - // set gl_Position |
51 |
| - gl_Position = projectionMatrix * viewPosition; |
52 | 49 |
|
| 50 | + vIntensity = 1.0 - saturate(distance(worldPosition.xyz, spotPosition) / attenuation); |
| 51 | +
|
| 52 | + gl_Position = projectionMatrix * viewPosition; |
| 53 | +
|
| 54 | + #include <logdepthbuf_vertex> |
53 | 55 | }`,
|
54 | 56 | /* glsl */ `
|
55 |
| - #include <packing> |
56 |
| -
|
57 | 57 | varying vec3 vNormal;
|
58 |
| - varying vec3 vWorldPosition; |
| 58 | + varying float vViewZ; |
| 59 | + varying float vIntensity; |
| 60 | +
|
59 | 61 | uniform vec3 lightColor;
|
60 |
| - uniform vec3 spotPosition; |
61 |
| - uniform float attenuation; |
62 | 62 | uniform float anglePower;
|
63 | 63 | uniform sampler2D depth;
|
64 | 64 | uniform vec2 resolution;
|
65 | 65 | uniform float cameraNear;
|
66 | 66 | uniform float cameraFar;
|
67 |
| - varying float vViewZ; |
68 |
| - varying float vIntensity; |
69 | 67 | uniform float opacity;
|
70 | 68 |
|
71 |
| - float readDepth( sampler2D depthSampler, vec2 coord ) { |
72 |
| - float fragCoordZ = texture2D( depthSampler, coord ).x; |
73 |
| - float viewZ = perspectiveDepthToViewZ(fragCoordZ, cameraNear, cameraFar); |
| 69 | + #include <packing> |
| 70 | + #include <logdepthbuf_pars_fragment> |
| 71 | +
|
| 72 | + float readDepth(sampler2D depthSampler, vec2 uv) { |
| 73 | + float fragCoordZ = texture(depthSampler, uv).r; |
| 74 | +
|
| 75 | + // https://github.com/mrdoob/three.js/issues/23072 |
| 76 | + #ifdef USE_LOGDEPTHBUF |
| 77 | + float viewZ = 1.0 - exp2(fragCoordZ * log(cameraFar + 1.0) / log(2.0)); |
| 78 | + #else |
| 79 | + float viewZ = perspectiveDepthToViewZ(fragCoordZ, cameraNear, cameraFar); |
| 80 | + #endif |
| 81 | +
|
74 | 82 | return viewZ;
|
75 | 83 | }
|
76 | 84 |
|
77 |
| - void main() { |
78 |
| - float d = 1.0; |
79 |
| - bool isSoft = resolution[0] > 0.0 && resolution[1] > 0.0; |
80 |
| - if (isSoft) { |
81 |
| - vec2 sUv = gl_FragCoord.xy / resolution; |
82 |
| - d = readDepth(depth, sUv); |
83 |
| - } |
84 |
| - float intensity = vIntensity; |
85 |
| - vec3 normal = vec3(vNormal.x, vNormal.y, abs(vNormal.z)); |
86 |
| - float angleIntensity = pow( dot(normal, vec3(0.0, 0.0, 1.0)), anglePower ); |
87 |
| - intensity *= angleIntensity; |
| 85 | + void main() { |
| 86 | + #include <logdepthbuf_fragment> |
| 87 | +
|
| 88 | + vec3 normal = vec3(vNormal.x, vNormal.y, abs(vNormal.z)); |
| 89 | + float angleIntensity = pow(dot(normal, vec3(0, 0, 1)), anglePower); |
| 90 | + float intensity = vIntensity * angleIntensity; |
| 91 | +
|
88 | 92 | // fades when z is close to sampled depth, meaning the cone is intersecting existing geometry
|
| 93 | + bool isSoft = resolution[0] > 0.0 && resolution[1] > 0.0; |
89 | 94 | if (isSoft) {
|
90 |
| - intensity *= smoothstep(0., 1., vViewZ - d); |
| 95 | + vec2 uv = gl_FragCoord.xy / resolution; |
| 96 | + intensity *= smoothstep(0.0, 1.0, vViewZ - readDepth(depth, uv)); |
91 | 97 | }
|
| 98 | +
|
92 | 99 | gl_FragColor = vec4(lightColor, intensity * opacity);
|
93 | 100 |
|
94 | 101 | #include <tonemapping_fragment>
|
95 |
| - #include <${parseInt(REVISION.replace(/\D+/g, '')) >= 154 ? 'colorspace_fragment' : 'encodings_fragment'}> |
| 102 | + #include <${version >= 154 ? 'colorspace_fragment' : 'encodings_fragment'}> |
96 | 103 | }`
|
97 | 104 | )
|
0 commit comments