diff --git a/src/model.ts b/src/model.ts index 3cd8fdf..1afab31 100644 --- a/src/model.ts +++ b/src/model.ts @@ -1,24 +1,21 @@ import { ModelType } from "skinview-utils"; import { BoxGeometry, DoubleSide, FrontSide, Group, Mesh, MeshBasicMaterial, Object3D, Texture, Vector2 } from "three"; -function toFaceVertices(x1: number, y1: number, x2: number, y2: number, w: number, h: number): Array { - return [ - new Vector2(x1 / w, 1.0 - y2 / h), - new Vector2(x2 / w, 1.0 - y2 / h), - new Vector2(x2 / w, 1.0 - y1 / h), - new Vector2(x1 / w, 1.0 - y1 / h) +function setUVs(box: BoxGeometry, u: number, v: number, width: number, height: number, depth: number, textureWidth: number, textureHeight: number): void { + const toFaceVertices = (x1: number, y1: number, x2: number, y2: number) => [ + new Vector2(x1 / textureWidth, 1.0 - y2 / textureHeight), + new Vector2(x2 / textureWidth, 1.0 - y2 / textureHeight), + new Vector2(x2 / textureWidth, 1.0 - y1 / textureHeight), + new Vector2(x1 / textureWidth, 1.0 - y1 / textureHeight) ]; -} -function toSkinVertices(x1: number, y1: number, x2: number, y2: number): Array { - return toFaceVertices(x1, y1, x2, y2, 64.0, 64.0); -} + const top = toFaceVertices(u + depth, v, u + width + depth, v + depth); + const bottom = toFaceVertices(u + width + depth, v, u + width * 2 + depth, v + depth); + const left = toFaceVertices(u, v + depth, u + depth, v + depth + height); + const front = toFaceVertices(u + depth, v + depth, u + width + depth, v + depth + height); + const right = toFaceVertices(u + width + depth, v + depth, u + width + depth * 2, v + height + depth); + const back = toFaceVertices(u + width + depth * 2, v + depth, u + width * 2 + depth * 2, v + height + depth); -function toCapeVertices(x1: number, y1: number, x2: number, y2: number): Array { - return toFaceVertices(x1, y1, x2, y2, 64.0, 32.0); -} - -function setVertices(box: BoxGeometry, top: Array, bottom: Array, left: Array, front: Array, right: Array, back: Array): void { box.faceVertexUvs[0] = []; box.faceVertexUvs[0][0] = [right[3], right[0], right[2]]; box.faceVertexUvs[0][1] = [right[0], right[1], right[2]]; @@ -34,6 +31,14 @@ function setVertices(box: BoxGeometry, top: Array, bottom: Array