@xeokit/xeokit-sdk 2.4.0-alpha-4 → 2.4.0-alpha-6

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.
@@ -15101,7 +15101,9 @@ class SAOOcclusionRenderer {
15101
15101
 
15102
15102
  const uv = new Float32Array([1, 1, 0, 1, 0, 0, 1, 0]);
15103
15103
  const positions = new Float32Array([1, 1, 0, -1, 1, 0, -1, -1, 0, 1, -1, 0]);
15104
- const indices = new Uint8Array([0, 1, 2, 0, 2, 3]);
15104
+
15105
+ // Mitigation: if Uint8Array is used, the geometry is corrupted on OSX when using Chrome with data-textures
15106
+ const indices = new Uint32Array([0, 1, 2, 0, 2, 3]);
15105
15107
 
15106
15108
  this._positionsBuf = new ArrayBuf(gl, gl.ARRAY_BUFFER, positions, positions.length, 3, gl.STATIC_DRAW);
15107
15109
  this._uvBuf = new ArrayBuf(gl, gl.ARRAY_BUFFER, uv, uv.length, 2, gl.STATIC_DRAW);
@@ -15337,7 +15339,9 @@ class SAODepthLimitedBlurRenderer {
15337
15339
 
15338
15340
  const uv = new Float32Array([1, 1, 0, 1, 0, 0, 1, 0]);
15339
15341
  const positions = new Float32Array([1, 1, 0, -1, 1, 0, -1, -1, 0, 1, -1, 0]);
15340
- const indices = new Uint8Array([0, 1, 2, 0, 2, 3]);
15342
+
15343
+ // Mitigation: if Uint8Array is used, the geometry is corrupted on OSX when using Chrome with data-textures
15344
+ const indices = new Uint32Array([0, 1, 2, 0, 2, 3]);
15341
15345
 
15342
15346
  this._positionsBuf = new ArrayBuf(gl, gl.ARRAY_BUFFER, positions, positions.length, 3, gl.STATIC_DRAW);
15343
15347
  this._uvBuf = new ArrayBuf(gl, gl.ARRAY_BUFFER, uv, uv.length, 2, gl.STATIC_DRAW);
@@ -19887,7 +19891,7 @@ class TrianglesDataTextureColorRenderer {
19887
19891
  tempVec4$9[2] = sao.blendCutoff;
19888
19892
  tempVec4$9[3] = sao.blendFactor;
19889
19893
  gl.uniform4fv(this._uSAOParams, tempVec4$9);
19890
- this._program.bindTexture(this._uOcclusionTexture, frameCtx.occlusionTexture, 0);
19894
+ this._program.bindTexture(this._uOcclusionTexture, frameCtx.occlusionTexture, 10);
19891
19895
  }
19892
19896
  }
19893
19897
 
@@ -20218,7 +20222,7 @@ class TrianglesDataTextureColorRenderer {
20218
20222
  src.push(" float blendCutoff = uSAOParams[2];");
20219
20223
  src.push(" float blendFactor = uSAOParams[3];");
20220
20224
  src.push(" vec2 uv = vec2(gl_FragCoord.x / viewportWidth, gl_FragCoord.y / viewportHeight);");
20221
- src.push(" float ambient = smoothstep(blendCutoff, 1.0, unpackRGBToFloat(texture2D(uOcclusionTexture, uv))) * blendFactor;");
20225
+ src.push(" float ambient = smoothstep(blendCutoff, 1.0, unpackRGBToFloat(texture(uOcclusionTexture, uv))) * blendFactor;");
20222
20226
  src.push(" outColor = vec4(vColor.rgb * ambient, 1.0);");
20223
20227
  } else {
20224
20228
  src.push(" outColor = vColor;");
@@ -23076,6 +23080,7 @@ class TrianglesDataTextureOcclusionRenderer {
23076
23080
  }
23077
23081
  }
23078
23082
 
23083
+ math.vec4();
23079
23084
  const tempVec3a$17 = math.vec3();
23080
23085
 
23081
23086
  /**
@@ -23098,12 +23103,12 @@ class TrianglesDataTextureDepthRenderer {
23098
23103
  }
23099
23104
 
23100
23105
  drawLayer(frameCtx, dataTextureLayer, renderPass) {
23101
-
23102
- const model = dataTextureLayer.model;
23103
- const scene = model.scene;
23106
+ const scene = this._scene;
23104
23107
  const camera = scene.camera;
23108
+ const model = dataTextureLayer.model;
23105
23109
  const gl = scene.canvas.gl;
23106
23110
  const state = dataTextureLayer._state;
23111
+ const textureState = state.textureState;
23107
23112
  const origin = dataTextureLayer._state.origin;
23108
23113
 
23109
23114
  if (!this._program) {
@@ -23115,13 +23120,38 @@ class TrianglesDataTextureDepthRenderer {
23115
23120
 
23116
23121
  if (frameCtx.lastProgramId !== this._program.id) {
23117
23122
  frameCtx.lastProgramId = this._program.id;
23118
- this._bindProgram();
23123
+ this._bindProgram(frameCtx, state);
23119
23124
  }
23125
+
23126
+ textureState.bindCommonTextures (
23127
+ this._program,
23128
+ this._uTexturePerObjectIdPositionsDecodeMatrix,
23129
+ this._uTexturePerVertexIdCoordinates,
23130
+ this._uTexturePerObjectIdColorsAndFlags,
23131
+ this._uTextureCameraMatrices,
23132
+ this._uTextureModelMatrices,
23133
+ this._uTexturePerObjectIdOffsets
23134
+ );
23120
23135
 
23121
- gl.uniform1i(this._uRenderPass, renderPass);
23136
+ let cameraEye = camera.eye;
23122
23137
 
23123
- gl.uniformMatrix4fv(this._uWorldMatrix, false, model.worldMatrix);
23124
- gl.uniformMatrix4fv(this._uViewMatrix, false, (origin) ? createRTCViewMat(camera.viewMatrix, origin) : camera.viewMatrix);
23138
+ if (frameCtx.pickViewMatrix) {
23139
+ textureState.bindPickCameraTexture (
23140
+ this._program,
23141
+ this._uTextureCameraMatrices
23142
+ );
23143
+ cameraEye = frameCtx.pickOrigin || cameraEye;
23144
+ }
23145
+
23146
+ const originCameraEye = [
23147
+ cameraEye[0] - origin[0],
23148
+ cameraEye[1] - origin[1],
23149
+ cameraEye[2] - origin[2],
23150
+ ];
23151
+
23152
+ gl.uniform3fv(this._uCameraEyeRtc, originCameraEye);
23153
+
23154
+ gl.uniform1i(this._uRenderPass, renderPass);
23125
23155
 
23126
23156
  const numSectionPlanes = scene._sectionPlanesState.sectionPlanes.length;
23127
23157
  if (numSectionPlanes > 0) {
@@ -23147,23 +23177,43 @@ class TrianglesDataTextureDepthRenderer {
23147
23177
  }
23148
23178
  }
23149
23179
 
23150
- gl.uniformMatrix4fv(this._uPositionsDecodeMatrix, false, dataTextureLayer._state.positionsDecodeMatrix);
23151
-
23152
- this._aPosition.bindArrayBuffer(state.positionsBuf);
23180
+ if (state.numIndices8Bits > 0)
23181
+ {
23182
+ textureState.bindTriangleIndicesTextures(
23183
+ this._program,
23184
+ this._uTexturePerPolygonIdPortionIds,
23185
+ this._uTexturePerPolygonIdIndices,
23186
+ 8 // 8 bits indices
23187
+ );
23153
23188
 
23154
- if (this._aOffset) {
23155
- this._aOffset.bindArrayBuffer(state.offsetsBuf);
23189
+ gl.drawArrays(gl.TRIANGLES, 0, state.numIndices8Bits);
23156
23190
  }
23157
23191
 
23158
- this._aFlags.bindArrayBuffer(state.flagsBuf);
23192
+ if (state.numIndices16Bits > 0)
23193
+ {
23194
+ textureState.bindTriangleIndicesTextures(
23195
+ this._program,
23196
+ this._uTexturePerPolygonIdPortionIds,
23197
+ this._uTexturePerPolygonIdIndices,
23198
+ 16 // 16 bits indices
23199
+ );
23159
23200
 
23160
- if (this._aFlags2) {
23161
- this._aFlags2.bindArrayBuffer(state.flags2Buf);
23201
+ gl.drawArrays(gl.TRIANGLES, 0, state.numIndices16Bits);
23162
23202
  }
23203
+
23204
+ if (state.numIndices32Bits > 0)
23205
+ {
23206
+ textureState.bindTriangleIndicesTextures(
23207
+ this._program,
23208
+ this._uTexturePerPolygonIdPortionIds,
23209
+ this._uTexturePerPolygonIdIndices,
23210
+ 32 // 32 bits indices
23211
+ );
23163
23212
 
23164
- state.indicesBuf.bind();
23213
+ gl.drawArrays(gl.TRIANGLES, 0, state.numIndices32Bits);
23214
+ }
23165
23215
 
23166
- gl.drawElements(gl.TRIANGLES, state.indicesBuf.numItems, state.indicesBuf.itemType, 0);
23216
+ frameCtx.drawElements++;
23167
23217
  }
23168
23218
 
23169
23219
  _allocate() {
@@ -23195,23 +23245,30 @@ class TrianglesDataTextureDepthRenderer {
23195
23245
  });
23196
23246
  }
23197
23247
 
23198
- this._aPosition = program.getAttribute("position");
23199
- this._aOffset = program.getAttribute("offset");
23200
- this._aFlags = program.getAttribute("flags");
23201
- this._aFlags2 = program.getAttribute("flags2");
23202
-
23203
23248
  if (scene.logarithmicDepthBufferEnabled) {
23204
23249
  this._uLogDepthBufFC = program.getLocation("logDepthBufFC");
23205
23250
  }
23251
+
23252
+ this._uTexturePerObjectIdPositionsDecodeMatrix = "uTexturePerObjectIdPositionsDecodeMatrix"; // chipmunk
23253
+ this._uTexturePerObjectIdColorsAndFlags = "uTexturePerObjectIdColorsAndFlags"; // chipmunk
23254
+ this._uTexturePerVertexIdCoordinates = "uTexturePerVertexIdCoordinates"; // chipmunk
23255
+ this._uTexturePerPolygonIdNormals = "uTexturePerPolygonIdNormals"; // chipmunk
23256
+ this._uTexturePerPolygonIdIndices = "uTexturePerPolygonIdIndices"; // chipmunk
23257
+ this._uTexturePerPolygonIdPortionIds = "uTexturePerPolygonIdPortionIds"; // chipmunk
23258
+ this._uTextureCameraMatrices = "uTextureCameraMatrices"; // chipmunk
23259
+ this._uTextureModelMatrices = "uTextureModelMatrices"; // chipmunk
23260
+ this._uTexturePerObjectIdOffsets = "uTexturePerObjectIdOffsets"; // chipmunk
23261
+ this._uCameraEyeRtc = program.getLocation("uCameraEyeRtc"); // chipmunk
23206
23262
  }
23207
23263
 
23208
- _bindProgram() {
23264
+ _bindProgram(frameCtx) {
23209
23265
 
23210
23266
  const scene = this._scene;
23211
23267
  const gl = scene.canvas.gl;
23268
+ const program = this._program;
23212
23269
  const project = scene.camera.project;
23213
23270
 
23214
- this._program.bind();
23271
+ program.bind();
23215
23272
 
23216
23273
  gl.uniformMatrix4fv(this._uProjMatrix, false, project.matrix);
23217
23274
 
@@ -23230,131 +23287,241 @@ class TrianglesDataTextureDepthRenderer {
23230
23287
 
23231
23288
  _buildVertexShader() {
23232
23289
  const scene = this._scene;
23233
- const clipping = scene._sectionPlanesState.sectionPlanes.length > 0;
23290
+ const sectionPlanesState = scene._sectionPlanesState;
23291
+ scene._lightsState;
23292
+ const clipping = sectionPlanesState.sectionPlanes.length > 0;
23234
23293
  const src = [];
23235
- src.push("// Triangles dataTexture depth vertex shader");
23236
- if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) {
23237
- src.push("#extension GL_EXT_frag_depth : enable");
23238
- }
23294
+ src.push("#version 300 es");
23295
+ src.push("// Triangles dataTexture draw vertex shader");
23296
+
23297
+ src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH");
23298
+ src.push("precision highp float;");
23299
+ src.push("precision highp int;");
23300
+ src.push("precision highp usampler2D;");
23301
+ src.push("precision highp isampler2D;");
23302
+ src.push("precision highp sampler2D;");
23303
+ src.push("#else");
23304
+ src.push("precision mediump float;");
23305
+ src.push("precision mediump int;");
23306
+ src.push("precision mediump usampler2D;");
23307
+ src.push("precision mediump isampler2D;");
23308
+ src.push("precision mediump sampler2D;");
23309
+ src.push("#endif");
23310
+
23239
23311
  src.push("uniform int renderPass;");
23240
- src.push("attribute vec3 position;");
23312
+
23241
23313
  if (scene.entityOffsetsEnabled) {
23242
- src.push("attribute vec3 offset;");
23314
+ src.push("in vec3 offset;");
23243
23315
  }
23244
- src.push("attribute vec4 flags;");
23245
- src.push("attribute vec4 flags2;");
23246
- src.push("uniform mat4 worldMatrix;");
23247
- src.push("uniform mat4 viewMatrix;");
23248
- src.push("uniform mat4 projMatrix;");
23249
- src.push("uniform mat4 positionsDecodeMatrix;");
23316
+
23317
+ // src.push("uniform sampler2D uOcclusionTexture;"); // chipmunk
23318
+
23319
+ src.push("uniform highp sampler2D uTexturePerObjectIdPositionsDecodeMatrix;"); // chipmunk
23320
+ src.push("uniform lowp usampler2D uTexturePerObjectIdColorsAndFlags;"); // chipmunk
23321
+ src.push("uniform highp sampler2D uTexturePerObjectIdOffsets;"); // chipmunk
23322
+ src.push("uniform mediump usampler2D uTexturePerVertexIdCoordinates;"); // chipmunk
23323
+ src.push("uniform highp usampler2D uTexturePerPolygonIdIndices;"); // chipmunk
23324
+ src.push("uniform mediump usampler2D uTexturePerPolygonIdPortionIds;"); // chipmunk
23325
+ src.push("uniform highp sampler2D uTextureCameraMatrices;"); // chipmunk
23326
+ src.push("uniform highp sampler2D uTextureModelMatrices;"); // chipmunk
23327
+ src.push("uniform vec3 uCameraEyeRtc;"); // chipmunk
23328
+
23329
+ src.push("vec3 positions[3];");
23330
+
23250
23331
  if (scene.logarithmicDepthBufferEnabled) {
23251
23332
  src.push("uniform float logDepthBufFC;");
23252
- if (WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) {
23253
- src.push("varying float vFragDepth;");
23254
- }
23255
- src.push("bool isPerspectiveMatrix(mat4 m) {");
23256
- src.push(" return (m[2][3] == - 1.0);");
23257
- src.push("}");
23258
- src.push("varying float isPerspective;");
23333
+ src.push("out float vFragDepth;");
23334
+ src.push("out float isPerspective;");
23259
23335
  }
23336
+
23337
+ src.push("bool isPerspectiveMatrix(mat4 m) {");
23338
+ src.push(" return (m[2][3] == - 1.0);");
23339
+ src.push("}");
23340
+
23341
+ src.push("out highp vec2 vHighPrecisionZW;");
23342
+
23260
23343
  if (clipping) {
23261
- src.push("varying vec4 vWorldPosition;");
23262
- src.push("varying vec4 vFlags2;");
23344
+ src.push("out vec4 vWorldPosition;");
23345
+ src.push("flat out uint vFlags2;");
23263
23346
  }
23264
- src.push("varying vec2 vHighPrecisionZW;");
23347
+
23265
23348
  src.push("void main(void) {");
23266
23349
 
23350
+ // camera matrices
23351
+ src.push ("mat4 viewMatrix = mat4 (texelFetch (uTextureCameraMatrices, ivec2(0, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(1, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(2, 0), 0), texelFetch (uTextureCameraMatrices, ivec2(3, 0), 0));");
23352
+ src.push ("mat4 viewNormalMatrix = mat4 (texelFetch (uTextureCameraMatrices, ivec2(0, 1), 0), texelFetch (uTextureCameraMatrices, ivec2(1, 1), 0), texelFetch (uTextureCameraMatrices, ivec2(2, 1), 0), texelFetch (uTextureCameraMatrices, ivec2(3, 1), 0));");
23353
+ src.push ("mat4 projMatrix = mat4 (texelFetch (uTextureCameraMatrices, ivec2(0, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(1, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(2, 2), 0), texelFetch (uTextureCameraMatrices, ivec2(3, 2), 0));");
23354
+
23355
+ // model matrices
23356
+ src.push ("mat4 worldMatrix = mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 0), 0), texelFetch (uTextureModelMatrices, ivec2(1, 0), 0), texelFetch (uTextureModelMatrices, ivec2(2, 0), 0), texelFetch (uTextureModelMatrices, ivec2(3, 0), 0));");
23357
+ src.push ("mat4 worldNormalMatrix = mat4 (texelFetch (uTextureModelMatrices, ivec2(0, 1), 0), texelFetch (uTextureModelMatrices, ivec2(1, 1), 0), texelFetch (uTextureModelMatrices, ivec2(2, 1), 0), texelFetch (uTextureModelMatrices, ivec2(3, 1), 0));");
23358
+
23359
+ // constants
23360
+ src.push("int polygonIndex = gl_VertexID / 3;");
23361
+
23362
+ // get packed object-id
23363
+ src.push("int h_packed_object_id_index = (polygonIndex >> 3) & 4095;");
23364
+ src.push("int v_packed_object_id_index = (polygonIndex >> 3) >> 12;");
23365
+
23366
+ src.push("int objectIndex = int(texelFetch(uTexturePerPolygonIdPortionIds, ivec2(h_packed_object_id_index, v_packed_object_id_index), 0).r);");
23367
+ src.push("ivec2 objectIndexCoords = ivec2(objectIndex % 512, objectIndex / 512);");
23368
+
23369
+ // get flags & flags2
23370
+ src.push("uvec4 flags = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0);"); // chipmunk
23371
+ src.push("uvec4 flags2 = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0);"); // chipmunk
23372
+
23267
23373
  // flags.x = NOT_RENDERED | COLOR_OPAQUE | COLOR_TRANSPARENT
23268
23374
  // renderPass = COLOR_OPAQUE
23269
23375
 
23270
- src.push(`if (int(flags.x) != renderPass) {`);
23271
- src.push(" gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"); // Cull vertex
23272
- src.push(" } else {");
23273
- src.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); ");
23274
- if (scene.entityOffsetsEnabled) {
23275
- src.push(" worldPosition.xyz = worldPosition.xyz + offset;");
23276
- }
23277
- src.push(" vec4 viewPosition = viewMatrix * worldPosition; ");
23278
- if (clipping) {
23279
- src.push(" vWorldPosition = worldPosition;");
23280
- src.push(" vFlags2 = flags2;");
23281
- }
23376
+ // src.push(`if (int(flags.x) != renderPass) {`);
23377
+ // src.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"); // Cull vertex
23378
+ // src.push(" return;"); // Cull vertex
23379
+ // src.push("} else {");
23380
+ src.push("{");
23381
+
23382
+ // get vertex base
23383
+ src.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));"); // chipmunk
23384
+
23385
+ src.push("ivec4 packedIndexBaseOffset = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0));"); // chipmunk
23386
+
23387
+ src.push("int indexBaseOffset = (packedIndexBaseOffset.r << 24) + (packedIndexBaseOffset.g << 16) + (packedIndexBaseOffset.b << 8) + packedIndexBaseOffset.a;");
23388
+
23389
+ src.push("int h_index = (polygonIndex - indexBaseOffset) & 4095;");
23390
+ src.push("int v_index = (polygonIndex - indexBaseOffset) >> 12;");
23391
+
23392
+ src.push("ivec3 vertexIndices = ivec3(texelFetch(uTexturePerPolygonIdIndices, ivec2(h_index, v_index), 0));");
23393
+ src.push("ivec3 uniqueVertexIndexes = vertexIndices + (packedVertexBase.r << 24) + (packedVertexBase.g << 16) + (packedVertexBase.b << 8) + packedVertexBase.a;");
23394
+
23395
+ src.push("ivec3 indexPositionH = uniqueVertexIndexes & 4095;");
23396
+ src.push("ivec3 indexPositionV = uniqueVertexIndexes >> 12;");
23397
+
23398
+ src.push("mat4 positionsDecodeMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*8+1, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*8+2, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*8+3, objectIndexCoords.y), 0));");
23399
+ src.push("mat4 entityMatrix = mat4 (texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*8+5, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*8+6, objectIndexCoords.y), 0), texelFetch (uTexturePerObjectIdPositionsDecodeMatrix, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0));");
23400
+
23401
+ src.push("positionsDecodeMatrix = entityMatrix * positionsDecodeMatrix;");
23402
+
23403
+ src.push("uint solid = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+7, objectIndexCoords.y), 0).r;"); // chipmunk
23404
+
23405
+ // get position
23406
+ src.push("positions[0] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.r, indexPositionV.r), 0));");
23407
+ src.push("positions[1] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.g, indexPositionV.g), 0));");
23408
+ src.push("positions[2] = vec3(texelFetch(uTexturePerVertexIdCoordinates, ivec2(indexPositionH.b, indexPositionV.b), 0));");
23409
+
23410
+ // get color
23411
+ src.push("uvec4 color = texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+0, objectIndexCoords.y), 0);"); // chipmunk
23412
+
23413
+ src.push(`if (color.a == 0u) {`);
23414
+ src.push(" gl_Position = vec4(3.0, 3.0, 3.0, 1.0);"); // Cull vertex
23415
+ src.push(" return;");
23416
+ src.push("};");
23417
+
23418
+ // get normal
23419
+ src.push("vec3 normal = normalize(cross(positions[2] - positions[0], positions[1] - positions[0]));");
23420
+
23421
+ src.push("vec3 position;");
23422
+ src.push("position = positions[gl_VertexID % 3];");
23423
+
23424
+ src.push("vec3 viewNormal = -normalize((transpose(inverse(viewMatrix*positionsDecodeMatrix)) * vec4(normal,1)).xyz);");
23425
+
23426
+ // when the geometry is not solid, if needed, flip the triangle winding
23427
+ src.push("if (solid != 1u) {");
23428
+ src.push("if (isPerspectiveMatrix(projMatrix)) {");
23429
+ src.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;");
23430
+ src.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {");
23431
+ src.push("position = positions[2 - (gl_VertexID % 3)];");
23432
+ src.push("viewNormal = -viewNormal;");
23433
+ src.push("}");
23434
+ src.push("} else {");
23435
+ src.push("if (viewNormal.z < 0.0) {");
23436
+ src.push("position = positions[2 - (gl_VertexID % 3)];");
23437
+ src.push("viewNormal = -viewNormal;");
23438
+ src.push("}");
23439
+ src.push("}");
23440
+ src.push("}");
23441
+
23442
+ src.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); ");
23443
+
23444
+ // get XYZ offset
23445
+ src.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);");
23446
+
23447
+ src.push("worldPosition.xyz = worldPosition.xyz + offset.xyz;");
23448
+
23449
+ src.push("vec4 viewPosition = viewMatrix * worldPosition; ");
23450
+
23282
23451
  src.push("vec4 clipPos = projMatrix * viewPosition;");
23283
23452
  if (scene.logarithmicDepthBufferEnabled) {
23284
- if (WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) {
23285
- src.push("vFragDepth = 1.0 + clipPos.w;");
23286
- } else {
23287
- src.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;");
23288
- src.push("clipPos.z *= clipPos.w;");
23289
- }
23453
+ src.push("vFragDepth = 1.0 + clipPos.w;");
23290
23454
  src.push("isPerspective = float (isPerspectiveMatrix(projMatrix));");
23291
23455
  }
23456
+ if (clipping) {
23457
+ src.push("vWorldPosition = worldPosition;");
23458
+ src.push("vFlags2 = flags2.r;");
23459
+ }
23292
23460
  src.push("gl_Position = clipPos;");
23293
23461
  src.push("vHighPrecisionZW = gl_Position.zw;");
23294
- src.push(" }");
23295
23462
  src.push("}");
23463
+
23464
+ src.push("}");
23465
+
23296
23466
  return src;
23297
23467
  }
23298
23468
 
23299
23469
  _buildFragmentShader() {
23300
23470
  const scene = this._scene;
23301
23471
  const sectionPlanesState = scene._sectionPlanesState;
23302
- const clipping = (sectionPlanesState.sectionPlanes.length > 0);
23472
+ const clipping = sectionPlanesState.sectionPlanes.length > 0;
23303
23473
  const src = [];
23304
- src.push("// Triangles dataTexture depth fragment shader");
23305
- if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) {
23306
- src.push("#extension GL_EXT_frag_depth : enable");
23307
- }
23474
+ src.push ('#version 300 es');
23475
+ src.push("// Triangles dataTexture draw fragment shader");
23476
+ src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH");
23308
23477
  src.push("precision highp float;");
23309
23478
  src.push("precision highp int;");
23310
- if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) {
23311
- src.push("varying float isPerspective;");
23479
+ src.push("#else");
23480
+ src.push("precision mediump float;");
23481
+ src.push("precision mediump int;");
23482
+ src.push("#endif");
23483
+
23484
+ src.push("in highp vec2 vHighPrecisionZW;");
23485
+ src.push("out vec4 outColor;");
23486
+
23487
+ if (scene.logarithmicDepthBufferEnabled) {
23488
+ src.push("in float isPerspective;");
23312
23489
  src.push("uniform float logDepthBufFC;");
23313
- src.push("varying float vFragDepth;");
23490
+ src.push("in float vFragDepth;");
23314
23491
  }
23315
23492
  if (clipping) {
23316
- src.push("varying vec4 vWorldPosition;");
23317
- src.push("varying vec4 vFlags2;");
23318
- for (let i = 0; i < sectionPlanesState.sectionPlanes.length; i++) {
23493
+ src.push("in vec4 vWorldPosition;");
23494
+ src.push("flat in uint vFlags2;");
23495
+ for (let i = 0, len = sectionPlanesState.sectionPlanes.length; i < len; i++) {
23319
23496
  src.push("uniform bool sectionPlaneActive" + i + ";");
23320
23497
  src.push("uniform vec3 sectionPlanePos" + i + ";");
23321
23498
  src.push("uniform vec3 sectionPlaneDir" + i + ";");
23322
23499
  }
23323
23500
  }
23324
- src.push("const float packUpScale = 256. / 255.;");
23325
- src.push("const float unpackDownscale = 255. / 256.;");
23326
- src.push("const vec3 packFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );");
23327
- src.push("const vec4 unpackFactors = unpackDownscale / vec4( packFactors, 1. );");
23328
- src.push("const float shiftRight8 = 1.0 / 256.;");
23329
-
23330
- src.push("vec4 packDepthToRGBA( const in float v ) {");
23331
- src.push(" vec4 r = vec4( fract( v * packFactors ), v );");
23332
- src.push(" r.yzw -= r.xyz * shiftRight8;");
23333
- src.push(" return r * packUpScale;");
23334
- src.push("}");
23335
- src.push("varying vec2 vHighPrecisionZW;");
23336
23501
  src.push("void main(void) {");
23502
+
23337
23503
  if (clipping) {
23338
- src.push(" bool clippable = (float(vFlags2.x) > 0.0);");
23504
+ src.push(" bool clippable = vFlags2 > 0u;");
23339
23505
  src.push(" if (clippable) {");
23340
- src.push(" float dist = 0.0;");
23341
- for (var i = 0; i < sectionPlanesState.sectionPlanes.length; i++) {
23342
- src.push(" if (sectionPlaneActive" + i + ") {");
23343
- src.push(" dist += clamp(dot(-sectionPlaneDir" + i + ".xyz, vWorldPosition.xyz - sectionPlanePos" + i + ".xyz), 0.0, 1000.0);");
23344
- src.push(" }");
23506
+ src.push(" float dist = 0.0;");
23507
+ for (let i = 0, len = sectionPlanesState.sectionPlanes.length; i < len; i++) {
23508
+ src.push("if (sectionPlaneActive" + i + ") {");
23509
+ src.push(" dist += clamp(dot(-sectionPlaneDir" + i + ".xyz, vWorldPosition.xyz - sectionPlanePos" + i + ".xyz), 0.0, 1000.0);");
23510
+ src.push("}");
23345
23511
  }
23346
- src.push(" if (dist > 0.0) { discard; }");
23347
- src.push(" }");
23512
+ src.push(" if (dist > 0.0) { ");
23513
+ src.push(" discard;");
23514
+ src.push(" }");
23515
+ src.push("}");
23348
23516
  }
23349
- if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) {
23350
- src.push(" gl_FragDepthEXT = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;");
23517
+
23518
+ if (scene.logarithmicDepthBufferEnabled) {
23519
+ // src.push(" gl_FragDepth = isPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;");
23520
+ src.push(" gl_FragDepth = log2( vFragDepth ) * logDepthBufFC * 0.5;");
23351
23521
  }
23352
23522
  src.push("float fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;");
23353
- if (WEBGL_INFO.SUPPORTED_EXTENSIONS["WEBGL_depth_texture"]) {
23354
- src.push(" gl_FragColor = vec4(vec3(1.0 - fragCoordZ), 1.0); ");
23355
- } else {
23356
- src.push(" gl_FragColor = packDepthToRGBA(fragCoordZ); "); // Must be linear depth
23357
- }
23523
+ src.push(" outColor = vec4(vec3(1.0 - fragCoordZ), 1.0); ");
23524
+ // src.push(" outColor = vec4(1.0, 1.0, 1.0, 1.0); ");
23358
23525
  src.push("}");
23359
23526
  return src;
23360
23527
  }
@@ -23368,7 +23535,6 @@ class TrianglesDataTextureDepthRenderer {
23368
23535
  this._program.destroy();
23369
23536
  }
23370
23537
  this._program = null;
23371
- stats.memory.programs--;
23372
23538
  }
23373
23539
  }
23374
23540
 
@@ -29214,40 +29380,49 @@ class TrianglesDataTextureLayer {
29214
29380
  *
29215
29381
  * @returns {object} The mesh information enrichened with `.preparedBuckets` key.
29216
29382
  */
29217
- function prepareMeshGeometry (geometryCfg) {
29383
+ function prepareMeshGeometry (geometryCfg, enableVertexWelding, enableIndexRebucketing) {
29218
29384
  let uniquePositions, uniqueIndices, uniqueEdgeIndices;
29219
29385
 
29220
- [
29221
- uniquePositions,
29222
- uniqueIndices,
29223
- uniqueEdgeIndices,
29224
- ] = uniquifyPositions ({
29225
- positions: geometryCfg.positions,
29226
- indices: geometryCfg.indices,
29227
- edgeIndices: geometryCfg.edgeIndices
29228
- });
29386
+ if (enableVertexWelding) {
29387
+ [
29388
+ uniquePositions,
29389
+ uniqueIndices,
29390
+ uniqueEdgeIndices,
29391
+ ] = uniquifyPositions ({
29392
+ positions: geometryCfg.positions,
29393
+ indices: geometryCfg.indices,
29394
+ edgeIndices: geometryCfg.edgeIndices
29395
+ });
29396
+ } else {
29397
+ uniquePositions = geometryCfg.positions;
29398
+ uniqueIndices = geometryCfg.indices;
29399
+ uniqueEdgeIndices = geometryCfg.edgeIndices;
29400
+ }
29229
29401
 
29230
- let numUniquePositions = uniquePositions.length / 3;
29402
+ let buckets;
29231
29403
 
29232
- let buckets = rebucketPositions (
29233
- {
29404
+ if (enableIndexRebucketing) {
29405
+ let numUniquePositions = uniquePositions.length / 3;
29406
+
29407
+ buckets = rebucketPositions (
29408
+ {
29409
+ positions: uniquePositions,
29410
+ indices: uniqueIndices,
29411
+ edgeIndices: uniqueEdgeIndices,
29412
+ },
29413
+ (numUniquePositions > (1<< 16)) ? 16 : 8,
29414
+ // true
29415
+ );
29416
+ } else {
29417
+ buckets = [{
29234
29418
  positions: uniquePositions,
29235
29419
  indices: uniqueIndices,
29236
29420
  edgeIndices: uniqueEdgeIndices,
29237
- },
29238
- (numUniquePositions > (1<< 16)) ? 16 : 8,
29239
- // true
29240
- );
29421
+ }];
29422
+ }
29241
29423
 
29242
29424
  geometryCfg.preparedBuckets = buckets;
29243
29425
 
29244
- // chipmunk
29245
- // geometryCfg.preparedBuckets = [{
29246
- // positions: uniquePositions,
29247
- // indices: uniqueIndices,
29248
- // edgeIndices: uniqueEdgeIndices,
29249
- // }];
29250
-
29251
29426
  return geometryCfg;
29252
29427
  }
29253
29428
 
@@ -31475,7 +31650,7 @@ const VISIBILITY_CHECK_ENVOLVES_V = (1 << 14);
31475
31650
  this._camera = model.scene.camera;
31476
31651
  }
31477
31652
 
31478
- this._ensureFrustumPropsUpdated ();
31653
+ this._ensureFrustumPropsUpdated (model);
31479
31654
 
31480
31655
  this._initializeCullingDataIfNeeded (model);
31481
31656
 
@@ -31654,9 +31829,11 @@ const VISIBILITY_CHECK_ENVOLVES_V = (1 << 14);
31654
31829
  }
31655
31830
 
31656
31831
  /**
31832
+ * @param {DataTextureSceneModel} model
31833
+ *
31657
31834
  * @private
31658
31835
  */
31659
- _ensureFrustumPropsUpdated ()
31836
+ _ensureFrustumPropsUpdated (model)
31660
31837
  {
31661
31838
  // Assuming "min" for fovAxis
31662
31839
  const min = Math.min (
@@ -31685,10 +31862,33 @@ const VISIBILITY_CHECK_ENVOLVES_V = (1 << 14);
31685
31862
  // return;
31686
31863
  // }
31687
31864
 
31865
+ // Adjust camera eye/look to take into account the `model.worldMatrix`:
31866
+ // - the entities' AABBs don't take it into account
31867
+ // - and they can't, since `model.worldMatrix` is dynamic
31868
+ // So, instead of transformating the positions of the r*tree's AABBs,
31869
+ // apply the inverse transform to the camera eye/look, since the culling
31870
+ // result is equivalent.
31871
+ const invWorldMatrix = math.inverseMat4(
31872
+ model.worldMatrix,
31873
+ math.mat4()
31874
+ );
31875
+
31876
+ const modelCamEye = math.transformVec3(
31877
+ this._camera.eye,
31878
+ invWorldMatrix,
31879
+ [ 0, 0, 0 ]
31880
+ );
31881
+
31882
+ const modelCamLook = math.transformVec3(
31883
+ this._camera.look,
31884
+ invWorldMatrix,
31885
+ [ 0, 0, 0 ]
31886
+ );
31887
+
31688
31888
  this._frustumProps.forward = math.normalizeVec3 (
31689
31889
  math.subVec3 (
31690
- this._camera.look,
31691
- this._camera.eye,
31890
+ modelCamLook,
31891
+ modelCamEye,
31692
31892
  [ 0, 0, 0]
31693
31893
  ),
31694
31894
  [ 0, 0, 0]
@@ -31708,7 +31908,7 @@ const VISIBILITY_CHECK_ENVOLVES_V = (1 << 14);
31708
31908
  [ 0, 0, 0 ]
31709
31909
  );
31710
31910
 
31711
- this._frustumProps.eye = this._camera.eye.slice ();
31911
+ this._frustumProps.eye = modelCamEye.slice ();
31712
31912
 
31713
31913
  this._frustumProps.CAM_FACTOR_1 = this._frustumProps.fov / 2 * this._frustumProps.wMultiply / _180_DIV_MATH_PI;
31714
31914
  this._frustumProps.CAM_FACTOR_2 = this._frustumProps.fov / 2 * this._frustumProps.hMultiply / _180_DIV_MATH_PI;
@@ -31979,6 +32179,8 @@ class DataTextureSceneModel extends Component {
31979
32179
  * @param {Boolean} [cfg.saoEnabled=true] Indicates if Scalable Ambient Obscurance (SAO) will apply to this dataTexturePerformanceModel. SAO is configured by the Scene's {@link SAO} component.
31980
32180
  * @param {Boolean} [cfg.pbrEnabled=false] Indicates if physically-based rendering (PBR) will apply to the dataTexturePerformanceModel. Only works when {@link Scene#pbrEnabled} is also ````true````.
31981
32181
  * @param {Number} [cfg.edgeThreshold=10] When xraying, highlighting, selecting or edging, this is the threshold angle between normals of adjacent triangles, below which their shared wireframe edge is not drawn.
32182
+ * @param {Boolean} [cfg.disableVertexWelding] Disable vertex welding when loading geometry into the GPU. Default is ```false```.
32183
+ * @param {Boolean} [cfg.disableIndexRebucketing] Disable index rebucketing when loading geometry into the GPU. Default is ```false```.
31982
32184
  */
31983
32185
  constructor(owner, cfg = {}) {
31984
32186
  super(owner, cfg);
@@ -31987,6 +32189,24 @@ class DataTextureSceneModel extends Component {
31987
32189
  throw "Using a DataTextureSceneModel requires the usage of webgl2";
31988
32190
  }
31989
32191
 
32192
+ /**
32193
+ * Enable welding vertices when loading geometry into the ```TrianglesDataTextureLayer```.
32194
+ *
32195
+ * The welding is applied per-geometry.
32196
+ *
32197
+ * @type {Boolean}
32198
+ */
32199
+ this._enableVertexWelding = !cfg.disableVertexWelding;
32200
+
32201
+ /**
32202
+ * Enable demotion of index bitness then loading geometry into the ```TrianglesDataTextureLayer```.
32203
+ *
32204
+ * The rebucketing is applied per-geometry.
32205
+ *
32206
+ * @type {Boolean}
32207
+ */
32208
+ this._enableIndexRebucketing = !cfg.disableIndexRebucketing;
32209
+
31990
32210
  this._targetLodFps = cfg.targetLodFps;
31991
32211
 
31992
32212
  if (cfg.enableViewFrustumCulling) {
@@ -33038,7 +33258,11 @@ class DataTextureSceneModel extends Component {
33038
33258
 
33039
33259
  geometryCfg.edgeIndices = edgeIndices;
33040
33260
 
33041
- preparedGeometryCfg = prepareMeshGeometry (geometryCfg);
33261
+ preparedGeometryCfg = prepareMeshGeometry (
33262
+ geometryCfg,
33263
+ this._enableVertexWelding,
33264
+ this._enableIndexRebucketing
33265
+ );
33042
33266
 
33043
33267
  if (instancing) {
33044
33268
  this._preparedInstancingGeometries[geometryId] = preparedGeometryCfg;
@@ -34136,7 +34360,7 @@ const Renderer = function (scene, options) {
34136
34360
 
34137
34361
  const drawable = drawableList[i];
34138
34362
 
34139
- if (drawable.culled === true || drawable.visible === false || !drawable.drawDepth) {
34363
+ if (drawable.culled === true || drawable.visible === false || !drawable.drawDepth || !drawable.saoEnabled) {
34140
34364
  continue;
34141
34365
  }
34142
34366
 
@@ -131487,6 +131711,8 @@ class XKTLoaderPlugin extends Plugin {
131487
131711
  origin: params.origin,
131488
131712
  targetLodFps: params.useDataTextures.targetLodFps || false,
131489
131713
  enableViewFrustumCulling: params.useDataTextures.enableViewFrustumCulling || false,
131714
+ disableVertexWelding: params.useDataTextures.disableVertexWelding || false,
131715
+ disableIndexRebucketing: params.useDataTextures.disableIndexRebucketing || false,
131490
131716
  }));
131491
131717
  } else {
131492
131718
  sceneModel = new VBOSceneModel(this.viewer.scene, utils.apply(params, {