Skip to content

Commit e4188f8

Browse files
authored
Merge pull request #21005 from Mugen87/dev44
Examples: More clean up.
2 parents 273011d + 37c5ec0 commit e4188f8

File tree

1 file changed

+86
-73
lines changed

1 file changed

+86
-73
lines changed

examples/webgl_geometry_minecraft_ao.html

Lines changed: 86 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import { FirstPersonControls } from './jsm/controls/FirstPersonControls.js';
3232
import { ImprovedNoise } from './jsm/math/ImprovedNoise.js';
33+
import { BufferGeometryUtils } from './jsm/utils/BufferGeometryUtils.js';
3334

3435
let container, stats;
3536

@@ -63,63 +64,63 @@
6364

6465
const matrix = new THREE.Matrix4();
6566

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;
7273
pxGeometry.rotateY( Math.PI / 2 );
7374
pxGeometry.translate( 50, 0, 0 );
7475

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;
8182
nxGeometry.rotateY( - Math.PI / 2 );
8283
nxGeometry.translate( - 50, 0, 0 );
8384

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;
9091
pyGeometry.rotateX( - Math.PI / 2 );
9192
pyGeometry.translate( 0, 50, 0 );
9293

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;
99100
py2Geometry.rotateX( - Math.PI / 2 );
100101
py2Geometry.rotateY( Math.PI / 2 );
101102
py2Geometry.translate( 0, 50, 0 );
102103

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;
109110
pzGeometry.translate( 0, 0, 50 );
110111

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;
117118
nzGeometry.rotateY( Math.PI );
118119
nzGeometry.translate( 0, 0, - 50 );
119120

120121
//
121122

122-
let geometry = new THREE.Geometry();
123+
const geometries = [];
123124

124125
for ( let z = 0; z < worldDepth; z ++ ) {
125126

@@ -150,95 +151,107 @@
150151

151152
if ( a + c > b + d ) {
152153

153-
let colors = py2Geometry.faces[ 0 ].vertexColors;
154+
const colors = [];
155+
154156
colors[ 0 ] = b === 0 ? shadow : light;
155157
colors[ 1 ] = c === 0 ? shadow : light;
156158
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;
157162

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 ) );
164165

165166
} else {
166167

167-
let colors = pyGeometry.faces[ 0 ].vertexColors;
168+
const colors = [];
169+
168170
colors[ 0 ] = a === 0 ? shadow : light;
169171
colors[ 1 ] = b === 0 ? shadow : light;
170172
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;
171176

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 ) );
178179

179180
}
180181

181182
if ( ( px != h && px != h + 1 ) || x == 0 ) {
182183

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 = [];
186185

187-
colors = pxGeometry.faces[ 1 ].vertexColors;
186+
colors[ 0 ] = pxpz > px && x > 0 ? shadow : light;
187+
colors[ 1 ] = shadow;
188188
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;
189192

190-
geometry.merge( pxGeometry, matrix );
193+
pxGeometry.attributes.color.copyColorsArray( colors );
194+
geometries.push( pxGeometry.clone().applyMatrix4( matrix ) );
191195

192196
}
193197

194198
if ( ( nx != h && nx != h + 1 ) || x == worldWidth - 1 ) {
195199

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 = [];
199201

200-
colors = nxGeometry.faces[ 1 ].vertexColors;
202+
colors[ 0 ] = nxnz > nx && x < worldWidth - 1 ? shadow : light;
203+
colors[ 1 ] = shadow;
201204
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;
202208

203-
geometry.merge( nxGeometry, matrix );
209+
nxGeometry.attributes.color.copyColorsArray( colors );
210+
geometries.push( nxGeometry.clone().applyMatrix4( matrix ) );
204211

205212
}
206213

207214
if ( ( pz != h && pz != h + 1 ) || z == worldDepth - 1 ) {
208215

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 = [];
212217

213-
colors = pzGeometry.faces[ 1 ].vertexColors;
218+
colors[ 0 ] = nxpz > pz && z < worldDepth - 1 ? shadow : light;
219+
colors[ 1 ] = shadow;
214220
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;
215224

216-
geometry.merge( pzGeometry, matrix );
225+
pzGeometry.attributes.color.copyColorsArray( colors );
226+
geometries.push( pzGeometry.clone().applyMatrix4( matrix ) );
217227

218228
}
219229

220230
if ( ( nz != h && nz != h + 1 ) || z == 0 ) {
221231

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 = [];
225233

226-
colors = nzGeometry.faces[ 1 ].vertexColors;
234+
colors[ 0 ] = pxnz > nz && z > 0 ? shadow : light;
235+
colors[ 1 ] = shadow;
227236
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;
228240

229-
geometry.merge( nzGeometry, matrix );
241+
nzGeometry.attributes.color.copyColorsArray( colors );
242+
geometries.push( nzGeometry.clone().applyMatrix4( matrix ) );
230243

231244
}
232245

233246
}
234247

235248
}
236249

237-
geometry = new THREE.BufferGeometry().fromGeometry( geometry );
250+
const geometry = BufferGeometryUtils.mergeBufferGeometries( geometries );
251+
geometry.computeBoundingSphere();
238252

239253
const texture = new THREE.TextureLoader().load( 'textures/minecraft/atlas.png' );
240254
texture.magFilter = THREE.NearestFilter;
241-
texture.minFilter = THREE.LinearMipmapLinearFilter;
242255

243256
const mesh = new THREE.Mesh(
244257
geometry,

0 commit comments

Comments
 (0)