|
30 | 30 |
|
31 | 31 | import { FirstPersonControls } from './jsm/controls/FirstPersonControls.js'; |
32 | 32 | import { ImprovedNoise } from './jsm/math/ImprovedNoise.js'; |
| 33 | + import { BufferGeometryUtils } from './jsm/utils/BufferGeometryUtils.js'; |
33 | 34 |
|
34 | 35 | let container, stats; |
35 | 36 |
|
|
63 | 64 |
|
64 | 65 | const matrix = new THREE.Matrix4(); |
65 | 66 |
|
66 | | - const pxGeometry = new THREE.PlaneGeometry( 100, 100 ); |
67 | | - pxGeometry.faces[ 0 ].vertexColors = [ light, shadow, light ]; |
68 | | - pxGeometry.faces[ 1 ].vertexColors = [ shadow, shadow, light ]; |
69 | | - pxGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5; |
70 | | - pxGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5; |
71 | | - pxGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5; |
| 67 | + const pxGeometry = new THREE.PlaneBufferGeometry( 100, 100 ).toNonIndexed(); |
| 68 | + pxGeometry.setAttribute( 'color', pxGeometry.attributes.position.clone() ); |
| 69 | + pxGeometry.attributes.color.copyColorsArray( [ light, shadow, light, shadow, shadow, light ] ); |
| 70 | + pxGeometry.attributes.uv.array[ 1 ] = 0.5; |
| 71 | + pxGeometry.attributes.uv.array[ 5 ] = 0.5; |
| 72 | + pxGeometry.attributes.uv.array[ 11 ] = 0.5; |
72 | 73 | pxGeometry.rotateY( Math.PI / 2 ); |
73 | 74 | pxGeometry.translate( 50, 0, 0 ); |
74 | 75 |
|
75 | | - const nxGeometry = new THREE.PlaneGeometry( 100, 100 ); |
76 | | - nxGeometry.faces[ 0 ].vertexColors = [ light, shadow, light ]; |
77 | | - nxGeometry.faces[ 1 ].vertexColors = [ shadow, shadow, light ]; |
78 | | - nxGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5; |
79 | | - nxGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5; |
80 | | - nxGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5; |
| 76 | + const nxGeometry = new THREE.PlaneBufferGeometry( 100, 100 ).toNonIndexed(); |
| 77 | + nxGeometry.setAttribute( 'color', nxGeometry.attributes.position.clone() ); |
| 78 | + nxGeometry.attributes.color.copyColorsArray( [ light, shadow, light, shadow, shadow, light ] ); |
| 79 | + nxGeometry.attributes.uv.array[ 1 ] = 0.5; |
| 80 | + nxGeometry.attributes.uv.array[ 5 ] = 0.5; |
| 81 | + nxGeometry.attributes.uv.array[ 11 ] = 0.5; |
81 | 82 | nxGeometry.rotateY( - Math.PI / 2 ); |
82 | 83 | nxGeometry.translate( - 50, 0, 0 ); |
83 | 84 |
|
84 | | - const pyGeometry = new THREE.PlaneGeometry( 100, 100 ); |
85 | | - pyGeometry.faces[ 0 ].vertexColors = [ light, light, light ]; |
86 | | - pyGeometry.faces[ 1 ].vertexColors = [ light, light, light ]; |
87 | | - pyGeometry.faceVertexUvs[ 0 ][ 0 ][ 1 ].y = 0.5; |
88 | | - pyGeometry.faceVertexUvs[ 0 ][ 1 ][ 0 ].y = 0.5; |
89 | | - pyGeometry.faceVertexUvs[ 0 ][ 1 ][ 1 ].y = 0.5; |
| 85 | + const pyGeometry = new THREE.PlaneBufferGeometry( 100, 100 ).toNonIndexed(); |
| 86 | + pyGeometry.setAttribute( 'color', pyGeometry.attributes.position.clone() ); |
| 87 | + pyGeometry.attributes.color.copyColorsArray( [ light, light, light, light, light, light ] ); |
| 88 | + pyGeometry.attributes.uv.array[ 3 ] = 0.5; |
| 89 | + pyGeometry.attributes.uv.array[ 7 ] = 0.5; |
| 90 | + pyGeometry.attributes.uv.array[ 9 ] = 0.5; |
90 | 91 | pyGeometry.rotateX( - Math.PI / 2 ); |
91 | 92 | pyGeometry.translate( 0, 50, 0 ); |
92 | 93 |
|
93 | | - const py2Geometry = new THREE.PlaneGeometry( 100, 100 ); |
94 | | - py2Geometry.faces[ 0 ].vertexColors = [ light, light, light ]; |
95 | | - py2Geometry.faces[ 1 ].vertexColors = [ light, light, light ]; |
96 | | - py2Geometry.faceVertexUvs[ 0 ][ 0 ][ 1 ].y = 0.5; |
97 | | - py2Geometry.faceVertexUvs[ 0 ][ 1 ][ 0 ].y = 0.5; |
98 | | - py2Geometry.faceVertexUvs[ 0 ][ 1 ][ 1 ].y = 0.5; |
| 94 | + const py2Geometry = new THREE.PlaneBufferGeometry( 100, 100 ).toNonIndexed(); |
| 95 | + py2Geometry.setAttribute( 'color', py2Geometry.attributes.position.clone() ); |
| 96 | + py2Geometry.attributes.color.copyColorsArray( [ light, light, light, light, light, light ] ); |
| 97 | + py2Geometry.attributes.uv.array[ 3 ] = 0.5; |
| 98 | + py2Geometry.attributes.uv.array[ 7 ] = 0.5; |
| 99 | + py2Geometry.attributes.uv.array[ 9 ] = 0.5; |
99 | 100 | py2Geometry.rotateX( - Math.PI / 2 ); |
100 | 101 | py2Geometry.rotateY( Math.PI / 2 ); |
101 | 102 | py2Geometry.translate( 0, 50, 0 ); |
102 | 103 |
|
103 | | - const pzGeometry = new THREE.PlaneGeometry( 100, 100 ); |
104 | | - pzGeometry.faces[ 0 ].vertexColors = [ light, shadow, light ]; |
105 | | - pzGeometry.faces[ 1 ].vertexColors = [ shadow, shadow, light ]; |
106 | | - pzGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5; |
107 | | - pzGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5; |
108 | | - pzGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5; |
| 104 | + const pzGeometry = new THREE.PlaneBufferGeometry( 100, 100 ).toNonIndexed(); |
| 105 | + pzGeometry.setAttribute( 'color', pzGeometry.attributes.position.clone() ); |
| 106 | + pzGeometry.attributes.color.copyColorsArray( [ light, shadow, light, shadow, shadow, light ] ); |
| 107 | + pzGeometry.attributes.uv.array[ 1 ] = 0.5; |
| 108 | + pzGeometry.attributes.uv.array[ 5 ] = 0.5; |
| 109 | + pzGeometry.attributes.uv.array[ 11 ] = 0.5; |
109 | 110 | pzGeometry.translate( 0, 0, 50 ); |
110 | 111 |
|
111 | | - const nzGeometry = new THREE.PlaneGeometry( 100, 100 ); |
112 | | - nzGeometry.faces[ 0 ].vertexColors = [ light, shadow, light ]; |
113 | | - nzGeometry.faces[ 1 ].vertexColors = [ shadow, shadow, light ]; |
114 | | - nzGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5; |
115 | | - nzGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5; |
116 | | - nzGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5; |
| 112 | + const nzGeometry = new THREE.PlaneBufferGeometry( 100, 100 ).toNonIndexed(); |
| 113 | + nzGeometry.setAttribute( 'color', nzGeometry.attributes.position.clone() ); |
| 114 | + nzGeometry.attributes.color.copyColorsArray( [ light, shadow, light, shadow, shadow, light ] ); |
| 115 | + nzGeometry.attributes.uv.array[ 1 ] = 0.5; |
| 116 | + nzGeometry.attributes.uv.array[ 5 ] = 0.5; |
| 117 | + nzGeometry.attributes.uv.array[ 11 ] = 0.5; |
117 | 118 | nzGeometry.rotateY( Math.PI ); |
118 | 119 | nzGeometry.translate( 0, 0, - 50 ); |
119 | 120 |
|
120 | 121 | // |
121 | 122 |
|
122 | | - let geometry = new THREE.Geometry(); |
| 123 | + const geometries = []; |
123 | 124 |
|
124 | 125 | for ( let z = 0; z < worldDepth; z ++ ) { |
125 | 126 |
|
|
150 | 151 |
|
151 | 152 | if ( a + c > b + d ) { |
152 | 153 |
|
153 | | - let colors = py2Geometry.faces[ 0 ].vertexColors; |
| 154 | + const colors = []; |
| 155 | + |
154 | 156 | colors[ 0 ] = b === 0 ? shadow : light; |
155 | 157 | colors[ 1 ] = c === 0 ? shadow : light; |
156 | 158 | colors[ 2 ] = a === 0 ? shadow : light; |
| 159 | + colors[ 3 ] = c === 0 ? shadow : light; |
| 160 | + colors[ 4 ] = d === 0 ? shadow : light; |
| 161 | + colors[ 5 ] = a === 0 ? shadow : light; |
157 | 162 |
|
158 | | - colors = py2Geometry.faces[ 1 ].vertexColors; |
159 | | - colors[ 0 ] = c === 0 ? shadow : light; |
160 | | - colors[ 1 ] = d === 0 ? shadow : light; |
161 | | - colors[ 2 ] = a === 0 ? shadow : light; |
162 | | - |
163 | | - geometry.merge( py2Geometry, matrix ); |
| 163 | + py2Geometry.attributes.color.copyColorsArray( colors ); |
| 164 | + geometries.push( py2Geometry.clone().applyMatrix4( matrix ) ); |
164 | 165 |
|
165 | 166 | } else { |
166 | 167 |
|
167 | | - let colors = pyGeometry.faces[ 0 ].vertexColors; |
| 168 | + const colors = []; |
| 169 | + |
168 | 170 | colors[ 0 ] = a === 0 ? shadow : light; |
169 | 171 | colors[ 1 ] = b === 0 ? shadow : light; |
170 | 172 | colors[ 2 ] = d === 0 ? shadow : light; |
| 173 | + colors[ 3 ] = b === 0 ? shadow : light; |
| 174 | + colors[ 4 ] = c === 0 ? shadow : light; |
| 175 | + colors[ 5 ] = d === 0 ? shadow : light; |
171 | 176 |
|
172 | | - colors = pyGeometry.faces[ 1 ].vertexColors; |
173 | | - colors[ 0 ] = b === 0 ? shadow : light; |
174 | | - colors[ 1 ] = c === 0 ? shadow : light; |
175 | | - colors[ 2 ] = d === 0 ? shadow : light; |
176 | | - |
177 | | - geometry.merge( pyGeometry, matrix ); |
| 177 | + pyGeometry.attributes.color.copyColorsArray( colors ); |
| 178 | + geometries.push( pyGeometry.clone().applyMatrix4( matrix ) ); |
178 | 179 |
|
179 | 180 | } |
180 | 181 |
|
181 | 182 | if ( ( px != h && px != h + 1 ) || x == 0 ) { |
182 | 183 |
|
183 | | - let colors = pxGeometry.faces[ 0 ].vertexColors; |
184 | | - colors[ 0 ] = pxpz > px && x > 0 ? shadow : light; |
185 | | - colors[ 2 ] = pxnz > px && x > 0 ? shadow : light; |
| 184 | + const colors = []; |
186 | 185 |
|
187 | | - colors = pxGeometry.faces[ 1 ].vertexColors; |
| 186 | + colors[ 0 ] = pxpz > px && x > 0 ? shadow : light; |
| 187 | + colors[ 1 ] = shadow; |
188 | 188 | colors[ 2 ] = pxnz > px && x > 0 ? shadow : light; |
| 189 | + colors[ 3 ] = shadow; |
| 190 | + colors[ 4 ] = shadow; |
| 191 | + colors[ 5 ] = pxnz > px && x > 0 ? shadow : light; |
189 | 192 |
|
190 | | - geometry.merge( pxGeometry, matrix ); |
| 193 | + pxGeometry.attributes.color.copyColorsArray( colors ); |
| 194 | + geometries.push( pxGeometry.clone().applyMatrix4( matrix ) ); |
191 | 195 |
|
192 | 196 | } |
193 | 197 |
|
194 | 198 | if ( ( nx != h && nx != h + 1 ) || x == worldWidth - 1 ) { |
195 | 199 |
|
196 | | - let colors = nxGeometry.faces[ 0 ].vertexColors; |
197 | | - colors[ 0 ] = nxnz > nx && x < worldWidth - 1 ? shadow : light; |
198 | | - colors[ 2 ] = nxpz > nx && x < worldWidth - 1 ? shadow : light; |
| 200 | + const colors = []; |
199 | 201 |
|
200 | | - colors = nxGeometry.faces[ 1 ].vertexColors; |
| 202 | + colors[ 0 ] = nxnz > nx && x < worldWidth - 1 ? shadow : light; |
| 203 | + colors[ 1 ] = shadow; |
201 | 204 | colors[ 2 ] = nxpz > nx && x < worldWidth - 1 ? shadow : light; |
| 205 | + colors[ 3 ] = shadow; |
| 206 | + colors[ 4 ] = shadow; |
| 207 | + colors[ 5 ] = nxpz > nx && x < worldWidth - 1 ? shadow : light; |
202 | 208 |
|
203 | | - geometry.merge( nxGeometry, matrix ); |
| 209 | + nxGeometry.attributes.color.copyColorsArray( colors ); |
| 210 | + geometries.push( nxGeometry.clone().applyMatrix4( matrix ) ); |
204 | 211 |
|
205 | 212 | } |
206 | 213 |
|
207 | 214 | if ( ( pz != h && pz != h + 1 ) || z == worldDepth - 1 ) { |
208 | 215 |
|
209 | | - let colors = pzGeometry.faces[ 0 ].vertexColors; |
210 | | - colors[ 0 ] = nxpz > pz && z < worldDepth - 1 ? shadow : light; |
211 | | - colors[ 2 ] = pxpz > pz && z < worldDepth - 1 ? shadow : light; |
| 216 | + const colors = []; |
212 | 217 |
|
213 | | - colors = pzGeometry.faces[ 1 ].vertexColors; |
| 218 | + colors[ 0 ] = nxpz > pz && z < worldDepth - 1 ? shadow : light; |
| 219 | + colors[ 1 ] = shadow; |
214 | 220 | colors[ 2 ] = pxpz > pz && z < worldDepth - 1 ? shadow : light; |
| 221 | + colors[ 3 ] = shadow; |
| 222 | + colors[ 4 ] = shadow; |
| 223 | + colors[ 5 ] = pxpz > pz && z < worldDepth - 1 ? shadow : light; |
215 | 224 |
|
216 | | - geometry.merge( pzGeometry, matrix ); |
| 225 | + pzGeometry.attributes.color.copyColorsArray( colors ); |
| 226 | + geometries.push( pzGeometry.clone().applyMatrix4( matrix ) ); |
217 | 227 |
|
218 | 228 | } |
219 | 229 |
|
220 | 230 | if ( ( nz != h && nz != h + 1 ) || z == 0 ) { |
221 | 231 |
|
222 | | - let colors = nzGeometry.faces[ 0 ].vertexColors; |
223 | | - colors[ 0 ] = pxnz > nz && z > 0 ? shadow : light; |
224 | | - colors[ 2 ] = nxnz > nz && z > 0 ? shadow : light; |
| 232 | + const colors = []; |
225 | 233 |
|
226 | | - colors = nzGeometry.faces[ 1 ].vertexColors; |
| 234 | + colors[ 0 ] = pxnz > nz && z > 0 ? shadow : light; |
| 235 | + colors[ 1 ] = shadow; |
227 | 236 | colors[ 2 ] = nxnz > nz && z > 0 ? shadow : light; |
| 237 | + colors[ 3 ] = shadow; |
| 238 | + colors[ 4 ] = shadow; |
| 239 | + colors[ 5 ] = nxnz > nz && z > 0 ? shadow : light; |
228 | 240 |
|
229 | | - geometry.merge( nzGeometry, matrix ); |
| 241 | + nzGeometry.attributes.color.copyColorsArray( colors ); |
| 242 | + geometries.push( nzGeometry.clone().applyMatrix4( matrix ) ); |
230 | 243 |
|
231 | 244 | } |
232 | 245 |
|
233 | 246 | } |
234 | 247 |
|
235 | 248 | } |
236 | 249 |
|
237 | | - geometry = new THREE.BufferGeometry().fromGeometry( geometry ); |
| 250 | + const geometry = BufferGeometryUtils.mergeBufferGeometries( geometries ); |
| 251 | + geometry.computeBoundingSphere(); |
238 | 252 |
|
239 | 253 | const texture = new THREE.TextureLoader().load( 'textures/minecraft/atlas.png' ); |
240 | 254 | texture.magFilter = THREE.NearestFilter; |
241 | | - texture.minFilter = THREE.LinearMipmapLinearFilter; |
242 | 255 |
|
243 | 256 | const mesh = new THREE.Mesh( |
244 | 257 | geometry, |
|
0 commit comments