From 20c92e9afd374132268028e709688dc286f9d7df Mon Sep 17 00:00:00 2001 From: Lucas Dower Date: Sat, 14 May 2022 20:22:38 +0100 Subject: [PATCH] Improved handling of malformed meshes --- src/voxelisers/base-voxeliser.ts | 8 ++++++-- src/voxelisers/bvh-ray-voxeliser.ts | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/voxelisers/base-voxeliser.ts b/src/voxelisers/base-voxeliser.ts index c7826e8..c82c366 100644 --- a/src/voxelisers/base-voxeliser.ts +++ b/src/voxelisers/base-voxeliser.ts @@ -1,5 +1,5 @@ import { UVTriangle, Triangle } from '../triangle'; -import { RGB, UV } from '../util'; +import { ASSERT, RGB, UV } from '../util'; import { Vector3 } from '../vector'; import { Mesh } from '../mesh'; import { VoxelMesh, VoxelMeshParams} from '../voxel_mesh'; @@ -20,7 +20,7 @@ export abstract class IVoxeliser { protected abstract _voxelise(mesh: Mesh, voxelMeshParams: VoxelMeshParams): VoxelMesh; - protected _getVoxelColour(mesh: Mesh, triangle: UVTriangle, materialName: string, location: Vector3, filtering: TextureFiltering): RGB { + protected _getVoxelColour(mesh: Mesh, triangle: UVTriangle, materialName: string, location: Vector3, filtering: TextureFiltering): (RGB | undefined) { const area01 = new Triangle(triangle.v0, triangle.v1, location).getArea(); const area12 = new Triangle(triangle.v1, triangle.v2, location).getArea(); const area20 = new Triangle(triangle.v2, triangle.v0, location).getArea(); @@ -35,6 +35,10 @@ export abstract class IVoxeliser { triangle.uv0.v * w0 + triangle.uv1.v * w1 + triangle.uv2.v * w2, ); + if (isNaN(uv.u) || isNaN(uv.v)) { + return undefined; + } + return mesh.sampleMaterial(materialName, uv, filtering); } } diff --git a/src/voxelisers/bvh-ray-voxeliser.ts b/src/voxelisers/bvh-ray-voxeliser.ts index cf16927..9d3b7ce 100644 --- a/src/voxelisers/bvh-ray-voxeliser.ts +++ b/src/voxelisers/bvh-ray-voxeliser.ts @@ -90,7 +90,10 @@ export class BVHRayVoxeliser extends IVoxeliser { position, voxelMeshParams.textureFiltering, ); - voxelMesh.addVoxel(position, voxelColour); + + if (voxelColour) { + voxelMesh.addVoxel(position, voxelColour); + } } }