@xeokit/xeokit-sdk 2.6.92 → 2.6.93

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