@@ -129,42 +129,52 @@ async function generateItemMesh(location: string, model: IItemModel): Promise<It
129
129
130
130
const positionArray : number [ ] = [ ]
131
131
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 [ ] = [ ]
133
133
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 [ ] = [ ]
135
135
const addNormal = ( x : number , y : number , z : number ) => {
136
136
normals . push ( x , y , z , x , y , z , x , y , z , x , y , z )
137
137
}
138
138
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 )
160
139
if ( texture && texture . image . width ) {
161
140
const canvas = document . createElement ( 'canvas' )
162
141
const ctx = canvas . getContext ( '2d' ) !
163
142
canvas . width = texture . image . width
164
143
canvas . height = texture . image . height
165
144
ctx . drawImage ( texture . image as HTMLImageElement , 0 , 0 )
166
145
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 = (
168
178
startX : number ,
169
179
startY : number ,
170
180
endX : number ,
@@ -214,18 +224,33 @@ async function generateItemMesh(location: string, model: IItemModel): Promise<It
214
224
}
215
225
216
226
const result = ctx . getImageData ( 0 , 0 , canvas . width , canvas . height )
227
+
217
228
const matrix1 = [ ]
218
229
for ( let i = 0 ; i < result . data . length ; i += 4 ) {
219
230
matrix1 . push ( result . data [ i + 3 ] > 140 ? 1 : 0 )
220
231
}
221
232
const matrix2 = matrix1 . slice ( )
222
233
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
+
223
248
for ( let y = 0 ; y < canvas . height ; y ++ ) {
224
249
for ( let x = 0 ; x <= canvas . width ; x ++ ) {
225
250
const px0 = x == 0 ? 0 : matrix1 [ y * canvas . width + x - 1 ]
226
251
const px1 = x == canvas . width ? 0 : matrix1 [ y * canvas . width + x ]
227
252
if ( ! px0 !== ! px1 ) {
228
- addFace ( x , y , x , y + 1 , px0 ? 1 : - 1 )
253
+ addEdge ( x , y , x , y + 1 , px0 ? 1 : - 1 )
229
254
}
230
255
}
231
256
}
@@ -235,7 +260,7 @@ async function generateItemMesh(location: string, model: IItemModel): Promise<It
235
260
const px0 = y == 0 ? 0 : matrix2 [ ( y - 1 ) * canvas . width + x ]
236
261
const px1 = y == canvas . height ? 0 : matrix2 [ y * canvas . width + x ]
237
262
if ( ! px0 !== ! px1 ) {
238
- addFace ( x , y , x + 1 , y , px0 ? - 1 : 1 )
263
+ addEdge ( x , y , x + 1 , y , px0 ? - 1 : 1 )
239
264
}
240
265
}
241
266
}
0 commit comments