Three Blocks

Minimal scene / source

Open preview
src/scene.tsPlain text ↗
import * as THREE from 'three/webgpu';
import { BrandStage, createBrandTrio } from './look';
import type { PointerState, ScrollState } from 'three-blocks/app';

export const sceneKey = 'scene';

export class Experience extends THREE.Group {

	readonly hot = { spin: new THREE.Euler(), velocity: new THREE.Vector2( 0.16, 0.24 ) };

	private readonly stage = new BrandStage();
	private readonly subjects: THREE.Mesh<THREE.BufferGeometry, THREE.MeshStandardMaterial>[];

	constructor() {

		super();
		this.subjects = createBrandTrio( [ 0.78, 0.59, 1.02 ], { color: 0xf4ae0c, metalness: 0.18, roughness: 0.24 } );
		this.subjects.forEach( ( subject, index ) => {

			subject.position.set( ( index - 1 ) * 1.28, index === 1 ? 0.22 : - 0.08, 0 );
			this.add( subject );

		} );
		this.add( this.stage );

	}

	pointer( pointer: PointerState ): void {

		this.hot.velocity.x = THREE.MathUtils.lerp( this.hot.velocity.x, 0.16 + pointer.y * 0.2, 0.12 );
		this.hot.velocity.y = THREE.MathUtils.lerp( this.hot.velocity.y, 0.24 + pointer.x * 0.2, 0.12 );

	}

	update( delta: number, _scroll: ScrollState ): void {

		this.hot.spin.x += this.hot.velocity.x * delta;
		this.hot.spin.y += this.hot.velocity.y * delta;
		const { x, y } = this.hot.spin;
		this.subjects.forEach( ( mesh, i ) => mesh.rotation.set( x * ( 1 + i * 0.12 ), y * ( 1 - i * 0.1 ), i * 0.18 ) );

	}

	dispose(): void {

		this.stage.dispose();
		this.subjects.flatMap( mesh => [ mesh.geometry, mesh.material ] ).forEach( resource => resource.dispose() );

	}

}

export function createScene(): Experience {

	return new Experience();

}