Compare commits

..

No commits in common. "0a06d7970d04bd3416f01bf004a546cbc753db57" and "c290a0e977125de54615f98be68f4b31fe9b3259" have entirely different histories.

3 changed files with 36 additions and 59 deletions

View File

@ -2,13 +2,12 @@ import { resolve } from 'path';
import { ModelStore, MsonEvaluate } from '.';
import { fillStoreFromFilesystem, saveGeometry, saveModel } from './util/node';
import { ThreeBuilder } from './three';
import { DoubleSide, MeshLambertMaterial } from 'three';
import { MeshLambertMaterial } from 'three';
async function init() {
const store = new ModelStore();
const evaluate = new MsonEvaluate(store);
const mat = new MeshLambertMaterial();
// mat.side = DoubleSide;
const builder = new ThreeBuilder(mat);
await fillStoreFromFilesystem(store, resolve(process.cwd(), 'inputs'));

View File

@ -27,41 +27,40 @@ import {
Vec3,
} from '../mson';
import { isArrayOfArrays } from '../util/array-of-array';
import { UVMapper } from './uv-mapper';
import { UVMapper } from './uv';
export class ThreeBuilder {
/**
* This is used to rotate the plane to face the right direction.
*/
static planeFaceNormals: Record<MsonFace, Vector3> = {
static normals: Record<MsonFace, Vector3> = {
up: new Vector3(0, 1, 0),
down: new Vector3(0, -1, 0),
east: new Vector3(-1, 0, 0),
west: new Vector3(1, 0, 0),
east: new Vector3(1, 0, 0),
west: new Vector3(-1, 0, 0),
south: new Vector3(0, 0, 1),
north: new Vector3(0, 0, -1),
};
/**
* This is used to correctly calculate the translation offset for the plane
* in each axis.
*/
static planeFaceOffsetConversions: Record<
static pocConvert: Record<
MsonFace,
(pos: Vector3, size: Vector2) => Vector3
> = {
up: (pos: Vector3, size: Vector2): Vector3 =>
new Vector3(pos.x + size.x / 2, pos.y, pos.z + size.y / 2),
down: (pos: Vector3, size: Vector2): Vector3 =>
new Vector3(pos.x + size.x / 2, pos.y, pos.z + size.y / 2),
east: (pos: Vector3, size: Vector2): Vector3 =>
new Vector3(pos.x, pos.y - size.y / 2, pos.z + size.x / 2),
west: (pos: Vector3, size: Vector2): Vector3 =>
new Vector3(pos.x, pos.y - size.y / 2, pos.z + size.x / 2),
south: (pos: Vector3, size: Vector2): Vector3 =>
new Vector3(pos.x + size.x / 2, pos.y - size.y / 2, pos.z),
north: (pos: Vector3, size: Vector2): Vector3 =>
new Vector3(pos.x + size.x / 2, pos.y - size.y / 2, pos.z),
up: function (pos: Vector3, size: Vector2): Vector3 {
return new Vector3(pos.x + size.x / 2, pos.y, pos.z + size.y / 2);
},
down: function (pos: Vector3, size: Vector2): Vector3 {
return new Vector3(pos.x + size.x / 2, pos.y, pos.z + size.y / 2);
},
east: function (pos: Vector3, size: Vector2): Vector3 {
return new Vector3(pos.x, pos.y - size.y / 2, pos.z + size.x / 2);
},
west: function (pos: Vector3, size: Vector2): Vector3 {
return new Vector3(pos.x, pos.y - size.y / 2, pos.z + size.x / 2);
},
south: function (pos: Vector3, size: Vector2): Vector3 {
return new Vector3(pos.x + size.x / 2, pos.y - size.y / 2, pos.z);
},
north: function (pos: Vector3, size: Vector2): Vector3 {
return new Vector3(pos.x + size.x / 2, pos.y - size.y / 2, pos.z);
},
};
constructor(private readonly material: Material) {}
@ -106,7 +105,7 @@ export class ThreeBuilder {
// Compound objects
if (!component.type || component.type === 'mson:compound') {
// mhm, go on ahead, nothing to see here
// mhm, go on ahead
}
if (
@ -202,9 +201,7 @@ export class ThreeBuilder {
else dilate.set(component.dilate, component.dilate, component.dilate);
}
const offset = new Vector3().fromArray(
(component.from as Vec3) || [0, 0, 0],
);
const offset = new Vector3().fromArray(component.from as Vec3);
const halfSize = size.clone().divideScalar(2);
offset.setY(offset.y * -1);
@ -225,8 +222,7 @@ export class ThreeBuilder {
adjustedTranslate.z,
);
// TODO: FIXME: this crap is to hack .toJSON() into including the entire geometry
// instead of just the properties used to initialize it.
// FIXME: hack toJSON
(geometry as any).type = 'BufferGeometry';
delete (geometry as any).parameters;
@ -288,8 +284,7 @@ export class ThreeBuilder {
geometry = geometry.toNonIndexed() as CylinderGeometry;
geometry.computeVertexNormals();
// TODO: FIXME: this crap is to hack .toJSON() into including the entire geometry
// instead of just the properties used to initialize it.
// FIXME: hack toJSON
(geometry as any).type = 'BufferGeometry';
delete (geometry as any).parameters;
@ -309,27 +304,24 @@ export class ThreeBuilder {
mirror?: boolean | [boolean, boolean],
) {
const planeGeom = new PlaneGeometry(size.x, size.y);
let axisNormal = ThreeBuilder.planeFaceNormals[face];
let axisNormal = ThreeBuilder.normals[face];
pos.setY(pos.y * -1);
const adjustedTranslate = ThreeBuilder.planeFaceOffsetConversions[face](
pos,
size,
);
const adjustedTranslate = ThreeBuilder.pocConvert[face](pos, size);
planeGeom.lookAt(axisNormal);
planeGeom.translate(
-adjustedTranslate.x,
adjustedTranslate.x,
adjustedTranslate.y,
adjustedTranslate.z,
);
// TODO: FIXME: this crap is to hack .toJSON() into including the entire geometry
// instead of just the properties used to initialize it.
// FIXME: hack toJSON
(planeGeom as any).type = 'BufferGeometry';
delete (planeGeom as any).parameters;
UVMapper.mapPlanarUvs(face, size, planeGeom, texture, mirror);
const mesh = new Mesh(planeGeom, this.material);
// mesh.position.copy(pos);
mesh.name = face;
parent.add(mesh);
}
@ -430,7 +422,7 @@ export class ThreeBuilder {
if (component?.rotate) {
rotate.fromArray(
(component.rotate as Vec3).map((entry, index) =>
MathUtils.degToRad(index !== 2 ? entry * -1 : entry),
MathUtils.degToRad(index !== 1 ? entry * -1 : entry),
),
);
}
@ -440,7 +432,6 @@ export class ThreeBuilder {
if (component?.pivot) {
pos.fromArray(component.pivot as Vec3);
}
pos.setX(pos.x * -1);
pos.setY(pos.y * -1);
wrapper.position.copy(pos);
wrapper.updateMatrix();

View File

@ -49,7 +49,6 @@ export class UVMapper {
};
/**
* Convert coordinates relative to the texture to UVs.
* @param sizeU Size of quad U
* @param sizeV Size of quad V
* @param txU Texture width
@ -105,11 +104,6 @@ export class UVMapper {
);
}
/**
* Return instructions on which UV offsets to use when unwrapping a box.
* @param size Object size
* @returns Box unwrap instructions
*/
static unwrapBox(size: Vector3) {
const widths = UVMapper.boxFaceOrder.map(
(item) => size[UVMapper.boxWidthSide[item]],
@ -118,16 +112,14 @@ export class UVMapper {
(item) => size[UVMapper.boxHeightSide[item]],
);
const layout: any = {
// top row
up: { x: 'west', y: 0 },
down: { x: 'up', y: 0 },
// bottom row
west: { x: 0, y: 'up' },
south: { x: 'west', y: 'up' },
east: { x: 'south', y: 'down' },
north: { x: 'east', y: 'down' },
};
Object.keys(layout).forEach((key) => {
const entry = layout[key];
if (typeof entry.x === 'string') {
@ -140,7 +132,6 @@ export class UVMapper {
entry.y = prev.y + heights[UVMapper.boxFaceOrder.indexOf(entry.y)];
}
});
return layout;
}
@ -244,16 +235,12 @@ export class UVMapper {
: [mirror, mirror]
: [false, false];
if (['up', 'down'].includes(face)) mirroring[0] = !mirroring[0];
for (let i = 0; i < index!.count; i += 3) {
const triIndex = Math.ceil((i / 6) % 1);
const sizeU = size[UVMapper.planeWidthSide[face]];
const sizeV = size[UVMapper.planeHeightSide[face]];
let finalU = textureOffsetU;
let finalV = textureOffsetV;
const faceCoords = UVMapper.uvOffsetConvert(
sizeU,
sizeV,
@ -261,7 +248,7 @@ export class UVMapper {
textureHeight,
finalU,
finalV,
mirroring[0],
!mirroring[0],
mirroring[1],
);