@@ -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 }
0 commit comments