@xeokit/xeokit-sdk 2.6.84 → 2.6.85-renderers-refactor-v2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/dist/xeokit-sdk.cjs.js +13077 -44659
  2. package/dist/xeokit-sdk.es.js +13078 -44658
  3. package/dist/xeokit-sdk.es5.js +1402 -3502
  4. package/dist/xeokit-sdk.min.cjs.js +8 -8
  5. package/dist/xeokit-sdk.min.es.js +8 -8
  6. package/dist/xeokit-sdk.min.es5.js +7 -7
  7. package/package.json +1 -1
  8. package/src/plugins/lib/ui/index.js +12 -0
  9. package/src/viewer/Viewer.js +55 -103
  10. package/src/viewer/scene/canvas/Canvas.js +3 -43
  11. package/src/viewer/scene/geometry/ReadableGeometry.js +0 -25
  12. package/src/viewer/scene/lights/CubeTexture.js +0 -13
  13. package/src/viewer/scene/lights/DirLight.js +2 -1
  14. package/src/viewer/scene/lights/PointLight.js +2 -1
  15. package/src/viewer/scene/materials/Texture.js +0 -9
  16. package/src/viewer/scene/math/rtcCoords.js +1 -38
  17. package/src/viewer/scene/mesh/Mesh.js +454 -212
  18. package/src/viewer/scene/mesh/draw/DrawShaderSource.js +305 -1502
  19. package/src/viewer/scene/mesh/draw/LambertShaderSource.js +35 -0
  20. package/src/viewer/scene/mesh/emphasis/EmphasisShaderSource.js +34 -0
  21. package/src/viewer/scene/mesh/occlusion/OcclusionShaderSource.js +9 -157
  22. package/src/viewer/scene/mesh/pick/PickMeshShaderSource.js +21 -150
  23. package/src/viewer/scene/mesh/pick/PickTriangleShaderSource.js +12 -124
  24. package/src/viewer/scene/mesh/shadow/ShadowShaderSource.js +22 -97
  25. package/src/viewer/scene/model/SceneModel.js +245 -861
  26. package/src/viewer/scene/model/SceneModelEntity.js +4 -4
  27. package/src/viewer/scene/model/SceneModelMesh.js +3 -3
  28. package/src/viewer/scene/model/layer/DTXLayer.js +940 -0
  29. package/src/viewer/scene/model/layer/Layer.js +786 -0
  30. package/src/viewer/scene/model/layer/VBOLayer.js +1019 -0
  31. package/src/viewer/scene/model/layer/programs/ColorProgram.js +35 -0
  32. package/src/viewer/scene/model/layer/programs/ColorTextureProgram.js +46 -0
  33. package/src/viewer/scene/model/layer/programs/DepthProgram.js +11 -0
  34. package/src/viewer/scene/model/layer/programs/EdgesProgram.js +12 -0
  35. package/src/viewer/scene/model/layer/programs/FlatColorProgram.js +24 -0
  36. package/src/viewer/scene/model/layer/programs/OcclusionProgram.js +12 -0
  37. package/src/viewer/scene/model/layer/programs/PBRProgram.js +180 -0
  38. package/src/viewer/scene/model/layer/programs/PickDepthProgram.js +30 -0
  39. package/src/viewer/scene/model/layer/programs/PickMeshProgram.js +12 -0
  40. package/src/viewer/scene/model/layer/programs/PickNormalsProgram.js +18 -0
  41. package/src/viewer/scene/model/layer/programs/ShadowProgram.js +23 -0
  42. package/src/viewer/scene/model/layer/programs/SilhouetteProgram.js +15 -0
  43. package/src/viewer/scene/model/layer/programs/SnapProgram.js +28 -0
  44. package/src/viewer/scene/scene/Scene.js +21 -38
  45. package/src/viewer/scene/webgl/ArrayBuf.js +5 -20
  46. package/src/viewer/scene/webgl/FrameContext.js +14 -86
  47. package/src/viewer/scene/webgl/RenderBuffer.js +62 -262
  48. package/src/viewer/scene/webgl/Renderer.js +880 -843
  49. package/src/viewer/scene/webgl/WebGLRenderer.js +612 -0
  50. package/src/viewer/scene/webgl/occlusion/OcclusionLayer.js +60 -132
  51. package/src/viewer/scene/webgl/occlusion/OcclusionTester.js +167 -328
  52. package/src/viewer/scene/mesh/draw/DrawRenderer.js +0 -996
  53. package/src/viewer/scene/mesh/emphasis/EmphasisEdgesRenderer.js +0 -259
  54. package/src/viewer/scene/mesh/emphasis/EmphasisEdgesShaderSource.js +0 -159
  55. package/src/viewer/scene/mesh/emphasis/EmphasisFillRenderer.js +0 -286
  56. package/src/viewer/scene/mesh/emphasis/EmphasisFillShaderSource.js +0 -262
  57. package/src/viewer/scene/mesh/occlusion/OcclusionRenderer.js +0 -221
  58. package/src/viewer/scene/mesh/pick/PickMeshRenderer.js +0 -223
  59. package/src/viewer/scene/mesh/pick/PickTriangleRenderer.js +0 -200
  60. package/src/viewer/scene/mesh/shadow/ShadowRenderer.js +0 -214
  61. package/src/viewer/scene/model/RENDER_PASSES.js +0 -34
  62. package/src/viewer/scene/model/dtx/BindableDataTexture.js +0 -31
  63. package/src/viewer/scene/model/dtx/lines/DTXLinesBuffer.js +0 -27
  64. package/src/viewer/scene/model/dtx/lines/DTXLinesLayer.js +0 -897
  65. package/src/viewer/scene/model/dtx/lines/DTXLinesState.js +0 -46
  66. package/src/viewer/scene/model/dtx/lines/DTXLinesTextureFactory.js +0 -411
  67. package/src/viewer/scene/model/dtx/lines/dataTextureRamStats.js +0 -49
  68. package/src/viewer/scene/model/dtx/lines/rebucketPositions.js +0 -208
  69. package/src/viewer/scene/model/dtx/lines/renderers/DTXLinesColorRenderer.js +0 -389
  70. package/src/viewer/scene/model/dtx/lines/renderers/DTXLinesRenderers.js +0 -65
  71. package/src/viewer/scene/model/dtx/triangles/DTXTrianglesLayer.js +0 -1399
  72. package/src/viewer/scene/model/dtx/triangles/lib/DTXTrianglesBuffer.js +0 -46
  73. package/src/viewer/scene/model/dtx/triangles/lib/DTXTrianglesState.js +0 -250
  74. package/src/viewer/scene/model/dtx/triangles/lib/DTXTrianglesTextureFactory.js +0 -518
  75. package/src/viewer/scene/model/dtx/triangles/lib/dataTextureRamStats.js +0 -54
  76. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesColorRenderer.js +0 -639
  77. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesDepthRenderer.js +0 -464
  78. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesEdgesColorRenderer.js +0 -423
  79. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesEdgesRenderer.js +0 -431
  80. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesNormalsRenderer.js +0 -345
  81. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesOcclusionRenderer.js +0 -436
  82. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesPickDepthRenderer.js +0 -466
  83. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesPickMeshRenderer.js +0 -437
  84. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesPickNormalsFlatRenderer.js +0 -460
  85. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesPickNormalsRenderer.js +0 -437
  86. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesRenderers.js +0 -307
  87. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesSilhouetteRenderer.js +0 -472
  88. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesSnapInitRenderer.js +0 -474
  89. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesSnapRenderer.js +0 -435
  90. package/src/viewer/scene/model/vbo/ScratchMemory.js +0 -63
  91. package/src/viewer/scene/model/vbo/VBORenderer.js +0 -664
  92. package/src/viewer/scene/model/vbo/batching/lines/VBOBatchingLinesLayer.js +0 -704
  93. package/src/viewer/scene/model/vbo/batching/lines/lib/VBOBatchingLinesBuffer.js +0 -19
  94. package/src/viewer/scene/model/vbo/batching/lines/renderers/VBOBatchingLineSnapInitRenderer.js +0 -318
  95. package/src/viewer/scene/model/vbo/batching/lines/renderers/VBOBatchingLinesColorRenderer.js +0 -114
  96. package/src/viewer/scene/model/vbo/batching/lines/renderers/VBOBatchingLinesRenderers.js +0 -99
  97. package/src/viewer/scene/model/vbo/batching/lines/renderers/VBOBatchingLinesSilhouetteRenderer.js +0 -124
  98. package/src/viewer/scene/model/vbo/batching/lines/renderers/VBOBatchingLinesSnapInitRenderer.js +0 -313
  99. package/src/viewer/scene/model/vbo/batching/lines/renderers/VBOBatchingLinesSnapRenderer.js +0 -311
  100. package/src/viewer/scene/model/vbo/batching/lines/renderers/VBOSceneModelLineBatchingRenderer.js +0 -24
  101. package/src/viewer/scene/model/vbo/batching/points/VBOBatchingPointsLayer.js +0 -688
  102. package/src/viewer/scene/model/vbo/batching/points/VBOBatchingPointsRenderer.js +0 -24
  103. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsColorRenderer.js +0 -168
  104. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsOcclusionRenderer.js +0 -136
  105. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsPickDepthRenderer.js +0 -156
  106. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsPickMeshRenderer.js +0 -148
  107. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsRenderers.js +0 -144
  108. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsShadowRenderer.js +0 -107
  109. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsSilhouetteRenderer.js +0 -146
  110. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsSnapInitRenderer.js +0 -315
  111. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsSnapRenderer.js +0 -305
  112. package/src/viewer/scene/model/vbo/batching/triangles/VBOBatchingTrianglesBuffer.js +0 -24
  113. package/src/viewer/scene/model/vbo/batching/triangles/VBOBatchingTrianglesLayer.js +0 -1346
  114. package/src/viewer/scene/model/vbo/batching/triangles/renderers/EdgesColorRenderer.js +0 -131
  115. package/src/viewer/scene/model/vbo/batching/triangles/renderers/EdgesEmphasisRenderer.js +0 -135
  116. package/src/viewer/scene/model/vbo/batching/triangles/renderers/EdgesRenderer.js +0 -12
  117. package/src/viewer/scene/model/vbo/batching/triangles/renderers/Renderers.js +0 -403
  118. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesBatchingRenderer.js +0 -35
  119. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesColorRenderer.js +0 -252
  120. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesColorTextureRenderer.js +0 -294
  121. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesDepthRenderer.js +0 -115
  122. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesFlatColorRenderer.js +0 -246
  123. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesNormalsRenderer.js +0 -139
  124. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesOcclusionRenderer.js +0 -125
  125. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesPBRRenderer.js +0 -561
  126. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesPickDepthRenderer.js +0 -144
  127. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesPickMeshRenderer.js +0 -130
  128. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesPickNormalsFlatRenderer.js +0 -121
  129. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesPickNormalsRenderer.js +0 -131
  130. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesShadowRenderer.js +0 -107
  131. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesSilhouetteRenderer.js +0 -144
  132. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesSnapInitRenderer.js +0 -313
  133. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesSnapRenderer.js +0 -312
  134. package/src/viewer/scene/model/vbo/float16.js +0 -760
  135. package/src/viewer/scene/model/vbo/instancing/lines/VBOInstancingLinesLayer.js +0 -711
  136. package/src/viewer/scene/model/vbo/instancing/lines/renderers/VBOInstancingLinesColorRenderer.js +0 -148
  137. package/src/viewer/scene/model/vbo/instancing/lines/renderers/VBOInstancingLinesRenderer.js +0 -26
  138. package/src/viewer/scene/model/vbo/instancing/lines/renderers/VBOInstancingLinesRenderers.js +0 -139
  139. package/src/viewer/scene/model/vbo/instancing/lines/renderers/VBOInstancingLinesSilhouetteRenderer.js +0 -127
  140. package/src/viewer/scene/model/vbo/instancing/lines/renderers/VBOInstancingLinesSnapInitRenderer.js +0 -308
  141. package/src/viewer/scene/model/vbo/instancing/lines/renderers/VBOInstancingLinesSnapRenderer.js +0 -299
  142. package/src/viewer/scene/model/vbo/instancing/points/VBOInstancingPointsLayer.js +0 -733
  143. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsColorRenderer.js +0 -174
  144. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsDepthRenderer.js +0 -156
  145. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsOcclusionRenderer.js +0 -149
  146. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsPickDepthRenderer.js +0 -170
  147. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsPickMeshRenderer.js +0 -152
  148. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsRenderer.js +0 -26
  149. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsRenderers.js +0 -174
  150. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsShadowRenderer.js +0 -119
  151. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsSilhouetteRenderer.js +0 -154
  152. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsSnapInitRenderer.js +0 -307
  153. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsSnapRenderer.js +0 -293
  154. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingTrianglesRenderer.js +0 -150
  155. package/src/viewer/scene/model/vbo/instancing/triangles/VBOInstancingTrianglesLayer.js +0 -1289
  156. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/EdgesColorRenderer.js +0 -135
  157. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/EdgesEmphasisRenderer.js +0 -137
  158. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/EdgesRenderer.js +0 -12
  159. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/Renderers.js +0 -403
  160. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesColorRenderer.js +0 -267
  161. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesColorTextureRenderer.js +0 -299
  162. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesDepthRenderer.js +0 -126
  163. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesFlatColorRenderer.js +0 -255
  164. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesFlatNormalsRenderer.js +0 -130
  165. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesInstancingRenderer.js +0 -32
  166. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesNormalsRenderer.js +0 -138
  167. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesOcclusionRenderer.js +0 -128
  168. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesPBRRenderer.js +0 -561
  169. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesPickDepthRenderer.js +0 -149
  170. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesPickMeshRenderer.js +0 -136
  171. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesPickNormalsFlatRenderer.js +0 -130
  172. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesPickNormalsRenderer.js +0 -145
  173. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesShadowRenderer.js +0 -113
  174. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesSilhouetteRenderer.js +0 -148
  175. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesSnapInitRenderer.js +0 -319
  176. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesSnapRenderer.js +0 -310
  177. package/src/viewer/scene/webgl/Attribute.js +0 -23
  178. package/src/viewer/scene/webgl/Program.js +0 -181
  179. package/src/viewer/scene/webgl/RenderBufferManager.js +0 -34
  180. package/src/viewer/scene/webgl/Sampler.js +0 -18
  181. package/src/viewer/scene/webgl/Shader.js +0 -49
  182. package/src/viewer/scene/webgl/sao/SAODepthLimitedBlurRenderer.js +0 -324
  183. package/src/viewer/scene/webgl/sao/SAOOcclusionRenderer.js +0 -384
@@ -1,753 +1,126 @@
1
- /**
2
- * @private
3
- */
1
+ import {createLightSetup, setupTexture} from "../../webgl/WebGLRenderer.js";
2
+ import {math} from "../../math/math.js";
3
+ const tempVec4 = math.vec4();
4
4
 
5
- import {LinearEncoding, sRGBEncoding} from "../../constants/constants.js";
6
-
7
- const DrawShaderSource = function (mesh) {
8
- if (mesh._material._state.type === "LambertMaterial") {
9
- this.vertex = buildVertexLambert(mesh);
10
- this.fragment = buildFragmentLambert(mesh);
11
- } else {
12
- this.vertex = buildVertexDraw(mesh);
13
- this.fragment = buildFragmentDraw(mesh);
14
- }
15
- };
16
-
17
- const TEXTURE_DECODE_FUNCS = {};
18
- TEXTURE_DECODE_FUNCS[LinearEncoding] = "linearToLinear";
19
- TEXTURE_DECODE_FUNCS[sRGBEncoding] = "sRGBToLinear";
20
-
21
- function getReceivesShadow(mesh) {
22
- if (!mesh.receivesShadow) {
23
- return false;
24
- }
25
- const lights = mesh.scene._lightsState.lights;
26
- if (!lights || lights.length === 0) {
27
- return false;
28
- }
29
- for (let i = 0, len = lights.length; i < len; i++) {
30
- if (lights[i].castsShadow) {
31
- return true;
32
- }
33
- }
34
- return false;
35
- }
36
-
37
- function hasTextures(mesh) {
38
- if (!mesh._geometry._state.uvBuf) {
39
- return false;
40
- }
41
- const material = mesh._material;
42
- return !!(material._ambientMap ||
43
- material._occlusionMap ||
44
- material._baseColorMap ||
45
- material._diffuseMap ||
46
- material._alphaMap ||
47
- material._specularMap ||
48
- material._glossinessMap ||
49
- material._specularGlossinessMap ||
50
- material._emissiveMap ||
51
- material._metallicMap ||
52
- material._roughnessMap ||
53
- material._metallicRoughnessMap ||
54
- material._reflectivityMap ||
55
- material._normalMap);
56
- }
57
-
58
- function hasNormals(mesh) {
59
- const primitive = mesh._geometry._state.primitiveName;
60
- if ((mesh._geometry._state.autoVertexNormals || mesh._geometry._state.normalsBuf) && (primitive === "triangles" || primitive === "triangle-strip" || primitive === "triangle-fan")) {
61
- return true;
62
- }
63
- return false;
64
- }
65
-
66
- function buildVertexLambert(mesh) {
67
-
68
- const scene = mesh.scene;
69
- const sectionPlanesState = mesh.scene._sectionPlanesState;
70
- const lightsState = mesh.scene._lightsState;
71
- const geometryState = mesh._geometry._state;
72
- const billboard = mesh._state.billboard;
73
- const stationary = mesh._state.stationary;
74
- const clipping = sectionPlanesState.getNumAllocatedSectionPlanes() > 0;
75
- const quantizedGeometry = !!geometryState.compressGeometry;
76
-
77
- const src = [];
78
- src.push("#version 300 es");
79
- src.push("// Lambertian drawing vertex shader");
80
- src.push("in vec3 position;");
81
- src.push("uniform mat4 modelMatrix;");
82
- src.push("uniform mat4 viewMatrix;");
83
- src.push("uniform mat4 projMatrix;");
84
- src.push("uniform vec4 colorize;");
85
- src.push("uniform vec3 offset;");
86
- if (quantizedGeometry) {
87
- src.push("uniform mat4 positionsDecodeMatrix;");
88
- }
89
- if (scene.logarithmicDepthBufferEnabled) {
90
- src.push("uniform float logDepthBufFC;");
91
- src.push("out float vFragDepth;");
92
- src.push("bool isPerspectiveMatrix(mat4 m) {");
93
- src.push(" return (m[2][3] == - 1.0);");
94
- src.push("}");
95
- src.push("out float isPerspective;");
96
- }
97
- if (clipping) {
98
- src.push("out vec4 vWorldPosition;");
99
- }
100
- src.push("uniform vec4 lightAmbient;");
101
- src.push("uniform vec4 materialColor;");
102
- src.push("uniform vec3 materialEmissive;");
103
- if (geometryState.normalsBuf) {
104
- src.push("in vec3 normal;");
105
- src.push("uniform mat4 modelNormalMatrix;");
106
- src.push("uniform mat4 viewNormalMatrix;");
107
- for (let i = 0, len = lightsState.lights.length; i < len; i++) {
108
- const light = lightsState.lights[i];
109
- if (light.type === "ambient") {
110
- continue;
111
- }
112
- src.push("uniform vec4 lightColor" + i + ";");
113
- if (light.type === "dir") {
114
- src.push("uniform vec3 lightDir" + i + ";");
115
- }
116
- if (light.type === "point") {
117
- src.push("uniform vec3 lightPos" + i + ";");
118
- }
119
- if (light.type === "spot") {
120
- src.push("uniform vec3 lightPos" + i + ";");
121
- src.push("uniform vec3 lightDir" + i + ";");
122
- }
123
- }
124
- if (quantizedGeometry) {
125
- src.push("vec3 octDecode(vec2 oct) {");
126
- src.push(" vec3 v = vec3(oct.xy, 1.0 - abs(oct.x) - abs(oct.y));");
127
- src.push(" if (v.z < 0.0) {");
128
- src.push(" v.xy = (1.0 - abs(v.yx)) * vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0);");
129
- src.push(" }");
130
- src.push(" return normalize(v);");
131
- src.push("}");
132
- }
133
- }
134
- src.push("out vec4 vColor;");
135
- if (geometryState.primitiveName === "points") {
136
- src.push("uniform float pointSize;");
137
- }
138
- if (billboard === "spherical" || billboard === "cylindrical") {
139
- src.push("void billboard(inout mat4 mat) {");
140
- src.push(" mat[0][0] = 1.0;");
141
- src.push(" mat[0][1] = 0.0;");
142
- src.push(" mat[0][2] = 0.0;");
143
- if (billboard === "spherical") {
144
- src.push(" mat[1][0] = 0.0;");
145
- src.push(" mat[1][1] = 1.0;");
146
- src.push(" mat[1][2] = 0.0;");
147
- }
148
- src.push(" mat[2][0] = 0.0;");
149
- src.push(" mat[2][1] = 0.0;");
150
- src.push(" mat[2][2] =1.0;");
151
- src.push("}");
152
- }
153
- src.push("void main(void) {");
154
- src.push("vec4 localPosition = vec4(position, 1.0); ");
155
- src.push("vec4 worldPosition;");
156
- if (quantizedGeometry) {
157
- src.push("localPosition = positionsDecodeMatrix * localPosition;");
158
- }
159
- if (geometryState.normalsBuf) {
160
- if (quantizedGeometry) {
161
- src.push("vec4 localNormal = vec4(octDecode(normal.xy), 0.0); ");
162
- } else {
163
- src.push("vec4 localNormal = vec4(normal, 0.0); ");
164
- }
165
- src.push("mat4 modelNormalMatrix2 = modelNormalMatrix;");
166
- src.push("mat4 viewNormalMatrix2 = viewNormalMatrix;");
167
- }
168
- src.push("mat4 viewMatrix2 = viewMatrix;");
169
- src.push("mat4 modelMatrix2 = modelMatrix;");
170
- if (stationary) {
171
- src.push("viewMatrix2[3][0] = viewMatrix2[3][1] = viewMatrix2[3][2] = 0.0;")
172
- }
173
- if (billboard === "spherical" || billboard === "cylindrical") {
174
- src.push("mat4 modelViewMatrix = viewMatrix2 * modelMatrix2;");
175
- src.push("billboard(modelMatrix2);");
176
- src.push("billboard(viewMatrix2);");
177
- src.push("billboard(modelViewMatrix);");
178
- if (geometryState.normalsBuf) {
179
- src.push("mat4 modelViewNormalMatrix = viewNormalMatrix2 * modelNormalMatrix2;");
180
- src.push("billboard(modelNormalMatrix2);");
181
- src.push("billboard(viewNormalMatrix2);");
182
- src.push("billboard(modelViewNormalMatrix);");
183
- }
184
- src.push("worldPosition = modelMatrix2 * localPosition;");
185
- src.push("worldPosition.xyz = worldPosition.xyz + offset;");
186
- src.push("vec4 viewPosition = modelViewMatrix * localPosition;");
187
- } else {
188
- src.push("worldPosition = modelMatrix2 * localPosition;");
189
- src.push("worldPosition.xyz = worldPosition.xyz + offset;");
190
- src.push("vec4 viewPosition = viewMatrix2 * worldPosition; ");
191
- }
192
- if (geometryState.normalsBuf) {
193
- src.push("vec3 viewNormal = normalize((viewNormalMatrix2 * modelNormalMatrix2 * localNormal).xyz);");
194
- }
195
- src.push("vec3 reflectedColor = vec3(0.0, 0.0, 0.0);");
196
- src.push("vec3 viewLightDir = vec3(0.0, 0.0, -1.0);");
197
- src.push("float lambertian = 1.0;");
198
- if (geometryState.normalsBuf) {
199
- for (let i = 0, len = lightsState.lights.length; i < len; i++) {
200
- const light = lightsState.lights[i];
201
- if (light.type === "ambient") {
202
- continue;
203
- }
204
- if (light.type === "dir") {
205
- if (light.space === "view") {
206
- src.push("viewLightDir = normalize(lightDir" + i + ");");
207
- } else {
208
- src.push("viewLightDir = normalize((viewMatrix2 * vec4(lightDir" + i + ", 0.0)).xyz);");
209
- }
210
- } else if (light.type === "point") {
211
- if (light.space === "view") {
212
- src.push("viewLightDir = -normalize(lightPos" + i + " - viewPosition.xyz);");
213
- } else {
214
- src.push("viewLightDir = -normalize((viewMatrix2 * vec4(lightPos" + i + ", 0.0)).xyz);");
215
- }
216
- } else if (light.type === "spot") {
217
- if (light.space === "view") {
218
- src.push("viewLightDir = normalize(lightDir" + i + ");");
219
- } else {
220
- src.push("viewLightDir = normalize((viewMatrix2 * vec4(lightDir" + i + ", 0.0)).xyz);");
221
- }
222
- } else {
223
- continue;
224
- }
225
- src.push("lambertian = max(dot(-viewNormal, viewLightDir), 0.0);");
226
- src.push("reflectedColor += lambertian * (lightColor" + i + ".rgb * lightColor" + i + ".a);");
227
- }
228
- }
229
- src.push("vColor = vec4((lightAmbient.rgb * lightAmbient.a * materialColor.rgb) + materialEmissive.rgb + (reflectedColor * materialColor.rgb), materialColor.a) * colorize;"); // TODO: How to have ambient bright enough for canvas BG but not too bright for scene?
230
- if (clipping) {
231
- src.push("vWorldPosition = worldPosition;");
232
- }
233
- if (geometryState.primitiveName === "points") {
234
- src.push("gl_PointSize = pointSize;");
235
- }
236
- src.push("vec4 clipPos = projMatrix * viewPosition;");
237
- if (scene.logarithmicDepthBufferEnabled) {
238
- src.push("vFragDepth = 1.0 + clipPos.w;");
239
- src.push("isPerspective = float (isPerspectiveMatrix(projMatrix));");
240
- }
241
- src.push("gl_Position = clipPos;");
242
- src.push("}");
243
- return src;
244
- }
245
-
246
- function buildFragmentLambert(mesh) {
247
- const scene = mesh.scene;
248
- const sectionPlanesState = scene._sectionPlanesState;
249
- const materialState = mesh._material._state;
250
- const geometryState = mesh._geometry._state;
251
- const clipping = sectionPlanesState.getNumAllocatedSectionPlanes() > 0;
252
- const solid = false && materialState.backfaces;
253
- const gammaOutput = scene.gammaOutput; // If set, then it expects that all textures and colors need to be outputted in premultiplied gamma. Default is false.
254
- const src = [];
255
- src.push("#version 300 es");
256
- src.push("// Lambertian drawing fragment shader");
257
- src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH");
258
- src.push("precision highp float;");
259
- src.push("precision highp int;");
260
- src.push("#else");
261
- src.push("precision mediump float;");
262
- src.push("precision mediump int;");
263
- src.push("#endif");
264
- if (scene.logarithmicDepthBufferEnabled) {
265
- src.push("in float isPerspective;");
266
- src.push("uniform float logDepthBufFC;");
267
- src.push("in float vFragDepth;");
268
- }
269
- if (clipping) {
270
- src.push("in vec4 vWorldPosition;");
271
- src.push("uniform bool clippable;");
272
- for (let i = 0, len = sectionPlanesState.getNumAllocatedSectionPlanes(); i < len; i++) {
273
- src.push("uniform bool sectionPlaneActive" + i + ";");
274
- src.push("uniform vec3 sectionPlanePos" + i + ";");
275
- src.push("uniform vec3 sectionPlaneDir" + i + ";");
276
- }
277
- }
278
- src.push("in vec4 vColor;");
279
- if (gammaOutput) {
280
- src.push("uniform float gammaFactor;");
281
- src.push(" vec4 linearToGamma( in vec4 value, in float gammaFactor ) {");
282
- src.push(" return vec4( pow( value.xyz, vec3( 1.0 / gammaFactor ) ), value.w );");
283
- src.push("}");
284
- }
285
- src.push("out vec4 outColor;");
286
- src.push("void main(void) {");
287
- if (clipping) {
288
- src.push("if (clippable) {");
289
- src.push(" float dist = 0.0;");
290
- for (let i = 0, len = sectionPlanesState.getNumAllocatedSectionPlanes(); i < len; i++) {
291
- src.push("if (sectionPlaneActive" + i + ") {");
292
- src.push(" dist += clamp(dot(-sectionPlaneDir" + i + ".xyz, vWorldPosition.xyz - sectionPlanePos" + i + ".xyz), 0.0, 1000.0);");
293
- src.push("}");
294
- }
295
- src.push(" if (dist > 0.0) { discard; }");
296
- if (solid) {
297
- src.push(" if (gl_FrontFacing == false) {");
298
- src.push(" outColor = vec4(1.0, 0.0, 0.0, 1.0);");
299
- src.push(" return;");
300
- src.push(" }");
301
- }
302
- src.push("}");
303
- }
304
- if (geometryState.primitiveName === "points") {
305
- src.push("vec2 cxy = 2.0 * gl_PointCoord - 1.0;");
306
- src.push("float r = dot(cxy, cxy);");
307
- src.push("if (r > 1.0) {");
308
- src.push(" discard;");
309
- src.push("}");
310
-
311
- }
312
- if (scene.logarithmicDepthBufferEnabled) {
313
- src.push("gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;");
314
- }
315
- if (gammaOutput) {
316
- src.push("outColor = linearToGamma(vColor, gammaFactor);");
317
- } else {
318
- src.push("outColor = vColor;");
319
- }
320
- src.push("}");
321
- return src;
322
- }
323
-
324
- function buildVertexDraw(mesh) {
325
- const scene = mesh.scene;
326
- const material = mesh._material;
327
- const meshState = mesh._state;
328
- const sectionPlanesState = scene._sectionPlanesState;
329
- const geometryState = mesh._geometry._state;
330
- const lightsState = scene._lightsState;
331
- let i;
332
- let len;
333
- let light;
334
- const billboard = meshState.billboard;
335
- const background = meshState.background;
336
- const stationary = meshState.stationary;
337
- const texturing = hasTextures(mesh);
338
- const normals = hasNormals(mesh);
339
- const clipping = sectionPlanesState.getNumAllocatedSectionPlanes() > 0;
340
- const receivesShadow = getReceivesShadow(mesh);
341
- const quantizedGeometry = !!geometryState.compressGeometry;
342
- const src = [];
343
- src.push("#version 300 es");
344
- src.push("// Drawing vertex shader");
345
- src.push("in vec3 position;");
346
- if (quantizedGeometry) {
347
- src.push("uniform mat4 positionsDecodeMatrix;");
348
- }
349
- src.push("uniform mat4 modelMatrix;");
350
- src.push("uniform mat4 viewMatrix;");
351
- src.push("uniform mat4 projMatrix;");
352
- src.push("out vec3 vViewPosition;");
353
- src.push("uniform vec3 offset;");
354
- if (clipping) {
355
- src.push("out vec4 vWorldPosition;");
356
- }
357
- if (scene.logarithmicDepthBufferEnabled) {
358
- src.push("uniform float logDepthBufFC;");
359
- src.push("out float vFragDepth;");
360
- src.push("bool isPerspectiveMatrix(mat4 m) {");
361
- src.push(" return (m[2][3] == - 1.0);");
362
- src.push("}");
363
- src.push("out float isPerspective;");
364
- }
365
- if (lightsState.lightMaps.length > 0) {
366
- src.push("out vec3 vWorldNormal;");
367
- }
368
- if (normals) {
369
- src.push("in vec3 normal;");
370
- src.push("uniform mat4 modelNormalMatrix;");
371
- src.push("uniform mat4 viewNormalMatrix;");
372
- src.push("out vec3 vViewNormal;");
373
- for (let i = 0, len = lightsState.lights.length; i < len; i++) {
374
- light = lightsState.lights[i];
375
- if (light.type === "ambient") {
376
- continue;
377
- }
378
- if (light.type === "dir") {
379
- src.push("uniform vec3 lightDir" + i + ";");
380
- }
381
- if (light.type === "point") {
382
- src.push("uniform vec3 lightPos" + i + ";");
383
- }
384
- if (light.type === "spot") {
385
- src.push("uniform vec3 lightPos" + i + ";");
386
- src.push("uniform vec3 lightDir" + i + ";");
387
- }
388
- if (!(light.type === "dir" && light.space === "view")) {
389
- src.push("out vec4 vViewLightReverseDirAndDist" + i + ";");
390
- }
391
- }
392
- if (quantizedGeometry) {
393
- src.push("vec3 octDecode(vec2 oct) {");
394
- src.push(" vec3 v = vec3(oct.xy, 1.0 - abs(oct.x) - abs(oct.y));");
395
- src.push(" if (v.z < 0.0) {");
396
- src.push(" v.xy = (1.0 - abs(v.yx)) * vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0);");
397
- src.push(" }");
398
- src.push(" return normalize(v);");
399
- src.push("}");
400
- }
401
- }
402
- if (texturing) {
403
- src.push("in vec2 uv;");
404
- src.push("out vec2 vUV;");
405
- if (quantizedGeometry) {
406
- src.push("uniform mat3 uvDecodeMatrix;")
407
- }
408
- }
409
- if (geometryState.colors) {
410
- src.push("in vec4 color;");
411
- src.push("out vec4 vColor;");
412
- }
413
- if (geometryState.primitiveName === "points") {
414
- src.push("uniform float pointSize;");
415
- }
416
- if (billboard === "spherical" || billboard === "cylindrical") {
417
- src.push("void billboard(inout mat4 mat) {");
418
- src.push(" mat[0][0] = 1.0;");
419
- src.push(" mat[0][1] = 0.0;");
420
- src.push(" mat[0][2] = 0.0;");
421
- if (billboard === "spherical") {
422
- src.push(" mat[1][0] = 0.0;");
423
- src.push(" mat[1][1] = 1.0;");
424
- src.push(" mat[1][2] = 0.0;");
425
- }
426
- src.push(" mat[2][0] = 0.0;");
427
- src.push(" mat[2][1] = 0.0;");
428
- src.push(" mat[2][2] =1.0;");
429
- src.push("}");
430
- }
431
- if (receivesShadow) {
432
- src.push("const mat4 texUnitConverter = mat4(0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.5, 1.0);");
433
- for (let i = 0, len = lightsState.lights.length; i < len; i++) { // Light sources
434
- if (lightsState.lights[i].castsShadow) {
435
- src.push("uniform mat4 shadowViewMatrix" + i + ";");
436
- src.push("uniform mat4 shadowProjMatrix" + i + ";");
437
- src.push("out vec4 vShadowPosFromLight" + i + ";");
438
- }
439
- }
440
- }
441
- src.push("void main(void) {");
442
- src.push("vec4 localPosition = vec4(position, 1.0); ");
443
- src.push("vec4 worldPosition;");
444
- if (quantizedGeometry) {
445
- src.push("localPosition = positionsDecodeMatrix * localPosition;");
446
- }
447
- if (normals) {
448
- if (quantizedGeometry) {
449
- src.push("vec4 localNormal = vec4(octDecode(normal.xy), 0.0); ");
450
- } else {
451
- src.push("vec4 localNormal = vec4(normal, 0.0); ");
452
- }
453
- src.push("mat4 modelNormalMatrix2 = modelNormalMatrix;");
454
- src.push("mat4 viewNormalMatrix2 = viewNormalMatrix;");
455
- }
456
- src.push("mat4 viewMatrix2 = viewMatrix;");
457
- src.push("mat4 modelMatrix2 = modelMatrix;");
458
- if (stationary) {
459
- src.push("viewMatrix2[3][0] = viewMatrix2[3][1] = viewMatrix2[3][2] = 0.0;")
460
- } else if (background) {
461
- src.push("viewMatrix2[3] = vec4(0.0, 0.0, 0.0 ,1.0);");
462
- }
463
- if (billboard === "spherical" || billboard === "cylindrical") {
464
- src.push("mat4 modelViewMatrix = viewMatrix2 * modelMatrix2;");
465
- src.push("billboard(modelMatrix2);");
466
- src.push("billboard(viewMatrix2);");
467
- src.push("billboard(modelViewMatrix);");
468
- if (normals) {
469
- src.push("mat4 modelViewNormalMatrix = viewNormalMatrix2 * modelNormalMatrix2;");
470
- src.push("billboard(modelNormalMatrix2);");
471
- src.push("billboard(viewNormalMatrix2);");
472
- src.push("billboard(modelViewNormalMatrix);");
473
- }
474
- src.push("worldPosition = modelMatrix2 * localPosition;");
475
- src.push("worldPosition.xyz = worldPosition.xyz + offset;");
476
- src.push("vec4 viewPosition = modelViewMatrix * localPosition;");
477
- } else {
478
- src.push("worldPosition = modelMatrix2 * localPosition;");
479
- src.push("worldPosition.xyz = worldPosition.xyz + offset;");
480
- src.push("vec4 viewPosition = viewMatrix2 * worldPosition; ");
481
- }
482
- if (normals) {
483
- src.push("vec3 worldNormal = (modelNormalMatrix2 * localNormal).xyz; ");
484
- if (lightsState.lightMaps.length > 0) {
485
- src.push("vWorldNormal = worldNormal;");
486
- }
487
- src.push("vViewNormal = normalize((viewNormalMatrix2 * vec4(worldNormal, 1.0)).xyz);");
488
- src.push("vec3 tmpVec3;");
489
- src.push("float lightDist;");
490
- for (let i = 0, len = lightsState.lights.length; i < len; i++) { // Lights
491
- light = lightsState.lights[i];
492
- if (light.type === "ambient") {
493
- continue;
494
- }
495
- if (light.type === "dir") {
496
- if (light.space === "world") {
497
- src.push("tmpVec3 = vec3(viewMatrix2 * vec4(lightDir" + i + ", 0.0) ).xyz;");
498
- src.push("vViewLightReverseDirAndDist" + i + " = vec4(-tmpVec3, 0.0);");
499
- }
500
- }
501
- if (light.type === "point") {
502
- if (light.space === "world") {
503
- src.push("tmpVec3 = (viewMatrix2 * vec4(lightPos" + i + ", 1.0)).xyz - viewPosition.xyz;");
504
- src.push("lightDist = abs(length(tmpVec3));");
505
- } else {
506
- src.push("tmpVec3 = lightPos" + i + ".xyz - viewPosition.xyz;");
507
- src.push("lightDist = abs(length(tmpVec3));");
508
- }
509
- src.push("vViewLightReverseDirAndDist" + i + " = vec4(tmpVec3, lightDist);");
510
- }
511
- }
512
- }
513
- if (texturing) {
514
- if (quantizedGeometry) {
515
- src.push("vUV = (uvDecodeMatrix * vec3(uv, 1.0)).xy;");
516
- } else {
517
- src.push("vUV = uv;");
518
- }
519
- }
520
- if (geometryState.colors) {
521
- src.push("vColor = color;");
522
- }
523
- if (geometryState.primitiveName === "points") {
524
- src.push("gl_PointSize = pointSize;");
525
- }
526
- if (clipping) {
527
- src.push("vWorldPosition = worldPosition;");
528
- }
529
- src.push(" vViewPosition = viewPosition.xyz;");
530
- src.push("vec4 clipPos = projMatrix * viewPosition;");
531
- if (scene.logarithmicDepthBufferEnabled) {
532
- src.push("vFragDepth = 1.0 + clipPos.w;");
533
- src.push("isPerspective = float (isPerspectiveMatrix(projMatrix));");
534
- }
535
- if (background) {
536
- src.push("clipPos.z = clipPos.w;");
537
- }
538
- src.push("gl_Position = clipPos;");
539
- if (receivesShadow) {
540
- src.push("const mat4 texUnitConverter = mat4(0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.5, 1.0);");
541
- src.push("vec4 tempx; ");
542
- for (let i = 0, len = lightsState.lights.length; i < len; i++) { // Light sources
543
- if (lightsState.lights[i].castsShadow) {
544
- src.push("vShadowPosFromLight" + i + " = texUnitConverter * shadowProjMatrix" + i + " * (shadowViewMatrix" + i + " * worldPosition); ");
545
- }
546
- }
547
- }
548
- src.push("}");
549
- return src;
550
- }
551
-
552
- function buildFragmentDraw(mesh) {
553
-
554
- const scene = mesh.scene;
555
- const gl = scene.canvas.gl;
556
- const material = mesh._material;
557
- const geometryState = mesh._geometry._state;
558
- const sectionPlanesState = mesh.scene._sectionPlanesState;
559
- const lightsState = mesh.scene._lightsState;
560
- const materialState = mesh._material._state;
561
- const clipping = sectionPlanesState.getNumAllocatedSectionPlanes() > 0;
562
- const normals = hasNormals(mesh);
563
- const uvs = geometryState.uvBuf;
564
- const solid = false && materialState.backfaces;
565
- const phongMaterial = (materialState.type === "PhongMaterial");
5
+ export const DrawShaderSource = function(meshDrawHash, programVariables, geometry, material, scene) {
6
+ const materialState = material._state;
7
+ const phongMaterial = (materialState.type === "PhongMaterial");
566
8
  const metallicMaterial = (materialState.type === "MetallicMaterial");
567
9
  const specularMaterial = (materialState.type === "SpecularMaterial");
568
- const receivesShadow = getReceivesShadow(mesh);
569
- const gammaInput = scene.gammaInput; // If set, then it expects that all textures and colors are premultiplied gamma. Default is false.
570
- const gammaOutput = scene.gammaOutput; // If set, then it expects that all textures and colors need to be outputted in premultiplied gamma. Default is false.
571
-
572
- let light;
573
- const src = [];
574
- src.push("#version 300 es");
575
- src.push("// Drawing fragment shader");
576
-
577
- src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH");
578
- src.push("precision highp float;");
579
- src.push("precision highp int;");
580
- src.push("#else");
581
- src.push("precision mediump float;");
582
- src.push("precision mediump int;");
583
- src.push("#endif");
584
-
585
- if (scene.logarithmicDepthBufferEnabled) {
586
- src.push("in float isPerspective;");
587
- src.push("uniform float logDepthBufFC;");
588
- src.push("in float vFragDepth;");
589
- }
590
-
591
- if (receivesShadow) {
592
- src.push("float unpackDepth (vec4 color) {");
593
- src.push(" const vec4 bitShift = vec4(1.0, 1.0/256.0, 1.0/(256.0 * 256.0), 1.0/(256.0*256.0*256.0));");
594
- src.push(" return dot(color, bitShift);");
595
- src.push("}");
596
- }
597
-
598
- //--------------------------------------------------------------------------------
599
- // GAMMA CORRECTION
600
- //--------------------------------------------------------------------------------
601
-
602
- src.push("uniform float gammaFactor;");
603
- src.push("vec4 linearToLinear( in vec4 value ) {");
604
- src.push(" return value;");
605
- src.push("}");
606
- src.push("vec4 sRGBToLinear( in vec4 value ) {");
607
- src.push(" return vec4( mix( pow( value.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), value.rgb * 0.0773993808, vec3( lessThanEqual( value.rgb, vec3( 0.04045 ) ) ) ), value.w );");
608
- src.push("}");
609
- src.push("vec4 gammaToLinear( in vec4 value) {");
610
- src.push(" return vec4( pow( value.xyz, vec3( gammaFactor ) ), value.w );");
611
- src.push("}");
612
- if (gammaOutput) {
613
- src.push("vec4 linearToGamma( in vec4 value, in float gammaFactor ) {");
614
- src.push(" return vec4( pow( value.xyz, vec3( 1.0 / gammaFactor ) ), value.w );");
615
- src.push("}");
616
- }
617
-
618
- //--------------------------------------------------------------------------------
619
- // USER CLIP PLANES
620
- //--------------------------------------------------------------------------------
621
-
622
- if (clipping) {
623
- src.push("in vec4 vWorldPosition;");
624
- src.push("uniform bool clippable;");
625
- for (var i = 0; i < sectionPlanesState.getNumAllocatedSectionPlanes(); i++) {
626
- src.push("uniform bool sectionPlaneActive" + i + ";");
627
- src.push("uniform vec3 sectionPlanePos" + i + ";");
628
- src.push("uniform vec3 sectionPlaneDir" + i + ";");
629
- }
630
- }
631
-
632
- if (normals) {
633
-
634
- //--------------------------------------------------------------------------------
635
- // LIGHT AND REFLECTION MAP INPUTS
636
- // Define here so available globally to shader functions
637
- //--------------------------------------------------------------------------------
638
-
639
- if (lightsState.lightMaps.length > 0) {
640
- src.push("uniform samplerCube lightMap;");
641
- src.push("uniform mat4 viewNormalMatrix;");
642
- }
643
- if (lightsState.reflectionMaps.length > 0) {
644
- src.push("uniform samplerCube reflectionMap;");
645
- }
646
- if (lightsState.lightMaps.length > 0 || lightsState.reflectionMaps.length > 0) {
647
- src.push("uniform mat4 viewMatrix;");
648
- }
649
-
650
- //--------------------------------------------------------------------------------
651
- // SHADING FUNCTIONS
652
- //--------------------------------------------------------------------------------
653
-
654
- // CONSTANT DEFINITIONS
655
-
656
- src.push("#define PI 3.14159265359");
657
- src.push("#define RECIPROCAL_PI 0.31830988618");
658
- src.push("#define RECIPROCAL_PI2 0.15915494");
659
- src.push("#define EPSILON 1e-6");
660
-
661
- src.push("#define saturate(a) clamp( a, 0.0, 1.0 )");
662
-
663
- // UTILITY DEFINITIONS
664
-
665
- src.push("vec3 inverseTransformDirection(in vec3 dir, in mat4 matrix) {");
666
- src.push(" return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );");
667
- src.push("}");
668
-
669
- // STRUCTURES
670
-
671
- src.push("struct IncidentLight {");
672
- src.push(" vec3 color;");
673
- src.push(" vec3 direction;");
674
- src.push("};");
675
-
676
- src.push("struct ReflectedLight {");
677
- src.push(" vec3 diffuse;");
678
- src.push(" vec3 specular;");
679
- src.push("};");
680
-
681
- src.push("struct Geometry {");
682
- src.push(" vec3 position;");
683
- src.push(" vec3 viewNormal;");
684
- src.push(" vec3 worldNormal;");
685
- src.push(" vec3 viewEyeDir;");
686
- src.push("};");
687
-
688
- src.push("struct Material {");
689
- src.push(" vec3 diffuseColor;");
690
- src.push(" float specularRoughness;");
691
- src.push(" vec3 specularColor;");
692
- src.push(" float shine;"); // Only used for Phong
693
- src.push("};");
694
10
 
695
- // COMMON UTILS
11
+ const attributes = geometry.attributes;
12
+ const lightSetup = createLightSetup(programVariables, scene._lightsState);
696
13
 
697
- if (phongMaterial) {
14
+ const colorize = programVariables.createUniform("vec4", "colorize", (set, state) => set(state.meshColorize));
698
15
 
699
- if (lightsState.lightMaps.length > 0 || lightsState.reflectionMaps.length > 0) {
700
-
701
- src.push("void computePhongLightMapping(const in Geometry geometry, const in Material material, inout ReflectedLight reflectedLight) {");
702
-
703
- if (lightsState.lightMaps.length > 0) {
704
- src.push(" vec3 irradiance = " + TEXTURE_DECODE_FUNCS[lightsState.lightMaps[0].encoding] + "(texture(lightMap, geometry.worldNormal)).rgb;");
705
- src.push(" irradiance *= PI;");
706
- src.push(" vec3 diffuseBRDFContrib = (RECIPROCAL_PI * material.diffuseColor);");
707
- src.push(" reflectedLight.diffuse += irradiance * diffuseBRDFContrib;");
708
- }
709
- if (lightsState.reflectionMaps.length > 0) {
710
- src.push(" vec3 reflectVec = reflect(-geometry.viewEyeDir, geometry.viewNormal);");
711
- src.push(" vec3 radiance = texture(reflectionMap, reflectVec).rgb * 0.2;");
712
- src.push(" radiance *= PI;");
713
- src.push(" reflectedLight.specular += radiance;");
714
- }
715
- src.push("}");
716
- }
717
-
718
- src.push("void computePhongLighting(const in IncidentLight directLight, const in Geometry geometry, const in Material material, inout ReflectedLight reflectedLight) {");
719
- src.push(" float dotNL = saturate(dot(geometry.viewNormal, directLight.direction));");
720
- src.push(" vec3 irradiance = dotNL * directLight.color * PI;");
721
- src.push(" reflectedLight.diffuse += irradiance * (RECIPROCAL_PI * material.diffuseColor);");
722
- src.push(" reflectedLight.specular += directLight.color * material.specularColor * pow(max(dot(reflect(-directLight.direction, -geometry.viewNormal), geometry.viewEyeDir), 0.0), material.shine);");
16
+ const fresnel = programVariables.createFragmentDefinition(
17
+ "fresnel",
18
+ (name, src) => {
19
+ src.push(`float ${name}(vec3 eyeDir, vec3 normal, float edgeBias, float centerBias, float power) {`);
20
+ src.push(" float fr = abs(dot(eyeDir, normal));");
21
+ src.push(" float finalFr = clamp((fr - edgeBias) / (centerBias - edgeBias), 0.0, 1.0);");
22
+ src.push(" return pow(finalFr, power);");
723
23
  src.push("}");
24
+ });
25
+
26
+ const setupFresnel = (name, colorSwizzle, getMaterialValue) => {
27
+ const fresnelUniform = (type, namePostfix, getParameterValue) => {
28
+ return programVariables.createUniform(type, name + "Fresnel" + namePostfix, (set, state) => set(getParameterValue(getMaterialValue(state.material))));
29
+ };
30
+ const edgeBias = fresnelUniform("float", "EdgeBias", (fresnelValue => fresnelValue.edgeBias));
31
+ const centerBias = fresnelUniform("float", "CenterBias", (fresnelValue => fresnelValue.centerBias));
32
+ const power = fresnelUniform("float", "Power", (fresnelValue => fresnelValue.power));
33
+ const edgeColor = fresnelUniform("vec3", "EdgeColor", (fresnelValue => fresnelValue.edgeColor));
34
+ const centerColor = fresnelUniform("vec3", "CenterColor", (fresnelValue => fresnelValue.centerColor));
35
+
36
+ return getMaterialValue(material) && ((viewEyeDir, viewNormal) => {
37
+ const f = `${fresnel}(${viewEyeDir}, ${viewNormal}, ${edgeBias}, ${centerBias}, ${power})`;
38
+ return `mix(${edgeColor + colorSwizzle}, ${centerColor + colorSwizzle}, ${f})`;
39
+ });
40
+ };
41
+
42
+ const diffuseFresnel = setupFresnel("diffuse", "", mtl => mtl._diffuseFresnel);
43
+ const specularFresnel = setupFresnel("specular", "", mtl => mtl._specularFresnel);
44
+ const emissiveFresnel = setupFresnel("emissive", "", mtl => mtl._emissiveFresnel);
45
+ const alphaFresnel = setupFresnel("alpha", ".r", mtl => mtl._alphaFresnel);
46
+
47
+
48
+ const setup2dTexture = (name, getMaterialValue) => {
49
+ const initTex = attributes.uv && getMaterialValue(material);
50
+ const tex = initTex && setupTexture(programVariables, "sampler2D", name, initTex.encoding, (set, state) => set(getMaterialValue(state.material)._state.texture));
51
+ return tex && (function() {
52
+ const matrix = initTex._state.matrix && programVariables.createUniform("mat4", name + "MapMatrix", (set, state) => set(getMaterialValue(state.material)._state.matrix));
53
+ const getTexCoordExpression = matrix ? (texPos => `(${matrix} * ${texPos}).st`) : (texPos => `${texPos}.st`);
54
+ const sample = (texturePos, bias) => tex(getTexCoordExpression(texturePos, bias));
55
+ sample.getTexCoordExpression = getTexCoordExpression;
56
+ return sample;
57
+ })();
58
+ };
59
+
60
+ const ambientMap = setup2dTexture("ambient", mtl => mtl._ambientMap);
61
+ const baseColorMap = setup2dTexture("baseColor", mtl => mtl._baseColorMap);
62
+ const diffuseMap = setup2dTexture("diffuse", mtl => mtl._diffuseMap);
63
+ const emissiveMap = setup2dTexture("emissive", mtl => mtl._emissiveMap);
64
+ const occlusionMap = setup2dTexture("occlusion", mtl => mtl._occlusionMap);
65
+ const alphaMap = setup2dTexture("alpha", mtl => mtl._alphaMap);
66
+ const metallicMap = setup2dTexture("metallic", mtl => mtl._metallicMap);
67
+ const roughnessMap = setup2dTexture("roughness", mtl => mtl._roughnessMap);
68
+ const metallicRoughnessMap = setup2dTexture("metallicRoughness", mtl => mtl._metallicRoughnessMap);
69
+ const specularMap = setup2dTexture("specular", mtl => mtl._specularMap);
70
+ const glossinessMap = setup2dTexture("glossiness", mtl => mtl._glossinessMap);
71
+ const specularGlossinessMap = setup2dTexture("specularGlossiness", mtl => mtl._specularGlossinessMap);
72
+ const normalMap = setup2dTexture("normal", mtl => mtl._normalMap);
73
+
74
+
75
+ const setupUniform = (name, type, getMaterialValue) => {
76
+ const initValue = getMaterialValue(material);
77
+ const isDefined = (type === "float") ? ((initValue !== undefined) && (initValue !== null)) : initValue;
78
+ return isDefined && programVariables.createUniform(type, name, (set, state) => set(getMaterialValue(state.material)));
79
+ };
80
+
81
+ const materialAmbient = setupUniform("materialAmbient", "vec3", mtl => mtl.ambient);
82
+ const materialDiffuse = setupUniform("materialDiffuse", "vec3", mtl => mtl.diffuse);
83
+ const materialBaseColor = setupUniform("materialBaseColor", "vec3", mtl => mtl.baseColor);
84
+ const materialEmissive = setupUniform("materialEmissive", "vec3", mtl => mtl.emissive);
85
+ const materialSpecular = setupUniform("materialSpecular", "vec3", mtl => mtl.specular);
86
+ const materialGlossiness = setupUniform("materialGlossiness", "float", mtl => mtl.glossiness);
87
+ const materialMetallic = setupUniform("materialMetallic", "float", mtl => mtl.metallic);
88
+ const materialRoughness = setupUniform("materialRoughness", "float", mtl => mtl.roughness);
89
+ const materialShininess = setupUniform("materialShininess", "float", mtl => mtl.shininess);
90
+ const materialSpecularF0 = setupUniform("materialSpecularF0", "float", mtl => mtl.specularF0);
91
+ const materialAlphaModeCutoff = setupUniform("materialAlphaModeCutoff", "vec4", mtl => {
92
+ const alpha = mtl.alpha;
93
+ if ((alpha !== undefined) && (alpha !== null)) {
94
+ tempVec4[0] = alpha;
95
+ tempVec4[1] = (mtl.alphaMode === 1 ? 1 : 0);
96
+ tempVec4[2] = mtl.alphaCutoff;
97
+ return tempVec4;
98
+ } else {
99
+ return null;
724
100
  }
101
+ });
725
102
 
726
- if (metallicMaterial || specularMaterial) {
727
-
728
- // IRRADIANCE EVALUATION
103
+ const vViewPosition = programVariables.createVarying("vec3", "vViewPosition", () => `${attributes.position.view}.xyz`);
104
+ const texturePos = programVariables.createVarying("vec4", "texturePos", () => `vec4(${attributes.uv}, 1.0, 1.0)`);
105
+ const vColor = attributes.color && programVariables.createVarying("vec4", "vColor", () => attributes.color);
729
106
 
730
- src.push("float GGXRoughnessToBlinnExponent(const in float ggxRoughness) {");
731
- src.push(" float r = ggxRoughness + 0.0001;");
732
- src.push(" return (2.0 / (r * r) - 2.0);");
733
- src.push("}");
734
-
735
- src.push("float getSpecularMIPLevel(const in float blinnShininessExponent, const in int maxMIPLevel) {");
736
- src.push(" float maxMIPLevelScalar = float( maxMIPLevel );");
737
- src.push(" float desiredMIPLevel = maxMIPLevelScalar - 0.79248 - 0.5 * log2( ( blinnShininessExponent * blinnShininessExponent ) + 1.0 );");
738
- src.push(" return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );");
739
- src.push("}");
107
+ const vViewNormal = programVariables.createVarying("vec3", "vViewNormal", () => attributes.normal.view);
108
+ const texUnitConverter = `mat4(0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.5, 1.0)`;
109
+ const vDirectionals = lightSetup.directionalLights.map((light, i) => ({
110
+ vViewLightReverseDir: programVariables.createVarying("vec3", `vViewLightReverseDir${i}`, () => light.getDirection(geometry.viewMatrix, null)),
111
+ vShadowPosFromLight: programVariables.createVarying("vec3", `vShadowPosFromLight${i}`, () => `(${texUnitConverter} * ${light.shadowParameters.getShadowProjMatrix()} * (${light.shadowParameters.getShadowViewMatrix()} * ${attributes.position.world})).xyz`)
112
+ }));
113
+ const vWorldNormal = programVariables.createVarying("vec3", "vWorldNormal", () => attributes.normal.world);
740
114
 
741
- if (lightsState.reflectionMaps.length > 0) {
742
- src.push("vec3 getLightProbeIndirectRadiance(const in vec3 reflectVec, const in float blinnShininessExponent, const in int maxMIPLevel) {");
743
- src.push(" float mipLevel = 0.5 * getSpecularMIPLevel(blinnShininessExponent, maxMIPLevel);"); //TODO: a random factor - fix this
744
- src.push(" vec3 envMapColor = " + TEXTURE_DECODE_FUNCS[lightsState.reflectionMaps[0].encoding] + "(texture(reflectionMap, reflectVec, mipLevel)).rgb;");
745
- src.push(" return envMapColor;");
746
- src.push("}");
747
- }
115
+ const outColor = programVariables.createOutput("vec4", "outColor");
748
116
 
749
- // SPECULAR BRDF EVALUATION
117
+ const saturate = programVariables.createFragmentDefinition(
118
+ "saturate",
119
+ (name, src) => src.push(`float ${name}(const in float a) { return clamp(a, 0.0, 1.0); }`));
750
120
 
121
+ const BRDF_Specular_GGX = programVariables.createFragmentDefinition(
122
+ "BRDF_Specular_GGX",
123
+ (name, src) => {
751
124
  src.push("vec3 F_Schlick(const in vec3 specularColor, const in float dotLH) {");
752
125
  src.push(" float fresnel = exp2( ( -5.55473 * dotLH - 6.98316 ) * dotLH );");
753
126
  src.push(" return ( 1.0 - specularColor ) * fresnel + specularColor;");
@@ -764,30 +137,34 @@ function buildFragmentDraw(mesh) {
764
137
  src.push(" float a2 = ( alpha * alpha );");
765
138
  src.push(" float gv = dotNL * sqrt( a2 + ( 1.0 - a2 ) * ( dotNV * dotNV ) );");
766
139
  src.push(" float gl = dotNV * sqrt( a2 + ( 1.0 - a2 ) * ( dotNL * dotNL ) );");
767
- src.push(" return 0.5 / max( gv + gl, EPSILON );");
140
+ src.push(" return 0.5 / max( gv + gl, 1e-6 );");
768
141
  src.push("}");
769
142
 
770
143
  src.push("float D_GGX(const in float alpha, const in float dotNH) {");
771
144
  src.push(" float a2 = ( alpha * alpha );");
772
145
  src.push(" float denom = ( dotNH * dotNH) * ( a2 - 1.0 ) + 1.0;");
773
- src.push(" return RECIPROCAL_PI * a2 / ( denom * denom);");
146
+ src.push(" return 0.31830988618 * a2 / ( denom * denom);"); // 1/PI
774
147
  src.push("}");
775
148
 
776
- src.push("vec3 BRDF_Specular_GGX(const in IncidentLight incidentLight, const in Geometry geometry, const in vec3 specularColor, const in float roughness) {");
149
+ src.push(`vec3 ${name}(const in vec3 incidentLightDirection, const in vec3 viewNormal, const in vec3 viewEyeDir, const in vec3 specularColor, const in float roughness) {`);
777
150
  src.push(" float alpha = ( roughness * roughness );");
778
- src.push(" vec3 halfDir = normalize( incidentLight.direction + geometry.viewEyeDir );");
779
- src.push(" float dotNL = saturate( dot( geometry.viewNormal, incidentLight.direction ) );");
780
- src.push(" float dotNV = saturate( dot( geometry.viewNormal, geometry.viewEyeDir ) );");
781
- src.push(" float dotNH = saturate( dot( geometry.viewNormal, halfDir ) );");
782
- src.push(" float dotLH = saturate( dot( incidentLight.direction, halfDir ) );");
151
+ src.push(" vec3 halfDir = normalize( incidentLightDirection + viewEyeDir );");
152
+ src.push(` float dotNL = ${saturate}( dot( viewNormal, incidentLightDirection ) );`);
153
+ src.push(` float dotNV = ${saturate}( dot( viewNormal, viewEyeDir ) );`);
154
+ src.push(` float dotNH = ${saturate}( dot( viewNormal, halfDir ) );`);
155
+ src.push(` float dotLH = ${saturate}( dot( incidentLightDirection, halfDir ) );`);
783
156
  src.push(" vec3 F = F_Schlick( specularColor, dotLH );");
784
157
  src.push(" float G = G_GGX_SmithCorrelated( alpha, dotNL, dotNV );");
785
158
  src.push(" float D = D_GGX( alpha, dotNH );");
786
159
  src.push(" return F * (G * D);");
787
160
  src.push("}");
161
+ });
788
162
 
789
- src.push("vec3 BRDF_Specular_GGX_Environment(const in Geometry geometry, const in vec3 specularColor, const in float roughness) {");
790
- src.push(" float dotNV = saturate(dot(geometry.viewNormal, geometry.viewEyeDir));");
163
+ const BRDF_Specular_GGX_Environment = programVariables.createFragmentDefinition(
164
+ "BRDF_Specular_GGX_Environment",
165
+ (name, src) => {
166
+ src.push(`vec3 ${name}(const in vec3 viewNormal, const in vec3 viewEyeDir, const in vec3 specularColor, const in float roughness) {`);
167
+ src.push(` float dotNV = ${saturate}(dot(viewNormal, viewEyeDir));`);
791
168
  src.push(" const vec4 c0 = vec4( -1, -0.0275, -0.572, 0.022);");
792
169
  src.push(" const vec4 c1 = vec4( 1, 0.0425, 1.04, -0.04);");
793
170
  src.push(" vec4 r = roughness * c0 + c1;");
@@ -795,776 +172,202 @@ function buildFragmentDraw(mesh) {
795
172
  src.push(" vec2 AB = vec2(-1.04, 1.04) * a004 + r.zw;");
796
173
  src.push(" return specularColor * AB.x + AB.y;");
797
174
  src.push("}");
798
-
799
- if (lightsState.lightMaps.length > 0 || lightsState.reflectionMaps.length > 0) {
800
-
801
- src.push("void computePBRLightMapping(const in Geometry geometry, const in Material material, inout ReflectedLight reflectedLight) {");
802
- if (lightsState.lightMaps.length > 0) {
803
- src.push(" vec3 irradiance = sRGBToLinear(texture(lightMap, geometry.worldNormal)).rgb;");
804
- src.push(" irradiance *= PI;");
805
- src.push(" vec3 diffuseBRDFContrib = (RECIPROCAL_PI * material.diffuseColor);");
806
- src.push(" reflectedLight.diffuse += irradiance * diffuseBRDFContrib;");
807
- // src.push(" reflectedLight.diffuse = vec3(1.0, 0.0, 0.0);");
808
- }
809
- if (lightsState.reflectionMaps.length > 0) {
810
- src.push(" vec3 reflectVec = reflect(-geometry.viewEyeDir, geometry.viewNormal);");
811
- src.push(" reflectVec = inverseTransformDirection(reflectVec, viewMatrix);");
812
- src.push(" float blinnExpFromRoughness = GGXRoughnessToBlinnExponent(material.specularRoughness);");
813
- src.push(" vec3 radiance = getLightProbeIndirectRadiance(reflectVec, blinnExpFromRoughness, 8);");
814
- src.push(" vec3 specularBRDFContrib = BRDF_Specular_GGX_Environment(geometry, material.specularColor, material.specularRoughness);");
815
- src.push(" reflectedLight.specular += radiance * specularBRDFContrib;");
816
- }
817
- src.push("}");
818
- }
819
-
820
- // MAIN LIGHTING COMPUTATION FUNCTION
821
-
822
- src.push("void computePBRLighting(const in IncidentLight incidentLight, const in Geometry geometry, const in Material material, inout ReflectedLight reflectedLight) {");
823
- src.push(" float dotNL = saturate(dot(geometry.viewNormal, incidentLight.direction));");
824
- src.push(" vec3 irradiance = dotNL * incidentLight.color * PI;");
825
- src.push(" reflectedLight.diffuse += irradiance * (RECIPROCAL_PI * material.diffuseColor);");
826
- src.push(" reflectedLight.specular += irradiance * BRDF_Specular_GGX(incidentLight, geometry, material.specularColor, material.specularRoughness);");
827
- src.push("}");
828
-
829
- } // (metallicMaterial || specularMaterial)
830
-
831
- } // geometry.normals
832
-
833
- //--------------------------------------------------------------------------------
834
- // GEOMETRY INPUTS
835
- //--------------------------------------------------------------------------------
836
-
837
- src.push("in vec3 vViewPosition;");
838
-
839
- if (geometryState.colors) {
840
- src.push("in vec4 vColor;");
841
- }
842
-
843
- if (uvs &&
844
- ((normals && material._normalMap)
845
- || material._ambientMap
846
- || material._baseColorMap
847
- || material._diffuseMap
848
- || material._emissiveMap
849
- || material._metallicMap
850
- || material._roughnessMap
851
- || material._metallicRoughnessMap
852
- || material._specularMap
853
- || material._glossinessMap
854
- || material._specularGlossinessMap
855
- || material._occlusionMap
856
- || material._alphaMap)) {
857
- src.push("in vec2 vUV;");
858
- }
859
-
860
- if (normals) {
861
- if (lightsState.lightMaps.length > 0) {
862
- src.push("in vec3 vWorldNormal;");
863
- }
864
- src.push("in vec3 vViewNormal;");
865
- }
866
-
867
- //--------------------------------------------------------------------------------
868
- // MATERIAL CHANNEL INPUTS
869
- //--------------------------------------------------------------------------------
870
-
871
- if (materialState.ambient) {
872
- src.push("uniform vec3 materialAmbient;");
873
- }
874
- if (materialState.baseColor) {
875
- src.push("uniform vec3 materialBaseColor;");
876
- }
877
- if (materialState.alpha !== undefined && materialState.alpha !== null) {
878
- src.push("uniform vec4 materialAlphaModeCutoff;"); // [alpha, alphaMode, alphaCutoff]
879
- }
880
- if (materialState.emissive) {
881
- src.push("uniform vec3 materialEmissive;");
882
- }
883
- if (materialState.diffuse) {
884
- src.push("uniform vec3 materialDiffuse;");
885
- }
886
- if (materialState.glossiness !== undefined && materialState.glossiness !== null) {
887
- src.push("uniform float materialGlossiness;");
888
- }
889
- if (materialState.shininess !== undefined && materialState.shininess !== null) {
890
- src.push("uniform float materialShininess;"); // Phong channel
891
- }
892
- if (materialState.specular) {
893
- src.push("uniform vec3 materialSpecular;");
894
- }
895
- if (materialState.metallic !== undefined && materialState.metallic !== null) {
896
- src.push("uniform float materialMetallic;");
897
- }
898
- if (materialState.roughness !== undefined && materialState.roughness !== null) {
899
- src.push("uniform float materialRoughness;");
900
- }
901
- if (materialState.specularF0 !== undefined && materialState.specularF0 !== null) {
902
- src.push("uniform float materialSpecularF0;");
903
- }
904
-
905
- //--------------------------------------------------------------------------------
906
- // MATERIAL TEXTURE INPUTS
907
- //--------------------------------------------------------------------------------
908
-
909
- if (uvs && material._ambientMap) {
910
- src.push("uniform sampler2D ambientMap;");
911
- if (material._ambientMap._state.matrix) {
912
- src.push("uniform mat4 ambientMapMatrix;");
913
- }
914
- }
915
- if (uvs && material._baseColorMap) {
916
- src.push("uniform sampler2D baseColorMap;");
917
- if (material._baseColorMap._state.matrix) {
918
- src.push("uniform mat4 baseColorMapMatrix;");
919
- }
920
- }
921
- if (uvs && material._diffuseMap) {
922
- src.push("uniform sampler2D diffuseMap;");
923
- if (material._diffuseMap._state.matrix) {
924
- src.push("uniform mat4 diffuseMapMatrix;");
925
- }
926
- }
927
- if (uvs && material._emissiveMap) {
928
- src.push("uniform sampler2D emissiveMap;");
929
- if (material._emissiveMap._state.matrix) {
930
- src.push("uniform mat4 emissiveMapMatrix;");
931
- }
932
- }
933
- if (normals && uvs && material._metallicMap) {
934
- src.push("uniform sampler2D metallicMap;");
935
- if (material._metallicMap._state.matrix) {
936
- src.push("uniform mat4 metallicMapMatrix;");
937
- }
938
- }
939
- if (normals && uvs && material._roughnessMap) {
940
- src.push("uniform sampler2D roughnessMap;");
941
- if (material._roughnessMap._state.matrix) {
942
- src.push("uniform mat4 roughnessMapMatrix;");
943
- }
944
- }
945
- if (normals && uvs && material._metallicRoughnessMap) {
946
- src.push("uniform sampler2D metallicRoughnessMap;");
947
- if (material._metallicRoughnessMap._state.matrix) {
948
- src.push("uniform mat4 metallicRoughnessMapMatrix;");
949
- }
950
- }
951
- if (normals && material._normalMap) {
952
- src.push("uniform sampler2D normalMap;");
953
- if (material._normalMap._state.matrix) {
954
- src.push("uniform mat4 normalMapMatrix;");
955
- }
956
- src.push("vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm, vec2 uv ) {");
957
- src.push(" vec3 q0 = vec3( dFdx( eye_pos.x ), dFdx( eye_pos.y ), dFdx( eye_pos.z ) );");
958
- src.push(" vec3 q1 = vec3( dFdy( eye_pos.x ), dFdy( eye_pos.y ), dFdy( eye_pos.z ) );");
959
- src.push(" vec2 st0 = dFdx( uv.st );");
960
- src.push(" vec2 st1 = dFdy( uv.st );");
961
- src.push(" vec3 S = normalize( q0 * st1.t - q1 * st0.t );");
962
- src.push(" vec3 T = normalize( -q0 * st1.s + q1 * st0.s );");
963
- src.push(" vec3 N = normalize( surf_norm );");
964
- src.push(" vec3 mapN = texture( normalMap, uv ).xyz * 2.0 - 1.0;");
965
- src.push(" mat3 tsn = mat3( S, T, N );");
966
- // src.push(" mapN *= 3.0;");
967
- src.push(" return normalize( tsn * mapN );");
968
- src.push("}");
969
- }
970
- if (uvs && material._occlusionMap) {
971
- src.push("uniform sampler2D occlusionMap;");
972
- if (material._occlusionMap._state.matrix) {
973
- src.push("uniform mat4 occlusionMapMatrix;");
974
- }
975
- }
976
- if (uvs && material._alphaMap) {
977
- src.push("uniform sampler2D alphaMap;");
978
- if (material._alphaMap._state.matrix) {
979
- src.push("uniform mat4 alphaMapMatrix;");
980
- }
981
- }
982
- if (normals && uvs && material._specularMap) {
983
- src.push("uniform sampler2D specularMap;");
984
- if (material._specularMap._state.matrix) {
985
- src.push("uniform mat4 specularMapMatrix;");
986
- }
987
- }
988
- if (normals && uvs && material._glossinessMap) {
989
- src.push("uniform sampler2D glossinessMap;");
990
- if (material._glossinessMap._state.matrix) {
991
- src.push("uniform mat4 glossinessMapMatrix;");
992
- }
993
- }
994
- if (normals && uvs && material._specularGlossinessMap) {
995
- src.push("uniform sampler2D materialSpecularGlossinessMap;");
996
- if (material._specularGlossinessMap._state.matrix) {
997
- src.push("uniform mat4 materialSpecularGlossinessMapMatrix;");
998
- }
999
- }
1000
-
1001
- //--------------------------------------------------------------------------------
1002
- // MATERIAL FRESNEL INPUTS
1003
- //--------------------------------------------------------------------------------
1004
-
1005
- if (normals && (material._diffuseFresnel ||
1006
- material._specularFresnel ||
1007
- material._alphaFresnel ||
1008
- material._emissiveFresnel ||
1009
- material._reflectivityFresnel)) {
1010
- src.push("float fresnel(vec3 eyeDir, vec3 normal, float edgeBias, float centerBias, float power) {");
1011
- src.push(" float fr = abs(dot(eyeDir, normal));");
1012
- src.push(" float finalFr = clamp((fr - edgeBias) / (centerBias - edgeBias), 0.0, 1.0);");
1013
- src.push(" return pow(finalFr, power);");
1014
- src.push("}");
1015
- if (material._diffuseFresnel) {
1016
- src.push("uniform float diffuseFresnelCenterBias;");
1017
- src.push("uniform float diffuseFresnelEdgeBias;");
1018
- src.push("uniform float diffuseFresnelPower;");
1019
- src.push("uniform vec3 diffuseFresnelCenterColor;");
1020
- src.push("uniform vec3 diffuseFresnelEdgeColor;");
1021
- }
1022
- if (material._specularFresnel) {
1023
- src.push("uniform float specularFresnelCenterBias;");
1024
- src.push("uniform float specularFresnelEdgeBias;");
1025
- src.push("uniform float specularFresnelPower;");
1026
- src.push("uniform vec3 specularFresnelCenterColor;");
1027
- src.push("uniform vec3 specularFresnelEdgeColor;");
1028
- }
1029
- if (material._alphaFresnel) {
1030
- src.push("uniform float alphaFresnelCenterBias;");
1031
- src.push("uniform float alphaFresnelEdgeBias;");
1032
- src.push("uniform float alphaFresnelPower;");
1033
- src.push("uniform vec3 alphaFresnelCenterColor;");
1034
- src.push("uniform vec3 alphaFresnelEdgeColor;");
1035
- }
1036
- if (material._reflectivityFresnel) {
1037
- src.push("uniform float materialSpecularF0FresnelCenterBias;");
1038
- src.push("uniform float materialSpecularF0FresnelEdgeBias;");
1039
- src.push("uniform float materialSpecularF0FresnelPower;");
1040
- src.push("uniform vec3 materialSpecularF0FresnelCenterColor;");
1041
- src.push("uniform vec3 materialSpecularF0FresnelEdgeColor;");
1042
- }
1043
- if (material._emissiveFresnel) {
1044
- src.push("uniform float emissiveFresnelCenterBias;");
1045
- src.push("uniform float emissiveFresnelEdgeBias;");
1046
- src.push("uniform float emissiveFresnelPower;");
1047
- src.push("uniform vec3 emissiveFresnelCenterColor;");
1048
- src.push("uniform vec3 emissiveFresnelEdgeColor;");
1049
- }
1050
- }
1051
-
1052
- //--------------------------------------------------------------------------------
1053
- // LIGHT SOURCES
1054
- //--------------------------------------------------------------------------------
1055
-
1056
- src.push("uniform vec4 lightAmbient;");
1057
-
1058
- if (normals) {
1059
- for (let i = 0, len = lightsState.lights.length; i < len; i++) { // Light sources
1060
- const light = lightsState.lights[i];
1061
- if (light.type === "ambient") {
1062
- continue;
1063
- }
1064
- src.push("uniform vec4 lightColor" + i + ";");
1065
- if (light.type === "point") {
1066
- src.push("uniform vec3 lightAttenuation" + i + ";");
1067
- }
1068
- if (light.type === "dir" && light.space === "view") {
1069
- src.push("uniform vec3 lightDir" + i + ";");
1070
- }
1071
- if (light.type === "point" && light.space === "view") {
1072
- src.push("uniform vec3 lightPos" + i + ";");
1073
- } else {
1074
- src.push("in vec4 vViewLightReverseDirAndDist" + i + ";");
1075
- }
1076
- }
1077
- }
1078
-
1079
- if (receivesShadow) {
1080
-
1081
- // Variance castsShadow mapping filter
1082
-
1083
- // src.push("float linstep(float low, float high, float v){");
1084
- // src.push(" return clamp((v-low)/(high-low), 0.0, 1.0);");
1085
- // src.push("}");
1086
- //
1087
- // src.push("float VSM(sampler2D depths, vec2 uv, float compare){");
1088
- // src.push(" vec2 moments = texture(depths, uv).xy;");
1089
- // src.push(" float p = smoothstep(compare-0.02, compare, moments.x);");
1090
- // src.push(" float variance = max(moments.y - moments.x*moments.x, -0.001);");
1091
- // src.push(" float d = compare - moments.x;");
1092
- // src.push(" float p_max = linstep(0.2, 1.0, variance / (variance + d*d));");
1093
- // src.push(" return clamp(max(p, p_max), 0.0, 1.0);");
1094
- // src.push("}");
1095
-
1096
- for (let i = 0, len = lightsState.lights.length; i < len; i++) { // Light sources
1097
- if (lightsState.lights[i].castsShadow) {
1098
- src.push("in vec4 vShadowPosFromLight" + i + ";");
1099
- src.push("uniform sampler2D shadowMap" + i + ";");
1100
- }
1101
- }
1102
- }
1103
-
1104
- src.push("uniform vec4 colorize;");
1105
-
1106
- //================================================================================
1107
- // MAIN
1108
- //================================================================================
1109
- src.push("out vec4 outColor;");
1110
- src.push("void main(void) {");
1111
-
1112
- if (clipping) {
1113
- src.push("if (clippable) {");
1114
- src.push(" float dist = 0.0;");
1115
- for (var i = 0; i < sectionPlanesState.getNumAllocatedSectionPlanes(); i++) {
1116
- src.push("if (sectionPlaneActive" + i + ") {");
1117
- src.push(" dist += clamp(dot(-sectionPlaneDir" + i + ".xyz, vWorldPosition.xyz - sectionPlanePos" + i + ".xyz), 0.0, 1000.0);");
175
+ });
176
+
177
+ const perturbNormal2Arb = programVariables.createFragmentDefinition(
178
+ "perturbNormal2Arb",
179
+ (name, src) => {
180
+ src.push(`vec3 ${name}( vec3 eye_pos, vec3 surf_norm, vec2 uv, vec4 texel ) {`);
181
+ src.push(" vec3 q0 = vec3( dFdx( eye_pos.x ), dFdx( eye_pos.y ), dFdx( eye_pos.z ) );");
182
+ src.push(" vec3 q1 = vec3( dFdy( eye_pos.x ), dFdy( eye_pos.y ), dFdy( eye_pos.z ) );");
183
+ src.push(" vec2 st0 = dFdx( uv.st );");
184
+ src.push(" vec2 st1 = dFdy( uv.st );");
185
+ src.push(" vec3 S = normalize( q0 * st1.t - q1 * st0.t );");
186
+ src.push(" vec3 T = normalize( -q0 * st1.s + q1 * st0.s );");
187
+ src.push(" vec3 N = normalize( surf_norm );");
188
+ src.push(" vec3 mapN = texel.xyz * 2.0 - 1.0;");
189
+ src.push(" mat3 tsn = mat3( S, T, N );");
190
+ // src.push(" mapN *= 3.0;");
191
+ src.push(" return normalize( tsn * mapN );");
1118
192
  src.push("}");
1119
- }
1120
- src.push(" if (dist > 0.0) { discard; }");
1121
- if (solid) {
1122
- src.push(" if (gl_FrontFacing == false) {");
1123
- src.push(" outColor = vec4(1.0, 0.0, 0.0, 1.0);");
1124
- src.push(" return;");
1125
- src.push(" }");
1126
- }
1127
- src.push("}");
1128
- }
1129
-
1130
- if (geometryState.primitiveName === "points") {
1131
- src.push("vec2 cxy = 2.0 * gl_PointCoord - 1.0;");
1132
- src.push("float r = dot(cxy, cxy);");
1133
- src.push("if (r > 1.0) {");
1134
- src.push(" discard;");
1135
- src.push("}");
1136
- }
1137
-
1138
- src.push("float occlusion = 1.0;");
1139
-
1140
- if (materialState.ambient) {
1141
- src.push("vec3 ambientColor = materialAmbient;");
1142
- } else {
1143
- src.push("vec3 ambientColor = vec3(1.0, 1.0, 1.0);");
1144
- }
1145
-
1146
- if (materialState.diffuse) {
1147
- src.push("vec3 diffuseColor = materialDiffuse;");
1148
- } else if (materialState.baseColor) {
1149
- src.push("vec3 diffuseColor = materialBaseColor;");
1150
- } else {
1151
- src.push("vec3 diffuseColor = vec3(1.0, 1.0, 1.0);");
1152
- }
1153
-
1154
- if (geometryState.colors) {
1155
- src.push("diffuseColor *= vColor.rgb;");
1156
- }
1157
-
1158
- if (materialState.emissive) {
1159
- src.push("vec3 emissiveColor = materialEmissive;"); // Emissive default is (0,0,0), so initializing here
1160
- } else {
1161
- src.push("vec3 emissiveColor = vec3(0.0, 0.0, 0.0);");
1162
- }
1163
-
1164
- if (materialState.specular) {
1165
- src.push("vec3 specular = materialSpecular;");
1166
- } else {
1167
- src.push("vec3 specular = vec3(1.0, 1.0, 1.0);");
1168
- }
1169
-
1170
- if (materialState.alpha !== undefined) {
1171
- src.push("float alpha = materialAlphaModeCutoff[0];");
1172
- } else {
1173
- src.push("float alpha = 1.0;");
1174
- }
1175
-
1176
- if (geometryState.colors) {
1177
- src.push("alpha *= vColor.a;");
1178
- }
1179
-
1180
- if (materialState.glossiness !== undefined) {
1181
- src.push("float glossiness = materialGlossiness;");
1182
- } else {
1183
- src.push("float glossiness = 1.0;");
1184
- }
1185
-
1186
- if (materialState.metallic !== undefined) {
1187
- src.push("float metallic = materialMetallic;");
1188
- } else {
1189
- src.push("float metallic = 1.0;");
1190
- }
1191
-
1192
- if (materialState.roughness !== undefined) {
1193
- src.push("float roughness = materialRoughness;");
1194
- } else {
1195
- src.push("float roughness = 1.0;");
1196
- }
1197
-
1198
- if (materialState.specularF0 !== undefined) {
1199
- src.push("float specularF0 = materialSpecularF0;");
1200
- } else {
1201
- src.push("float specularF0 = 1.0;");
1202
- }
1203
-
1204
- //--------------------------------------------------------------------------------
1205
- // TEXTURING
1206
- //--------------------------------------------------------------------------------
1207
-
1208
- if (uvs && ((normals && material._normalMap)
1209
- || material._ambientMap
1210
- || material._baseColorMap
1211
- || material._diffuseMap
1212
- || material._occlusionMap
1213
- || material._emissiveMap
1214
- || material._metallicMap
1215
- || material._roughnessMap
1216
- || material._metallicRoughnessMap
1217
- || material._specularMap
1218
- || material._glossinessMap
1219
- || material._specularGlossinessMap
1220
- || material._alphaMap)) {
1221
- src.push("vec4 texturePos = vec4(vUV.s, vUV.t, 1.0, 1.0);");
1222
- src.push("vec2 textureCoord;");
1223
- }
1224
-
1225
- if (uvs && material._ambientMap) {
1226
- if (material._ambientMap._state.matrix) {
1227
- src.push("textureCoord = (ambientMapMatrix * texturePos).xy;");
1228
- } else {
1229
- src.push("textureCoord = texturePos.xy;");
1230
- }
1231
- src.push("vec4 ambientTexel = texture(ambientMap, textureCoord).rgb;");
1232
- src.push("ambientTexel = " + TEXTURE_DECODE_FUNCS[material._ambientMap._state.encoding] + "(ambientTexel);");
1233
- src.push("ambientColor *= ambientTexel.rgb;");
1234
- }
1235
-
1236
- if (uvs && material._diffuseMap) {
1237
- if (material._diffuseMap._state.matrix) {
1238
- src.push("textureCoord = (diffuseMapMatrix * texturePos).xy;");
1239
- } else {
1240
- src.push("textureCoord = texturePos.xy;");
1241
- }
1242
- src.push("vec4 diffuseTexel = texture(diffuseMap, textureCoord);");
1243
- src.push("diffuseTexel = " + TEXTURE_DECODE_FUNCS[material._diffuseMap._state.encoding] + "(diffuseTexel);");
1244
- src.push("diffuseColor *= diffuseTexel.rgb;");
1245
- src.push("alpha *= diffuseTexel.a;");
1246
- }
1247
-
1248
- if (uvs && material._baseColorMap) {
1249
- if (material._baseColorMap._state.matrix) {
1250
- src.push("textureCoord = (baseColorMapMatrix * texturePos).xy;");
1251
- } else {
1252
- src.push("textureCoord = texturePos.xy;");
1253
- }
1254
- src.push("vec4 baseColorTexel = texture(baseColorMap, textureCoord);");
1255
- src.push("baseColorTexel = " + TEXTURE_DECODE_FUNCS[material._baseColorMap._state.encoding] + "(baseColorTexel);");
1256
- src.push("diffuseColor *= baseColorTexel.rgb;");
1257
- src.push("alpha *= baseColorTexel.a;");
1258
- }
1259
-
1260
- if (uvs && material._emissiveMap) {
1261
- if (material._emissiveMap._state.matrix) {
1262
- src.push("textureCoord = (emissiveMapMatrix * texturePos).xy;");
1263
- } else {
1264
- src.push("textureCoord = texturePos.xy;");
1265
- }
1266
- src.push("vec4 emissiveTexel = texture(emissiveMap, textureCoord);");
1267
- src.push("emissiveTexel = " + TEXTURE_DECODE_FUNCS[material._emissiveMap._state.encoding] + "(emissiveTexel);");
1268
- src.push("emissiveColor = emissiveTexel.rgb;");
1269
- }
1270
-
1271
- if (uvs && material._alphaMap) {
1272
- if (material._alphaMap._state.matrix) {
1273
- src.push("textureCoord = (alphaMapMatrix * texturePos).xy;");
1274
- } else {
1275
- src.push("textureCoord = texturePos.xy;");
1276
- }
1277
- src.push("alpha *= texture(alphaMap, textureCoord).r;");
1278
- }
1279
-
1280
- if (uvs && material._occlusionMap) {
1281
- if (material._occlusionMap._state.matrix) {
1282
- src.push("textureCoord = (occlusionMapMatrix * texturePos).xy;");
1283
- } else {
1284
- src.push("textureCoord = texturePos.xy;");
1285
- }
1286
- src.push("occlusion *= texture(occlusionMap, textureCoord).r;");
1287
- }
1288
-
1289
- if (normals && ((lightsState.lights.length > 0) || lightsState.lightMaps.length > 0 || lightsState.reflectionMaps.length > 0)) {
1290
-
1291
- //--------------------------------------------------------------------------------
1292
- // SHADING
1293
- //--------------------------------------------------------------------------------
1294
-
1295
- if (uvs && material._normalMap) {
1296
- if (material._normalMap._state.matrix) {
1297
- src.push("textureCoord = (normalMapMatrix * texturePos).xy;");
1298
- } else {
1299
- src.push("textureCoord = texturePos.xy;");
1300
- }
1301
- src.push("vec3 viewNormal = perturbNormal2Arb( vViewPosition, normalize(vViewNormal), textureCoord );");
1302
- } else {
1303
- src.push("vec3 viewNormal = normalize(vViewNormal);");
1304
- }
1305
-
1306
- if (uvs && material._specularMap) {
1307
- if (material._specularMap._state.matrix) {
1308
- src.push("textureCoord = (specularMapMatrix * texturePos).xy;");
1309
- } else {
1310
- src.push("textureCoord = texturePos.xy;");
1311
- }
1312
- src.push("specular *= texture(specularMap, textureCoord).rgb;");
1313
- }
1314
-
1315
- if (uvs && material._glossinessMap) {
1316
- if (material._glossinessMap._state.matrix) {
1317
- src.push("textureCoord = (glossinessMapMatrix * texturePos).xy;");
1318
- } else {
1319
- src.push("textureCoord = texturePos.xy;");
1320
- }
1321
- src.push("glossiness *= texture(glossinessMap, textureCoord).r;");
1322
- }
1323
-
1324
- if (uvs && material._specularGlossinessMap) {
1325
- if (material._specularGlossinessMap._state.matrix) {
1326
- src.push("textureCoord = (materialSpecularGlossinessMapMatrix * texturePos).xy;");
1327
- } else {
1328
- src.push("textureCoord = texturePos.xy;");
1329
- }
1330
- src.push("vec4 specGlossRGB = texture(materialSpecularGlossinessMap, textureCoord).rgba;"); // TODO: what if only RGB texture?
1331
- src.push("specular *= specGlossRGB.rgb;");
1332
- src.push("glossiness *= specGlossRGB.a;");
1333
- }
1334
-
1335
- if (uvs && material._metallicMap) {
1336
- if (material._metallicMap._state.matrix) {
1337
- src.push("textureCoord = (metallicMapMatrix * texturePos).xy;");
1338
- } else {
1339
- src.push("textureCoord = texturePos.xy;");
1340
- }
1341
- src.push("metallic *= texture(metallicMap, textureCoord).r;");
1342
- }
1343
-
1344
- if (uvs && material._roughnessMap) {
1345
- if (material._roughnessMap._state.matrix) {
1346
- src.push("textureCoord = (roughnessMapMatrix * texturePos).xy;");
1347
- } else {
1348
- src.push("textureCoord = texturePos.xy;");
1349
- }
1350
- src.push("roughness *= texture(roughnessMap, textureCoord).r;");
1351
- }
1352
-
1353
- if (uvs && material._metallicRoughnessMap) {
1354
- if (material._metallicRoughnessMap._state.matrix) {
1355
- src.push("textureCoord = (metallicRoughnessMapMatrix * texturePos).xy;");
1356
- } else {
1357
- src.push("textureCoord = texturePos.xy;");
1358
- }
1359
- src.push("vec3 metalRoughRGB = texture(metallicRoughnessMap, textureCoord).rgb;");
1360
- src.push("metallic *= metalRoughRGB.b;");
1361
- src.push("roughness *= metalRoughRGB.g;");
1362
- }
1363
-
1364
- src.push("vec3 viewEyeDir = normalize(-vViewPosition);");
1365
-
1366
- if (material._diffuseFresnel) {
1367
- src.push("float diffuseFresnel = fresnel(viewEyeDir, viewNormal, diffuseFresnelEdgeBias, diffuseFresnelCenterBias, diffuseFresnelPower);");
1368
- src.push("diffuseColor *= mix(diffuseFresnelEdgeColor, diffuseFresnelCenterColor, diffuseFresnel);");
1369
- }
1370
- if (material._specularFresnel) {
1371
- src.push("float specularFresnel = fresnel(viewEyeDir, viewNormal, specularFresnelEdgeBias, specularFresnelCenterBias, specularFresnelPower);");
1372
- src.push("specular *= mix(specularFresnelEdgeColor, specularFresnelCenterColor, specularFresnel);");
1373
- }
1374
- if (material._alphaFresnel) {
1375
- src.push("float alphaFresnel = fresnel(viewEyeDir, viewNormal, alphaFresnelEdgeBias, alphaFresnelCenterBias, alphaFresnelPower);");
1376
- src.push("alpha *= mix(alphaFresnelEdgeColor.r, alphaFresnelCenterColor.r, alphaFresnel);");
1377
- }
1378
- if (material._emissiveFresnel) {
1379
- src.push("float emissiveFresnel = fresnel(viewEyeDir, viewNormal, emissiveFresnelEdgeBias, emissiveFresnelCenterBias, emissiveFresnelPower);");
1380
- src.push("emissiveColor *= mix(emissiveFresnelEdgeColor, emissiveFresnelCenterColor, emissiveFresnel);");
1381
- }
1382
-
1383
- src.push("if (materialAlphaModeCutoff[1] == 1.0 && alpha < materialAlphaModeCutoff[2]) {"); // ie. (alphaMode == "mask" && alpha < alphaCutoff)
1384
- src.push(" discard;"); // TODO: Discard earlier within this shader?
1385
- src.push("}");
1386
-
1387
- // PREPARE INPUTS FOR SHADER FUNCTIONS
1388
-
1389
- src.push("IncidentLight light;");
1390
- src.push("Material material;");
1391
- src.push("Geometry geometry;");
1392
- src.push("ReflectedLight reflectedLight = ReflectedLight(vec3(0.0,0.0,0.0), vec3(0.0,0.0,0.0));");
1393
- src.push("vec3 viewLightDir;");
1394
-
1395
- if (phongMaterial) {
1396
- src.push("material.diffuseColor = diffuseColor;");
1397
- src.push("material.specularColor = specular;");
1398
- src.push("material.shine = materialShininess;");
1399
- }
1400
-
1401
- if (specularMaterial) {
1402
- src.push("float oneMinusSpecularStrength = 1.0 - max(max(specular.r, specular.g ),specular.b);"); // Energy conservation
1403
- src.push("material.diffuseColor = diffuseColor * oneMinusSpecularStrength;");
1404
- src.push("material.specularRoughness = clamp( 1.0 - glossiness, 0.04, 1.0 );");
1405
- src.push("material.specularColor = specular;");
1406
- }
1407
-
1408
- if (metallicMaterial) {
1409
- src.push("float dielectricSpecular = 0.16 * specularF0 * specularF0;");
1410
- src.push("material.diffuseColor = diffuseColor * (1.0 - dielectricSpecular) * (1.0 - metallic);");
1411
- src.push("material.specularRoughness = clamp(roughness, 0.04, 1.0);");
1412
- src.push("material.specularColor = mix(vec3(dielectricSpecular), diffuseColor, metallic);");
1413
- }
1414
-
1415
- src.push("geometry.position = vViewPosition;");
1416
- if (lightsState.lightMaps.length > 0) {
1417
- src.push("geometry.worldNormal = normalize(vWorldNormal);");
1418
- }
1419
- src.push("geometry.viewNormal = viewNormal;");
1420
- src.push("geometry.viewEyeDir = viewEyeDir;");
1421
-
1422
- // ENVIRONMENT AND REFLECTION MAP SHADING
1423
-
1424
- if ((phongMaterial) && (lightsState.lightMaps.length > 0 || lightsState.reflectionMaps.length > 0)) {
1425
- src.push("computePhongLightMapping(geometry, material, reflectedLight);");
1426
- }
1427
-
1428
- if ((specularMaterial || metallicMaterial) && (lightsState.lightMaps.length > 0 || lightsState.reflectionMaps.length > 0)) {
1429
- src.push("computePBRLightMapping(geometry, material, reflectedLight);");
1430
- }
193
+ });
194
+
195
+ return {
196
+ getHash: () => [
197
+ meshDrawHash,
198
+ scene.gammaOutput ? "go" : "",
199
+ lightSetup.getHash(),
200
+ material._state.hash
201
+ ],
202
+ programName: "Draw",
203
+ canActAsBackground: true,
204
+ discardPoints: true,
205
+ setupPointSize: true,
206
+ setsLineWidth: true,
207
+ appendFragmentOutputs: (src, getGammaOutputExpression, gl_FragCoord) => {
208
+ const hasNonAmbientLighting = attributes.normal && ((lightSetup.directionalLights.length > 0) || lightSetup.getIrradiance || lightSetup.getReflection);
209
+ src.push(`vec3 diffuseColor = ${(hasNonAmbientLighting && (phongMaterial || specularMaterial) && materialDiffuse) || (metallicMaterial && materialBaseColor) || "vec3(1.0)"};`);
210
+ src.push(`vec4 alphaModeCutoff = ${((phongMaterial || metallicMaterial || specularMaterial) && materialAlphaModeCutoff) || "vec4(1.0, 0.0, 0.0, 0.0)"};`);
211
+ src.push("float alpha = alphaModeCutoff[0];");
212
+ (phongMaterial || metallicMaterial || specularMaterial) && alphaMap && src.push(`alpha *= ${alphaMap(texturePos)}.r;`);
213
+
214
+ if (vColor) {
215
+ src.push(`diffuseColor *= ${vColor}.rgb;`);
216
+ src.push(`alpha *= ${vColor}.a;`);
217
+ }
218
+ if (metallicMaterial && baseColorMap) {
219
+ src.push("vec4 baseColorTexel = " + baseColorMap(texturePos) + ";");
220
+ src.push("diffuseColor *= baseColorTexel.rgb;");
221
+ src.push("alpha *= baseColorTexel.a;");
222
+ }
223
+ if ((phongMaterial || specularMaterial) && diffuseMap) {
224
+ src.push("vec4 diffuseTexel = " + diffuseMap(texturePos) + ";");
225
+ src.push("diffuseColor *= diffuseTexel.rgb;");
226
+ src.push("alpha *= diffuseTexel.a;");
227
+ }
228
+
229
+ src.push(`vec3 emissiveColor = ${((phongMaterial || metallicMaterial || specularMaterial) && materialEmissive) || "vec3(0.0)"};`);
230
+ (phongMaterial || metallicMaterial || specularMaterial) && emissiveMap && src.push(`emissiveColor = ${emissiveMap(texturePos)}.rgb;`);
231
+
232
+ if (hasNonAmbientLighting) {
233
+
234
+ src.push(`vec3 specular = ${((phongMaterial || specularMaterial) && materialSpecular) || "vec3(1.0)"};`);
235
+ src.push(`float glossiness = ${(specularMaterial && materialGlossiness) || "1.0"};`);
236
+ src.push(`float metallic = ${(metallicMaterial && materialMetallic) || "1.0"};`);
237
+ src.push(`float roughness = ${((metallicMaterial || specularMaterial) && materialRoughness) || "1.0"};`);
238
+ src.push(`float shininess = ${(phongMaterial && materialShininess)|| "1.0"};`);
239
+ src.push(`float specularF0 = ${(metallicMaterial && materialSpecularF0) || "1.0"};`);
240
+ src.push(`float occlusion = ${((phongMaterial || metallicMaterial || specularMaterial) && occlusionMap) ? `${occlusionMap(texturePos)}.r` : "1.0"};`);
241
+
242
+ //--------------------------------------------------------------------------------
243
+ // SHADING
244
+ //--------------------------------------------------------------------------------
245
+
246
+ metallicMaterial && metallicMap && src.push(`metallic *= ${metallicMap(texturePos)}.r;`);
247
+ metallicMaterial && roughnessMap && src.push(`roughness *= ${roughnessMap(texturePos)}.r;`);
248
+
249
+ if (metallicMaterial && metallicRoughnessMap) {
250
+ src.push("vec4 metalRoughTexel = " + metallicRoughnessMap(texturePos) + ";");
251
+ src.push("metallic *= metalRoughTexel.b;");
252
+ src.push("roughness *= metalRoughTexel.g;");
253
+ }
1431
254
 
1432
- // LIGHT SOURCE SHADING
255
+ (phongMaterial || specularMaterial) && specularMap && src.push(`specular *= ${specularMap(texturePos)}.rgb;`);
256
+ specularMaterial && glossinessMap && src.push(`glossiness *= ${glossinessMap(texturePos)}.r;`);
1433
257
 
1434
- src.push("float shadow = 1.0;");
258
+ if (specularMaterial && specularGlossinessMap) {
259
+ src.push("vec4 specGlossTexel = " + specularGlossinessMap(texturePos) + ";"); // TODO: what if only RGB texture?
260
+ src.push("specular *= specGlossTexel.rgb;");
261
+ src.push("glossiness *= specGlossTexel.a;");
262
+ }
1435
263
 
1436
- // if (receivesShadow) {
1437
- //
1438
- // src.push("float lightDepth2 = clamp(length(lightPos)/40.0, 0.0, 1.0);");
1439
- // src.push("float illuminated = VSM(sLightDepth, lightUV, lightDepth2);");
1440
- //
1441
- src.push("float shadowAcneRemover = 0.007;");
1442
- src.push("vec3 fragmentDepth;");
1443
- src.push("float texelSize = 1.0 / 1024.0;");
1444
- src.push("float amountInLight = 0.0;");
1445
- src.push("vec3 shadowCoord;");
1446
- src.push('vec4 rgbaDepth;');
1447
- src.push("float depth;");
1448
- // }
264
+ const vViewNormalized = `normalize(${vViewNormal})`;
265
+ const viewNormal = (((phongMaterial || metallicMaterial || specularMaterial) && normalMap)
266
+ ? `${perturbNormal2Arb}(${vViewPosition}, ${vViewNormalized}, ${normalMap.getTexCoordExpression(texturePos)}, ${normalMap(texturePos)})`
267
+ : vViewNormalized);
268
+ src.push(`vec3 viewNormal = ${viewNormal};`);
1449
269
 
1450
- const numShadows = 0;
1451
- for (let i = 0, len = lightsState.lights.length; i < len; i++) {
270
+ src.push(`vec3 viewEyeDir = normalize(-${vViewPosition});`);
1452
271
 
1453
- const light = lightsState.lights[i];
272
+ diffuseFresnel && src.push(`diffuseColor *= ${diffuseFresnel ("viewEyeDir", "viewNormal")};`);
273
+ specularFresnel && src.push(`specular *= ${specularFresnel("viewEyeDir", "viewNormal")};`);
274
+ emissiveFresnel && src.push(`emissiveColor *= ${emissiveFresnel("viewEyeDir", "viewNormal")};`);
275
+ alphaFresnel && src.push(`alpha *= ${alphaFresnel ("viewEyeDir", "viewNormal")};`);
1454
276
 
1455
- if (light.type === "ambient") {
1456
- continue;
1457
- }
1458
- if (light.type === "dir" && light.space === "view") {
1459
- src.push("viewLightDir = -normalize(lightDir" + i + ");");
1460
- } else if (light.type === "point" && light.space === "view") {
1461
- src.push("viewLightDir = normalize(lightPos" + i + " - vViewPosition);");
1462
- //src.push("tmpVec3 = lightPos" + i + ".xyz - viewPosition.xyz;");
1463
- //src.push("lightDist = abs(length(tmpVec3));");
1464
- } else {
1465
- src.push("viewLightDir = normalize(vViewLightReverseDirAndDist" + i + ".xyz);"); // If normal mapping, the fragment->light vector will be in tangent space
1466
- }
277
+ src.push("if (alphaModeCutoff[1] == 1.0 && alpha < alphaModeCutoff[2]) {"); // ie. (alphaMode == "mask" && alpha < alphaCutoff)
278
+ src.push(" discard;"); // TODO: Discard earlier within this shader?
279
+ src.push("}");
1467
280
 
1468
- if (receivesShadow && light.castsShadow) {
281
+ src.push("vec3 material_diffuseColor;");
1469
282
 
1470
- // if (true) {
1471
- // src.push('shadowCoord = (vShadowPosFromLight' + i + '.xyz/vShadowPosFromLight' + i + '.w)/2.0 + 0.5;');
1472
- // src.push("lightDepth2 = clamp(length(vec3[0.0, 20.0, 20.0])/40.0, 0.0, 1.0);");
1473
- // src.push("castsShadow *= VSM(shadowMap' + i + ', shadowCoord, lightDepth2);");
1474
- // }
1475
- //
1476
- // if (false) {
1477
- //
1478
- // PCF
283
+ if (phongMaterial) {
284
+ src.push("material_diffuseColor = diffuseColor;");
285
+ src.push("roughness = 0.0;");
286
+ }
1479
287
 
1480
- src.push("shadow = 0.0;");
288
+ if (specularMaterial) {
289
+ src.push("float oneMinusSpecularStrength = 1.0 - max(max(specular.r, specular.g ),specular.b);"); // Energy conservation
290
+ src.push("material_diffuseColor = diffuseColor * oneMinusSpecularStrength;");
291
+ src.push("roughness = clamp(1.0 - glossiness, 0.04, 1.0);");
292
+ }
1481
293
 
1482
- src.push("fragmentDepth = vShadowPosFromLight" + i + ".xyz;");
1483
- src.push("fragmentDepth.z -= shadowAcneRemover;");
1484
- src.push("for (int x = -3; x <= 3; x++) {");
1485
- src.push(" for (int y = -3; y <= 3; y++) {");
1486
- src.push(" float texelDepth = unpackDepth(texture(shadowMap" + i + ", fragmentDepth.xy + vec2(x, y) * texelSize));");
1487
- src.push(" if (fragmentDepth.z < texelDepth) {");
1488
- src.push(" shadow += 1.0;");
1489
- src.push(" }");
1490
- src.push(" }");
1491
- src.push("}");
294
+ if (metallicMaterial) {
295
+ src.push("float dielectricSpecular = 0.16 * specularF0 * specularF0;");
296
+ src.push("material_diffuseColor = diffuseColor * (1.0 - dielectricSpecular) * (1.0 - metallic);");
297
+ src.push("roughness = clamp(roughness, 0.04, 1.0);");
298
+ src.push("specular = mix(vec3(dielectricSpecular), diffuseColor, metallic);");
299
+ }
1492
300
 
1493
- src.push("shadow = shadow / 9.0;");
301
+ // ENVIRONMENT AND REFLECTION MAP SHADING
302
+
303
+ if (phongMaterial || metallicMaterial || specularMaterial) {
304
+ src.push("const float PI = 3.14159265359;");
305
+ src.push("vec3 reflDiff = vec3(0.0);");
306
+ src.push("vec3 reflSpec = vec3(0.0);");
307
+
308
+ lightSetup.getIrradiance && src.push(`reflDiff += ${lightSetup.getIrradiance(`normalize(${vWorldNormal})`)};`);
309
+
310
+ if (lightSetup.getReflection) {
311
+ const reflectVec = `reflect(-viewEyeDir, viewNormal)`;
312
+ const spec = (phongMaterial
313
+ ? `0.2 * PI * ${lightSetup.getReflection(reflectVec)}`
314
+ : (function() {
315
+ const blinnExpFromRoughness = `2.0 / pow(roughness + 0.0001, 2.0) - 2.0`;
316
+ const specularBRDFContrib = `${BRDF_Specular_GGX_Environment}(viewNormal, viewEyeDir, specular, roughness)`;
317
+
318
+ const viewReflectVec = `normalize((vec4(${reflectVec}, 0.0) * ${geometry.viewMatrix}).xyz)`;
319
+ const maxMIPLevelScalar = "4.0";
320
+ const desiredMIPLevel = `${maxMIPLevelScalar} - 0.39624 - 0.25 * log2(pow(${blinnExpFromRoughness}, 2.0) + 1.0)`;
321
+ const mipLevel = `clamp(${desiredMIPLevel}, 0.0, ${maxMIPLevelScalar})`; //TODO: a random factor - fix this
322
+ return `${specularBRDFContrib} * ${lightSetup.getReflection(viewReflectVec, mipLevel)}`;
323
+ })());
324
+ src.push(`reflSpec += ${spec};`);
325
+ }
326
+ if (lightSetup.directionalLights.some(ligth => ligth.shadowParameters)) {
327
+ src.push("const vec4 bitShift = vec4(1.0, 1.0/256.0, 1.0/(256.0 * 256.0), 1.0/(256.0*256.0*256.0));");
328
+ src.push("const float texelSize = 1.0 / 1024.0;");
329
+ src.push("float shadow;");
330
+ src.push("float fragmentDepth;");
331
+ }
332
+ lightSetup.directionalLights.forEach((light, i) => {
333
+ if (light.shadowParameters) {
334
+ const shadowAcneRemover = "0.007";
335
+ src.push(`fragmentDepth = ${vDirectionals[i].vShadowPosFromLight}.z - ${shadowAcneRemover};`);
336
+ src.push("shadow = 0.0;");
337
+ src.push("for (int x = -3; x <= 3; x++) {");
338
+ src.push(" for (int y = -3; y <= 3; y++) {");
339
+ src.push(` float texelDepth = dot(texture(${light.getShadowMap()}, ${vDirectionals[i].vShadowPosFromLight}.xy + vec2(x, y) * texelSize), bitShift);`);
340
+ src.push(` if (fragmentDepth < texelDepth) {`);
341
+ src.push(" shadow += 1.0;");
342
+ src.push(" }");
343
+ src.push(" }");
344
+ src.push("}");
345
+ src.push("shadow /= 9.0;");
346
+ }
347
+ src.push(`vec3 lightColor${i} = ${light.getColor()}${light.shadowParameters ? (" * " + "shadow") : ""};`);
348
+ src.push(`vec3 lightDirection${i} = ${light.isWorldSpace ? vDirectionals[i].vViewLightReverseDir : light.getDirection(null, vViewPosition)};`);
349
+ const dotNL = `${saturate}(dot(viewNormal, lightDirection${i}))`;
350
+ src.push(`vec3 irradiance${i} = ${dotNL} * lightColor${i};`);
351
+ src.push(`reflDiff += irradiance${i};`);
352
+ const spec = (phongMaterial
353
+ ? `lightColor${i} * specular * pow(max(dot(reflect(-lightDirection${i}, -viewNormal), viewEyeDir), 0.0), shininess)`
354
+ : `irradiance${i} * PI * ${BRDF_Specular_GGX}(lightDirection${i}, viewNormal, viewEyeDir, specular, roughness)`);
355
+ src.push(`reflSpec += ${spec};`);
356
+ });
357
+ }
1494
358
 
1495
- src.push("light.color = lightColor" + i + ".rgb * (lightColor" + i + ".a * shadow);"); // a is intensity
1496
- //
1497
- // }
1498
- //
1499
- // if (false){
1500
- //
1501
- // src.push("shadow = 1.0;");
1502
- //
1503
- // src.push('shadowCoord = (vShadowPosFromLight' + i + '.xyz/vShadowPosFromLight' + i + '.w)/2.0 + 0.5;');
1504
- //
1505
- // src.push('shadow -= (shadowCoord.z > unpackDepth(texture(shadowMap' + i + ', shadowCoord.xy + vec2( -0.94201624, -0.39906216 ) / 700.0)) + 0.0015) ? 0.2 : 0.0;');
1506
- // src.push('shadow -= (shadowCoord.z > unpackDepth(texture(shadowMap' + i + ', shadowCoord.xy + vec2( 0.94558609, -0.76890725 ) / 700.0)) + 0.0015) ? 0.2 : 0.0;');
1507
- // src.push('shadow -= (shadowCoord.z > unpackDepth(texture(shadowMap' + i + ', shadowCoord.xy + vec2( -0.094184101, -0.92938870 ) / 700.0)) + 0.0015) ? 0.2 : 0.0;');
1508
- // src.push('shadow -= (shadowCoord.z > unpackDepth(texture(shadowMap' + i + ', shadowCoord.xy + vec2( 0.34495938, 0.29387760 ) / 700.0)) + 0.0015) ? 0.2 : 0.0;');
1509
- //
1510
- // src.push("light.color = lightColor" + i + ".rgb * (lightColor" + i + ".a * shadow);");
1511
- // }
359
+ const ambient = phongMaterial && `${lightSetup.getAmbientColor()} * diffuseColor`;
360
+ src.push("vec3 outgoingLight = emissiveColor + occlusion * (reflDiff * material_diffuseColor + reflSpec)" + (ambient ? (" + " + ambient) : "") + ";");
1512
361
  } else {
1513
- src.push("light.color = lightColor" + i + ".rgb * (lightColor" + i + ".a );"); // a is intensity
362
+ src.push(`vec3 ambientColor = ${(phongMaterial && materialAmbient) || "vec3(1.0)"};`);
363
+ phongMaterial && ambientMap && src.push(`ambientColor *= ${ambientMap(texturePos)}.rgb;`);
364
+ src.push(`ambientColor *= ${lightSetup.getAmbientColor()};`);
365
+ src.push("vec3 outgoingLight = emissiveColor + ambientColor;");
1514
366
  }
1515
367
 
1516
- src.push("light.direction = viewLightDir;");
1517
-
1518
- if (phongMaterial) {
1519
- src.push("computePhongLighting(light, geometry, material, reflectedLight);");
1520
- }
368
+ src.push(`vec4 fragColor = ${colorize} * vec4(outgoingLight, alpha);`);
1521
369
 
1522
- if (specularMaterial || metallicMaterial) {
1523
- src.push("computePBRLighting(light, geometry, material, reflectedLight);");
1524
- }
1525
- }
1526
-
1527
- if (numShadows > 0) {
1528
- //src.push("shadow /= " + (9 * numShadows) + ".0;");
370
+ src.push(`${outColor} = ${getGammaOutputExpression ? getGammaOutputExpression("fragColor") : "fragColor"};`);
1529
371
  }
1530
-
1531
- //src.push("reflectedLight.diffuse *= shadow;");
1532
-
1533
- // COMBINE TERMS
1534
-
1535
- if (phongMaterial) {
1536
- src.push("vec3 outgoingLight = (lightAmbient.rgb * lightAmbient.a * diffuseColor) + ((occlusion * (( reflectedLight.diffuse + reflectedLight.specular)))) + emissiveColor;");
1537
-
1538
- } else {
1539
- src.push("vec3 outgoingLight = (occlusion * (reflectedLight.diffuse)) + (occlusion * reflectedLight.specular) + emissiveColor;");
1540
- }
1541
-
1542
- } else {
1543
-
1544
- //--------------------------------------------------------------------------------
1545
- // NO SHADING - EMISSIVE and AMBIENT ONLY
1546
- //--------------------------------------------------------------------------------
1547
-
1548
- src.push("ambientColor *= (lightAmbient.rgb * lightAmbient.a);");
1549
-
1550
- src.push("vec3 outgoingLight = emissiveColor + ambientColor;");
1551
- }
1552
-
1553
- src.push("vec4 fragColor = vec4(outgoingLight, alpha) * colorize;");
1554
-
1555
- if (gammaOutput) {
1556
- src.push("fragColor = linearToGamma(fragColor, gammaFactor);");
1557
- }
1558
-
1559
- src.push("outColor = fragColor;");
1560
-
1561
- if (scene.logarithmicDepthBufferEnabled) {
1562
- src.push("gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;");
1563
- }
1564
-
1565
- src.push("}");
1566
-
1567
- return src;
1568
- }
1569
-
1570
- export {DrawShaderSource};
372
+ };
373
+ };