Three Blocks

Wave of Connection / source

Open preview
helpers/exampleGeometries.jsPlain text ↗
/**
 * Gallery-local geometry helpers.
 *
 * These deliberately live with the examples instead of re-exporting private
 * package source. Gallery bundles can use them without turning implementation
 * details into supported package entry points.
 */

import * as THREE from 'three/webgpu';

class BirdGeometry extends THREE.BufferGeometry {

	constructor( bodyLength = 50, bodyBellyDrop = 8, wingSpan = 40, wingChord = 30, wingTipForward = 5 ) {

		super();
		const halfWingChord = wingChord / 2;
		const halfWingSpan = wingSpan / 2;
		const tailZ = - bodyLength * 0.4;
		const bellyZ = bodyLength * 0.2;
		const headZ = bodyLength * 0.6;
		const positions = new THREE.BufferAttribute( new Float32Array( 27 ), 3 );
		const normals = new THREE.BufferAttribute( new Float32Array( 27 ), 3 );
		this.setAttribute( 'position', positions );
		this.setAttribute( 'normal', normals );
		let offset = 0;
		const push = ( ...values ) => {

			for ( const value of values ) positions.array[ offset ++ ] = value;

		};
		push( 0, 0, tailZ, 0, - bodyBellyDrop, bellyZ, 0, 0, headZ );
		push( 0, 0, - halfWingChord, - halfWingSpan, 0, wingTipForward, 0, 0, halfWingChord );
		push( 0, 0, halfWingChord, halfWingSpan, 0, wingTipForward, 0, 0, - halfWingChord );

		const a = new THREE.Vector3();
		const b = new THREE.Vector3();
		const c = new THREE.Vector3();
		const edge = new THREE.Vector3();
		const normal = new THREE.Vector3();
		const setFaceNormal = ( i0, i1, i2 ) => {

			a.fromBufferAttribute( positions, i0 );
			b.fromBufferAttribute( positions, i1 );
			c.fromBufferAttribute( positions, i2 );
			normal.subVectors( c, b ).cross( edge.subVectors( a, b ) ).normalize();
			normals.setXYZ( i0, normal.x, normal.y, normal.z );
			normals.setXYZ( i1, normal.x, normal.y, normal.z );
			normals.setXYZ( i2, normal.x, normal.y, normal.z );

		};
		setFaceNormal( 0, 1, 2 );
		setFaceNormal( 3, 4, 5 );
		setFaceNormal( 6, 7, 8 );

	}

}

class BoxNoFaceGeometry extends THREE.BufferGeometry {

	constructor( width = 1, height = 1, depth = 1 ) {

		super();
		const x = width / 2;
		const y = height / 2;
		const z = depth / 2;
		const vertices = new Float32Array( [
			- x, - y, z, x, - y, z,
			x, - y, z, x, y, z,
			x, y, z, - x, y, z,
			- x, y, z, - x, - y, z,
			- x, - y, - z, x, - y, - z,
			x, - y, - z, x, y, - z,
			x, y, - z, - x, y, - z,
			- x, y, - z, - x, - y, - z,
			- x, - y, z, - x, - y, - z,
			x, - y, z, x, - y, - z,
			x, y, z, x, y, - z,
			- x, y, z, - x, y, - z,
		] );
		this.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
		this.setAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( vertices.length ), 3 ) );
		this.computeBoundingBox();
		this.computeBoundingSphere();

	}

}

const triangleVertices = new Float32Array( [
	- 1, - 1, 0,
	3, - 1, 0,
	- 1, 3, 0,
] );

class TriangleGeometry extends THREE.BufferGeometry {

	constructor() {

		super();
		this.setAttribute( 'position', new THREE.BufferAttribute( triangleVertices, 3 ) );
		this.setAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( [
			0, 0, 1,
			0, 0, 1,
			0, 0, 1,
		] ), 3 ) );
		this.setAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( [
			0, 0,
			2, 0,
			0, 2,
		] ), 2 ) );
		this.computeBoundingBox();
		this.computeBoundingSphere();

	}

}

export { BirdGeometry, BoxNoFaceGeometry, TriangleGeometry };