@@ -39,7 +39,7 @@ class ChunkMesher {
3939 ]
4040 }
4141
42- getUvForFace ( block , type ) {
42+ getUvForFace ( block , type , opt ) {
4343 let xd , toxX , toxY
4444 if (
4545 this . blocksTex [ block . name ] !== undefined ||
@@ -85,11 +85,21 @@ class ChunkMesher {
8585 const y1 = 1 - this . q * toxY - this . q
8686 const x2 = this . q * toxX + this . q
8787 const y2 = 1 - this . q * toxY
88- return [ x1 , y1 , x2 , y1 , x1 , y2 , x1 , y2 , x2 , y1 , x2 , y2 ]
88+ const uv = [
89+ [ x1 , y1 ] ,
90+ [ x1 , y2 ] ,
91+ [ x2 , y1 ] ,
92+ [ x2 , y2 ]
93+ ]
94+ if ( opt ) {
95+ return [ ...uv [ 0 ] , ...uv [ 1 ] , ...uv [ 2 ] , ...uv [ 2 ] , ...uv [ 1 ] , ...uv [ 3 ] ]
96+ } else {
97+ return [ ...uv [ 0 ] , ...uv [ 2 ] , ...uv [ 1 ] , ...uv [ 1 ] , ...uv [ 2 ] , ...uv [ 3 ] ]
98+ }
8999 }
90100
91- genBlockFace ( type , block , pos ) {
92- const uv = this . getUvForFace ( block , type )
101+ genBlockFace ( type , block , pos , opt ) {
102+ const uv = this . getUvForFace ( block , type , opt )
93103 const norm = [ ]
94104 for ( let i = 0 ; i < 6 ; i ++ ) {
95105 norm . push ( ...this . neighbours [ type ] )
@@ -102,7 +112,13 @@ class ChunkMesher {
102112 py : [ 5 , 7 , 3 , 2 ] ,
103113 ny : [ 1 , 0 , 4 , 6 ]
104114 } ) [ type ]
105- const ids = [ 0 , 1 , 2 , 2 , 1 , 3 ]
115+ let ids
116+ if ( opt ) {
117+ ids = [ 1 , 3 , 0 , 0 , 3 , 2 ]
118+ } else {
119+ ids = [ 0 , 1 , 2 , 2 , 1 , 3 ]
120+ }
121+
106122 const posx = [ ]
107123 const pt = [
108124 [ - 0.5 , - 0.5 , 0.5 ] ,
@@ -126,8 +142,31 @@ class ChunkMesher {
126142
127143 addFace ( tVertexBuffer , VertexBuffer , type , pos ) {
128144 const block = this . chunkTerrain . getBlock ( ...pos )
129- const faceVertex = this . genBlockFace ( type , block , pos )
130- this . ambientOcclusion ( block , pos , faceVertex , type )
145+
146+ const col = this . ambientOcclusion ( block , pos , type )
147+ let faceVertex
148+ if ( col [ 0 ] + col [ 3 ] >= col [ 1 ] + col [ 2 ] ) {
149+ faceVertex = this . genBlockFace ( type , block , pos , true )
150+ faceVertex . color = [
151+ ...col [ 2 ] ,
152+ ...col [ 3 ] ,
153+ ...col [ 0 ] ,
154+ ...col [ 0 ] ,
155+ ...col [ 3 ] ,
156+ ...col [ 1 ]
157+ ]
158+ } else {
159+ faceVertex = this . genBlockFace ( type , block , pos , false )
160+ faceVertex . color = [
161+ ...col [ 0 ] ,
162+ ...col [ 2 ] ,
163+ ...col [ 1 ] ,
164+ ...col [ 1 ] ,
165+ ...col [ 2 ] ,
166+ ...col [ 3 ]
167+ ]
168+ }
169+
131170 this . push (
132171 tVertexBuffer ,
133172 VertexBuffer ,
@@ -136,7 +175,7 @@ class ChunkMesher {
136175 )
137176 }
138177
139- ambientOcclusion ( block , pos , faceVertex , type ) {
178+ ambientOcclusion ( block , pos , type ) {
140179 const loaded = { }
141180 for ( let x = - 1 ; x <= 1 ; x ++ ) {
142181 for ( let y = - 1 ; y <= 1 ; y ++ ) {
@@ -202,15 +241,7 @@ class ChunkMesher {
202241 col [ i ] [ 2 ] /= ile
203242 }
204243 }
205-
206- faceVertex . color = [
207- ...col [ 0 ] ,
208- ...col [ 2 ] ,
209- ...col [ 1 ] ,
210- ...col [ 1 ] ,
211- ...col [ 2 ] ,
212- ...col [ 3 ]
213- ]
244+ return col
214245 }
215246
216247 push ( tVertexBuffer , VertexBuffer , faceVertex , transparent ) {
0 commit comments