|
| 1 | +import { Document, Mesh, Node } from "@gltf-transform/core" |
| 2 | +import { compactPrimitive } from "@gltf-transform/functions" |
| 3 | +import { unreachable } from "../comTypes/util" |
| 4 | +import { TextureAtlas } from "./AtlasManager" |
| 5 | +import { BlockModel } from "./BlockModel" |
| 6 | +import { BlockState } from "./BlockState" |
| 7 | +import { FaceInfo } from "./FaceInfo" |
| 8 | +import { FACE_DOWN, FACE_EAST, FACE_NORTH, FACE_SOUTH, FACE_UP, FACE_WEST } from "./FACES" |
| 9 | +import { warn } from "./log" |
| 10 | +import { ModelManager } from "./ModelManager" |
| 11 | + |
| 12 | +const _FACE_DATA = [ |
| 13 | + // Vertex data: 4 vertices, each has 3 components |
| 14 | + // 2---3 |
| 15 | + // | | |
| 16 | + // 0---1 |
| 17 | + [FACE_SOUTH, [-0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5]], |
| 18 | + [FACE_NORTH, [0.5, -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, -0.5]], |
| 19 | + [FACE_WEST, [-0.5, -0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5]], |
| 20 | + [FACE_EAST, [0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5]], |
| 21 | + [FACE_UP, [-0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5]], |
| 22 | + [FACE_DOWN, [-0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5]], |
| 23 | +] as const |
| 24 | + |
| 25 | +export class BlockBuilder { |
| 26 | + protected _meshCache = new Map<string, Mesh>() |
| 27 | + protected _buffer = this.document.createBuffer() |
| 28 | + |
| 29 | + public getBlockMesh(model: BlockModel, elementIdx: number, faceMask: number, faces: (FaceInfo | null)[]) { |
| 30 | + const key = `${model.name}_${elementIdx}_${faceMask}` |
| 31 | + const existing = this._meshCache.get(key) |
| 32 | + if (existing) return existing |
| 33 | + |
| 34 | + const vertexValues: number[] = [] |
| 35 | + const indexValues: number[] = [] |
| 36 | + const uvValues: number[] = [] |
| 37 | + |
| 38 | + let indexStart = 0 |
| 39 | + |
| 40 | + for (const [face, vertices] of _FACE_DATA) { |
| 41 | + if ((faceMask & face) == 0) continue |
| 42 | + |
| 43 | + indexValues.push(indexStart + 0, indexStart + 1, indexStart + 2, indexStart + 1, indexStart + 3, indexStart + 2) |
| 44 | + |
| 45 | + // Increment index start by 4 because we will add 4 vertices |
| 46 | + indexStart += 4 |
| 47 | + |
| 48 | + vertexValues.push(...vertices) |
| 49 | + |
| 50 | + const index = 31 - Math.clz32(face) |
| 51 | + const faceInfo = faces[index] ?? unreachable() |
| 52 | + const texture = model.resolveTexture(faceInfo.texture) |
| 53 | + uvValues.push(...this.atlas.getUVs(texture, faceInfo.uv)) |
| 54 | + } |
| 55 | + |
| 56 | + const vertices = this.document.createAccessor() |
| 57 | + .setType("VEC3") |
| 58 | + .setArray(new Float32Array(vertexValues)) |
| 59 | + .setBuffer(this._buffer) |
| 60 | + |
| 61 | + const uvs = this.document.createAccessor() |
| 62 | + .setType("VEC2") |
| 63 | + .setArray(new Float32Array(uvValues)) |
| 64 | + .setBuffer(this._buffer) |
| 65 | + |
| 66 | + const indices = this.document.createAccessor() |
| 67 | + .setArray(new Uint16Array(indexValues)) |
| 68 | + .setBuffer(this._buffer) |
| 69 | + |
| 70 | + const prim = this.document.createPrimitive() |
| 71 | + .setAttribute("POSITION", vertices) |
| 72 | + .setAttribute("TEXCOORD_0", uvs) |
| 73 | + .setIndices(indices) |
| 74 | + .setMaterial(this.atlas.getOpaqueMaterial()) |
| 75 | + |
| 76 | + compactPrimitive(prim) |
| 77 | + |
| 78 | + const mesh = this.document.createMesh(key) |
| 79 | + .addPrimitive(prim) |
| 80 | + |
| 81 | + this._meshCache.set(key, mesh) |
| 82 | + return mesh |
| 83 | + } |
| 84 | + |
| 85 | + public buildBlockState(state: BlockState, node: Node) { |
| 86 | + const possibleStates = this.models.getBlockModels(state.block) |
| 87 | + if (possibleStates == null) { |
| 88 | + // We already warned about this in ModelManager.prepareAssets |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + if (possibleStates.multipart) { |
| 93 | + let j = 0 |
| 94 | + |
| 95 | + for (const part of possibleStates.findModels(state)) { |
| 96 | + const partNode = this.document.createNode(`part_${j++}`) |
| 97 | + node.addChild(partNode) |
| 98 | + |
| 99 | + for (let i = 0; i < part.elements.length; i++) { |
| 100 | + const child = this.document.createNode(`part_${j}_element_${i}`) |
| 101 | + part.elements[i].apply(child, part, this.document, this) |
| 102 | + partNode.addChild(child) |
| 103 | + } |
| 104 | + |
| 105 | + if (part.rotation) { |
| 106 | + partNode.setRotation(part.rotation) |
| 107 | + } |
| 108 | + } |
| 109 | + } else { |
| 110 | + const model = possibleStates.findModel(state) |
| 111 | + |
| 112 | + if (model == null) { |
| 113 | + warn(`Failed to find matching model for block state ${state}`) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + if (model.elements.length == 0) return |
| 118 | + |
| 119 | + for (let i = 0; i < model.elements.length; i++) { |
| 120 | + const child = this.document.createNode(`element_${i}`) |
| 121 | + model.elements[i].apply(child, model, this.document, this) |
| 122 | + node.addChild(child) |
| 123 | + } |
| 124 | + |
| 125 | + if (model.rotation) { |
| 126 | + node.setRotation(model.rotation) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + constructor( |
| 132 | + public readonly document: Document, |
| 133 | + public readonly models: ModelManager, |
| 134 | + public readonly atlas: TextureAtlas, |
| 135 | + ) { } |
| 136 | +} |
0 commit comments