Fixed incorrect getTriangles, fixed _sampleNearest and _sampleLinear being swapped

This commit is contained in:
Lucas Dower 2023-10-13 23:40:11 +01:00
parent b7d6bce1a2
commit 803cc6c326
5 changed files with 85 additions and 11 deletions

View File

@ -158,7 +158,7 @@ export class OtS_Mesh {
ASSERT(sectionCount > 0); // TODO: Don't assert,
let sectionIndex = 0;
let triangleCount = this._sections[0].positionData.length / 3;
let triangleCount = this._sections[0].indexData.length / 3;
let triangleIndex = 0;
return {
@ -166,9 +166,10 @@ export class OtS_Mesh {
return this;
},
next: () => {
if (triangleIndex >= triangleCount && sectionIndex < sectionCount) {
if (triangleIndex >= triangleCount && sectionIndex < sectionCount - 1) {
++sectionIndex;
triangleCount = this._sections[sectionIndex].positionData.length / 3;
triangleCount = this._sections[sectionIndex].indexData.length / 3;
triangleIndex = 0;
}
if (triangleIndex < triangleCount) {

View File

@ -65,9 +65,9 @@ export class OtS_Texture {
}
// Assumes `u` and `v` are in the range [0, 1]
private _sampleNearest(u: number, v: number): RGBA {
const x = Math.floor(u * this._width - 1);
const y = Math.floor(v * this._height - 1);
private _sampleLinear(u: number, v: number): RGBA {
const x = Math.floor(u * (this._width - 1));
const y = Math.floor(v * (this._height - 1));
const left = Math.floor(x);
const right = left + 1;
@ -91,7 +91,7 @@ export class OtS_Texture {
}
// Assumes `u` and `v` are in the range [0, 1]
private _sampleLinear(u: number, v: number): RGBA {
private _sampleNearest(u: number, v: number): RGBA {
const x = Math.floor(u * (this._width - 1));
const y = Math.floor(v * (this._height - 1));

View File

@ -104,7 +104,6 @@ export class OtS_VoxelMesh_Converter {
const samples: RGBA[] = [];
for (let i = 0; i < 8; ++i) {
samples.push(this._getVoxelColour(
mesh,
triangle,
Vector3.random().divScalar(2.0).add(voxelPosition),
))
@ -112,7 +111,6 @@ export class OtS_VoxelMesh_Converter {
voxelColour = RGBAUtil.average(...samples);
} else {
voxelColour = this._getVoxelColour(
mesh,
triangle,
voxelPosition,
);
@ -123,7 +121,7 @@ export class OtS_VoxelMesh_Converter {
};
}
private _getVoxelColour(mesh: OtS_Mesh, triangle: OtS_Triangle, location: Vector3): RGBA {
private _getVoxelColour(triangle: OtS_Triangle, location: Vector3): RGBA {
if (triangle.type === 'solid') {
return triangle.colour;
}

View File

@ -0,0 +1,56 @@
import { RGBAColours, RGBAUtil } from "../src/colour";
import { OtS_Mesh } from "../src/ots_mesh";
test('Mesh Triangles', () => {
const mesh = OtS_Mesh.create();
mesh.addSection({
name: 'Test Section 1',
type: 'solid',
colour: RGBAUtil.copy(RGBAColours.WHITE),
positionData: Float32Array.from([
0.0, 0.0, 0.0,
1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
0.0, 0.0, 0.0,
1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
]),
normalData: Float32Array.from([
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
]),
indexData: Uint32Array.from([
0, 1, 2,
3, 4, 5
]),
});
mesh.addSection({
name: 'Test Section 2',
type: 'solid',
colour: RGBAUtil.copy(RGBAColours.WHITE),
positionData: Float32Array.from([
0.0, 0.0, 0.0,
1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
]),
normalData: Float32Array.from([
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
]),
indexData: Uint32Array.from([
0, 1, 2,
]),
});
expect(mesh.calcTriangleCount()).toBe(3);
const triangles = Array.from(mesh.getTriangles());
expect(triangles.length).toBe(3);
});

View File

@ -1,11 +1,13 @@
import { RGBAColours, RGBAUtil } from 'ots-core/src/colour';
import { OtS_Mesh } from 'ots-core/src/ots_mesh';
import { OtS_Texture } from 'ots-core/src/ots_texture';
import { OtS_VoxelMesh_Converter } from '../Core/src/ots_voxel_mesh_converter';
(async () => {
const mesh = OtS_Mesh.create();
{
mesh.addSection({
name: 'Test Section 1',
type: 'solid',
colour: RGBAUtil.copy(RGBAColours.WHITE),
positionData: Float32Array.from([
@ -13,18 +15,29 @@ import { OtS_Texture } from 'ots-core/src/ots_texture';
1.0, 2.0, 3.0,
4.0, 5.0, 6.0,
]),
normalData: Float32Array.from([
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
]),
indexData: Uint32Array.from([
0, 1, 2
]),
});
mesh.addSection({
name: 'Test Section 2',
type: 'colour',
positionData: Float32Array.from([
0.0, 10.0, 0.0,
1.0, 12.0, 3.0,
4.0, 15.0, 6.0,
]),
normalData: Float32Array.from([
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
]),
colourData: Float32Array.from([
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
@ -36,6 +49,7 @@ import { OtS_Texture } from 'ots-core/src/ots_texture';
});
mesh.addSection({
name: 'Test Section 3',
type: 'textured',
texture: new OtS_Texture(new Uint8ClampedArray(), 0, 0, 'nearest', 'repeat'),
positionData: Float32Array.from([
@ -43,6 +57,11 @@ import { OtS_Texture } from 'ots-core/src/ots_texture';
1.0, 22.0, 3.0,
4.0, 25.0, 6.0,
]),
normalData: Float32Array.from([
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
]),
texcoordData: Float32Array.from([
0.0, 0.0,
1.0, 0.0,
@ -55,7 +74,7 @@ import { OtS_Texture } from 'ots-core/src/ots_texture';
}
// 3. Construct a voxel mesh from the mesh
const converter = new OTS.voxelMeshConverter();
const converter = new OtS_VoxelMesh_Converter();
converter.setConfig({
constraintAxis: 'y',
size: 380,