-
-
Notifications
You must be signed in to change notification settings - Fork 36.5k
Expand file tree
/
Copy pathSpotLightNode.js
More file actions
178 lines (124 loc) · 4.6 KB
/
Copy pathSpotLightNode.js
File metadata and controls
178 lines (124 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import AnalyticLightNode from './AnalyticLightNode.js';
import { getDistanceAttenuation } from './LightUtils.js';
import { uniform } from '../core/UniformNode.js';
import { smoothstep } from '../math/MathNode.js';
import { renderGroup } from '../core/UniformGroupNode.js';
import { lightTargetDirection, lightProjectionUV, lightPosition, lightTargetPosition } from '../accessors/Lights.js';
import { texture } from '../accessors/TextureNode.js';
import { positionWorld } from '../accessors/Position.js';
/**
* Module for representing spot lights as nodes.
*
* @augments AnalyticLightNode
*/
class SpotLightNode extends AnalyticLightNode {
static get type() {
return 'SpotLightNode';
}
/**
* Constructs a new spot light node.
*
* @param {?SpotLight} [light=null] - The spot light source.
*/
constructor( light = null ) {
super( light );
/**
* Uniform node representing the cone cosine.
*
* @type {UniformNode<float>}
*/
this.coneCosNode = uniform( 0 ).setGroup( renderGroup );
/**
* Uniform node representing the penumbra cosine.
*
* @type {UniformNode<float>}
*/
this.penumbraCosNode = uniform( 0 ).setGroup( renderGroup );
/**
* Uniform node representing the cutoff distance.
*
* @type {UniformNode<float>}
*/
this.cutoffDistanceNode = uniform( 0 ).setGroup( renderGroup );
/**
* Uniform node representing the decay exponent.
*
* @type {UniformNode<float>}
*/
this.decayExponentNode = uniform( 0 ).setGroup( renderGroup );
/**
* Uniform node representing the light color.
*
* @type {UniformNode<Color>}
*/
this.colorNode = uniform( this.color ).setGroup( renderGroup );
}
/**
* Overwritten to updated spot light specific uniforms.
*
* @param {NodeFrame} frame - A reference to the current node frame.
*/
update( frame ) {
super.update( frame );
const { light } = this;
this.coneCosNode.value = Math.cos( light.angle );
this.penumbraCosNode.value = Math.cos( light.angle * ( 1 - light.penumbra ) );
this.cutoffDistanceNode.value = light.distance;
this.decayExponentNode.value = light.decay;
}
/**
* Computes the spot attenuation for the given angle.
*
* @param {NodeBuilder} builder - The node builder.
* @param {Node<float>} angleCosine - The angle to compute the spot attenuation for.
* @param {Node<float>} azimuthAngle - The azimuthal angle
* @return {Node<float>} The spot attenuation.
*/
getSpotAttenuation( builder, angleCosine, azimuthAngle ) {
const { coneCosNode, penumbraCosNode } = this;
return smoothstep( coneCosNode, penumbraCosNode, angleCosine );
}
getLightCoord( builder ) {
const properties = builder.getNodeProperties( this );
let projectionUV = properties.projectionUV;
if ( projectionUV === undefined ) {
projectionUV = lightProjectionUV( this.light, builder.context.positionWorld );
properties.projectionUV = projectionUV;
}
return projectionUV;
}
setupDirect( builder ) {
const { colorNode, cutoffDistanceNode, decayExponentNode, light } = this;
const vecLight = lightPosition( light ).sub( builder.context.positionWorld || positionWorld ).normalize();
const vecTargetToLight = lightPosition( light ).sub( lightTargetPosition( light ) ).normalize();
const vecTarget = lightTargetPosition( light ).normalize();
const vecRight = vecTarget.cross( vecTargetToLight ).normalize();
const vecUp = vecTargetToLight.cross( vecRight ).normalize();
const rotationAngle = vecLight.dot( vecRight ).atan( vecLight.dot( vecUp ) ).add( Math.PI * 2 );
const lightVector = this.getLightVector( builder );
const lightDirection = lightVector.normalize();
const angleCos = lightDirection.dot( lightTargetDirection( light ) );
const spotAttenuation = this.getSpotAttenuation( builder, angleCos, rotationAngle );
const lightDistance = lightVector.length();
const lightAttenuation = getDistanceAttenuation( {
lightDistance,
cutoffDistance: cutoffDistanceNode,
decayExponent: decayExponentNode
} );
let lightColor = colorNode.mul( spotAttenuation ).mul( lightAttenuation );
let projected, lightCoord;
if ( light.colorNode ) {
lightCoord = this.getLightCoord( builder );
projected = light.colorNode( lightCoord );
} else if ( light.map ) {
lightCoord = this.getLightCoord( builder );
projected = texture( light.map, lightCoord.xy ).onRenderUpdate( () => light.map );
}
if ( projected ) {
const inSpotLightMap = lightCoord.mul( 2. ).sub( 1. ).abs().lessThan( 1. ).all();
lightColor = inSpotLightMap.select( lightColor.mul( projected ), lightColor );
}
return { lightColor, lightDirection };
}
}
export default SpotLightNode;