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

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 (184) hide show
  1. package/dist/xeokit-sdk.cjs.js +44618 -13007
  2. package/dist/xeokit-sdk.es.js +44617 -13008
  3. package/dist/xeokit-sdk.es5.js +3488 -1387
  4. package/dist/xeokit-sdk.min.cjs.js +8 -8
  5. package/dist/xeokit-sdk.min.es.js +8 -8
  6. package/dist/xeokit-sdk.min.es5.js +7 -7
  7. package/package.json +1 -1
  8. package/src/plugins/lib/html/MenuEvent.js +2 -2
  9. package/src/viewer/Viewer.js +103 -55
  10. package/src/viewer/scene/canvas/Canvas.js +43 -3
  11. package/src/viewer/scene/geometry/ReadableGeometry.js +25 -0
  12. package/src/viewer/scene/lights/CubeTexture.js +13 -0
  13. package/src/viewer/scene/lights/DirLight.js +1 -2
  14. package/src/viewer/scene/lights/PointLight.js +1 -2
  15. package/src/viewer/scene/materials/Texture.js +9 -0
  16. package/src/viewer/scene/math/rtcCoords.js +38 -1
  17. package/src/viewer/scene/mesh/Mesh.js +212 -454
  18. package/src/viewer/scene/mesh/draw/DrawRenderer.js +998 -0
  19. package/src/viewer/scene/mesh/draw/DrawShaderSource.js +1504 -305
  20. package/src/viewer/scene/mesh/emphasis/EmphasisEdgesRenderer.js +259 -0
  21. package/src/viewer/scene/mesh/emphasis/EmphasisEdgesShaderSource.js +160 -0
  22. package/src/viewer/scene/mesh/emphasis/EmphasisFillRenderer.js +286 -0
  23. package/src/viewer/scene/mesh/emphasis/EmphasisFillShaderSource.js +263 -0
  24. package/src/viewer/scene/mesh/occlusion/OcclusionRenderer.js +221 -0
  25. package/src/viewer/scene/mesh/occlusion/OcclusionShaderSource.js +158 -9
  26. package/src/viewer/scene/mesh/pick/PickMeshRenderer.js +223 -0
  27. package/src/viewer/scene/mesh/pick/PickMeshShaderSource.js +151 -21
  28. package/src/viewer/scene/mesh/pick/PickTriangleRenderer.js +200 -0
  29. package/src/viewer/scene/mesh/pick/PickTriangleShaderSource.js +124 -12
  30. package/src/viewer/scene/mesh/shadow/ShadowRenderer.js +214 -0
  31. package/src/viewer/scene/mesh/shadow/ShadowShaderSource.js +97 -22
  32. package/src/viewer/scene/model/RENDER_PASSES.js +34 -0
  33. package/src/viewer/scene/model/SceneModel.js +861 -245
  34. package/src/viewer/scene/model/SceneModelEntity.js +4 -4
  35. package/src/viewer/scene/model/SceneModelMesh.js +3 -3
  36. package/src/viewer/scene/model/dtx/BindableDataTexture.js +31 -0
  37. package/src/viewer/scene/model/dtx/lines/DTXLinesBuffer.js +27 -0
  38. package/src/viewer/scene/model/dtx/lines/DTXLinesLayer.js +897 -0
  39. package/src/viewer/scene/model/dtx/lines/DTXLinesState.js +46 -0
  40. package/src/viewer/scene/model/dtx/lines/DTXLinesTextureFactory.js +411 -0
  41. package/src/viewer/scene/model/dtx/lines/dataTextureRamStats.js +49 -0
  42. package/src/viewer/scene/model/dtx/lines/rebucketPositions.js +208 -0
  43. package/src/viewer/scene/model/dtx/lines/renderers/DTXLinesColorRenderer.js +389 -0
  44. package/src/viewer/scene/model/dtx/lines/renderers/DTXLinesRenderers.js +65 -0
  45. package/src/viewer/scene/model/dtx/triangles/DTXTrianglesLayer.js +1399 -0
  46. package/src/viewer/scene/model/dtx/triangles/lib/DTXTrianglesBuffer.js +46 -0
  47. package/src/viewer/scene/model/dtx/triangles/lib/DTXTrianglesState.js +250 -0
  48. package/src/viewer/scene/model/dtx/triangles/lib/DTXTrianglesTextureFactory.js +518 -0
  49. package/src/viewer/scene/model/dtx/triangles/lib/dataTextureRamStats.js +54 -0
  50. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesColorRenderer.js +639 -0
  51. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesDepthRenderer.js +464 -0
  52. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesEdgesColorRenderer.js +423 -0
  53. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesEdgesRenderer.js +431 -0
  54. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesNormalsRenderer.js +345 -0
  55. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesOcclusionRenderer.js +436 -0
  56. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesPickDepthRenderer.js +466 -0
  57. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesPickMeshRenderer.js +437 -0
  58. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesPickNormalsFlatRenderer.js +460 -0
  59. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesPickNormalsRenderer.js +437 -0
  60. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesRenderers.js +307 -0
  61. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesSilhouetteRenderer.js +472 -0
  62. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesSnapInitRenderer.js +474 -0
  63. package/src/viewer/scene/model/dtx/triangles/renderers/DTXTrianglesSnapRenderer.js +435 -0
  64. package/src/viewer/scene/model/vbo/ScratchMemory.js +63 -0
  65. package/src/viewer/scene/model/vbo/VBORenderer.js +664 -0
  66. package/src/viewer/scene/model/vbo/batching/lines/VBOBatchingLinesLayer.js +704 -0
  67. package/src/viewer/scene/model/vbo/batching/lines/lib/VBOBatchingLinesBuffer.js +19 -0
  68. package/src/viewer/scene/model/vbo/batching/lines/renderers/VBOBatchingLineSnapInitRenderer.js +318 -0
  69. package/src/viewer/scene/model/vbo/batching/lines/renderers/VBOBatchingLinesColorRenderer.js +114 -0
  70. package/src/viewer/scene/model/vbo/batching/lines/renderers/VBOBatchingLinesRenderers.js +99 -0
  71. package/src/viewer/scene/model/vbo/batching/lines/renderers/VBOBatchingLinesSilhouetteRenderer.js +124 -0
  72. package/src/viewer/scene/model/vbo/batching/lines/renderers/VBOBatchingLinesSnapInitRenderer.js +313 -0
  73. package/src/viewer/scene/model/vbo/batching/lines/renderers/VBOBatchingLinesSnapRenderer.js +311 -0
  74. package/src/viewer/scene/model/vbo/batching/lines/renderers/VBOSceneModelLineBatchingRenderer.js +24 -0
  75. package/src/viewer/scene/model/vbo/batching/points/VBOBatchingPointsLayer.js +688 -0
  76. package/src/viewer/scene/model/vbo/batching/points/VBOBatchingPointsRenderer.js +24 -0
  77. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsColorRenderer.js +168 -0
  78. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsOcclusionRenderer.js +136 -0
  79. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsPickDepthRenderer.js +156 -0
  80. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsPickMeshRenderer.js +148 -0
  81. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsRenderers.js +144 -0
  82. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsShadowRenderer.js +107 -0
  83. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsSilhouetteRenderer.js +146 -0
  84. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsSnapInitRenderer.js +315 -0
  85. package/src/viewer/scene/model/vbo/batching/points/renderers/VBOBatchingPointsSnapRenderer.js +305 -0
  86. package/src/viewer/scene/model/vbo/batching/triangles/VBOBatchingTrianglesBuffer.js +24 -0
  87. package/src/viewer/scene/model/vbo/batching/triangles/VBOBatchingTrianglesLayer.js +1346 -0
  88. package/src/viewer/scene/model/vbo/batching/triangles/renderers/EdgesColorRenderer.js +131 -0
  89. package/src/viewer/scene/model/vbo/batching/triangles/renderers/EdgesEmphasisRenderer.js +135 -0
  90. package/src/viewer/scene/model/vbo/batching/triangles/renderers/EdgesRenderer.js +12 -0
  91. package/src/viewer/scene/model/vbo/batching/triangles/renderers/Renderers.js +403 -0
  92. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesBatchingRenderer.js +35 -0
  93. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesColorRenderer.js +252 -0
  94. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesColorTextureRenderer.js +294 -0
  95. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesDepthRenderer.js +115 -0
  96. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesFlatColorRenderer.js +246 -0
  97. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesNormalsRenderer.js +139 -0
  98. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesOcclusionRenderer.js +125 -0
  99. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesPBRRenderer.js +561 -0
  100. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesPickDepthRenderer.js +144 -0
  101. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesPickMeshRenderer.js +130 -0
  102. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesPickNormalsFlatRenderer.js +121 -0
  103. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesPickNormalsRenderer.js +131 -0
  104. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesShadowRenderer.js +107 -0
  105. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesSilhouetteRenderer.js +144 -0
  106. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesSnapInitRenderer.js +313 -0
  107. package/src/viewer/scene/model/vbo/batching/triangles/renderers/TrianglesSnapRenderer.js +312 -0
  108. package/src/viewer/scene/model/vbo/float16.js +760 -0
  109. package/src/viewer/scene/model/vbo/instancing/lines/VBOInstancingLinesLayer.js +711 -0
  110. package/src/viewer/scene/model/vbo/instancing/lines/renderers/VBOInstancingLinesColorRenderer.js +148 -0
  111. package/src/viewer/scene/model/vbo/instancing/lines/renderers/VBOInstancingLinesRenderer.js +26 -0
  112. package/src/viewer/scene/model/vbo/instancing/lines/renderers/VBOInstancingLinesRenderers.js +139 -0
  113. package/src/viewer/scene/model/vbo/instancing/lines/renderers/VBOInstancingLinesSilhouetteRenderer.js +127 -0
  114. package/src/viewer/scene/model/vbo/instancing/lines/renderers/VBOInstancingLinesSnapInitRenderer.js +308 -0
  115. package/src/viewer/scene/model/vbo/instancing/lines/renderers/VBOInstancingLinesSnapRenderer.js +299 -0
  116. package/src/viewer/scene/model/vbo/instancing/points/VBOInstancingPointsLayer.js +733 -0
  117. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsColorRenderer.js +174 -0
  118. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsDepthRenderer.js +156 -0
  119. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsOcclusionRenderer.js +149 -0
  120. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsPickDepthRenderer.js +170 -0
  121. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsPickMeshRenderer.js +152 -0
  122. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsRenderer.js +26 -0
  123. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsRenderers.js +174 -0
  124. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsShadowRenderer.js +119 -0
  125. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsSilhouetteRenderer.js +154 -0
  126. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsSnapInitRenderer.js +307 -0
  127. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingPointsSnapRenderer.js +293 -0
  128. package/src/viewer/scene/model/vbo/instancing/points/renderers/VBOInstancingTrianglesRenderer.js +150 -0
  129. package/src/viewer/scene/model/vbo/instancing/triangles/VBOInstancingTrianglesLayer.js +1289 -0
  130. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/EdgesColorRenderer.js +135 -0
  131. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/EdgesEmphasisRenderer.js +137 -0
  132. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/EdgesRenderer.js +12 -0
  133. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/Renderers.js +403 -0
  134. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesColorRenderer.js +267 -0
  135. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesColorTextureRenderer.js +299 -0
  136. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesDepthRenderer.js +126 -0
  137. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesFlatColorRenderer.js +255 -0
  138. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesFlatNormalsRenderer.js +130 -0
  139. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesInstancingRenderer.js +32 -0
  140. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesNormalsRenderer.js +138 -0
  141. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesOcclusionRenderer.js +128 -0
  142. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesPBRRenderer.js +561 -0
  143. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesPickDepthRenderer.js +149 -0
  144. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesPickMeshRenderer.js +136 -0
  145. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesPickNormalsFlatRenderer.js +130 -0
  146. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesPickNormalsRenderer.js +145 -0
  147. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesShadowRenderer.js +113 -0
  148. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesSilhouetteRenderer.js +148 -0
  149. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesSnapInitRenderer.js +319 -0
  150. package/src/viewer/scene/model/vbo/instancing/triangles/renderers/TrianglesSnapRenderer.js +310 -0
  151. package/src/viewer/scene/scene/Scene.js +38 -21
  152. package/src/viewer/scene/webgl/ArrayBuf.js +20 -5
  153. package/src/viewer/scene/webgl/Attribute.js +23 -0
  154. package/src/viewer/scene/webgl/FrameContext.js +86 -14
  155. package/src/viewer/scene/webgl/Program.js +181 -0
  156. package/src/viewer/scene/webgl/RenderBuffer.js +262 -62
  157. package/src/viewer/scene/webgl/RenderBufferManager.js +34 -0
  158. package/src/viewer/scene/webgl/Renderer.js +843 -880
  159. package/src/viewer/scene/webgl/Sampler.js +18 -0
  160. package/src/viewer/scene/webgl/Shader.js +49 -0
  161. package/src/viewer/scene/webgl/occlusion/OcclusionLayer.js +132 -60
  162. package/src/viewer/scene/webgl/occlusion/OcclusionTester.js +328 -167
  163. package/src/viewer/scene/webgl/sao/SAODepthLimitedBlurRenderer.js +324 -0
  164. package/src/viewer/scene/webgl/sao/SAOOcclusionRenderer.js +384 -0
  165. package/src/viewer/utils/os.js +9 -0
  166. package/src/viewer/scene/mesh/draw/LambertShaderSource.js +0 -35
  167. package/src/viewer/scene/mesh/emphasis/EmphasisShaderSource.js +0 -34
  168. package/src/viewer/scene/model/layer/DTXLayer.js +0 -940
  169. package/src/viewer/scene/model/layer/Layer.js +0 -786
  170. package/src/viewer/scene/model/layer/VBOLayer.js +0 -1019
  171. package/src/viewer/scene/model/layer/programs/ColorProgram.js +0 -35
  172. package/src/viewer/scene/model/layer/programs/ColorTextureProgram.js +0 -46
  173. package/src/viewer/scene/model/layer/programs/DepthProgram.js +0 -11
  174. package/src/viewer/scene/model/layer/programs/EdgesProgram.js +0 -12
  175. package/src/viewer/scene/model/layer/programs/FlatColorProgram.js +0 -24
  176. package/src/viewer/scene/model/layer/programs/OcclusionProgram.js +0 -12
  177. package/src/viewer/scene/model/layer/programs/PBRProgram.js +0 -180
  178. package/src/viewer/scene/model/layer/programs/PickDepthProgram.js +0 -30
  179. package/src/viewer/scene/model/layer/programs/PickMeshProgram.js +0 -12
  180. package/src/viewer/scene/model/layer/programs/PickNormalsProgram.js +0 -18
  181. package/src/viewer/scene/model/layer/programs/ShadowProgram.js +0 -23
  182. package/src/viewer/scene/model/layer/programs/SilhouetteProgram.js +0 -15
  183. package/src/viewer/scene/model/layer/programs/SnapProgram.js +0 -28
  184. package/src/viewer/scene/webgl/WebGLRenderer.js +0 -612
@@ -0,0 +1,998 @@
1
+ /**
2
+ * @author xeolabs / https://github.com/xeolabs
3
+ */
4
+
5
+ import {Map} from "../../utils/Map.js";
6
+ import {DrawShaderSource} from "./DrawShaderSource.js";
7
+ import {Program} from "../../webgl/Program.js";
8
+ import {stats} from '../../stats.js';
9
+ import {WEBGL_INFO} from '../../webglInfo.js';
10
+ import {math} from "../../math/math.js";
11
+ import {getPlaneRTCPos} from "../../math/rtcCoords.js";
12
+
13
+ const tempVec3a = math.vec3();
14
+
15
+ const ids = new Map({});
16
+
17
+ /**
18
+ * @private
19
+ */
20
+ const DrawRenderer = function (hash, mesh) {
21
+ this.id = ids.addItem({});
22
+ this._hash = hash;
23
+ this._scene = mesh.scene;
24
+ this._useCount = 0;
25
+ this._shaderSource = new DrawShaderSource(mesh);
26
+ this._allocate(mesh);
27
+ };
28
+
29
+ const drawRenderers = {};
30
+
31
+ DrawRenderer.get = function (mesh) {
32
+ const scene = mesh.scene;
33
+ const hash = [
34
+ scene.canvas.canvas.id,
35
+ (scene.gammaInput ? "gi;" : ";") + (scene.gammaOutput ? "go" : ""),
36
+ scene._lightsState.getHash(),
37
+ scene._sectionPlanesState.getHash(),
38
+ mesh._geometry._state.hash,
39
+ mesh._material._state.hash,
40
+ mesh._state.drawHash
41
+ ].join(";");
42
+ let renderer = drawRenderers[hash];
43
+ if (!renderer) {
44
+ renderer = new DrawRenderer(hash, mesh);
45
+ if (renderer.errors) {
46
+ console.log(renderer.errors.join("\n"));
47
+ return null;
48
+ }
49
+ drawRenderers[hash] = renderer;
50
+ stats.memory.programs++;
51
+ }
52
+ renderer._useCount++;
53
+ return renderer;
54
+ };
55
+
56
+ DrawRenderer.prototype.put = function () {
57
+ if (--this._useCount === 0) {
58
+ ids.removeItem(this.id);
59
+ if (this._program) {
60
+ this._program.destroy();
61
+ }
62
+ delete drawRenderers[this._hash];
63
+ stats.memory.programs--;
64
+ }
65
+ };
66
+
67
+ DrawRenderer.prototype.webglContextRestored = function () {
68
+ this._program = null;
69
+ };
70
+
71
+ DrawRenderer.prototype.drawMesh = function (frameCtx, mesh) {
72
+
73
+ if (!this._program) {
74
+ this._allocate(mesh);
75
+ }
76
+
77
+ const maxTextureUnits = WEBGL_INFO.MAX_TEXTURE_UNITS;
78
+ const scene = mesh.scene;
79
+ const material = mesh._material;
80
+ const gl = scene.canvas.gl;
81
+ const program = this._program;
82
+ const meshState = mesh._state;
83
+ const materialState = mesh._material._state;
84
+ const geometryState = mesh._geometry._state;
85
+ const camera = scene.camera;
86
+ const origin = mesh.origin;
87
+ const background = meshState.background;
88
+
89
+ if (frameCtx.lastProgramId !== this._program.id) {
90
+ frameCtx.lastProgramId = this._program.id;
91
+ if (background) {
92
+ gl.depthFunc(gl.LEQUAL);
93
+ }
94
+ this._bindProgram(frameCtx);
95
+ }
96
+
97
+ gl.uniformMatrix4fv(this._uViewMatrix, false, origin ? frameCtx.getRTCViewMatrix(meshState.originHash, origin) : camera.viewMatrix);
98
+ gl.uniformMatrix4fv(this._uViewNormalMatrix, false, camera.viewNormalMatrix);
99
+
100
+ if (meshState.clippable) {
101
+ const numAllocatedSectionPlanes = scene._sectionPlanesState.getNumAllocatedSectionPlanes();
102
+ const numSectionPlanes = scene._sectionPlanesState.sectionPlanes.length;
103
+ if (numAllocatedSectionPlanes > 0) {
104
+ const sectionPlanes = scene._sectionPlanesState.sectionPlanes;
105
+ const renderFlags = mesh.renderFlags;
106
+ for (let sectionPlaneIndex = 0; sectionPlaneIndex < numAllocatedSectionPlanes; sectionPlaneIndex++) {
107
+ const sectionPlaneUniforms = this._uSectionPlanes[sectionPlaneIndex];
108
+ if (sectionPlaneUniforms) {
109
+ if (sectionPlaneIndex < numSectionPlanes) {
110
+ const active = renderFlags.sectionPlanesActivePerLayer[sectionPlaneIndex];
111
+ gl.uniform1i(sectionPlaneUniforms.active, active ? 1 : 0);
112
+ if (active) {
113
+ const sectionPlane = sectionPlanes[sectionPlaneIndex];
114
+ if (origin) {
115
+ const rtcSectionPlanePos = getPlaneRTCPos(sectionPlane.dist, sectionPlane.dir, origin, tempVec3a);
116
+ gl.uniform3fv(sectionPlaneUniforms.pos, rtcSectionPlanePos);
117
+ } else {
118
+ gl.uniform3fv(sectionPlaneUniforms.pos, sectionPlane.pos);
119
+ }
120
+ gl.uniform3fv(sectionPlaneUniforms.dir, sectionPlane.dir);
121
+ }
122
+ } else {
123
+ gl.uniform1i(sectionPlaneUniforms.active, 0);
124
+ }
125
+ }
126
+ }
127
+ }
128
+ }
129
+
130
+ if (materialState.id !== this._lastMaterialId) {
131
+
132
+ frameCtx.textureUnit = this._baseTextureUnit;
133
+
134
+ const backfaces = materialState.backfaces;
135
+ if (frameCtx.backfaces !== backfaces) {
136
+ if (backfaces) {
137
+ gl.disable(gl.CULL_FACE);
138
+ } else {
139
+ gl.enable(gl.CULL_FACE);
140
+ }
141
+ frameCtx.backfaces = backfaces;
142
+ }
143
+
144
+ const frontface = materialState.frontface;
145
+ if (frameCtx.frontface !== frontface) {
146
+ if (frontface) {
147
+ gl.frontFace(gl.CCW);
148
+ } else {
149
+ gl.frontFace(gl.CW);
150
+ }
151
+ frameCtx.frontface = frontface;
152
+ }
153
+
154
+ if (frameCtx.lineWidth !== materialState.lineWidth) {
155
+ gl.lineWidth(materialState.lineWidth);
156
+ frameCtx.lineWidth = materialState.lineWidth;
157
+ }
158
+
159
+ if (this._uPointSize) {
160
+ gl.uniform1f(this._uPointSize, materialState.pointSize);
161
+ }
162
+
163
+ switch (materialState.type) {
164
+ case "LambertMaterial":
165
+ if (this._uMaterialAmbient) {
166
+ gl.uniform3fv(this._uMaterialAmbient, materialState.ambient);
167
+ }
168
+ if (this._uMaterialColor) {
169
+ gl.uniform4f(this._uMaterialColor, materialState.color[0], materialState.color[1], materialState.color[2], materialState.alpha);
170
+ }
171
+ if (this._uMaterialEmissive) {
172
+ gl.uniform3fv(this._uMaterialEmissive, materialState.emissive);
173
+ }
174
+ break;
175
+
176
+ case "PhongMaterial":
177
+ if (this._uMaterialShininess) {
178
+ gl.uniform1f(this._uMaterialShininess, materialState.shininess);
179
+ }
180
+ if (this._uMaterialAmbient) {
181
+ gl.uniform3fv(this._uMaterialAmbient, materialState.ambient);
182
+ }
183
+ if (this._uMaterialDiffuse) {
184
+ gl.uniform3fv(this._uMaterialDiffuse, materialState.diffuse);
185
+ }
186
+ if (this._uMaterialSpecular) {
187
+ gl.uniform3fv(this._uMaterialSpecular, materialState.specular);
188
+ }
189
+ if (this._uMaterialEmissive) {
190
+ gl.uniform3fv(this._uMaterialEmissive, materialState.emissive);
191
+ }
192
+ if (this._uAlphaModeCutoff) {
193
+ gl.uniform4f(
194
+ this._uAlphaModeCutoff,
195
+ 1.0 * materialState.alpha,
196
+ materialState.alphaMode === 1 ? 1.0 : 0.0,
197
+ materialState.alphaCutoff,
198
+ 0);
199
+ }
200
+ if (material._ambientMap && material._ambientMap._state.texture && this._uMaterialAmbientMap) {
201
+ program.bindTexture(this._uMaterialAmbientMap, material._ambientMap._state.texture, frameCtx.textureUnit);
202
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
203
+ frameCtx.bindTexture++;
204
+ if (this._uMaterialAmbientMapMatrix) {
205
+ gl.uniformMatrix4fv(this._uMaterialAmbientMapMatrix, false, material._ambientMap._state.matrix);
206
+ }
207
+ }
208
+ if (material._diffuseMap && material._diffuseMap._state.texture && this._uDiffuseMap) {
209
+ program.bindTexture(this._uDiffuseMap, material._diffuseMap._state.texture, frameCtx.textureUnit);
210
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
211
+ frameCtx.bindTexture++;
212
+ if (this._uDiffuseMapMatrix) {
213
+ gl.uniformMatrix4fv(this._uDiffuseMapMatrix, false, material._diffuseMap._state.matrix);
214
+ }
215
+ }
216
+ if (material._specularMap && material._specularMap._state.texture && this._uSpecularMap) {
217
+ program.bindTexture(this._uSpecularMap, material._specularMap._state.texture, frameCtx.textureUnit);
218
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
219
+ frameCtx.bindTexture++;
220
+ if (this._uSpecularMapMatrix) {
221
+ gl.uniformMatrix4fv(this._uSpecularMapMatrix, false, material._specularMap._state.matrix);
222
+ }
223
+ }
224
+ if (material._emissiveMap && material._emissiveMap._state.texture && this._uEmissiveMap) {
225
+ program.bindTexture(this._uEmissiveMap, material._emissiveMap._state.texture, frameCtx.textureUnit);
226
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
227
+ frameCtx.bindTexture++;
228
+ if (this._uEmissiveMapMatrix) {
229
+ gl.uniformMatrix4fv(this._uEmissiveMapMatrix, false, material._emissiveMap._state.matrix);
230
+ }
231
+ }
232
+ if (material._alphaMap && material._alphaMap._state.texture && this._uAlphaMap) {
233
+ program.bindTexture(this._uAlphaMap, material._alphaMap._state.texture, frameCtx.textureUnit);
234
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
235
+ frameCtx.bindTexture++;
236
+ if (this._uAlphaMapMatrix) {
237
+ gl.uniformMatrix4fv(this._uAlphaMapMatrix, false, material._alphaMap._state.matrix);
238
+ }
239
+ }
240
+ if (material._reflectivityMap && material._reflectivityMap._state.texture && this._uReflectivityMap) {
241
+ program.bindTexture(this._uReflectivityMap, material._reflectivityMap._state.texture, frameCtx.textureUnit);
242
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
243
+ if (this._uReflectivityMapMatrix) {
244
+ gl.uniformMatrix4fv(this._uReflectivityMapMatrix, false, material._reflectivityMap._state.matrix);
245
+ }
246
+ }
247
+ if (material._normalMap && material._normalMap._state.texture && this._uNormalMap) {
248
+ program.bindTexture(this._uNormalMap, material._normalMap._state.texture, frameCtx.textureUnit);
249
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
250
+ frameCtx.bindTexture++;
251
+ if (this._uNormalMapMatrix) {
252
+ gl.uniformMatrix4fv(this._uNormalMapMatrix, false, material._normalMap._state.matrix);
253
+ }
254
+ }
255
+ if (material._occlusionMap && material._occlusionMap._state.texture && this._uOcclusionMap) {
256
+ program.bindTexture(this._uOcclusionMap, material._occlusionMap._state.texture, frameCtx.textureUnit);
257
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
258
+ frameCtx.bindTexture++;
259
+ if (this._uOcclusionMapMatrix) {
260
+ gl.uniformMatrix4fv(this._uOcclusionMapMatrix, false, material._occlusionMap._state.matrix);
261
+ }
262
+ }
263
+ if (material._diffuseFresnel) {
264
+ if (this._uDiffuseFresnelEdgeBias) {
265
+ gl.uniform1f(this._uDiffuseFresnelEdgeBias, material._diffuseFresnel.edgeBias);
266
+ }
267
+ if (this._uDiffuseFresnelCenterBias) {
268
+ gl.uniform1f(this._uDiffuseFresnelCenterBias, material._diffuseFresnel.centerBias);
269
+ }
270
+ if (this._uDiffuseFresnelEdgeColor) {
271
+ gl.uniform3fv(this._uDiffuseFresnelEdgeColor, material._diffuseFresnel.edgeColor);
272
+ }
273
+ if (this._uDiffuseFresnelCenterColor) {
274
+ gl.uniform3fv(this._uDiffuseFresnelCenterColor, material._diffuseFresnel.centerColor);
275
+ }
276
+ if (this._uDiffuseFresnelPower) {
277
+ gl.uniform1f(this._uDiffuseFresnelPower, material._diffuseFresnel.power);
278
+ }
279
+ }
280
+ if (material._specularFresnel) {
281
+ if (this._uSpecularFresnelEdgeBias) {
282
+ gl.uniform1f(this._uSpecularFresnelEdgeBias, material._specularFresnel.edgeBias);
283
+ }
284
+ if (this._uSpecularFresnelCenterBias) {
285
+ gl.uniform1f(this._uSpecularFresnelCenterBias, material._specularFresnel.centerBias);
286
+ }
287
+ if (this._uSpecularFresnelEdgeColor) {
288
+ gl.uniform3fv(this._uSpecularFresnelEdgeColor, material._specularFresnel.edgeColor);
289
+ }
290
+ if (this._uSpecularFresnelCenterColor) {
291
+ gl.uniform3fv(this._uSpecularFresnelCenterColor, material._specularFresnel.centerColor);
292
+ }
293
+ if (this._uSpecularFresnelPower) {
294
+ gl.uniform1f(this._uSpecularFresnelPower, material._specularFresnel.power);
295
+ }
296
+ }
297
+ if (material._alphaFresnel) {
298
+ if (this._uAlphaFresnelEdgeBias) {
299
+ gl.uniform1f(this._uAlphaFresnelEdgeBias, material._alphaFresnel.edgeBias);
300
+ }
301
+ if (this._uAlphaFresnelCenterBias) {
302
+ gl.uniform1f(this._uAlphaFresnelCenterBias, material._alphaFresnel.centerBias);
303
+ }
304
+ if (this._uAlphaFresnelEdgeColor) {
305
+ gl.uniform3fv(this._uAlphaFresnelEdgeColor, material._alphaFresnel.edgeColor);
306
+ }
307
+ if (this._uAlphaFresnelCenterColor) {
308
+ gl.uniform3fv(this._uAlphaFresnelCenterColor, material._alphaFresnel.centerColor);
309
+ }
310
+ if (this._uAlphaFresnelPower) {
311
+ gl.uniform1f(this._uAlphaFresnelPower, material._alphaFresnel.power);
312
+ }
313
+ }
314
+ if (material._reflectivityFresnel) {
315
+ if (this._uReflectivityFresnelEdgeBias) {
316
+ gl.uniform1f(this._uReflectivityFresnelEdgeBias, material._reflectivityFresnel.edgeBias);
317
+ }
318
+ if (this._uReflectivityFresnelCenterBias) {
319
+ gl.uniform1f(this._uReflectivityFresnelCenterBias, material._reflectivityFresnel.centerBias);
320
+ }
321
+ if (this._uReflectivityFresnelEdgeColor) {
322
+ gl.uniform3fv(this._uReflectivityFresnelEdgeColor, material._reflectivityFresnel.edgeColor);
323
+ }
324
+ if (this._uReflectivityFresnelCenterColor) {
325
+ gl.uniform3fv(this._uReflectivityFresnelCenterColor, material._reflectivityFresnel.centerColor);
326
+ }
327
+ if (this._uReflectivityFresnelPower) {
328
+ gl.uniform1f(this._uReflectivityFresnelPower, material._reflectivityFresnel.power);
329
+ }
330
+ }
331
+ if (material._emissiveFresnel) {
332
+ if (this._uEmissiveFresnelEdgeBias) {
333
+ gl.uniform1f(this._uEmissiveFresnelEdgeBias, material._emissiveFresnel.edgeBias);
334
+ }
335
+ if (this._uEmissiveFresnelCenterBias) {
336
+ gl.uniform1f(this._uEmissiveFresnelCenterBias, material._emissiveFresnel.centerBias);
337
+ }
338
+ if (this._uEmissiveFresnelEdgeColor) {
339
+ gl.uniform3fv(this._uEmissiveFresnelEdgeColor, material._emissiveFresnel.edgeColor);
340
+ }
341
+ if (this._uEmissiveFresnelCenterColor) {
342
+ gl.uniform3fv(this._uEmissiveFresnelCenterColor, material._emissiveFresnel.centerColor);
343
+ }
344
+ if (this._uEmissiveFresnelPower) {
345
+ gl.uniform1f(this._uEmissiveFresnelPower, material._emissiveFresnel.power);
346
+ }
347
+ }
348
+ break;
349
+
350
+ case "MetallicMaterial":
351
+ if (this._uBaseColor) {
352
+ gl.uniform3fv(this._uBaseColor, materialState.baseColor);
353
+ }
354
+ if (this._uMaterialMetallic) {
355
+ gl.uniform1f(this._uMaterialMetallic, materialState.metallic);
356
+ }
357
+ if (this._uMaterialRoughness) {
358
+ gl.uniform1f(this._uMaterialRoughness, materialState.roughness);
359
+ }
360
+ if (this._uMaterialSpecularF0) {
361
+ gl.uniform1f(this._uMaterialSpecularF0, materialState.specularF0);
362
+ }
363
+ if (this._uMaterialEmissive) {
364
+ gl.uniform3fv(this._uMaterialEmissive, materialState.emissive);
365
+ }
366
+ if (this._uAlphaModeCutoff) {
367
+ gl.uniform4f(
368
+ this._uAlphaModeCutoff,
369
+ 1.0 * materialState.alpha,
370
+ materialState.alphaMode === 1 ? 1.0 : 0.0,
371
+ materialState.alphaCutoff,
372
+ 0.0);
373
+ }
374
+ const baseColorMap = material._baseColorMap;
375
+ if (baseColorMap && baseColorMap._state.texture && this._uBaseColorMap) {
376
+ program.bindTexture(this._uBaseColorMap, baseColorMap._state.texture, frameCtx.textureUnit);
377
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
378
+ frameCtx.bindTexture++;
379
+ if (this._uBaseColorMapMatrix) {
380
+ gl.uniformMatrix4fv(this._uBaseColorMapMatrix, false, baseColorMap._state.matrix);
381
+ }
382
+ }
383
+ const metallicMap = material._metallicMap;
384
+ if (metallicMap && metallicMap._state.texture && this._uMetallicMap) {
385
+ program.bindTexture(this._uMetallicMap, metallicMap._state.texture, frameCtx.textureUnit);
386
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
387
+ frameCtx.bindTexture++;
388
+ if (this._uMetallicMapMatrix) {
389
+ gl.uniformMatrix4fv(this._uMetallicMapMatrix, false, metallicMap._state.matrix);
390
+ }
391
+ }
392
+ const roughnessMap = material._roughnessMap;
393
+ if (roughnessMap && roughnessMap._state.texture && this._uRoughnessMap) {
394
+ program.bindTexture(this._uRoughnessMap, roughnessMap._state.texture, frameCtx.textureUnit);
395
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
396
+ frameCtx.bindTexture++;
397
+ if (this._uRoughnessMapMatrix) {
398
+ gl.uniformMatrix4fv(this._uRoughnessMapMatrix, false, roughnessMap._state.matrix);
399
+ }
400
+ }
401
+ const metallicRoughnessMap = material._metallicRoughnessMap;
402
+ if (metallicRoughnessMap && metallicRoughnessMap._state.texture && this._uMetallicRoughnessMap) {
403
+ program.bindTexture(this._uMetallicRoughnessMap, metallicRoughnessMap._state.texture, frameCtx.textureUnit);
404
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
405
+ frameCtx.bindTexture++;
406
+ if (this._uMetallicRoughnessMapMatrix) {
407
+ gl.uniformMatrix4fv(this._uMetallicRoughnessMapMatrix, false, metallicRoughnessMap._state.matrix);
408
+ }
409
+ }
410
+ var emissiveMap = material._emissiveMap;
411
+ if (emissiveMap && emissiveMap._state.texture && this._uEmissiveMap) {
412
+ program.bindTexture(this._uEmissiveMap, emissiveMap._state.texture, frameCtx.textureUnit);
413
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
414
+ frameCtx.bindTexture++;
415
+ if (this._uEmissiveMapMatrix) {
416
+ gl.uniformMatrix4fv(this._uEmissiveMapMatrix, false, emissiveMap._state.matrix);
417
+ }
418
+ }
419
+ var occlusionMap = material._occlusionMap;
420
+ if (occlusionMap && material._occlusionMap._state.texture && this._uOcclusionMap) {
421
+ program.bindTexture(this._uOcclusionMap, occlusionMap._state.texture, frameCtx.textureUnit);
422
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
423
+ frameCtx.bindTexture++;
424
+ if (this._uOcclusionMapMatrix) {
425
+ gl.uniformMatrix4fv(this._uOcclusionMapMatrix, false, occlusionMap._state.matrix);
426
+ }
427
+ }
428
+ var alphaMap = material._alphaMap;
429
+ if (alphaMap && alphaMap._state.texture && this._uAlphaMap) {
430
+ program.bindTexture(this._uAlphaMap, alphaMap._state.texture, frameCtx.textureUnit);
431
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
432
+ frameCtx.bindTexture++;
433
+ if (this._uAlphaMapMatrix) {
434
+ gl.uniformMatrix4fv(this._uAlphaMapMatrix, false, alphaMap._state.matrix);
435
+ }
436
+ }
437
+ var normalMap = material._normalMap;
438
+ if (normalMap && normalMap._state.texture && this._uNormalMap) {
439
+ program.bindTexture(this._uNormalMap, normalMap._state.texture, frameCtx.textureUnit);
440
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
441
+ frameCtx.bindTexture++;
442
+ if (this._uNormalMapMatrix) {
443
+ gl.uniformMatrix4fv(this._uNormalMapMatrix, false, normalMap._state.matrix);
444
+ }
445
+ }
446
+ break;
447
+
448
+ case "SpecularMaterial":
449
+ if (this._uMaterialDiffuse) {
450
+ gl.uniform3fv(this._uMaterialDiffuse, materialState.diffuse);
451
+ }
452
+ if (this._uMaterialSpecular) {
453
+ gl.uniform3fv(this._uMaterialSpecular, materialState.specular);
454
+ }
455
+ if (this._uMaterialGlossiness) {
456
+ gl.uniform1f(this._uMaterialGlossiness, materialState.glossiness);
457
+ }
458
+ if (this._uMaterialReflectivity) {
459
+ gl.uniform1f(this._uMaterialReflectivity, materialState.reflectivity);
460
+ }
461
+ if (this._uMaterialEmissive) {
462
+ gl.uniform3fv(this._uMaterialEmissive, materialState.emissive);
463
+ }
464
+ if (this._uAlphaModeCutoff) {
465
+ gl.uniform4f(
466
+ this._uAlphaModeCutoff,
467
+ 1.0 * materialState.alpha,
468
+ materialState.alphaMode === 1 ? 1.0 : 0.0,
469
+ materialState.alphaCutoff,
470
+ 0.0);
471
+ }
472
+ const diffuseMap = material._diffuseMap;
473
+ if (diffuseMap && diffuseMap._state.texture && this._uDiffuseMap) {
474
+ program.bindTexture(this._uDiffuseMap, diffuseMap._state.texture, frameCtx.textureUnit);
475
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
476
+ frameCtx.bindTexture++;
477
+ if (this._uDiffuseMapMatrix) {
478
+ gl.uniformMatrix4fv(this._uDiffuseMapMatrix, false, diffuseMap._state.matrix);
479
+ }
480
+ }
481
+ const specularMap = material._specularMap;
482
+ if (specularMap && specularMap._state.texture && this._uSpecularMap) {
483
+ program.bindTexture(this._uSpecularMap, specularMap._state.texture, frameCtx.textureUnit);
484
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
485
+ frameCtx.bindTexture++;
486
+ if (this._uSpecularMapMatrix) {
487
+ gl.uniformMatrix4fv(this._uSpecularMapMatrix, false, specularMap._state.matrix);
488
+ }
489
+ }
490
+ const glossinessMap = material._glossinessMap;
491
+ if (glossinessMap && glossinessMap._state.texture && this._uGlossinessMap) {
492
+ program.bindTexture(this._uGlossinessMap, glossinessMap._state.texture, frameCtx.textureUnit);
493
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
494
+ frameCtx.bindTexture++;
495
+ if (this._uGlossinessMapMatrix) {
496
+ gl.uniformMatrix4fv(this._uGlossinessMapMatrix, false, glossinessMap._state.matrix);
497
+ }
498
+ }
499
+ const specularGlossinessMap = material._specularGlossinessMap;
500
+ if (specularGlossinessMap && specularGlossinessMap._state.texture && this._uSpecularGlossinessMap) {
501
+ program.bindTexture(this._uSpecularGlossinessMap, specularGlossinessMap._state.texture, frameCtx.textureUnit);
502
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
503
+ frameCtx.bindTexture++;
504
+ if (this._uSpecularGlossinessMapMatrix) {
505
+ gl.uniformMatrix4fv(this._uSpecularGlossinessMapMatrix, false, specularGlossinessMap._state.matrix);
506
+ }
507
+ }
508
+ var emissiveMap = material._emissiveMap;
509
+ if (emissiveMap && emissiveMap._state.texture && this._uEmissiveMap) {
510
+ program.bindTexture(this._uEmissiveMap, emissiveMap._state.texture, frameCtx.textureUnit);
511
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
512
+ frameCtx.bindTexture++;
513
+ if (this._uEmissiveMapMatrix) {
514
+ gl.uniformMatrix4fv(this._uEmissiveMapMatrix, false, emissiveMap._state.matrix);
515
+ }
516
+ }
517
+ var occlusionMap = material._occlusionMap;
518
+ if (occlusionMap && occlusionMap._state.texture && this._uOcclusionMap) {
519
+ program.bindTexture(this._uOcclusionMap, occlusionMap._state.texture, frameCtx.textureUnit);
520
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
521
+ frameCtx.bindTexture++;
522
+ if (this._uOcclusionMapMatrix) {
523
+ gl.uniformMatrix4fv(this._uOcclusionMapMatrix, false, occlusionMap._state.matrix);
524
+ }
525
+ }
526
+ var alphaMap = material._alphaMap;
527
+ if (alphaMap && alphaMap._state.texture && this._uAlphaMap) {
528
+ program.bindTexture(this._uAlphaMap, alphaMap._state.texture, frameCtx.textureUnit);
529
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
530
+ frameCtx.bindTexture++;
531
+ if (this._uAlphaMapMatrix) {
532
+ gl.uniformMatrix4fv(this._uAlphaMapMatrix, false, alphaMap._state.matrix);
533
+ }
534
+ }
535
+ var normalMap = material._normalMap;
536
+ if (normalMap && normalMap._state.texture && this._uNormalMap) {
537
+ program.bindTexture(this._uNormalMap, normalMap._state.texture, frameCtx.textureUnit);
538
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
539
+ frameCtx.bindTexture++;
540
+ if (this._uNormalMapMatrix) {
541
+ gl.uniformMatrix4fv(this._uNormalMapMatrix, false, normalMap._state.matrix);
542
+ }
543
+ }
544
+ break;
545
+ }
546
+ this._lastMaterialId = materialState.id;
547
+ }
548
+
549
+ gl.uniformMatrix4fv(this._uModelMatrix, gl.FALSE, mesh.worldMatrix);
550
+ if (this._uModelNormalMatrix) {
551
+ gl.uniformMatrix4fv(this._uModelNormalMatrix, gl.FALSE, mesh.worldNormalMatrix);
552
+ }
553
+
554
+ if (this._uClippable) {
555
+ gl.uniform1i(this._uClippable, meshState.clippable);
556
+ }
557
+
558
+ if (this._uColorize) {
559
+ const colorize = meshState.colorize;
560
+ const lastColorize = this._lastColorize;
561
+ if (lastColorize[0] !== colorize[0] ||
562
+ lastColorize[1] !== colorize[1] ||
563
+ lastColorize[2] !== colorize[2] ||
564
+ lastColorize[3] !== colorize[3]) {
565
+ gl.uniform4fv(this._uColorize, colorize);
566
+ lastColorize[0] = colorize[0];
567
+ lastColorize[1] = colorize[1];
568
+ lastColorize[2] = colorize[2];
569
+ lastColorize[3] = colorize[3];
570
+ }
571
+ }
572
+
573
+ gl.uniform3fv(this._uOffset, meshState.offset);
574
+ gl.uniform3fv(this._uScale, mesh.scale);
575
+
576
+ // Bind VBOs
577
+
578
+ if (geometryState.id !== this._lastGeometryId) {
579
+ if (this._uPositionsDecodeMatrix) {
580
+ gl.uniformMatrix4fv(this._uPositionsDecodeMatrix, false, geometryState.positionsDecodeMatrix);
581
+ }
582
+ if (this._uUVDecodeMatrix) {
583
+ gl.uniformMatrix3fv(this._uUVDecodeMatrix, false, geometryState.uvDecodeMatrix);
584
+ }
585
+ if (this._aPosition) {
586
+ this._aPosition.bindArrayBuffer(geometryState.positionsBuf);
587
+ frameCtx.bindArray++;
588
+ }
589
+ if (this._aNormal) {
590
+ this._aNormal.bindArrayBuffer(geometryState.normalsBuf);
591
+ frameCtx.bindArray++;
592
+ }
593
+ if (this._aUV) {
594
+ this._aUV.bindArrayBuffer(geometryState.uvBuf);
595
+ frameCtx.bindArray++;
596
+ }
597
+ if (this._aColor) {
598
+ this._aColor.bindArrayBuffer(geometryState.colorsBuf);
599
+ frameCtx.bindArray++;
600
+ }
601
+ if (this._aFlags) {
602
+ this._aFlags.bindArrayBuffer(geometryState.flagsBuf);
603
+ frameCtx.bindArray++;
604
+ }
605
+ if (geometryState.indicesBuf) {
606
+ geometryState.indicesBuf.bind();
607
+ frameCtx.bindArray++;
608
+ }
609
+ this._lastGeometryId = geometryState.id;
610
+ }
611
+
612
+ // Draw (indices bound in prev step)
613
+
614
+ if (geometryState.indicesBuf) {
615
+ gl.drawElements(geometryState.primitive, geometryState.indicesBuf.numItems, geometryState.indicesBuf.itemType, 0);
616
+ frameCtx.drawElements++;
617
+ } else if (geometryState.positions) {
618
+ gl.drawArrays(gl.TRIANGLES, 0, geometryState.positions.numItems);
619
+ frameCtx.drawArrays++;
620
+ }
621
+
622
+ if (background) {
623
+ gl.depthFunc(gl.LESS);
624
+ }
625
+ };
626
+
627
+ DrawRenderer.prototype._allocate = function (mesh) {
628
+ const scene = mesh.scene;
629
+ const gl = scene.canvas.gl;
630
+ const material = mesh._material;
631
+ const lightsState = scene._lightsState;
632
+ const sectionPlanesState = scene._sectionPlanesState;
633
+ const materialState = mesh._material._state;
634
+
635
+ this._program = new Program(gl, this._shaderSource);
636
+ if (this._program.errors) {
637
+ this.errors = this._program.errors;
638
+ return;
639
+ }
640
+ const program = this._program;
641
+ this._uPositionsDecodeMatrix = program.getLocation("positionsDecodeMatrix");
642
+ this._uUVDecodeMatrix = program.getLocation("uvDecodeMatrix");
643
+ this._uModelMatrix = program.getLocation("modelMatrix");
644
+ this._uModelNormalMatrix = program.getLocation("modelNormalMatrix");
645
+ this._uViewMatrix = program.getLocation("viewMatrix");
646
+ this._uViewNormalMatrix = program.getLocation("viewNormalMatrix");
647
+ this._uProjMatrix = program.getLocation("projMatrix");
648
+ this._uGammaFactor = program.getLocation("gammaFactor");
649
+ this._uLightAmbient = [];
650
+ this._uLightColor = [];
651
+ this._uLightDir = [];
652
+ this._uLightPos = [];
653
+ this._uLightAttenuation = [];
654
+ this._uShadowViewMatrix = [];
655
+ this._uShadowProjMatrix = [];
656
+
657
+ if (scene.logarithmicDepthBufferEnabled) {
658
+ this._uLogDepthBufFC = program.getLocation("logDepthBufFC");
659
+ }
660
+
661
+ const lights = lightsState.lights;
662
+ let light;
663
+
664
+ for (var i = 0, len = lights.length; i < len; i++) {
665
+ light = lights[i];
666
+ switch (light.type) {
667
+
668
+ case "ambient":
669
+ this._uLightAmbient[i] = program.getLocation("lightAmbient");
670
+ break;
671
+
672
+ case "dir":
673
+ this._uLightColor[i] = program.getLocation("lightColor" + i);
674
+ this._uLightPos[i] = null;
675
+ this._uLightDir[i] = program.getLocation("lightDir" + i);
676
+ break;
677
+
678
+ case "point":
679
+ this._uLightColor[i] = program.getLocation("lightColor" + i);
680
+ this._uLightPos[i] = program.getLocation("lightPos" + i);
681
+ this._uLightDir[i] = null;
682
+ this._uLightAttenuation[i] = program.getLocation("lightAttenuation" + i);
683
+ break;
684
+
685
+ case "spot":
686
+ this._uLightColor[i] = program.getLocation("lightColor" + i);
687
+ this._uLightPos[i] = program.getLocation("lightPos" + i);
688
+ this._uLightDir[i] = program.getLocation("lightDir" + i);
689
+ this._uLightAttenuation[i] = program.getLocation("lightAttenuation" + i);
690
+ break;
691
+ }
692
+
693
+ if (light.castsShadow) {
694
+ this._uShadowViewMatrix[i] = program.getLocation("shadowViewMatrix" + i);
695
+ this._uShadowProjMatrix[i] = program.getLocation("shadowProjMatrix" + i);
696
+ }
697
+ }
698
+
699
+ if (lightsState.lightMaps.length > 0) {
700
+ this._uLightMap = "lightMap";
701
+ }
702
+
703
+ if (lightsState.reflectionMaps.length > 0) {
704
+ this._uReflectionMap = "reflectionMap";
705
+ }
706
+
707
+ this._uSectionPlanes = [];
708
+ const sectionPlanes = sectionPlanesState.sectionPlanes;
709
+ for (var i = 0, len = sectionPlanes.length; i < len; i++) {
710
+ this._uSectionPlanes.push({
711
+ active: program.getLocation("sectionPlaneActive" + i),
712
+ pos: program.getLocation("sectionPlanePos" + i),
713
+ dir: program.getLocation("sectionPlaneDir" + i)
714
+ });
715
+ }
716
+
717
+ this._uPointSize = program.getLocation("pointSize");
718
+
719
+ switch (materialState.type) {
720
+ case "LambertMaterial":
721
+ this._uMaterialColor = program.getLocation("materialColor");
722
+ this._uMaterialEmissive = program.getLocation("materialEmissive");
723
+ this._uAlphaModeCutoff = program.getLocation("materialAlphaModeCutoff");
724
+ break;
725
+
726
+ case "PhongMaterial":
727
+ this._uMaterialAmbient = program.getLocation("materialAmbient");
728
+ this._uMaterialDiffuse = program.getLocation("materialDiffuse");
729
+ this._uMaterialSpecular = program.getLocation("materialSpecular");
730
+ this._uMaterialEmissive = program.getLocation("materialEmissive");
731
+ this._uAlphaModeCutoff = program.getLocation("materialAlphaModeCutoff");
732
+ this._uMaterialShininess = program.getLocation("materialShininess");
733
+ if (material._ambientMap) {
734
+ this._uMaterialAmbientMap = "ambientMap";
735
+ this._uMaterialAmbientMapMatrix = program.getLocation("ambientMapMatrix");
736
+ }
737
+ if (material._diffuseMap) {
738
+ this._uDiffuseMap = "diffuseMap";
739
+ this._uDiffuseMapMatrix = program.getLocation("diffuseMapMatrix");
740
+ }
741
+ if (material._specularMap) {
742
+ this._uSpecularMap = "specularMap";
743
+ this._uSpecularMapMatrix = program.getLocation("specularMapMatrix");
744
+ }
745
+ if (material._emissiveMap) {
746
+ this._uEmissiveMap = "emissiveMap";
747
+ this._uEmissiveMapMatrix = program.getLocation("emissiveMapMatrix");
748
+ }
749
+ if (material._alphaMap) {
750
+ this._uAlphaMap = "alphaMap";
751
+ this._uAlphaMapMatrix = program.getLocation("alphaMapMatrix");
752
+ }
753
+ if (material._reflectivityMap) {
754
+ this._uReflectivityMap = "reflectivityMap";
755
+ this._uReflectivityMapMatrix = program.getLocation("reflectivityMapMatrix");
756
+ }
757
+ if (material._normalMap) {
758
+ this._uNormalMap = "normalMap";
759
+ this._uNormalMapMatrix = program.getLocation("normalMapMatrix");
760
+ }
761
+ if (material._occlusionMap) {
762
+ this._uOcclusionMap = "occlusionMap";
763
+ this._uOcclusionMapMatrix = program.getLocation("occlusionMapMatrix");
764
+ }
765
+ if (material._diffuseFresnel) {
766
+ this._uDiffuseFresnelEdgeBias = program.getLocation("diffuseFresnelEdgeBias");
767
+ this._uDiffuseFresnelCenterBias = program.getLocation("diffuseFresnelCenterBias");
768
+ this._uDiffuseFresnelEdgeColor = program.getLocation("diffuseFresnelEdgeColor");
769
+ this._uDiffuseFresnelCenterColor = program.getLocation("diffuseFresnelCenterColor");
770
+ this._uDiffuseFresnelPower = program.getLocation("diffuseFresnelPower");
771
+ }
772
+ if (material._specularFresnel) {
773
+ this._uSpecularFresnelEdgeBias = program.getLocation("specularFresnelEdgeBias");
774
+ this._uSpecularFresnelCenterBias = program.getLocation("specularFresnelCenterBias");
775
+ this._uSpecularFresnelEdgeColor = program.getLocation("specularFresnelEdgeColor");
776
+ this._uSpecularFresnelCenterColor = program.getLocation("specularFresnelCenterColor");
777
+ this._uSpecularFresnelPower = program.getLocation("specularFresnelPower");
778
+ }
779
+ if (material._alphaFresnel) {
780
+ this._uAlphaFresnelEdgeBias = program.getLocation("alphaFresnelEdgeBias");
781
+ this._uAlphaFresnelCenterBias = program.getLocation("alphaFresnelCenterBias");
782
+ this._uAlphaFresnelEdgeColor = program.getLocation("alphaFresnelEdgeColor");
783
+ this._uAlphaFresnelCenterColor = program.getLocation("alphaFresnelCenterColor");
784
+ this._uAlphaFresnelPower = program.getLocation("alphaFresnelPower");
785
+ }
786
+ if (material._reflectivityFresnel) {
787
+ this._uReflectivityFresnelEdgeBias = program.getLocation("reflectivityFresnelEdgeBias");
788
+ this._uReflectivityFresnelCenterBias = program.getLocation("reflectivityFresnelCenterBias");
789
+ this._uReflectivityFresnelEdgeColor = program.getLocation("reflectivityFresnelEdgeColor");
790
+ this._uReflectivityFresnelCenterColor = program.getLocation("reflectivityFresnelCenterColor");
791
+ this._uReflectivityFresnelPower = program.getLocation("reflectivityFresnelPower");
792
+ }
793
+ if (material._emissiveFresnel) {
794
+ this._uEmissiveFresnelEdgeBias = program.getLocation("emissiveFresnelEdgeBias");
795
+ this._uEmissiveFresnelCenterBias = program.getLocation("emissiveFresnelCenterBias");
796
+ this._uEmissiveFresnelEdgeColor = program.getLocation("emissiveFresnelEdgeColor");
797
+ this._uEmissiveFresnelCenterColor = program.getLocation("emissiveFresnelCenterColor");
798
+ this._uEmissiveFresnelPower = program.getLocation("emissiveFresnelPower");
799
+ }
800
+ break;
801
+
802
+ case "MetallicMaterial":
803
+ this._uBaseColor = program.getLocation("materialBaseColor");
804
+ this._uMaterialMetallic = program.getLocation("materialMetallic");
805
+ this._uMaterialRoughness = program.getLocation("materialRoughness");
806
+ this._uMaterialSpecularF0 = program.getLocation("materialSpecularF0");
807
+ this._uMaterialEmissive = program.getLocation("materialEmissive");
808
+ this._uAlphaModeCutoff = program.getLocation("materialAlphaModeCutoff");
809
+ if (material._baseColorMap) {
810
+ this._uBaseColorMap = "baseColorMap";
811
+ this._uBaseColorMapMatrix = program.getLocation("baseColorMapMatrix");
812
+ }
813
+ if (material._metallicMap) {
814
+ this._uMetallicMap = "metallicMap";
815
+ this._uMetallicMapMatrix = program.getLocation("metallicMapMatrix");
816
+ }
817
+ if (material._roughnessMap) {
818
+ this._uRoughnessMap = "roughnessMap";
819
+ this._uRoughnessMapMatrix = program.getLocation("roughnessMapMatrix");
820
+ }
821
+ if (material._metallicRoughnessMap) {
822
+ this._uMetallicRoughnessMap = "metallicRoughnessMap";
823
+ this._uMetallicRoughnessMapMatrix = program.getLocation("metallicRoughnessMapMatrix");
824
+ }
825
+ if (material._emissiveMap) {
826
+ this._uEmissiveMap = "emissiveMap";
827
+ this._uEmissiveMapMatrix = program.getLocation("emissiveMapMatrix");
828
+ }
829
+ if (material._occlusionMap) {
830
+ this._uOcclusionMap = "occlusionMap";
831
+ this._uOcclusionMapMatrix = program.getLocation("occlusionMapMatrix");
832
+ }
833
+ if (material._alphaMap) {
834
+ this._uAlphaMap = "alphaMap";
835
+ this._uAlphaMapMatrix = program.getLocation("alphaMapMatrix");
836
+ }
837
+ if (material._normalMap) {
838
+ this._uNormalMap = "normalMap";
839
+ this._uNormalMapMatrix = program.getLocation("normalMapMatrix");
840
+ }
841
+ break;
842
+
843
+ case "SpecularMaterial":
844
+ this._uMaterialDiffuse = program.getLocation("materialDiffuse");
845
+ this._uMaterialSpecular = program.getLocation("materialSpecular");
846
+ this._uMaterialGlossiness = program.getLocation("materialGlossiness");
847
+ this._uMaterialReflectivity = program.getLocation("reflectivityFresnel");
848
+ this._uMaterialEmissive = program.getLocation("materialEmissive");
849
+ this._uAlphaModeCutoff = program.getLocation("materialAlphaModeCutoff");
850
+ if (material._diffuseMap) {
851
+ this._uDiffuseMap = "diffuseMap";
852
+ this._uDiffuseMapMatrix = program.getLocation("diffuseMapMatrix");
853
+ }
854
+ if (material._specularMap) {
855
+ this._uSpecularMap = "specularMap";
856
+ this._uSpecularMapMatrix = program.getLocation("specularMapMatrix");
857
+ }
858
+ if (material._glossinessMap) {
859
+ this._uGlossinessMap = "glossinessMap";
860
+ this._uGlossinessMapMatrix = program.getLocation("glossinessMapMatrix");
861
+ }
862
+ if (material._specularGlossinessMap) {
863
+ this._uSpecularGlossinessMap = "materialSpecularGlossinessMap";
864
+ this._uSpecularGlossinessMapMatrix = program.getLocation("materialSpecularGlossinessMapMatrix");
865
+ }
866
+ if (material._emissiveMap) {
867
+ this._uEmissiveMap = "emissiveMap";
868
+ this._uEmissiveMapMatrix = program.getLocation("emissiveMapMatrix");
869
+ }
870
+ if (material._occlusionMap) {
871
+ this._uOcclusionMap = "occlusionMap";
872
+ this._uOcclusionMapMatrix = program.getLocation("occlusionMapMatrix");
873
+ }
874
+ if (material._alphaMap) {
875
+ this._uAlphaMap = "alphaMap";
876
+ this._uAlphaMapMatrix = program.getLocation("alphaMapMatrix");
877
+ }
878
+ if (material._normalMap) {
879
+ this._uNormalMap = "normalMap";
880
+ this._uNormalMapMatrix = program.getLocation("normalMapMatrix");
881
+ }
882
+ break;
883
+ }
884
+
885
+ this._aPosition = program.getAttribute("position");
886
+ this._aNormal = program.getAttribute("normal");
887
+ this._aUV = program.getAttribute("uv");
888
+ this._aColor = program.getAttribute("color");
889
+ this._aFlags = program.getAttribute("flags");
890
+
891
+ this._uClippable = program.getLocation("clippable");
892
+ this._uColorize = program.getLocation("colorize");
893
+ this._uOffset = program.getLocation("offset");
894
+ this._uScale = program.getLocation("scale");
895
+
896
+ this._lastMaterialId = null;
897
+ this._lastVertexBufsId = null;
898
+ this._lastGeometryId = null;
899
+
900
+ this._lastColorize = new Float32Array(4);
901
+
902
+ this._baseTextureUnit = 0;
903
+
904
+ };
905
+
906
+ DrawRenderer.prototype._bindProgram = function (frameCtx) {
907
+
908
+ const maxTextureUnits = WEBGL_INFO.MAX_TEXTURE_UNITS;
909
+ const scene = this._scene;
910
+ const gl = scene.canvas.gl;
911
+ const lightsState = scene._lightsState;
912
+ const project = scene.camera.project;
913
+ let light;
914
+
915
+ const program = this._program;
916
+
917
+ program.bind();
918
+
919
+ frameCtx.useProgram++;
920
+ frameCtx.textureUnit = 0;
921
+
922
+ this._lastMaterialId = null;
923
+ this._lastVertexBufsId = null;
924
+ this._lastGeometryId = null;
925
+
926
+ this._lastColorize[0] = -1;
927
+ this._lastColorize[1] = -1;
928
+ this._lastColorize[2] = -1;
929
+ this._lastColorize[3] = -1;
930
+
931
+ gl.uniformMatrix4fv(this._uProjMatrix, false, project.matrix);
932
+
933
+ if (scene.logarithmicDepthBufferEnabled) {
934
+ const logDepthBufFC = 2.0 / (Math.log(project.far + 1.0) / Math.LN2);
935
+ gl.uniform1f(this._uLogDepthBufFC, logDepthBufFC);
936
+ }
937
+
938
+ for (var i = 0, len = lightsState.lights.length; i < len; i++) {
939
+
940
+ light = lightsState.lights[i];
941
+
942
+ if (this._uLightAmbient[i]) {
943
+ gl.uniform4f(this._uLightAmbient[i], light.color[0], light.color[1], light.color[2], light.intensity);
944
+
945
+ } else {
946
+
947
+ if (this._uLightColor[i]) {
948
+ gl.uniform4f(this._uLightColor[i], light.color[0], light.color[1], light.color[2], light.intensity);
949
+ }
950
+
951
+ if (this._uLightPos[i]) {
952
+ gl.uniform3fv(this._uLightPos[i], light.pos);
953
+ if (this._uLightAttenuation[i]) {
954
+ gl.uniform1f(this._uLightAttenuation[i], light.attenuation);
955
+ }
956
+ }
957
+
958
+ if (this._uLightDir[i]) {
959
+ gl.uniform3fv(this._uLightDir[i], light.dir);
960
+ }
961
+
962
+ if (light.castsShadow) {
963
+ if (this._uShadowViewMatrix[i]) {
964
+ gl.uniformMatrix4fv(this._uShadowViewMatrix[i], false, light.getShadowViewMatrix());
965
+ }
966
+ if (this._uShadowProjMatrix[i]) {
967
+ gl.uniformMatrix4fv(this._uShadowProjMatrix[i], false, light.getShadowProjMatrix());
968
+ }
969
+ const shadowRenderBuf = light.getShadowRenderBuf();
970
+ if (shadowRenderBuf) {
971
+ program.bindTexture("shadowMap" + i, shadowRenderBuf.getTexture(), frameCtx.textureUnit);
972
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
973
+ frameCtx.bindTexture++;
974
+ }
975
+ }
976
+ }
977
+ }
978
+
979
+ if (lightsState.lightMaps.length > 0 && lightsState.lightMaps[0].texture && this._uLightMap) {
980
+ program.bindTexture(this._uLightMap, lightsState.lightMaps[0].texture, frameCtx.textureUnit);
981
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
982
+ frameCtx.bindTexture++;
983
+ }
984
+
985
+ if (lightsState.reflectionMaps.length > 0 && lightsState.reflectionMaps[0].texture && this._uReflectionMap) {
986
+ program.bindTexture(this._uReflectionMap, lightsState.reflectionMaps[0].texture, frameCtx.textureUnit);
987
+ frameCtx.textureUnit = (frameCtx.textureUnit + 1) % maxTextureUnits;
988
+ frameCtx.bindTexture++;
989
+ }
990
+
991
+ if (this._uGammaFactor) {
992
+ gl.uniform1f(this._uGammaFactor, scene.gammaFactor);
993
+ }
994
+
995
+ this._baseTextureUnit = frameCtx.textureUnit;
996
+ };
997
+
998
+ export {DrawRenderer};