Added voxel mesh wireframe debug view

This commit is contained in:
Lucas Dower 2022-03-19 17:41:17 +00:00
parent df2eff7e1f
commit b75de2c657
3 changed files with 41 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import { Vector3 } from './vector';
import { AttributeData, RenderBuffer } from './buffer'; import { AttributeData, RenderBuffer } from './buffer';
import { Bounds, RGB } from './util'; import { Bounds, RGB } from './util';
import { Mesh } from './mesh'; import { Mesh } from './mesh';
import { VoxelMesh } from './voxel_mesh';
export class GeometryTemplates { export class GeometryTemplates {
private static readonly _default_cube = twgl.primitives.createCubeVertices(1.0); private static readonly _default_cube = twgl.primitives.createCubeVertices(1.0);
@ -101,7 +102,14 @@ export class DebugGeometryTemplates {
}; };
} }
public static bounds(bounds: Bounds, colour: RGB, translate: Vector3 = new Vector3(0, 0, 0)): AttributeData { public static cube(centre: Vector3, size: number, colour: RGB): AttributeData {
const min = Vector3.subScalar(centre, size/2);
const max = Vector3.addScalar(centre, size/2);
const bounds = new Bounds(min, max);
return this.bounds(bounds, colour);
}
public static bounds(bounds: Bounds, colour: RGB): AttributeData {
return { return {
indices: new Uint32Array([ indices: new Uint32Array([
0, 1, 0, 1,
@ -119,14 +127,14 @@ export class DebugGeometryTemplates {
]), ]),
custom: { custom: {
position: [ position: [
bounds.min.x + translate.x, bounds.min.y + translate.y, bounds.min.z + translate.z, bounds.min.x, bounds.min.y, bounds.min.z,
bounds.max.x + translate.x, bounds.min.y + translate.y, bounds.min.z + translate.z, bounds.max.x, bounds.min.y, bounds.min.z,
bounds.max.x + translate.x, bounds.min.y + translate.y, bounds.max.z + translate.z, bounds.max.x, bounds.min.y, bounds.max.z,
bounds.min.x + translate.x, bounds.min.y + translate.y, bounds.max.z + translate.z, bounds.min.x, bounds.min.y, bounds.max.z,
bounds.min.x + translate.x, bounds.max.y + translate.y, bounds.min.z + translate.z, bounds.min.x, bounds.max.y, bounds.min.z,
bounds.max.x + translate.x, bounds.max.y + translate.y, bounds.min.z + translate.z, bounds.max.x, bounds.max.y, bounds.min.z,
bounds.max.x + translate.x, bounds.max.y + translate.y, bounds.max.z + translate.z, bounds.max.x, bounds.max.y, bounds.max.z,
bounds.min.x + translate.x, bounds.max.y + translate.y, bounds.max.z + translate.z, bounds.min.x, bounds.max.y, bounds.max.z,
], ],
colour: [ colour: [
colour.r, colour.g, colour.b, colour.r, colour.g, colour.b,
@ -318,6 +326,28 @@ export class DebugGeometryTemplates {
return buffer; return buffer;
} }
public static voxelMeshWireframe(voxelMesh: VoxelMesh, colour: RGB): RenderBuffer {
const buffer = new RenderBuffer([
{ name: 'position', numComponents: 3 },
{ name: 'colour', numComponents: 3 },
]);
const dimensions = voxelMesh.getBounds().getDimensions();
const gridOffset = new Vector3(
dimensions.x % 2 === 0 ? 0.5 : 0,
dimensions.y % 2 === 0 ? 0.5 : 0,
dimensions.z % 2 === 0 ? 0.5 : 0,
);
const voxelSize = voxelMesh.getVoxelSize();
for (const voxel of voxelMesh.getVoxels()) {
buffer.add(DebugGeometryTemplates.cube(
Vector3.mulScalar(Vector3.add(voxel.position, gridOffset), voxelSize), voxelSize, colour,
));
}
return buffer;
}
public static meshNormals(mesh: Mesh, colour: RGB): RenderBuffer { public static meshNormals(mesh: Mesh, colour: RGB): RenderBuffer {
const buffer = new RenderBuffer([ const buffer = new RenderBuffer([
{ name: 'position', numComponents: 3 }, { name: 'position', numComponents: 3 },

View File

@ -197,6 +197,7 @@ export class Renderer {
); );
this._debugBuffers[MeshType.VoxelMesh][EDebugBufferComponents.Grid] = DebugGeometryTemplates.grid(true, true, voxelMesh.getVoxelSize()); this._debugBuffers[MeshType.VoxelMesh][EDebugBufferComponents.Grid] = DebugGeometryTemplates.grid(true, true, voxelMesh.getVoxelSize());
this._debugBuffers[MeshType.VoxelMesh][EDebugBufferComponents.Wireframe] = DebugGeometryTemplates.voxelMeshWireframe(voxelMesh, new RGB(0.18, 0.52, 0.89));
this._modelsAvailable = 2; this._modelsAvailable = 2;
this.setModelToUse(MeshType.VoxelMesh); this.setModelToUse(MeshType.VoxelMesh);

View File

@ -196,7 +196,7 @@ export class UI {
return isEnabled; return isEnabled;
}, EAppEvent.onModelActiveChanged, (...args: any[]) => { }, EAppEvent.onModelActiveChanged, (...args: any[]) => {
const modelUsed = args[0][0][0] as MeshType; const modelUsed = args[0][0][0] as MeshType;
return modelUsed === MeshType.TriangleMesh; return modelUsed === MeshType.TriangleMesh || modelUsed === MeshType.VoxelMesh;
}), }),
'normals': new ToolbarItemElement('normal', () => { 'normals': new ToolbarItemElement('normal', () => {
Renderer.Get.toggleIsNormalsEnabled(); Renderer.Get.toggleIsNormalsEnabled();