From f0e741be0508b7c71a062fb68d0fb8ba5b874db9 Mon Sep 17 00:00:00 2001 From: Lucas Dower Date: Mon, 23 Jan 2023 19:47:21 +0000 Subject: [PATCH] Fix for some textures appearing as black when using gl.REPEAT wrapping * For some reason, non-power-of-two size textures that use 'repeat' wrapping appear as black. Implemented a workaround for now that default sets a material's wrapping to 'clamp' where appropriate which is usually most materials --- src/mesh.ts | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/mesh.ts b/src/mesh.ts index 88167c2..171046a 100644 --- a/src/mesh.ts +++ b/src/mesh.ts @@ -9,7 +9,7 @@ import { Texture, TextureConverter, TTransparencyOptions } from './texture'; import { Triangle, UVTriangle } from './triangle'; import { getRandomID, UV } from './util'; import { AppError, ASSERT } from './util/error_util'; -import { LOG_WARN } from './util/log_util'; +import { LOG, LOG_WARN } from './util/log_util'; import { TTexelExtension, TTexelInterpolation } from './util/type_util'; import { Vector3 } from './vector'; @@ -264,6 +264,38 @@ export class Mesh { } } }); + + // Deduce default texture wrap mode for each material type + const materialsWithUVsOutOfBounds = new Set(); + this._tris.forEach((tri, triIndex) => { + if (materialsWithUVsOutOfBounds.has(tri.material)) { + // Already know this material has OOB UVs so skip + return; + } + + const uv = this.getUVs(triIndex); + const uvsOutOfBounds = + (uv.uv0.u < 0.0) || (uv.uv0.u > 1.0) || + (uv.uv0.v < 0.0) || (uv.uv0.v > 1.0) || + (uv.uv1.u < 0.0) || (uv.uv1.u > 1.0) || + (uv.uv1.v < 0.0) || (uv.uv1.v > 1.0) || + (uv.uv2.u < 0.0) || (uv.uv2.u > 1.0) || + (uv.uv2.v < 0.0) || (uv.uv2.v > 1.0); + + if (uvsOutOfBounds) { + materialsWithUVsOutOfBounds.add(tri.material); + } + }); + + LOG(`Materials with OOB UVs`, JSON.stringify(materialsWithUVsOutOfBounds)); + + this._materials.forEach((material, materialName) => { + if (material.type === MaterialType.textured) { + material.extension = materialsWithUVsOutOfBounds.has(materialName) ? + 'repeat' : + 'clamp'; + } + }); } private _centreMesh() {