-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
/
LightsNode.js
213 lines (113 loc) · 3.88 KB
/
LightsNode.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import Node from '../core/Node.js';
import AnalyticLightNode from './AnalyticLightNode.js';
import { nodeObject, nodeProxy, vec3 } from '../shadernode/ShaderNode.js';
const LightNodes = new WeakMap();
const sortLights = ( lights ) => {
return lights.sort( ( a, b ) => a.id - b.id );
};
class LightsNode extends Node {
constructor( lightNodes = [] ) {
super( 'vec3' );
this.totalDiffuseNode = vec3().temp( 'totalDiffuse' );
this.totalSpecularNode = vec3().temp( 'totalSpecular' );
this.outgoingLightNode = vec3().temp( 'outgoingLight' );
this.lightNodes = lightNodes;
this._hash = null;
}
get hasLight() {
return this.lightNodes.length > 0;
}
getHash() {
if ( this._hash === null ) {
const hash = [];
for ( const lightNode of this.lightNodes ) {
hash.push( lightNode.getHash() );
}
this._hash = 'lights-' + hash.join( ',' );
}
return this._hash;
}
analyze( builder ) {
const properties = builder.getDataFromNode( this );
for ( const node of properties.nodes ) {
node.build( builder );
}
}
setup( builder ) {
const context = builder.context;
const lightingModel = context.lightingModel;
let outgoingLightNode = this.outgoingLightNode;
if ( lightingModel ) {
const { lightNodes, totalDiffuseNode, totalSpecularNode } = this;
context.outgoingLight = outgoingLightNode;
const stack = builder.addStack();
//
const properties = builder.getDataFromNode( this );
properties.nodes = stack.nodes;
//
lightingModel.start( context, stack, builder );
// lights
for ( const lightNode of lightNodes ) {
lightNode.build( builder );
}
//
lightingModel.indirect( context, stack, builder );
//
const { backdrop, backdropAlpha } = context;
const { directDiffuse, directSpecular, indirectDiffuse, indirectSpecular } = context.reflectedLight;
let totalDiffuse = directDiffuse.add( indirectDiffuse );
if ( backdrop !== null ) {
if ( backdropAlpha !== null ) {
totalDiffuse = vec3( backdropAlpha.mix( totalDiffuse, backdrop ) );
} else {
totalDiffuse = vec3( backdrop );
}
context.material.transparent = true;
}
totalDiffuseNode.assign( totalDiffuse );
totalSpecularNode.assign( directSpecular.add( indirectSpecular ) );
outgoingLightNode.assign( totalDiffuseNode.add( totalSpecularNode ) );
//
lightingModel.finish( context, stack, builder );
//
outgoingLightNode = outgoingLightNode.bypass( builder.removeStack() );
}
return outgoingLightNode;
}
_getLightNodeById( id ) {
for ( const lightNode of this.lightNodes ) {
if ( lightNode.isAnalyticLightNode && lightNode.light.id === id ) {
return lightNode;
}
}
return null;
}
fromLights( lights = [] ) {
const lightNodes = [];
lights = sortLights( lights );
for ( const light of lights ) {
let lightNode = this._getLightNodeById( light.id );
if ( lightNode === null ) {
const lightClass = light.constructor;
const lightNodeClass = LightNodes.has( lightClass ) ? LightNodes.get( lightClass ) : AnalyticLightNode;
lightNode = nodeObject( new lightNodeClass( light ) );
}
lightNodes.push( lightNode );
}
this.lightNodes = lightNodes;
this._hash = null;
return this;
}
}
export default LightsNode;
export const lights = ( lights ) => nodeObject( new LightsNode().fromLights( lights ) );
export const lightsNode = nodeProxy( LightsNode );
export function addLightNode( lightClass, lightNodeClass ) {
if ( LightNodes.has( lightClass ) ) {
console.warn( `Redefinition of light node ${ lightNodeClass.type }` );
return;
}
if ( typeof lightClass !== 'function' ) throw new Error( `Light ${ lightClass.name } is not a class` );
if ( typeof lightNodeClass !== 'function' || ! lightNodeClass.type ) throw new Error( `Light node ${ lightNodeClass.type } is not a class` );
LightNodes.set( lightClass, lightNodeClass );
}