mirror of
https://github.com/LucasDower/ObjToSchematic.git
synced 2025-12-11 20:15:30 +01:00
Fixed incorrect getTriangles, fixed _sampleNearest and _sampleLinear being swapped
This commit is contained in:
parent
b7d6bce1a2
commit
803cc6c326
@ -158,7 +158,7 @@ export class OtS_Mesh {
|
|||||||
ASSERT(sectionCount > 0); // TODO: Don't assert,
|
ASSERT(sectionCount > 0); // TODO: Don't assert,
|
||||||
|
|
||||||
let sectionIndex = 0;
|
let sectionIndex = 0;
|
||||||
let triangleCount = this._sections[0].positionData.length / 3;
|
let triangleCount = this._sections[0].indexData.length / 3;
|
||||||
let triangleIndex = 0;
|
let triangleIndex = 0;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -166,9 +166,10 @@ export class OtS_Mesh {
|
|||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
next: () => {
|
next: () => {
|
||||||
if (triangleIndex >= triangleCount && sectionIndex < sectionCount) {
|
if (triangleIndex >= triangleCount && sectionIndex < sectionCount - 1) {
|
||||||
++sectionIndex;
|
++sectionIndex;
|
||||||
triangleCount = this._sections[sectionIndex].positionData.length / 3;
|
triangleCount = this._sections[sectionIndex].indexData.length / 3;
|
||||||
|
triangleIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (triangleIndex < triangleCount) {
|
if (triangleIndex < triangleCount) {
|
||||||
|
|||||||
@ -65,9 +65,9 @@ export class OtS_Texture {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Assumes `u` and `v` are in the range [0, 1]
|
// Assumes `u` and `v` are in the range [0, 1]
|
||||||
private _sampleNearest(u: number, v: number): RGBA {
|
private _sampleLinear(u: number, v: number): RGBA {
|
||||||
const x = Math.floor(u * this._width - 1);
|
const x = Math.floor(u * (this._width - 1));
|
||||||
const y = Math.floor(v * this._height - 1);
|
const y = Math.floor(v * (this._height - 1));
|
||||||
|
|
||||||
const left = Math.floor(x);
|
const left = Math.floor(x);
|
||||||
const right = left + 1;
|
const right = left + 1;
|
||||||
@ -91,7 +91,7 @@ export class OtS_Texture {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Assumes `u` and `v` are in the range [0, 1]
|
// 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 x = Math.floor(u * (this._width - 1));
|
||||||
const y = Math.floor(v * (this._height - 1));
|
const y = Math.floor(v * (this._height - 1));
|
||||||
|
|
||||||
|
|||||||
@ -104,7 +104,6 @@ export class OtS_VoxelMesh_Converter {
|
|||||||
const samples: RGBA[] = [];
|
const samples: RGBA[] = [];
|
||||||
for (let i = 0; i < 8; ++i) {
|
for (let i = 0; i < 8; ++i) {
|
||||||
samples.push(this._getVoxelColour(
|
samples.push(this._getVoxelColour(
|
||||||
mesh,
|
|
||||||
triangle,
|
triangle,
|
||||||
Vector3.random().divScalar(2.0).add(voxelPosition),
|
Vector3.random().divScalar(2.0).add(voxelPosition),
|
||||||
))
|
))
|
||||||
@ -112,7 +111,6 @@ export class OtS_VoxelMesh_Converter {
|
|||||||
voxelColour = RGBAUtil.average(...samples);
|
voxelColour = RGBAUtil.average(...samples);
|
||||||
} else {
|
} else {
|
||||||
voxelColour = this._getVoxelColour(
|
voxelColour = this._getVoxelColour(
|
||||||
mesh,
|
|
||||||
triangle,
|
triangle,
|
||||||
voxelPosition,
|
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') {
|
if (triangle.type === 'solid') {
|
||||||
return triangle.colour;
|
return triangle.colour;
|
||||||
}
|
}
|
||||||
|
|||||||
56
Core/tests/ots_mesh.test.ts
Normal file
56
Core/tests/ots_mesh.test.ts
Normal 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);
|
||||||
|
});
|
||||||
@ -1,11 +1,13 @@
|
|||||||
import { RGBAColours, RGBAUtil } from 'ots-core/src/colour';
|
import { RGBAColours, RGBAUtil } from 'ots-core/src/colour';
|
||||||
import { OtS_Mesh } from 'ots-core/src/ots_mesh';
|
import { OtS_Mesh } from 'ots-core/src/ots_mesh';
|
||||||
import { OtS_Texture } from 'ots-core/src/ots_texture';
|
import { OtS_Texture } from 'ots-core/src/ots_texture';
|
||||||
|
import { OtS_VoxelMesh_Converter } from '../Core/src/ots_voxel_mesh_converter';
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
const mesh = OtS_Mesh.create();
|
const mesh = OtS_Mesh.create();
|
||||||
{
|
{
|
||||||
mesh.addSection({
|
mesh.addSection({
|
||||||
|
name: 'Test Section 1',
|
||||||
type: 'solid',
|
type: 'solid',
|
||||||
colour: RGBAUtil.copy(RGBAColours.WHITE),
|
colour: RGBAUtil.copy(RGBAColours.WHITE),
|
||||||
positionData: Float32Array.from([
|
positionData: Float32Array.from([
|
||||||
@ -13,18 +15,29 @@ import { OtS_Texture } from 'ots-core/src/ots_texture';
|
|||||||
1.0, 2.0, 3.0,
|
1.0, 2.0, 3.0,
|
||||||
4.0, 5.0, 6.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([
|
indexData: Uint32Array.from([
|
||||||
0, 1, 2
|
0, 1, 2
|
||||||
]),
|
]),
|
||||||
});
|
});
|
||||||
|
|
||||||
mesh.addSection({
|
mesh.addSection({
|
||||||
|
name: 'Test Section 2',
|
||||||
type: 'colour',
|
type: 'colour',
|
||||||
positionData: Float32Array.from([
|
positionData: Float32Array.from([
|
||||||
0.0, 10.0, 0.0,
|
0.0, 10.0, 0.0,
|
||||||
1.0, 12.0, 3.0,
|
1.0, 12.0, 3.0,
|
||||||
4.0, 15.0, 6.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([
|
colourData: Float32Array.from([
|
||||||
1.0, 0.0, 0.0, 1.0,
|
1.0, 0.0, 0.0, 1.0,
|
||||||
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({
|
mesh.addSection({
|
||||||
|
name: 'Test Section 3',
|
||||||
type: 'textured',
|
type: 'textured',
|
||||||
texture: new OtS_Texture(new Uint8ClampedArray(), 0, 0, 'nearest', 'repeat'),
|
texture: new OtS_Texture(new Uint8ClampedArray(), 0, 0, 'nearest', 'repeat'),
|
||||||
positionData: Float32Array.from([
|
positionData: Float32Array.from([
|
||||||
@ -43,6 +57,11 @@ import { OtS_Texture } from 'ots-core/src/ots_texture';
|
|||||||
1.0, 22.0, 3.0,
|
1.0, 22.0, 3.0,
|
||||||
4.0, 25.0, 6.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([
|
texcoordData: Float32Array.from([
|
||||||
0.0, 0.0,
|
0.0, 0.0,
|
||||||
1.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
|
// 3. Construct a voxel mesh from the mesh
|
||||||
const converter = new OTS.voxelMeshConverter();
|
const converter = new OtS_VoxelMesh_Converter();
|
||||||
converter.setConfig({
|
converter.setConfig({
|
||||||
constraintAxis: 'y',
|
constraintAxis: 'y',
|
||||||
size: 380,
|
size: 380,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user