Skip to content

Commit

Permalink
Remove unused data textures #1155
Browse files Browse the repository at this point in the history
  • Loading branch information
xeolabs committed Oct 12, 2023
1 parent f750043 commit 16a95d6
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 280 deletions.
182 changes: 0 additions & 182 deletions src/viewer/scene/model/dtx/triangles/DataTextureGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,188 +20,6 @@ export class DataTextureGenerator {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
}

/**
* Generate and return a `camera data texture`.
*
* The texture will automatically update its contents before each render when the camera matrix is dirty,
* and to do so will use the following events:
*
* - `scene.rendering` event will be used to know that the camera texture should be updated
* - `camera.matrix` event will be used to know that the camera matices changed
*
* @param {WebGL2RenderingContext} gl
* @param {Camera} camera
* @param {Scene} scene
* @param {null|number[3]} origin
* @returns {BindableDataTexture}
*/
generateCameraDataTexture(gl, camera, scene, origin) {
const textureWidth = 4;
const textureHeight = 3; // space for 3 matrices
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texStorage2D(gl.TEXTURE_2D, 1, gl.RGBA32F, textureWidth, textureHeight);
this.disableBindedTextureFiltering(gl);
gl.bindTexture(gl.TEXTURE_2D, null);
const cameraTexture = new BindableDataTexture(gl, texture, textureWidth, textureHeight);
let cameraDirty = true;
cameraTexture.updateViewMatrix = (viewMatrix, projMatrix) => {
gl.bindTexture(gl.TEXTURE_2D, cameraTexture._texture);
// Camera's "view matrix"
gl.texSubImage2D(
gl.TEXTURE_2D,
0,
0,
0, // 1st matrix: camera view matrix
4,
1,
gl.RGBA,
gl.FLOAT,
new Float32Array((origin) ? createRTCViewMat(viewMatrix, origin) : viewMatrix)
);

// Camera's "view normal matrix"
gl.texSubImage2D(
gl.TEXTURE_2D,
0,
0,
1, // 2nd matrix: camera view normal matrix
4,
1,
gl.RGBA,
gl.FLOAT,
new Float32Array(camera.viewNormalMatrix)
);

// Camera's "project matrix"
gl.texSubImage2D(
gl.TEXTURE_2D,
0,
0,
2, // 3rd matrix: camera project matrix
4,
1,
gl.RGBA,
gl.FLOAT,
new Float32Array(projMatrix)
);
};
const onCameraMatrix = () => {
if (!cameraDirty) {
return;
}
cameraDirty = false;
cameraTexture.updateViewMatrix(camera.viewMatrix, camera.project.matrix);
};
camera.on("matrix", () => cameraDirty = true);
scene.on("rendering", onCameraMatrix);
onCameraMatrix();
return cameraTexture;
}

/**
* Generate and return a texture containing camera view and projection
* matrices for picking, relative to the given RTC coordinate system origin.
*
* @param {WebGL2RenderingContext} gl
* @param {Camera} camera
* @param {null|number[3]} origin
* @returns {BindableDataTexture}
*/
generatePickCameraDataTexture(gl, camera, origin) {
const textureWidth = 4;
const textureHeight = 3; // space for 3 matrices
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texStorage2D(gl.TEXTURE_2D, 1, gl.RGBA32F, textureWidth, textureHeight);
this.disableBindedTextureFiltering(gl);
gl.bindTexture(gl.TEXTURE_2D, null);
const cameraTexture = new BindableDataTexture(gl, texture, textureWidth, textureHeight);
cameraTexture.updateViewMatrix = (viewMatrix, projMatrix) => {
gl.bindTexture(gl.TEXTURE_2D, cameraTexture._texture);
// Camera's "view matrix"
gl.texSubImage2D(
gl.TEXTURE_2D,
0,
0,
0, // 1st matrix: pick camera view matrix
4,
1,
gl.RGBA,
gl.FLOAT,
new Float32Array((origin) ? createRTCViewMat(viewMatrix, origin) : viewMatrix)
);

// Camera's "view normal matrix"
gl.texSubImage2D(
gl.TEXTURE_2D,
0,
0,
1, // 2nd matrix: pick camera view normal matrix
4,
1,
gl.RGBA,
gl.FLOAT,
new Float32Array(camera.viewNormalMatrix)
);

// Camera's "project matrix"
gl.texSubImage2D(
gl.TEXTURE_2D,
0,
0,
2, // 3rd matrix: pick camera project matrix
4,
1,
gl.RGBA,
gl.FLOAT,
new Float32Array(projMatrix)
);
};
return cameraTexture;
}

/**
* Generate and return a `model data texture`.
*
* @param {WebGL2RenderingContext} gl
* @param {DataTextureSceneModel} model
*
* @returns {BindableDataTexture}
*/
generateModelTexture(gl, model) {
const textureWidth = 4;
const textureHeight = 2; // space for 2 matrices
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texStorage2D(gl.TEXTURE_2D, 1, gl.RGBA32F, textureWidth, textureHeight);
gl.texSubImage2D(
gl.TEXTURE_2D,
0,
0, // x-offset
0, // y-offset (model world matrix)
4, // data width (4x4 values)
1, // data height (1 matrix)
gl.RGBA,
gl.FLOAT,
new Float32Array(model.worldMatrix)
);
gl.texSubImage2D(
gl.TEXTURE_2D,
0,
0, // x-offset
1, // y-offset (model normal matrix)
4, // data width (4x4 values)
1, // data height (1 matrix)
gl.RGBA,
gl.FLOAT,
new Float32Array(model.worldNormalMatrix)
);
this.disableBindedTextureFiltering(gl);
gl.bindTexture(gl.TEXTURE_2D, null);
return new BindableDataTexture(gl, texture, textureWidth, textureHeight);
}

/**
* This will generate an RGBA texture for:
* - colors
Expand Down
29 changes: 5 additions & 24 deletions src/viewer/scene/model/dtx/triangles/DataTextureState.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,32 +207,13 @@ export class DataTextureState {
3 // webgl texture unit
);

this.textureModelMatrices.bindTexture(
glProgram,
modelMatricesShaderName,
4 // webgl texture unit
);

this.texturePerObjectIdOffsets.bindTexture(
glProgram,
objectOffsetsShaderName,
5 // webgl texture unit
4 // webgl texture unit
);
}

/**
*
* @param {Program} glProgram
* @param {string} cameraMatricesShaderName
*/
bindPickCameraTexture(glProgram, cameraMatricesShaderName) {
// this.texturePickCameraMatrices.bindTexture(
// glProgram,
// cameraMatricesShaderName,
// 4 // webgl texture unit
// );
}

/**
*
* @param {Program} glProgram
Expand All @@ -249,13 +230,13 @@ export class DataTextureState {
this.indicesPortionIdsPerBitnessTextures[textureBitness].bindTexture(
glProgram,
portionIdsShaderName,
6 // webgl texture unit
5 // webgl texture unit
);

this.indicesPerBitnessTextures[textureBitness].bindTexture(
glProgram,
polygonIndicesShaderName,
7 // webgl texture unit
6 // webgl texture unit
);
}

Expand All @@ -275,13 +256,13 @@ export class DataTextureState {
this.edgeIndicesPortionIdsPerBitnessTextures[textureBitness].bindTexture(
glProgram,
edgePortionIdsShaderName,
6 // webgl texture unit
5 // webgl texture unit
);

this.edgeIndicesPerBitnessTextures[textureBitness].bindTexture(
glProgram,
edgeIndicesShaderName,
7 // webgl texture unit
6 // webgl texture unit
);
}
}
Expand Down
36 changes: 4 additions & 32 deletions src/viewer/scene/model/dtx/triangles/TrianglesDataTextureLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,34 +555,6 @@ export class TrianglesDataTextureLayer {
buffer.edgeIndices32Bits);
}

// if (buffer.metallicRoughness.length > 0) {
// const metallicRoughness = new Uint8Array(buffer.metallicRoughness);
// let normalized = false;
// state.metallicRoughnessBuf = new ArrayBuf(gl, gl.ARRAY_BUFFER, metallicRoughness, buffer.metallicRoughness.length, 2, gl.STATIC_DRAW, normalized);
// }

// Model matrices texture
if (!this.model._modelMatricesTexture) {
this.model._modelMatricesTexture = this._dataTextureGenerator.generateModelTexture(gl, this.model);
}

textureState.textureModelMatrices = this.model._modelMatricesTexture;

// Camera textures

textureState.cameraTexture = this._dataTextureGenerator.generateCameraDataTexture(
this.model.scene.canvas.gl,
this.model.scene.camera,
this.model.scene,
this._state.origin.slice());

textureState.textureCameraMatrices = textureState.cameraTexture;

textureState.texturePickCameraMatrices = this._dataTextureGenerator.generatePickCameraDataTexture(
this.model.scene.canvas.gl,
this.model.scene.camera,
this._state.origin.slice());

textureState.finalize();

// Free up memory
Expand Down Expand Up @@ -1254,10 +1226,10 @@ export class TrianglesDataTextureLayer {
//---- PICKING ----------------------------------------------------------------------------------------------------

setPickMatrices(pickViewMatrix, pickProjMatrix) {
if (this._numVisibleLayerPortions === 0) {
return;
}
this._dataTextureState.texturePickCameraMatrices.updateViewMatrix(pickViewMatrix, pickProjMatrix);
// if (this._numVisibleLayerPortions === 0) {
// return;
// }
// this._dataTextureState.texturePickCameraMatrices.updateViewMatrix(pickViewMatrix, pickProjMatrix);
}

drawPickMesh(renderFlags, frameCtx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,6 @@ export class TrianglesDataTextureColorRenderer {
src.push(" return;"); // Cull vertex
src.push("} else {");

// model matrices
src.push ("mat4 worldMatrix = sceneModelWorldMatrix * 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));");

// get vertex base
src.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));");

Expand Down Expand Up @@ -457,7 +454,7 @@ export class TrianglesDataTextureColorRenderer {
// when the geometry is not solid, if needed, flip the triangle winding
src.push("if (solid != 1u) {");
src.push("if (isPerspectiveMatrix(projMatrix)) {");
src.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;")
src.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(sceneModelWorldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;")
// src.push("vColor = vec4(vec3(1, -1, 0)*dot(normalize(position.xyz - uCameraEyeRtcInQuantizedSpace), normal), 1);")
src.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {");
src.push("position = positions[2 - (gl_VertexID % 3)];");
Expand All @@ -472,7 +469,7 @@ export class TrianglesDataTextureColorRenderer {
src.push("}");
src.push("}");

src.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); ");
src.push("vec4 worldPosition = sceneModelWorldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); ");

// get XYZ offset
src.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ export class TrianglesDataTextureDepthRenderer {
this._uRenderPass = program.getLocation("renderPass");
this._uPositionsDecodeMatrix = program.getLocation("positionsDecodeMatrix");
this._uSceneModelWorldMatrix = program.getLocation("sceneModelWorldMatrix");
this._uWorldMatrix = program.getLocation("worldMatrix");
this._uViewMatrix = program.getLocation("viewMatrix");
this._uProjMatrix = program.getLocation("projMatrix");
this._uSectionPlanes = [];
Expand Down Expand Up @@ -315,9 +314,6 @@ export class TrianglesDataTextureDepthRenderer {
src.push(" return;"); // Cull vertex
src.push("} else {");

// model matrices
src.push("mat4 worldMatrix = sceneModelWorldMatrix * 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));");

// get vertex base
src.push("ivec4 packedVertexBase = ivec4(texelFetch (uTexturePerObjectIdColorsAndFlags, ivec2(objectIndexCoords.x*8+4, objectIndexCoords.y), 0));");

Expand Down Expand Up @@ -362,7 +358,7 @@ export class TrianglesDataTextureDepthRenderer {
// when the geometry is not solid, if needed, flip the triangle winding
src.push("if (solid != 1u) {");
src.push("if (isPerspectiveMatrix(projMatrix)) {");
src.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(worldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;")
src.push("vec3 uCameraEyeRtcInQuantizedSpace = (inverse(sceneModelWorldMatrix * positionsDecodeMatrix) * vec4(uCameraEyeRtc, 1)).xyz;")
src.push("if (dot(position.xyz - uCameraEyeRtcInQuantizedSpace, normal) < 0.0) {");
src.push("position = positions[2 - (gl_VertexID % 3)];");
src.push("viewNormal = -viewNormal;");
Expand All @@ -375,7 +371,7 @@ export class TrianglesDataTextureDepthRenderer {
src.push("}");
src.push("}");

src.push("vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); ");
src.push("vec4 worldPosition = sceneModelWorldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); ");

// get XYZ offset
src.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,6 @@ export class TrianglesDataTextureEdgesColorRenderer {

src.push("void main(void) {");

// model matrices
src.push("mat4 worldMatrix = sceneModelWorldMatrix * 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));");

// constants
src.push("int edgeIndex = gl_VertexID / 2;")

Expand Down Expand Up @@ -333,7 +330,7 @@ export class TrianglesDataTextureEdgesColorRenderer {
src.push(" return;");
src.push("};");

src.push(" vec4 worldPosition = worldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); ");
src.push(" vec4 worldPosition = sceneModelWorldMatrix * (positionsDecodeMatrix * vec4(position, 1.0)); ");

// get XYZ offset
src.push("vec4 offset = vec4(texelFetch (uTexturePerObjectIdOffsets, objectIndexCoords, 0).rgb, 0.0);");
Expand Down
Loading

0 comments on commit 16a95d6

Please sign in to comment.