Skip to content

Commit 5aaec4c

Browse files
authored
Geometry: Convert to classes. (#21635)
1 parent 30c2c57 commit 5aaec4c

11 files changed

+525
-568
lines changed

examples/webgl_gpgpu_birds.html

Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -312,93 +312,96 @@
312312
const BIRDS = WIDTH * WIDTH;
313313

314314
// Custom Geometry - using 3 triangles each. No UVs, no normals currently.
315-
function BirdGeometry() {
315+
class BirdGeometry extends THREE.BufferGeometry {
316316

317-
const triangles = BIRDS * 3;
318-
const points = triangles * 3;
317+
constructor() {
319318

320-
THREE.BufferGeometry.call( this );
319+
super();
321320

322-
const vertices = new THREE.BufferAttribute( new Float32Array( points * 3 ), 3 );
323-
const birdColors = new THREE.BufferAttribute( new Float32Array( points * 3 ), 3 );
324-
const references = new THREE.BufferAttribute( new Float32Array( points * 2 ), 2 );
325-
const birdVertex = new THREE.BufferAttribute( new Float32Array( points ), 1 );
321+
const triangles = BIRDS * 3;
322+
const points = triangles * 3;
326323

327-
this.setAttribute( 'position', vertices );
328-
this.setAttribute( 'birdColor', birdColors );
329-
this.setAttribute( 'reference', references );
330-
this.setAttribute( 'birdVertex', birdVertex );
324+
const vertices = new THREE.BufferAttribute( new Float32Array( points * 3 ), 3 );
325+
const birdColors = new THREE.BufferAttribute( new Float32Array( points * 3 ), 3 );
326+
const references = new THREE.BufferAttribute( new Float32Array( points * 2 ), 2 );
327+
const birdVertex = new THREE.BufferAttribute( new Float32Array( points ), 1 );
331328

332-
// this.setAttribute( 'normal', new Float32Array( points * 3 ), 3 );
329+
this.setAttribute( 'position', vertices );
330+
this.setAttribute( 'birdColor', birdColors );
331+
this.setAttribute( 'reference', references );
332+
this.setAttribute( 'birdVertex', birdVertex );
333333

334+
// this.setAttribute( 'normal', new Float32Array( points * 3 ), 3 );
334335

335-
let v = 0;
336336

337-
function verts_push() {
337+
let v = 0;
338338

339-
for ( let i = 0; i < arguments.length; i ++ ) {
339+
function verts_push() {
340340

341-
vertices.array[ v ++ ] = arguments[ i ];
341+
for ( let i = 0; i < arguments.length; i ++ ) {
342+
343+
vertices.array[ v ++ ] = arguments[ i ];
344+
345+
}
342346

343347
}
344348

345-
}
349+
const wingsSpan = 20;
346350

347-
const wingsSpan = 20;
351+
for ( let f = 0; f < BIRDS; f ++ ) {
348352

349-
for ( let f = 0; f < BIRDS; f ++ ) {
353+
// Body
354+
verts_push(
355+
0, - 0, - 20,
356+
0, 4, - 20,
357+
0, 0, 30
358+
);
350359

351-
// Body
352-
verts_push(
353-
0, - 0, - 20,
354-
0, 4, - 20,
355-
0, 0, 30
356-
);
360+
// Left Wing
361+
verts_push(
362+
0, 0, - 15,
363+
- wingsSpan, 0, 0,
364+
0, 0, 15
365+
);
357366

358-
// Left Wing
359-
verts_push(
360-
0, 0, - 15,
361-
- wingsSpan, 0, 0,
362-
0, 0, 15
363-
);
367+
// Right Wing
368+
verts_push(
369+
0, 0, 15,
370+
wingsSpan, 0, 0,
371+
0, 0, - 15
372+
);
364373

365-
// Right Wing
366-
verts_push(
367-
0, 0, 15,
368-
wingsSpan, 0, 0,
369-
0, 0, - 15
370-
);
374+
}
371375

372-
}
376+
for ( let v = 0; v < triangles * 3; v ++ ) {
373377

374-
for ( let v = 0; v < triangles * 3; v ++ ) {
378+
const i = ~ ~ ( v / 3 );
379+
const x = ( i % WIDTH ) / WIDTH;
380+
const y = ~ ~ ( i / WIDTH ) / WIDTH;
375381

376-
const i = ~ ~ ( v / 3 );
377-
const x = ( i % WIDTH ) / WIDTH;
378-
const y = ~ ~ ( i / WIDTH ) / WIDTH;
382+
const c = new THREE.Color(
383+
0x444444 +
384+
~ ~ ( v / 9 ) / BIRDS * 0x666666
385+
);
379386

380-
const c = new THREE.Color(
381-
0x444444 +
382-
~ ~ ( v / 9 ) / BIRDS * 0x666666
383-
);
387+
birdColors.array[ v * 3 + 0 ] = c.r;
388+
birdColors.array[ v * 3 + 1 ] = c.g;
389+
birdColors.array[ v * 3 + 2 ] = c.b;
384390

385-
birdColors.array[ v * 3 + 0 ] = c.r;
386-
birdColors.array[ v * 3 + 1 ] = c.g;
387-
birdColors.array[ v * 3 + 2 ] = c.b;
391+
references.array[ v * 2 ] = x;
392+
references.array[ v * 2 + 1 ] = y;
388393

389-
references.array[ v * 2 ] = x;
390-
references.array[ v * 2 + 1 ] = y;
394+
birdVertex.array[ v ] = v % 9;
391395

392-
birdVertex.array[ v ] = v % 9;
396+
}
393397

394-
}
398+
this.scale( 0.2, 0.2, 0.2 );
395399

396-
this.scale( 0.2, 0.2, 0.2 );
400+
}
397401

398402
}
399403

400-
BirdGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
401-
404+
//
402405

403406
let container, stats;
404407
let camera, scene, renderer;

0 commit comments

Comments
 (0)