Skip to content

Commit 5bb2d52

Browse files
committed
🚧 Improve item rendering and bounding boxes
1 parent 7212422 commit 5bb2d52

File tree

2 files changed

+71
-28
lines changed

2 files changed

+71
-28
lines changed

src/systems/minecraft/itemModelManager.ts

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -129,42 +129,52 @@ async function generateItemMesh(location: string, model: IItemModel): Promise<It
129129

130130
const positionArray: number[] = []
131131
const indices: number[] = []
132-
const uvs = [1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1]
132+
const uvs: number[] = []
133133
const normals: number[] = []
134-
const colors = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
134+
const colors: number[] = []
135135
const addNormal = (x: number, y: number, z: number) => {
136136
normals.push(x, y, z, x, y, z, x, y, z, x, y, z)
137137
}
138138

139-
const corners: number[][] = [
140-
[-texture.image.width, 0, 0],
141-
[-texture.image.width, 0, texture.image.height],
142-
[0, 0, texture.image.height],
143-
[0, 0, 0],
144-
]
145-
corners.push(
146-
...corners.map(corner => {
147-
return [corner[0], -1, corner[2]]
148-
})
149-
)
150-
151-
corners.forEach(corner => {
152-
positionArray.push(...corner)
153-
})
154-
155-
indices.push(0, 1, 2, 0, 2, 3)
156-
indices.push(4 + 0, 4 + 2, 4 + 1, 4 + 0, 4 + 3, 4 + 2)
157-
158-
addNormal(0, 1, 0)
159-
addNormal(0, -1, 0)
160139
if (texture && texture.image.width) {
161140
const canvas = document.createElement('canvas')
162141
const ctx = canvas.getContext('2d')!
163142
canvas.width = texture.image.width
164143
canvas.height = texture.image.height
165144
ctx.drawImage(texture.image as HTMLImageElement, 0, 0)
166145

167-
const addFace = (
146+
const addFace = (x: number, z: number, w: number, h: number, dir: number) => {
147+
const s = positionArray.length / 3
148+
const y = dir === 1 ? -1 : 0
149+
// prettier-ignore
150+
positionArray.push(
151+
-x, y, z,
152+
-x, y, z + 1,
153+
-x - w, y, z + h,
154+
-x - w, y, z + h - 1
155+
)
156+
157+
if (dir === 1) {
158+
indices.push(s + 0, s + 1, s + 2, s + 0, s + 2, s + 3)
159+
} else if (dir === -1) {
160+
indices.push(s + 0, s + 2, s + 1, s + 0, s + 3, s + 2)
161+
}
162+
163+
addNormal(dir, 0, 0)
164+
uvs.push(
165+
(x + w) / canvas.width,
166+
1 - z / canvas.height,
167+
(x + w) / canvas.width,
168+
1 - (z + h) / canvas.height,
169+
x / canvas.width,
170+
1 - (z + h) / canvas.height,
171+
x / canvas.width,
172+
1 - z / canvas.height
173+
)
174+
colors.push(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
175+
}
176+
177+
const addEdge = (
168178
startX: number,
169179
startY: number,
170180
endX: number,
@@ -214,18 +224,33 @@ async function generateItemMesh(location: string, model: IItemModel): Promise<It
214224
}
215225

216226
const result = ctx.getImageData(0, 0, canvas.width, canvas.height)
227+
217228
const matrix1 = []
218229
for (let i = 0; i < result.data.length; i += 4) {
219230
matrix1.push(result.data[i + 3] > 140 ? 1 : 0)
220231
}
221232
const matrix2 = matrix1.slice()
222233

234+
for (let y = 0; y < canvas.height; y++) {
235+
let lengthX = 0
236+
for (let x = 0; x < canvas.width; x++) {
237+
const pixel = x == 0 ? 0 : matrix1[y * canvas.width + x]
238+
if (pixel) {
239+
lengthX++
240+
} else if (lengthX) {
241+
addFace(x - lengthX, y, lengthX, 1, 1)
242+
addFace(x - lengthX, y, lengthX, 1, -1)
243+
lengthX = 0
244+
}
245+
}
246+
}
247+
223248
for (let y = 0; y < canvas.height; y++) {
224249
for (let x = 0; x <= canvas.width; x++) {
225250
const px0 = x == 0 ? 0 : matrix1[y * canvas.width + x - 1]
226251
const px1 = x == canvas.width ? 0 : matrix1[y * canvas.width + x]
227252
if (!px0 !== !px1) {
228-
addFace(x, y, x, y + 1, px0 ? 1 : -1)
253+
addEdge(x, y, x, y + 1, px0 ? 1 : -1)
229254
}
230255
}
231256
}
@@ -235,7 +260,7 @@ async function generateItemMesh(location: string, model: IItemModel): Promise<It
235260
const px0 = y == 0 ? 0 : matrix2[(y - 1) * canvas.width + x]
236261
const px1 = y == canvas.height ? 0 : matrix2[y * canvas.width + x]
237262
if (!px0 !== !px1) {
238-
addFace(x, y, x + 1, y, px0 ? -1 : 1)
263+
addEdge(x, y, x + 1, y, px0 ? -1 : 1)
239264
}
240265
}
241266
}

test_blueprints/blockstates.ajblueprint

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@
326326
},
327327
{
328328
"name": "text_display",
329-
"position": [0, 27, 0],
329+
"position": [0, 27, 11],
330330
"rotation": [0, 0, 0],
331331
"scale": [1, 1, 1],
332332
"visibility": true,
@@ -340,6 +340,23 @@
340340
"align": "center",
341341
"uuid": "3988627b-b0c6-8a09-9d3f-230adeb648f8",
342342
"type": "animated_java:text_display"
343+
},
344+
{
345+
"name": "vanilla_item_display1",
346+
"position": [0, 25, 0],
347+
"rotation": [90, 0, 0],
348+
"scale": [1, 1, 1],
349+
"visibility": true,
350+
"item": "minecraft:diamond",
351+
"config": {},
352+
"block": "minecraft:stone",
353+
"text": "\"Hello World!\"",
354+
"lineWidth": 200,
355+
"backgroundColor": "#000000",
356+
"backgroundAlpha": 0.25,
357+
"align": "center",
358+
"uuid": "c6e25715-1cda-c39f-9693-be44241e1d48",
359+
"type": "animated_java:vanilla_item_display"
343360
}
344361
],
345362
"outliner": [
@@ -360,7 +377,8 @@
360377
"42bafea5-d05a-8106-0db4-0d1b0067a68e",
361378
"e1a16324-658b-a9af-1095-ca2a83c80595",
362379
"215cf9b7-1949-5e9b-b7b1-a35f59e58b2d",
363-
"3988627b-b0c6-8a09-9d3f-230adeb648f8"
380+
"3988627b-b0c6-8a09-9d3f-230adeb648f8",
381+
"c6e25715-1cda-c39f-9693-be44241e1d48"
364382
],
365383
"textures": [],
366384
"variants": {

0 commit comments

Comments
 (0)