Compare commits
No commits in common. "0a06d7970d04bd3416f01bf004a546cbc753db57" and "c290a0e977125de54615f98be68f4b31fe9b3259" have entirely different histories.
0a06d7970d
...
c290a0e977
@ -2,13 +2,12 @@ import { resolve } from 'path';
|
|||||||
import { ModelStore, MsonEvaluate } from '.';
|
import { ModelStore, MsonEvaluate } from '.';
|
||||||
import { fillStoreFromFilesystem, saveGeometry, saveModel } from './util/node';
|
import { fillStoreFromFilesystem, saveGeometry, saveModel } from './util/node';
|
||||||
import { ThreeBuilder } from './three';
|
import { ThreeBuilder } from './three';
|
||||||
import { DoubleSide, MeshLambertMaterial } from 'three';
|
import { MeshLambertMaterial } from 'three';
|
||||||
|
|
||||||
async function init() {
|
async function init() {
|
||||||
const store = new ModelStore();
|
const store = new ModelStore();
|
||||||
const evaluate = new MsonEvaluate(store);
|
const evaluate = new MsonEvaluate(store);
|
||||||
const mat = new MeshLambertMaterial();
|
const mat = new MeshLambertMaterial();
|
||||||
// mat.side = DoubleSide;
|
|
||||||
const builder = new ThreeBuilder(mat);
|
const builder = new ThreeBuilder(mat);
|
||||||
|
|
||||||
await fillStoreFromFilesystem(store, resolve(process.cwd(), 'inputs'));
|
await fillStoreFromFilesystem(store, resolve(process.cwd(), 'inputs'));
|
||||||
|
@ -27,41 +27,40 @@ import {
|
|||||||
Vec3,
|
Vec3,
|
||||||
} from '../mson';
|
} from '../mson';
|
||||||
import { isArrayOfArrays } from '../util/array-of-array';
|
import { isArrayOfArrays } from '../util/array-of-array';
|
||||||
import { UVMapper } from './uv-mapper';
|
import { UVMapper } from './uv';
|
||||||
|
|
||||||
export class ThreeBuilder {
|
export class ThreeBuilder {
|
||||||
/**
|
static normals: Record<MsonFace, Vector3> = {
|
||||||
* This is used to rotate the plane to face the right direction.
|
|
||||||
*/
|
|
||||||
static planeFaceNormals: Record<MsonFace, Vector3> = {
|
|
||||||
up: new Vector3(0, 1, 0),
|
up: new Vector3(0, 1, 0),
|
||||||
down: new Vector3(0, -1, 0),
|
down: new Vector3(0, -1, 0),
|
||||||
east: new Vector3(-1, 0, 0),
|
east: new Vector3(1, 0, 0),
|
||||||
west: new Vector3(1, 0, 0),
|
west: new Vector3(-1, 0, 0),
|
||||||
south: new Vector3(0, 0, 1),
|
south: new Vector3(0, 0, 1),
|
||||||
north: new Vector3(0, 0, -1),
|
north: new Vector3(0, 0, -1),
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
static pocConvert: Record<
|
||||||
* This is used to correctly calculate the translation offset for the plane
|
|
||||||
* in each axis.
|
|
||||||
*/
|
|
||||||
static planeFaceOffsetConversions: Record<
|
|
||||||
MsonFace,
|
MsonFace,
|
||||||
(pos: Vector3, size: Vector2) => Vector3
|
(pos: Vector3, size: Vector2) => Vector3
|
||||||
> = {
|
> = {
|
||||||
up: (pos: Vector3, size: Vector2): Vector3 =>
|
up: function (pos: Vector3, size: Vector2): Vector3 {
|
||||||
new Vector3(pos.x + size.x / 2, pos.y, pos.z + size.y / 2),
|
return 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),
|
down: function (pos: Vector3, size: Vector2): Vector3 {
|
||||||
east: (pos: Vector3, size: Vector2): Vector3 =>
|
return new Vector3(pos.x + size.x / 2, pos.y, pos.z + size.y / 2);
|
||||||
new Vector3(pos.x, pos.y - size.y / 2, pos.z + size.x / 2),
|
},
|
||||||
west: (pos: Vector3, size: Vector2): Vector3 =>
|
east: function (pos: Vector3, size: Vector2): Vector3 {
|
||||||
new Vector3(pos.x, pos.y - size.y / 2, pos.z + size.x / 2),
|
return 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),
|
west: function (pos: Vector3, size: Vector2): Vector3 {
|
||||||
north: (pos: Vector3, size: Vector2): Vector3 =>
|
return new Vector3(pos.x, pos.y - size.y / 2, pos.z + size.x / 2);
|
||||||
new Vector3(pos.x + size.x / 2, pos.y - size.y / 2, pos.z),
|
},
|
||||||
|
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) {}
|
constructor(private readonly material: Material) {}
|
||||||
@ -106,7 +105,7 @@ export class ThreeBuilder {
|
|||||||
|
|
||||||
// Compound objects
|
// Compound objects
|
||||||
if (!component.type || component.type === 'mson:compound') {
|
if (!component.type || component.type === 'mson:compound') {
|
||||||
// mhm, go on ahead, nothing to see here
|
// mhm, go on ahead
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@ -202,9 +201,7 @@ export class ThreeBuilder {
|
|||||||
else dilate.set(component.dilate, component.dilate, component.dilate);
|
else dilate.set(component.dilate, component.dilate, component.dilate);
|
||||||
}
|
}
|
||||||
|
|
||||||
const offset = new Vector3().fromArray(
|
const offset = new Vector3().fromArray(component.from as Vec3);
|
||||||
(component.from as Vec3) || [0, 0, 0],
|
|
||||||
);
|
|
||||||
const halfSize = size.clone().divideScalar(2);
|
const halfSize = size.clone().divideScalar(2);
|
||||||
|
|
||||||
offset.setY(offset.y * -1);
|
offset.setY(offset.y * -1);
|
||||||
@ -225,8 +222,7 @@ export class ThreeBuilder {
|
|||||||
adjustedTranslate.z,
|
adjustedTranslate.z,
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: FIXME: this crap is to hack .toJSON() into including the entire geometry
|
// FIXME: hack toJSON
|
||||||
// instead of just the properties used to initialize it.
|
|
||||||
(geometry as any).type = 'BufferGeometry';
|
(geometry as any).type = 'BufferGeometry';
|
||||||
delete (geometry as any).parameters;
|
delete (geometry as any).parameters;
|
||||||
|
|
||||||
@ -288,8 +284,7 @@ export class ThreeBuilder {
|
|||||||
geometry = geometry.toNonIndexed() as CylinderGeometry;
|
geometry = geometry.toNonIndexed() as CylinderGeometry;
|
||||||
geometry.computeVertexNormals();
|
geometry.computeVertexNormals();
|
||||||
|
|
||||||
// TODO: FIXME: this crap is to hack .toJSON() into including the entire geometry
|
// FIXME: hack toJSON
|
||||||
// instead of just the properties used to initialize it.
|
|
||||||
(geometry as any).type = 'BufferGeometry';
|
(geometry as any).type = 'BufferGeometry';
|
||||||
delete (geometry as any).parameters;
|
delete (geometry as any).parameters;
|
||||||
|
|
||||||
@ -309,27 +304,24 @@ export class ThreeBuilder {
|
|||||||
mirror?: boolean | [boolean, boolean],
|
mirror?: boolean | [boolean, boolean],
|
||||||
) {
|
) {
|
||||||
const planeGeom = new PlaneGeometry(size.x, size.y);
|
const planeGeom = new PlaneGeometry(size.x, size.y);
|
||||||
let axisNormal = ThreeBuilder.planeFaceNormals[face];
|
let axisNormal = ThreeBuilder.normals[face];
|
||||||
pos.setY(pos.y * -1);
|
pos.setY(pos.y * -1);
|
||||||
const adjustedTranslate = ThreeBuilder.planeFaceOffsetConversions[face](
|
const adjustedTranslate = ThreeBuilder.pocConvert[face](pos, size);
|
||||||
pos,
|
|
||||||
size,
|
|
||||||
);
|
|
||||||
planeGeom.lookAt(axisNormal);
|
planeGeom.lookAt(axisNormal);
|
||||||
planeGeom.translate(
|
planeGeom.translate(
|
||||||
-adjustedTranslate.x,
|
adjustedTranslate.x,
|
||||||
adjustedTranslate.y,
|
adjustedTranslate.y,
|
||||||
adjustedTranslate.z,
|
adjustedTranslate.z,
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: FIXME: this crap is to hack .toJSON() into including the entire geometry
|
// FIXME: hack toJSON
|
||||||
// instead of just the properties used to initialize it.
|
|
||||||
(planeGeom as any).type = 'BufferGeometry';
|
(planeGeom as any).type = 'BufferGeometry';
|
||||||
delete (planeGeom as any).parameters;
|
delete (planeGeom as any).parameters;
|
||||||
|
|
||||||
UVMapper.mapPlanarUvs(face, size, planeGeom, texture, mirror);
|
UVMapper.mapPlanarUvs(face, size, planeGeom, texture, mirror);
|
||||||
|
|
||||||
const mesh = new Mesh(planeGeom, this.material);
|
const mesh = new Mesh(planeGeom, this.material);
|
||||||
|
// mesh.position.copy(pos);
|
||||||
mesh.name = face;
|
mesh.name = face;
|
||||||
parent.add(mesh);
|
parent.add(mesh);
|
||||||
}
|
}
|
||||||
@ -430,7 +422,7 @@ export class ThreeBuilder {
|
|||||||
if (component?.rotate) {
|
if (component?.rotate) {
|
||||||
rotate.fromArray(
|
rotate.fromArray(
|
||||||
(component.rotate as Vec3).map((entry, index) =>
|
(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) {
|
if (component?.pivot) {
|
||||||
pos.fromArray(component.pivot as Vec3);
|
pos.fromArray(component.pivot as Vec3);
|
||||||
}
|
}
|
||||||
pos.setX(pos.x * -1);
|
|
||||||
pos.setY(pos.y * -1);
|
pos.setY(pos.y * -1);
|
||||||
wrapper.position.copy(pos);
|
wrapper.position.copy(pos);
|
||||||
wrapper.updateMatrix();
|
wrapper.updateMatrix();
|
||||||
|
@ -49,7 +49,6 @@ export class UVMapper {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert coordinates relative to the texture to UVs.
|
|
||||||
* @param sizeU Size of quad U
|
* @param sizeU Size of quad U
|
||||||
* @param sizeV Size of quad V
|
* @param sizeV Size of quad V
|
||||||
* @param txU Texture width
|
* @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) {
|
static unwrapBox(size: Vector3) {
|
||||||
const widths = UVMapper.boxFaceOrder.map(
|
const widths = UVMapper.boxFaceOrder.map(
|
||||||
(item) => size[UVMapper.boxWidthSide[item]],
|
(item) => size[UVMapper.boxWidthSide[item]],
|
||||||
@ -118,16 +112,14 @@ export class UVMapper {
|
|||||||
(item) => size[UVMapper.boxHeightSide[item]],
|
(item) => size[UVMapper.boxHeightSide[item]],
|
||||||
);
|
);
|
||||||
const layout: any = {
|
const layout: any = {
|
||||||
// top row
|
|
||||||
up: { x: 'west', y: 0 },
|
up: { x: 'west', y: 0 },
|
||||||
down: { x: 'up', y: 0 },
|
down: { x: 'up', y: 0 },
|
||||||
// bottom row
|
|
||||||
west: { x: 0, y: 'up' },
|
west: { x: 0, y: 'up' },
|
||||||
south: { x: 'west', y: 'up' },
|
south: { x: 'west', y: 'up' },
|
||||||
east: { x: 'south', y: 'down' },
|
east: { x: 'south', y: 'down' },
|
||||||
north: { x: 'east', y: 'down' },
|
north: { x: 'east', y: 'down' },
|
||||||
};
|
};
|
||||||
|
|
||||||
Object.keys(layout).forEach((key) => {
|
Object.keys(layout).forEach((key) => {
|
||||||
const entry = layout[key];
|
const entry = layout[key];
|
||||||
if (typeof entry.x === 'string') {
|
if (typeof entry.x === 'string') {
|
||||||
@ -140,7 +132,6 @@ export class UVMapper {
|
|||||||
entry.y = prev.y + heights[UVMapper.boxFaceOrder.indexOf(entry.y)];
|
entry.y = prev.y + heights[UVMapper.boxFaceOrder.indexOf(entry.y)];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return layout;
|
return layout;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -244,16 +235,12 @@ export class UVMapper {
|
|||||||
: [mirror, mirror]
|
: [mirror, mirror]
|
||||||
: [false, false];
|
: [false, false];
|
||||||
|
|
||||||
if (['up', 'down'].includes(face)) mirroring[0] = !mirroring[0];
|
|
||||||
|
|
||||||
for (let i = 0; i < index!.count; i += 3) {
|
for (let i = 0; i < index!.count; i += 3) {
|
||||||
const triIndex = Math.ceil((i / 6) % 1);
|
const triIndex = Math.ceil((i / 6) % 1);
|
||||||
const sizeU = size[UVMapper.planeWidthSide[face]];
|
const sizeU = size[UVMapper.planeWidthSide[face]];
|
||||||
const sizeV = size[UVMapper.planeHeightSide[face]];
|
const sizeV = size[UVMapper.planeHeightSide[face]];
|
||||||
|
|
||||||
let finalU = textureOffsetU;
|
let finalU = textureOffsetU;
|
||||||
let finalV = textureOffsetV;
|
let finalV = textureOffsetV;
|
||||||
|
|
||||||
const faceCoords = UVMapper.uvOffsetConvert(
|
const faceCoords = UVMapper.uvOffsetConvert(
|
||||||
sizeU,
|
sizeU,
|
||||||
sizeV,
|
sizeV,
|
||||||
@ -261,7 +248,7 @@ export class UVMapper {
|
|||||||
textureHeight,
|
textureHeight,
|
||||||
finalU,
|
finalU,
|
||||||
finalV,
|
finalV,
|
||||||
mirroring[0],
|
!mirroring[0],
|
||||||
mirroring[1],
|
mirroring[1],
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user