-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththree.material.note.js
4668 lines (3041 loc) · 134 KB
/
three.material.note.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* bindingState = {
// for backward compatibility on non-VAO support browser
geometry: null,
program: null,
wireframe: false,
newAttributes: newAttributes,
enabledAttributes: enabledAttributes,
attributeDivisors: attributeDivisors,
object: vao,
attributes: {},
index: null
};
* bindingStates[geometry.id] = state;
* if (state === undefined) {
state = createBindingState(createVertexArrayObject());
stateMap[wireframe] = state;
}
*
*/
/**
*
* renderBufferDirect (camera, scene, geometry, material, object, group)
* program = setProgram(camera, scene, geometry, material, object); // new WebGLPrograme(), 计算 refreshMaterial
* state.setMaterial(material, frontFaceCW); // 根据material设置gl的一些状态
* bindingStates.setup(object, material, program, geometry, index); // 当前bindingState与geometry里attr进行对比如果有差异则重新bindBuffer,完成后更新bindingState bindingState[geometry.id] = {,,,}
* renderer.setMode
* renderer.renderInstances
*
*/
/**
* setProgram
*/
/**
*
* properties = new WebGLProperties(); // weakmap <material, properties>
* const materialProperties = properties.get(material);
* 计算needsProgramChange
* if (!object.isInstancedMesh && materialProperties.instancing === true) {
needsProgramChange = true;
}
if (needsProgramChange === true) {
program = getProgram(material, scene, object);
}
const p_uniforms = program.getUniforms()
const m_uniforms = materialProperties.uniforms;
* state.useProgram(program.program)
设置p_uniforms
p_uniforms.setValue(_gl, 'projectionMatrix', camera.projectionMatrix);
* materials.refreshMaterialUniforms(m_uniforms, material, _pixelRatio, _height, _transmissionRenderTarget);
WebGLUniforms.upload(_gl, materialProperties.uniformsList, m_uniforms, textures);
// UBOs
const groups = material.uniformsGroups;
uniformsGroups.update(group, program);
uniformsGroups.bind(group, program);
*/
/**
* getProgram(material, scene, object)
* const materialProperties = properties.get(material);
* const parameters = programCache.getParameters(material, lights.state, shadowsArray, scene, object); // {map,envMap, uniforms...}
const programCacheKey = programCache.getProgramCacheKey(parameters); [k1,v1,k2,v2].join()
* let programs = materialProperties.programs;
if (programs === undefined) { programs = new Map(); materialProperties.programs = programs;}
* let program = programs.get(programCacheKey);
if (program !== undefined) {
updateCommonMaterialProperties(material, parameters);
} else {
parameters.uniforms = programCache.getUniforms(material);
program = programCache.acquireProgram(parameters, programCacheKey); // new WebGLProgram(), shaderstr, gl.createPrograme()
programs.set(programCacheKey, program);
}
const uniforms = materialProperties.uniforms;
updateCommonMaterialProperties(material, parameters); properties[material].x = parameters.x
if (materialProperties.needsLights) {
uniforms.*.value = *
}
const progUniforms = program.getUniforms();
const uniformsList = WebGLUniforms.seqWithValue(progUniforms.seq, uniforms);
materialProperties.currentProgram = program;
materialProperties.uniformsList = uniformsList;
return program
*/
/**
*
* materialProperties: {
* envMap,
* uniforms,
* programes: {
* programCacheKey1: programe1,
* programCacheKey2: programe2,
* },
* ...
* }
*/
/**
* materialProperties.programs
* programs = new Map();
materialProperties.programs = programs;
const p_uniforms = program.getUniforms(),
m_uniforms = materialProperties.uniforms;
*/
/**
*
* let program = materialProperties.currentProgram;
if (needsProgramChange === true) {
program = getProgram(material, scene, object);
}
*/
// setProgram(camera, scene, geometry, material, object);
// state.setMaterial(material, frontFaceCW);
/**
* if (material.wireframe === true) {
index = geometries.getWireframeAttribute(geometry);
if (index === undefined) return;
rangeFactor = 2;
}
*/
// bindingStates.setup(object, material, program, geometry, index); // bindingStates = new WebGLBindingStates( _gl, extensions, attributes, capabilities );
/**
*
* if (object.isMesh) {
if (material.wireframe === true) {
state.setLineWidth(material.wireframeLinewidth * getTargetPixelRatio());
renderer.setMode(_gl.LINES);
} else {
renderer.setMode(_gl.TRIANGLES);
}
}
*/
/**
*
* if (object.isInstancedMesh) {
renderer.renderInstances(drawStart, drawCount, object.count);
} else if (geometry.isInstancedBufferGeometry) {
const maxInstanceCount = geometry._maxInstanceCount !== undefined ? geometry._maxInstanceCount : Infinity;
const instanceCount = Math.min(geometry.instanceCount, maxInstanceCount);
renderer.renderInstances(drawStart, drawCount, instanceCount);
} else {
renderer.render(drawStart, drawCount);
}
*/
this.renderBufferDirect = function (camera, scene, geometry, material, object, group) {
if (scene === null) scene = _emptyScene; // renderBufferDirect second parameter used to be fog (could be null)
const frontFaceCW = (object.isMesh && object.matrixWorld.determinant() < 0);
const program = setProgram(camera, scene, geometry, material, object);
state.setMaterial(material, frontFaceCW);
let index = geometry.index;
let rangeFactor = 1;
if (material.wireframe === true) {
index = geometries.getWireframeAttribute(geometry);
if (index === undefined) return;
rangeFactor = 2;
}
const drawRange = geometry.drawRange;
const position = geometry.attributes.position;
let drawStart = drawRange.start * rangeFactor;
let drawEnd = (drawRange.start + drawRange.count) * rangeFactor;
if (group !== null) {
drawStart = Math.max(drawStart, group.start * rangeFactor);
drawEnd = Math.min(drawEnd, (group.start + group.count) * rangeFactor);
}
if (index !== null) {
drawStart = Math.max(drawStart, 0);
drawEnd = Math.min(drawEnd, index.count);
} else if (position !== undefined && position !== null) {
drawStart = Math.max(drawStart, 0);
drawEnd = Math.min(drawEnd, position.count);
}
const drawCount = drawEnd - drawStart;
if (drawCount < 0 || drawCount === Infinity) return;
bindingStates.setup(object, material, program, geometry, index);
let attribute;
let renderer = bufferRenderer;
if (index !== null) {
attribute = attributes.get(index);
renderer = indexedBufferRenderer;
renderer.setIndex(attribute);
}
if (object.isMesh) {
if (material.wireframe === true) {
state.setLineWidth(material.wireframeLinewidth * getTargetPixelRatio());
renderer.setMode(_gl.LINES);
} else {
renderer.setMode(_gl.TRIANGLES);
}
} else if (object.isLine) {
let lineWidth = material.linewidth;
if (lineWidth === undefined) lineWidth = 1; // Not using Line*Material
state.setLineWidth(lineWidth * getTargetPixelRatio());
if (object.isLineSegments) {
renderer.setMode(_gl.LINES);
} else if (object.isLineLoop) {
renderer.setMode(_gl.LINE_LOOP);
} else {
renderer.setMode(_gl.LINE_STRIP);
}
} else if (object.isPoints) {
renderer.setMode(_gl.POINTS);
} else if (object.isSprite) {
renderer.setMode(_gl.TRIANGLES);
}
if (object.isInstancedMesh) {
renderer.renderInstances(drawStart, drawCount, object.count);
} else if (geometry.isInstancedBufferGeometry) {
const maxInstanceCount = geometry._maxInstanceCount !== undefined ? geometry._maxInstanceCount : Infinity;
const instanceCount = Math.min(geometry.instanceCount, maxInstanceCount);
renderer.renderInstances(drawStart, drawCount, instanceCount);
} else {
renderer.render(drawStart, drawCount);
}
};
function setProgram(camera, scene, geometry, material, object) {
if (scene.isScene !== true) scene = _emptyScene; // scene could be a Mesh, Line, Points, ...
textures.resetTextureUnits();
const fog = scene.fog;
const environment = material.isMeshStandardMaterial ? scene.environment : null;
const colorSpace = (_currentRenderTarget === null) ? _this.outputColorSpace : (_currentRenderTarget.isXRRenderTarget === true ? _currentRenderTarget.texture.colorSpace : LinearSRGBColorSpace);
const envMap = (material.isMeshStandardMaterial ? cubeuvmaps : cubemaps).get(material.envMap || environment);
const vertexAlphas = material.vertexColors === true && !!geometry.attributes.color && geometry.attributes.color.itemSize === 4;
const vertexTangents = !!geometry.attributes.tangent && (!!material.normalMap || material.anisotropy > 0);
const morphTargets = !!geometry.morphAttributes.position;
const morphNormals = !!geometry.morphAttributes.normal;
const morphColors = !!geometry.morphAttributes.color;
let toneMapping = NoToneMapping;
if (material.toneMapped) {
if (_currentRenderTarget === null || _currentRenderTarget.isXRRenderTarget === true) {
toneMapping = _this.toneMapping;
}
}
const morphAttribute = geometry.morphAttributes.position || geometry.morphAttributes.normal || geometry.morphAttributes.color;
const morphTargetsCount = (morphAttribute !== undefined) ? morphAttribute.length : 0;
const materialProperties = properties.get(material);
const lights = currentRenderState.state.lights;
if (_clippingEnabled === true) {
if (_localClippingEnabled === true || camera !== _currentCamera) {
const useCache =
camera === _currentCamera &&
material.id === _currentMaterialId;
// we might want to call this function with some ClippingGroup
// object instead of the material, once it becomes feasible
// (#8465, #8379)
clipping.setState(material, camera, useCache);
}
}
//
let needsProgramChange = false;
if (material.version === materialProperties.__version) {
if (materialProperties.needsLights && (materialProperties.lightsStateVersion !== lights.state.version)) {
needsProgramChange = true;
} else if (materialProperties.outputColorSpace !== colorSpace) {
needsProgramChange = true;
} else if (object.isInstancedMesh && materialProperties.instancing === false) {
needsProgramChange = true;
} else if (!object.isInstancedMesh && materialProperties.instancing === true) {
needsProgramChange = true;
} else if (object.isSkinnedMesh && materialProperties.skinning === false) {
needsProgramChange = true;
} else if (!object.isSkinnedMesh && materialProperties.skinning === true) {
needsProgramChange = true;
} else if (object.isInstancedMesh && materialProperties.instancingColor === true && object.instanceColor === null) {
needsProgramChange = true;
} else if (object.isInstancedMesh && materialProperties.instancingColor === false && object.instanceColor !== null) {
needsProgramChange = true;
} else if (materialProperties.envMap !== envMap) {
needsProgramChange = true;
} else if (material.fog === true && materialProperties.fog !== fog) {
needsProgramChange = true;
} else if (materialProperties.numClippingPlanes !== undefined &&
(materialProperties.numClippingPlanes !== clipping.numPlanes ||
materialProperties.numIntersection !== clipping.numIntersection)) {
needsProgramChange = true;
} else if (materialProperties.vertexAlphas !== vertexAlphas) {
needsProgramChange = true;
} else if (materialProperties.vertexTangents !== vertexTangents) {
needsProgramChange = true;
} else if (materialProperties.morphTargets !== morphTargets) {
needsProgramChange = true;
} else if (materialProperties.morphNormals !== morphNormals) {
needsProgramChange = true;
} else if (materialProperties.morphColors !== morphColors) {
needsProgramChange = true;
} else if (materialProperties.toneMapping !== toneMapping) {
needsProgramChange = true;
} else if (capabilities.isWebGL2 === true && materialProperties.morphTargetsCount !== morphTargetsCount) {
needsProgramChange = true;
}
} else {
needsProgramChange = true;
materialProperties.__version = material.version;
}
//
let program = materialProperties.currentProgram;
if (needsProgramChange === true) {
program = getProgram(material, scene, object);
}
let refreshProgram = false;
let refreshMaterial = false;
let refreshLights = false;
const p_uniforms = program.getUniforms(),
m_uniforms = materialProperties.uniforms;
if (state.useProgram(program.program)) {
refreshProgram = true;
refreshMaterial = true;
refreshLights = true;
}
if (material.id !== _currentMaterialId) {
_currentMaterialId = material.id;
refreshMaterial = true;
}
if (refreshProgram || _currentCamera !== camera) {
// common camera uniforms
p_uniforms.setValue(_gl, 'projectionMatrix', camera.projectionMatrix);
p_uniforms.setValue(_gl, 'viewMatrix', camera.matrixWorldInverse);
const uCamPos = p_uniforms.map.cameraPosition;
if (uCamPos !== undefined) {
uCamPos.setValue(_gl, _vector3.setFromMatrixPosition(camera.matrixWorld));
}
if (capabilities.logarithmicDepthBuffer) {
p_uniforms.setValue(_gl, 'logDepthBufFC',
2.0 / (Math.log(camera.far + 1.0) / Math.LN2));
}
// consider moving isOrthographic to UniformLib and WebGLMaterials, see https://github.com/mrdoob/three.js/pull/26467#issuecomment-1645185067
if (material.isMeshPhongMaterial ||
material.isMeshToonMaterial ||
material.isMeshLambertMaterial ||
material.isMeshBasicMaterial ||
material.isMeshStandardMaterial ||
material.isShaderMaterial) {
p_uniforms.setValue(_gl, 'isOrthographic', camera.isOrthographicCamera === true);
}
if (_currentCamera !== camera) {
_currentCamera = camera;
// lighting uniforms depend on the camera so enforce an update
// now, in case this material supports lights - or later, when
// the next material that does gets activated:
refreshMaterial = true; // set to true on material change
refreshLights = true; // remains set until update done
}
}
// skinning and morph target uniforms must be set even if material didn't change
// auto-setting of texture unit for bone and morph texture must go before other textures
// otherwise textures used for skinning and morphing can take over texture units reserved for other material textures
if (object.isSkinnedMesh) {
p_uniforms.setOptional(_gl, object, 'bindMatrix');
p_uniforms.setOptional(_gl, object, 'bindMatrixInverse');
const skeleton = object.skeleton;
if (skeleton) {
if (capabilities.floatVertexTextures) {
if (skeleton.boneTexture === null) skeleton.computeBoneTexture();
p_uniforms.setValue(_gl, 'boneTexture', skeleton.boneTexture, textures);
p_uniforms.setValue(_gl, 'boneTextureSize', skeleton.boneTextureSize);
} else {
console.warn('THREE.WebGLRenderer: SkinnedMesh can only be used with WebGL 2. With WebGL 1 OES_texture_float and vertex textures support is required.');
}
}
}
const morphAttributes = geometry.morphAttributes;
if (morphAttributes.position !== undefined || morphAttributes.normal !== undefined || (morphAttributes.color !== undefined && capabilities.isWebGL2 === true)) {
morphtargets.update(object, geometry, program);
}
if (refreshMaterial || materialProperties.receiveShadow !== object.receiveShadow) {
materialProperties.receiveShadow = object.receiveShadow;
p_uniforms.setValue(_gl, 'receiveShadow', object.receiveShadow);
}
// https://github.com/mrdoob/three.js/pull/24467#issuecomment-1209031512
if (material.isMeshGouraudMaterial && material.envMap !== null) {
m_uniforms.envMap.value = envMap;
m_uniforms.flipEnvMap.value = (envMap.isCubeTexture && envMap.isRenderTargetTexture === false) ? - 1 : 1;
}
if (refreshMaterial) {
p_uniforms.setValue(_gl, 'toneMappingExposure', _this.toneMappingExposure);
if (materialProperties.needsLights) {
// the current material requires lighting info
// note: all lighting uniforms are always set correctly
// they simply reference the renderer's state for their
// values
//
// use the current material's .needsUpdate flags to set
// the GL state when required
markUniformsLightsNeedsUpdate(m_uniforms, refreshLights);
}
// refresh uniforms common to several materials
if (fog && material.fog === true) {
materials.refreshFogUniforms(m_uniforms, fog);
}
materials.refreshMaterialUniforms(m_uniforms, material, _pixelRatio, _height, _transmissionRenderTarget);
WebGLUniforms.upload(_gl, materialProperties.uniformsList, m_uniforms, textures);
}
if (material.isShaderMaterial && material.uniformsNeedUpdate === true) {
WebGLUniforms.upload(_gl, materialProperties.uniformsList, m_uniforms, textures);
material.uniformsNeedUpdate = false;
}
if (material.isSpriteMaterial) {
p_uniforms.setValue(_gl, 'center', object.center);
}
// common matrices
p_uniforms.setValue(_gl, 'modelViewMatrix', object.modelViewMatrix);
p_uniforms.setValue(_gl, 'normalMatrix', object.normalMatrix);
p_uniforms.setValue(_gl, 'modelMatrix', object.matrixWorld);
// UBOs
if (material.isShaderMaterial || material.isRawShaderMaterial) {
const groups = material.uniformsGroups;
for (let i = 0, l = groups.length; i < l; i++) {
if (capabilities.isWebGL2) {
const group = groups[i];
uniformsGroups.update(group, program);
uniformsGroups.bind(group, program);
} else {
console.warn('THREE.WebGLRenderer: Uniform Buffer Objects can only be used with WebGL 2.');
}
}
}
return program;
}
/**
*
* let programs = materialProperties.programs;
* const parameters = programCache.getParameters(material, lights.state, shadowsArray, scene, object); // mat,geo, object
const programCacheKey = programCache.getProgramCacheKey(parameters);
* const program = programs.get(programCacheKey);
* program = programCache.acquireProgram(parameters, programCacheKey); // WebGLPrograme()
programs.set(programCacheKey, program);
materialProperties.uniforms = parameters.uniforms;
updateCommonMaterialProperties(material, parameters);
materialProperties.needsLights = materialNeedsLights(material);
materialProperties.lightsStateVersion = lightsStateVersion;
if (materialProperties.needsLights) {
uniforms.ambientLightColor.value = lights.state.ambient;
...
}
const progUniforms = program.getUniforms();
const uniformsList = WebGLUniforms.seqWithValue(progUniforms.seq, uniforms);
materialProperties.currentProgram = program;
materialProperties.uniformsList = uniformsList;
return program;
*/
/**
* programCache
* programCache = new WebGLPrograms( _this, cubemaps, cubeuvmaps, extensions, capabilities, bindingStates, clipping );
*/
/**
* getProgram
*/
function getProgram(material, scene, object) {
if (scene.isScene !== true) scene = _emptyScene; // scene could be a Mesh, Line, Points, ...
const materialProperties = properties.get(material);
const lights = currentRenderState.state.lights;
const shadowsArray = currentRenderState.state.shadowsArray;
const lightsStateVersion = lights.state.version;
const parameters = programCache.getParameters(material, lights.state, shadowsArray, scene, object);
const programCacheKey = programCache.getProgramCacheKey(parameters);
let programs = materialProperties.programs;
// always update environment and fog - changing these trigger an getProgram call, but it's possible that the program doesn't change
materialProperties.environment = material.isMeshStandardMaterial ? scene.environment : null;
materialProperties.fog = scene.fog;
materialProperties.envMap = (material.isMeshStandardMaterial ? cubeuvmaps : cubemaps).get(material.envMap || materialProperties.environment);
if (programs === undefined) {
// new material
material.addEventListener('dispose', onMaterialDispose);
programs = new Map();
materialProperties.programs = programs;
}
let program = programs.get(programCacheKey);
if (program !== undefined) {
// early out if program and light state is identical
if (materialProperties.currentProgram === program && materialProperties.lightsStateVersion === lightsStateVersion) {
updateCommonMaterialProperties(material, parameters);
return program;
}
} else {
parameters.uniforms = programCache.getUniforms(material);
material.onBuild(object, parameters, _this);
material.onBeforeCompile(parameters, _this);
program = programCache.acquireProgram(parameters, programCacheKey);
programs.set(programCacheKey, program);
materialProperties.uniforms = parameters.uniforms;
}
const uniforms = materialProperties.uniforms;
if ((!material.isShaderMaterial && !material.isRawShaderMaterial) || material.clipping === true) {
uniforms.clippingPlanes = clipping.uniform;
}
updateCommonMaterialProperties(material, parameters);
// store the light setup it was created for
materialProperties.needsLights = materialNeedsLights(material);
materialProperties.lightsStateVersion = lightsStateVersion;
if (materialProperties.needsLights) {
// wire up the material to this renderer's lighting state
uniforms.ambientLightColor.value = lights.state.ambient;
uniforms.lightProbe.value = lights.state.probe;
uniforms.directionalLights.value = lights.state.directional;
uniforms.directionalLightShadows.value = lights.state.directionalShadow;
uniforms.spotLights.value = lights.state.spot;
uniforms.spotLightShadows.value = lights.state.spotShadow;
uniforms.rectAreaLights.value = lights.state.rectArea;
uniforms.ltc_1.value = lights.state.rectAreaLTC1;
uniforms.ltc_2.value = lights.state.rectAreaLTC2;
uniforms.pointLights.value = lights.state.point;
uniforms.pointLightShadows.value = lights.state.pointShadow;
uniforms.hemisphereLights.value = lights.state.hemi;
uniforms.directionalShadowMap.value = lights.state.directionalShadowMap;
uniforms.directionalShadowMatrix.value = lights.state.directionalShadowMatrix;
uniforms.spotShadowMap.value = lights.state.spotShadowMap;
uniforms.spotLightMatrix.value = lights.state.spotLightMatrix;
uniforms.spotLightMap.value = lights.state.spotLightMap;
uniforms.pointShadowMap.value = lights.state.pointShadowMap;
uniforms.pointShadowMatrix.value = lights.state.pointShadowMatrix;
// TODO (abelnation): add area lights shadow info to uniforms
}
const progUniforms = program.getUniforms();
const uniformsList = WebGLUniforms.seqWithValue(progUniforms.seq, uniforms);
materialProperties.currentProgram = program;
materialProperties.uniformsList = uniformsList;
return program;
}
function updateCommonMaterialProperties(material, parameters) {
const materialProperties = properties.get(material);
materialProperties.outputColorSpace = parameters.outputColorSpace;
materialProperties.instancing = parameters.instancing;
materialProperties.instancingColor = parameters.instancingColor;
materialProperties.skinning = parameters.skinning;
materialProperties.morphTargets = parameters.morphTargets;
materialProperties.morphNormals = parameters.morphNormals;
materialProperties.morphColors = parameters.morphColors;
materialProperties.morphTargetsCount = parameters.morphTargetsCount;
materialProperties.numClippingPlanes = parameters.numClippingPlanes;
materialProperties.numIntersection = parameters.numClipIntersection;
materialProperties.vertexAlphas = parameters.vertexAlphas;
materialProperties.vertexTangents = parameters.vertexTangents;
materialProperties.toneMapping = parameters.toneMapping;
}
function WebGLProperties() {
let properties = new WeakMap();
function get(object) {
let map = properties.get(object);
if (map === undefined) {
map = {};
properties.set(object, map);
}
return map;
}
function remove(object) {
properties.delete(object);
}
function update(object, key, value) {
properties.get(object)[key] = value;
}
function dispose() {
properties = new WeakMap();
}
return {
get: get,
remove: remove,
update: update,
dispose: dispose
};
}
function WebGLPrograms(renderer, cubemaps, cubeuvmaps, extensions, capabilities, bindingStates, clipping) {
const _programLayers = new Layers();
const _customShaders = new WebGLShaderCache();
const programs = [];
const IS_WEBGL2 = capabilities.isWebGL2;
const logarithmicDepthBuffer = capabilities.logarithmicDepthBuffer;
const SUPPORTS_VERTEX_TEXTURES = capabilities.vertexTextures;
let precision = capabilities.precision;
const shaderIDs = {
MeshDepthMaterial: 'depth',
MeshDistanceMaterial: 'distanceRGBA',
MeshNormalMaterial: 'normal',
MeshBasicMaterial: 'basic',
MeshLambertMaterial: 'lambert',
MeshPhongMaterial: 'phong',
MeshToonMaterial: 'toon',
MeshStandardMaterial: 'physical',
MeshPhysicalMaterial: 'physical',
MeshMatcapMaterial: 'matcap',
LineBasicMaterial: 'basic',
LineDashedMaterial: 'dashed',
PointsMaterial: 'points',
ShadowMaterial: 'shadow',
SpriteMaterial: 'sprite'
};
function getChannel(value) {
if (value === 0) return 'uv';
return `uv${value}`;
}
function getParameters(material, lights, shadows, scene, object) {
const fog = scene.fog;
const geometry = object.geometry;
const environment = material.isMeshStandardMaterial ? scene.environment : null;
const envMap = (material.isMeshStandardMaterial ? cubeuvmaps : cubemaps).get(material.envMap || environment);
const envMapCubeUVHeight = (!!envMap) && (envMap.mapping === CubeUVReflectionMapping) ? envMap.image.height : null;
const shaderID = shaderIDs[material.type];
// heuristics to create shader parameters according to lights in the scene
// (not to blow over maxLights budget)
if (material.precision !== null) {
precision = capabilities.getMaxPrecision(material.precision);
if (precision !== material.precision) {
console.warn('THREE.WebGLProgram.getParameters:', material.precision, 'not supported, using', precision, 'instead.');
}
}
//
const morphAttribute = geometry.morphAttributes.position || geometry.morphAttributes.normal || geometry.morphAttributes.color;
const morphTargetsCount = (morphAttribute !== undefined) ? morphAttribute.length : 0;
let morphTextureStride = 0;
if (geometry.morphAttributes.position !== undefined) morphTextureStride = 1;
if (geometry.morphAttributes.normal !== undefined) morphTextureStride = 2;
if (geometry.morphAttributes.color !== undefined) morphTextureStride = 3;
//
let vertexShader, fragmentShader;
let customVertexShaderID, customFragmentShaderID;
if (shaderID) {
const shader = ShaderLib[shaderID];
vertexShader = shader.vertexShader;
fragmentShader = shader.fragmentShader;
} else {
vertexShader = material.vertexShader;
fragmentShader = material.fragmentShader;
_customShaders.update(material);
customVertexShaderID = _customShaders.getVertexShaderID(material);
customFragmentShaderID = _customShaders.getFragmentShaderID(material);
}
const currentRenderTarget = renderer.getRenderTarget();
const IS_INSTANCEDMESH = object.isInstancedMesh === true;
const HAS_MAP = !!material.map;
const HAS_MATCAP = !!material.matcap;
const HAS_ENVMAP = !!envMap;
const HAS_AOMAP = !!material.aoMap;
const HAS_LIGHTMAP = !!material.lightMap;
const HAS_BUMPMAP = !!material.bumpMap;
const HAS_NORMALMAP = !!material.normalMap;
const HAS_DISPLACEMENTMAP = !!material.displacementMap;
const HAS_EMISSIVEMAP = !!material.emissiveMap;
const HAS_METALNESSMAP = !!material.metalnessMap;
const HAS_ROUGHNESSMAP = !!material.roughnessMap;
const HAS_ANISOTROPY = material.anisotropy > 0;
const HAS_CLEARCOAT = material.clearcoat > 0;
const HAS_IRIDESCENCE = material.iridescence > 0;
const HAS_SHEEN = material.sheen > 0;
const HAS_TRANSMISSION = material.transmission > 0;
const HAS_ANISOTROPYMAP = HAS_ANISOTROPY && !!material.anisotropyMap;
const HAS_CLEARCOATMAP = HAS_CLEARCOAT && !!material.clearcoatMap;
const HAS_CLEARCOAT_NORMALMAP = HAS_CLEARCOAT && !!material.clearcoatNormalMap;
const HAS_CLEARCOAT_ROUGHNESSMAP = HAS_CLEARCOAT && !!material.clearcoatRoughnessMap;
const HAS_IRIDESCENCEMAP = HAS_IRIDESCENCE && !!material.iridescenceMap;
const HAS_IRIDESCENCE_THICKNESSMAP = HAS_IRIDESCENCE && !!material.iridescenceThicknessMap;
const HAS_SHEEN_COLORMAP = HAS_SHEEN && !!material.sheenColorMap;
const HAS_SHEEN_ROUGHNESSMAP = HAS_SHEEN && !!material.sheenRoughnessMap;
const HAS_SPECULARMAP = !!material.specularMap;
const HAS_SPECULAR_COLORMAP = !!material.specularColorMap;
const HAS_SPECULAR_INTENSITYMAP = !!material.specularIntensityMap;
const HAS_TRANSMISSIONMAP = HAS_TRANSMISSION && !!material.transmissionMap;
const HAS_THICKNESSMAP = HAS_TRANSMISSION && !!material.thicknessMap;
const HAS_GRADIENTMAP = !!material.gradientMap;
const HAS_ALPHAMAP = !!material.alphaMap;
const HAS_ALPHATEST = material.alphaTest > 0;
const HAS_ALPHAHASH = !!material.alphaHash;
const HAS_EXTENSIONS = !!material.extensions;
const HAS_ATTRIBUTE_UV1 = !!geometry.attributes.uv1;
const HAS_ATTRIBUTE_UV2 = !!geometry.attributes.uv2;
const HAS_ATTRIBUTE_UV3 = !!geometry.attributes.uv3;
let toneMapping = NoToneMapping;
if (material.toneMapped) {
if (currentRenderTarget === null || currentRenderTarget.isXRRenderTarget === true) {
toneMapping = renderer.toneMapping;
}
}
const parameters = {
isWebGL2: IS_WEBGL2,
shaderID: shaderID,
shaderType: material.type,
shaderName: material.name,
vertexShader: vertexShader,
fragmentShader: fragmentShader,
defines: material.defines,
customVertexShaderID: customVertexShaderID,
customFragmentShaderID: customFragmentShaderID,
isRawShaderMaterial: material.isRawShaderMaterial === true,
glslVersion: material.glslVersion,
precision: precision,
instancing: IS_INSTANCEDMESH,
instancingColor: IS_INSTANCEDMESH && object.instanceColor !== null,
supportsVertexTextures: SUPPORTS_VERTEX_TEXTURES,
outputColorSpace: (currentRenderTarget === null) ? renderer.outputColorSpace : (currentRenderTarget.isXRRenderTarget === true ? currentRenderTarget.texture.colorSpace : LinearSRGBColorSpace),
map: HAS_MAP,
matcap: HAS_MATCAP,
envMap: HAS_ENVMAP,
envMapMode: HAS_ENVMAP && envMap.mapping,
envMapCubeUVHeight: envMapCubeUVHeight,
aoMap: HAS_AOMAP,
lightMap: HAS_LIGHTMAP,
bumpMap: HAS_BUMPMAP,
normalMap: HAS_NORMALMAP,
displacementMap: SUPPORTS_VERTEX_TEXTURES && HAS_DISPLACEMENTMAP,
emissiveMap: HAS_EMISSIVEMAP,
normalMapObjectSpace: HAS_NORMALMAP && material.normalMapType === ObjectSpaceNormalMap,
normalMapTangentSpace: HAS_NORMALMAP && material.normalMapType === TangentSpaceNormalMap,