Skip to content

Commit

Permalink
chore(model): skip texture transform uniforms if unused
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Feb 2, 2024
1 parent 7e0b863 commit 14f5883
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
16 changes: 14 additions & 2 deletions src/lib/model/ModelMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class ModelMaterial extends THREE.RawShaderMaterial {

this.#textureWeightIndex = textureWeightIndex;
this.#textureTransformIndices = textureTransformIndices;
this.#textureTransforms = [new THREE.Matrix4(), new THREE.Matrix4()];

this.#blend = blend;
this.#materialParams = new THREE.Vector4(0.0, 0.0, 0.0, 0.0);
Expand Down Expand Up @@ -83,11 +82,12 @@ class ModelMaterial extends THREE.RawShaderMaterial {
this.uniforms = {
...uniforms,
textures: { value: textures },
textureTransforms: { value: this.#textureTransforms },
materialParams: { value: this.#materialParams },
diffuseColor: { value: this.#diffuseColor },
emissiveColor: { value: this.#emissiveColor },
};

this.#setupTextureTransforms();
}

get alpha() {
Expand Down Expand Up @@ -213,6 +213,18 @@ class ModelMaterial extends THREE.RawShaderMaterial {
this.uniformsNeedUpdate = true;
}

#setupTextureTransforms() {
if (this.#textureTransformIndices.length === 0) {
return;
}

this.#textureTransforms = [new THREE.Matrix4(), new THREE.Matrix4()];

this.uniforms.textureTransforms = { value: this.#textureTransforms };

this.defines['TEXTURE_TRANSFORMS'] = this.#textureTransforms.length;
}

#updateBlending() {
// Adjust OPAQUE and ALPHA_KEY blends if the material's alpha value is below 1.0
const isOpaque = this.#blend <= M2_MATERIAL_BLEND.BLEND_ALPHA_KEY && this.alpha >= 0.99998999;
Expand Down
42 changes: 37 additions & 5 deletions src/lib/model/shader/vertex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const VERTEX_SHADER_UNIFORMS = [
{ name: 'normalMatrix', type: 'mat3' },
{ name: 'projectionMatrix', type: 'mat4' },
{ name: 'cameraPosition', type: 'vec3' },
{ name: 'textureTransforms[2]', type: 'mat4' },
{ name: 'textureTransforms[2]', type: 'mat4', if: 'TEXTURE_TRANSFORMS' },
];

const VERTEX_SHADER_INPUTS = [
Expand Down Expand Up @@ -160,17 +160,49 @@ const createVertexShader = (texCoord1?: M2_TEXTURE_COORD, texCoord2?: M2_TEXTURE
const main = [];

if (texCoord1 === M2_TEXTURE_COORD.COORD_T1) {
main.push(`vTexCoord1 = (textureTransforms[0] * vec4(texCoord1, 0.0, 1.0)).xy;`);
main.push(
...[
`#ifdef TEXTURE_TRANSFORMS`,
` vTexCoord1 = (textureTransforms[0] * vec4(texCoord1, 0.0, 1.0)).xy;`,
`#else`,
` vTexCoord1 = texCoord1;`,
`#endif`,
],
);
} else if (texCoord1 === M2_TEXTURE_COORD.COORD_T2) {
main.push(`vTexCoord1 = (textureTransforms[0] * vec4(texCoord2, 0.0, 1.0)).xy;`);
main.push(
...[
`#ifdef TEXTURE_TRANSFORMS`,
` vTexCoord1 = (textureTransforms[0] * vec4(texCoord2, 0.0, 1.0)).xy;`,
`#else`,
` vTexCoord1 = texCoord1;`,
`#endif`,
],
);
} else if (texCoord1 === M2_TEXTURE_COORD.COORD_ENV) {
main.push(`vTexCoord1 = sphereMap(position, normal);`);
}

if (texCoord2 === M2_TEXTURE_COORD.COORD_T1) {
main.push(`vTexCoord2 = (textureTransforms[1] * vec4(texCoord1, 0.0, 1.0)).xy;`);
main.push(
...[
`#ifdef TEXTURE_TRANSFORMS`,
` vTexCoord2 = (textureTransforms[1] * vec4(texCoord1, 0.0, 1.0)).xy;`,
`#else`,
` vTexCoord2 = texCoord1;`,
`#endif`,
],
);
} else if (texCoord2 === M2_TEXTURE_COORD.COORD_T2) {
main.push(`vTexCoord2 = (textureTransforms[1] * vec4(texCoord2, 0.0, 1.0)).xy;`);
main.push(
...[
`#ifdef TEXTURE_TRANSFORMS`,
` vTexCoord2 = (textureTransforms[1] * vec4(texCoord2, 0.0, 1.0)).xy;`,
`#else`,
` vTexCoord2 = texCoord2;`,
`#endif`,
],
);
} else if (texCoord2 === M2_TEXTURE_COORD.COORD_ENV) {
main.push(`vTexCoord2 = sphereMap(position, normal);`);
}
Expand Down

0 comments on commit 14f5883

Please sign in to comment.