-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
/
NodeMaterial.js
460 lines (251 loc) · 9.61 KB
/
NodeMaterial.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import { Material, ShaderMaterial, NoColorSpace } from 'three';
import { getNodeChildren, getCacheKey } from '../core/NodeUtils.js';
import { attribute } from '../core/AttributeNode.js';
import { diffuseColor } from '../core/PropertyNode.js';
import { materialNormal } from '../accessors/ExtendedMaterialNode.js';
import { materialAlphaTest, materialColor, materialOpacity, materialEmissive } from '../accessors/MaterialNode.js';
import { modelViewProjection } from '../accessors/ModelViewProjectionNode.js';
import { transformedNormalView } from '../accessors/NormalNode.js';
import { instance } from '../accessors/InstanceNode.js';
import { positionLocal } from '../accessors/PositionNode.js';
import { skinning } from '../accessors/SkinningNode.js';
import { texture } from '../accessors/TextureNode.js';
import { cubeTexture } from '../accessors/CubeTextureNode.js';
import { lightsWithoutWrap } from '../lighting/LightsNode.js';
import { mix } from '../math/MathNode.js';
import { float, vec3, vec4 } from '../shadernode/ShaderNode.js';
import AONode from '../lighting/AONode.js';
import EnvironmentNode from '../lighting/EnvironmentNode.js';
const NodeMaterials = new Map();
class NodeMaterial extends ShaderMaterial {
constructor() {
super();
this.isNodeMaterial = true;
this.type = this.constructor.name;
this.lights = true;
this.normals = true;
this.lightsNode = null;
this.envNode = null;
this.colorNode = null;
this.normalNode = null;
this.opacityNode = null;
this.backdropNode = null;
this.backdropAlphaNode = null;
this.alphaTestNode = null;
this.positionNode = null;
}
customProgramCacheKey() {
return getCacheKey( this );
}
build( builder ) {
this.construct( builder );
}
construct( builder ) {
// < VERTEX STAGE >
builder.addStack();
builder.stack.outputNode = this.constructPosition( builder );
builder.addFlow( 'vertex', builder.removeStack() );
// < FRAGMENT STAGE >
builder.addStack();
if ( this.normals === true ) this.constructNormal( builder );
this.constructDiffuseColor( builder );
this.constructVariants( builder );
const outgoingLightNode = this.constructLighting( builder );
builder.stack.outputNode = this.constructOutput( builder, outgoingLightNode, diffuseColor.a );
builder.addFlow( 'fragment', builder.removeStack() );
}
constructPosition( builder ) {
const object = builder.object;
let vertex = positionLocal;
if ( this.positionNode !== null ) {
vertex = vertex.bypass( positionLocal.assign( this.positionNode ) );
}
if ( ( object.instanceMatrix && object.instanceMatrix.isInstancedBufferAttribute === true ) && builder.isAvailable( 'instance' ) === true ) {
vertex = vertex.bypass( instance( object ) );
}
if ( object.isSkinnedMesh === true ) {
vertex = vertex.bypass( skinning( object ) );
}
builder.context.vertex = vertex;
return modelViewProjection();
}
constructDiffuseColor( { stack, geometry } ) {
let colorNode = this.colorNode ? vec4( this.colorNode ) : materialColor;
// VERTEX COLORS
if ( this.vertexColors === true && geometry.hasAttribute( 'color' ) ) {
colorNode = vec4( colorNode.xyz.mul( attribute( 'color' ) ), colorNode.a );
}
// COLOR
stack.assign( diffuseColor, colorNode );
// OPACITY
const opacityNode = this.opacityNode ? float( this.opacityNode ) : materialOpacity;
stack.assign( diffuseColor.a, diffuseColor.a.mul( opacityNode ) );
// ALPHA TEST
if ( this.alphaTestNode || this.alphaTest > 0 ) {
const alphaTestNode = this.alphaTestNode ? float( this.alphaTestNode ) : materialAlphaTest;
stack.add( diffuseColor.a.lessThanEqual( alphaTestNode ).discard() );
}
}
constructVariants( /*builder*/ ) {
// Interface function.
}
constructNormal( { stack } ) {
// NORMAL VIEW
const normalNode = this.normalNode ? vec3( this.normalNode ) : materialNormal;
stack.assign( transformedNormalView, normalNode );
return normalNode;
}
getEnvNode( builder ) {
let node = null;
if ( this.envNode ) {
node = this.envNode;
} else if ( this.envMap ) {
node = this.envMap.isCubeTexture ? cubeTexture( this.envMap ) : texture( this.envMap );
} else if ( builder.environmentNode ) {
node = builder.environmentNode;
}
return node;
}
constructLights( builder ) {
const envNode = this.getEnvNode( builder );
//
const materialLightsNode = [];
if ( envNode ) {
materialLightsNode.push( new EnvironmentNode( envNode ) );
}
if ( builder.material.aoMap ) {
materialLightsNode.push( new AONode( texture( builder.material.aoMap ) ) );
}
let lightsNode = this.lightsNode || builder.lightsNode;
if ( materialLightsNode.length > 0 ) {
lightsNode = lightsWithoutWrap( [ ...lightsNode.lightNodes, ...materialLightsNode ] );
}
return lightsNode;
}
constructLightingModel( /*builder*/ ) {
// Interface function.
}
constructLighting( builder ) {
const { material } = builder;
const { backdropNode, backdropAlphaNode, emissiveNode } = this;
// OUTGOING LIGHT
const lights = this.lights === true || this.lightsNode !== null;
const lightsNode = lights ? this.constructLights( builder ) : null;
let outgoingLightNode = diffuseColor.rgb;
if ( lightsNode && lightsNode.hasLight !== false ) {
const lightingModelNode = this.constructLightingModel( builder );
outgoingLightNode = lightsNode.lightingContext( lightingModelNode, backdropNode, backdropAlphaNode );
} else if ( backdropNode !== null ) {
outgoingLightNode = vec3( backdropAlphaNode !== null ? mix( outgoingLightNode, backdropNode, backdropAlphaNode ) : backdropNode );
}
// EMISSIVE
if ( ( emissiveNode && emissiveNode.isNode === true ) || ( material.emissive && material.emissive.isColor === true ) ) {
outgoingLightNode = outgoingLightNode.add( emissiveNode ? vec3( emissiveNode ) : materialEmissive );
}
return outgoingLightNode;
}
constructOutput( builder, outgoingLight, opacity ) {
const renderer = builder.renderer;
// TONE MAPPING
const toneMappingNode = builder.toneMappingNode;
if ( toneMappingNode ) {
outgoingLight = toneMappingNode.context( { color: outgoingLight } );
}
// @TODO: Optimize outputNode to vec3.
let outputNode = vec4( outgoingLight, opacity );
// ENCODING
const renderTarget = renderer.getRenderTarget();
let outputColorSpace;
if ( renderTarget !== null ) {
outputColorSpace = renderTarget.texture.colorSpace;
} else {
outputColorSpace = renderer.outputColorSpace;
}
if ( outputColorSpace !== NoColorSpace ) outputNode = outputNode.colorSpace( outputColorSpace );
// FOG
const fogNode = builder.fogNode;
if ( fogNode ) outputNode = vec4( fogNode.mixAssign( outputNode.rgb ), outputNode.a );
return outputNode;
}
setDefaultValues( material ) {
// This approach is to reuse the native refreshUniforms*
// and turn available the use of features like transmission and environment in core
for ( const property in material ) {
const value = material[ property ];
if ( this[ property ] === undefined ) {
this[ property ] = value;
if ( value && value.clone ) this[ property ] = value.clone();
}
}
Object.assign( this.defines, material.defines );
const descriptors = Object.getOwnPropertyDescriptors( material.constructor.prototype );
for ( const key in descriptors ) {
if ( Object.getOwnPropertyDescriptor( this.constructor.prototype, key ) === undefined &&
descriptors[ key ].get !== undefined ) {
Object.defineProperty( this.constructor.prototype, key, descriptors[ key ] );
}
}
}
toJSON( meta ) {
const isRoot = ( meta === undefined || typeof meta === 'string' );
if ( isRoot ) {
meta = {
textures: {},
images: {},
nodes: {}
};
}
const data = Material.prototype.toJSON.call( this, meta );
const nodeChildren = getNodeChildren( this );
data.inputNodes = {};
for ( const { property, childNode } of nodeChildren ) {
data.inputNodes[ property ] = childNode.toJSON( meta ).uuid;
}
// TODO: Copied from Object3D.toJSON
function extractFromCache( cache ) {
const values = [];
for ( const key in cache ) {
const data = cache[ key ];
delete data.metadata;
values.push( data );
}
return values;
}
if ( isRoot ) {
const textures = extractFromCache( meta.textures );
const images = extractFromCache( meta.images );
const nodes = extractFromCache( meta.nodes );
if ( textures.length > 0 ) data.textures = textures;
if ( images.length > 0 ) data.images = images;
if ( nodes.length > 0 ) data.nodes = nodes;
}
return data;
}
static fromMaterial( material ) {
if ( material.isNodeMaterial === true ) { // is already a node material
return material;
}
const type = material.type.replace( 'Material', 'NodeMaterial' );
const nodeMaterial = createNodeMaterialFromType( type );
if ( nodeMaterial === undefined ) {
throw new Error( `NodeMaterial: Material "${ material.type }" is not compatible.` );
}
for ( const key in material ) {
nodeMaterial[ key ] = material[ key ];
}
return nodeMaterial;
}
}
export default NodeMaterial;
export function addNodeMaterial( nodeMaterial ) {
if ( typeof nodeMaterial !== 'function' || ! nodeMaterial.name ) throw new Error( `Node material ${ nodeMaterial.name } is not a class` );
if ( NodeMaterials.has( nodeMaterial.name ) ) throw new Error( `Redefinition of node material ${ nodeMaterial.name }` );
NodeMaterials.set( nodeMaterial.name, nodeMaterial );
}
export function createNodeMaterialFromType( type ) {
const Material = NodeMaterials.get( type );
if ( Material !== undefined ) {
return new Material();
}
}
addNodeMaterial( NodeMaterial );