Improved handling of malformed meshes

This commit is contained in:
Lucas Dower 2022-05-14 20:22:38 +01:00
parent affdf247a9
commit 20c92e9afd
2 changed files with 10 additions and 3 deletions

View File

@ -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);
}
}

View File

@ -90,7 +90,10 @@ export class BVHRayVoxeliser extends IVoxeliser {
position,
voxelMeshParams.textureFiltering,
);
voxelMesh.addVoxel(position, voxelColour);
if (voxelColour) {
voxelMesh.addVoxel(position, voxelColour);
}
}
}