|
| 1 | +import { RouteMeta } from '@analogjs/router'; |
| 2 | +import { Component, computed, CUSTOM_ELEMENTS_SCHEMA, effect, Input } from '@angular/core'; |
| 3 | +import { Triplet } from '@pmndrs/cannon-worker-api'; |
| 4 | +import { injectBeforeRender, NgtArgs, NgtCanvas, NgtSignalStore } from 'angular-three'; |
| 5 | +import { NgtcPhysics } from 'angular-three-cannon'; |
| 6 | +import { injectBox, injectPlane, injectSphere } from 'angular-three-cannon/services'; |
| 7 | +import { NgtsStats } from 'angular-three-soba/performance'; |
| 8 | +// @ts-ignore |
| 9 | +import niceColors from 'nice-color-palettes'; |
| 10 | +import * as THREE from 'three'; |
| 11 | + |
| 12 | +const niceColor = niceColors[Math.floor(Math.random() * niceColors.length)]; |
| 13 | + |
| 14 | +export const routeMeta: RouteMeta = { |
| 15 | + title: 'Kinematic Cube', |
| 16 | +}; |
| 17 | + |
| 18 | +@Component({ |
| 19 | + selector: 'kinematic-spheres', |
| 20 | + standalone: true, |
| 21 | + templateUrl: 'kinematic-spheres.html', |
| 22 | + imports: [NgtArgs], |
| 23 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 24 | +}) |
| 25 | +class Spheres extends NgtSignalStore<{ count: number }> { |
| 26 | + @Input() set count(count: number) { |
| 27 | + this.set({ count }); |
| 28 | + } |
| 29 | + |
| 30 | + readonly sphereCount = this.select('count'); |
| 31 | + readonly colors = computed(() => new Float32Array(this.sphereCount() * 3)); |
| 32 | + |
| 33 | + readonly radius = 1; |
| 34 | + readonly sphereBody = injectSphere<THREE.InstancedMesh>((index) => ({ |
| 35 | + args: [this.radius], |
| 36 | + mass: 1, |
| 37 | + position: [Math.random() - 0.5, Math.random() - 0.5, index * 2], |
| 38 | + })); |
| 39 | + |
| 40 | + constructor() { |
| 41 | + super({ count: 100 }); |
| 42 | + effect(() => { |
| 43 | + const kinematicCount = this.sphereCount(); |
| 44 | + const colors = this.colors(); |
| 45 | + const color = new THREE.Color(); |
| 46 | + for (let i = 0; i < kinematicCount; i++) { |
| 47 | + color |
| 48 | + .set(niceColor[Math.floor(Math.random() * 5)]) |
| 49 | + .convertSRGBToLinear() |
| 50 | + .toArray(colors, i * 3); |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +@Component({ |
| 57 | + selector: 'kinematic-box', |
| 58 | + standalone: true, |
| 59 | + templateUrl: 'kinematic-box.html', |
| 60 | + imports: [NgtArgs], |
| 61 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 62 | +}) |
| 63 | +class Box { |
| 64 | + readonly boxSize = [4, 4, 4] as Triplet; |
| 65 | + readonly boxBody = injectBox<THREE.Mesh>(() => ({ |
| 66 | + mass: 1, |
| 67 | + type: 'Kinematic', |
| 68 | + args: this.boxSize, |
| 69 | + })); |
| 70 | + |
| 71 | + constructor() { |
| 72 | + injectBeforeRender(({ clock }) => { |
| 73 | + const t = clock.getElapsedTime(); |
| 74 | + this.boxBody.api().position.set(Math.sin(t * 2) * 5, Math.cos(t * 2) * 5, 3); |
| 75 | + this.boxBody.api().rotation.set(Math.sin(t * 6), Math.cos(t * 6), 0); |
| 76 | + }); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +@Component({ |
| 81 | + selector: 'kinematic-plane', |
| 82 | + standalone: true, |
| 83 | + templateUrl: 'kinematic-plane.html', |
| 84 | + imports: [NgtArgs], |
| 85 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 86 | +}) |
| 87 | +class Plane extends NgtSignalStore<{ color: THREE.ColorRepresentation; position: Triplet; rotation: Triplet }> { |
| 88 | + @Input({ required: true }) set color(color: THREE.ColorRepresentation) { |
| 89 | + this.set({ color }); |
| 90 | + } |
| 91 | + |
| 92 | + @Input() set position(position: Triplet) { |
| 93 | + this.set({ position }); |
| 94 | + } |
| 95 | + |
| 96 | + @Input() set rotation(rotation: Triplet) { |
| 97 | + this.set({ rotation }); |
| 98 | + } |
| 99 | + |
| 100 | + readonly planeColor = this.select('color'); |
| 101 | + readonly #position = this.select('position'); |
| 102 | + readonly #rotation = this.select('rotation'); |
| 103 | + |
| 104 | + readonly args = [1000, 1000]; |
| 105 | + readonly planeBody = injectPlane<THREE.Mesh>(() => ({ |
| 106 | + args: this.args, |
| 107 | + position: this.#position(), |
| 108 | + rotation: this.#rotation(), |
| 109 | + })); |
| 110 | + |
| 111 | + constructor() { |
| 112 | + super({ position: [0, 0, 0], rotation: [0, 0, 0] }); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +@Component({ |
| 117 | + standalone: true, |
| 118 | + templateUrl: 'scene.html', |
| 119 | + imports: [NgtArgs, NgtcPhysics, Plane, Box, Spheres], |
| 120 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 121 | +}) |
| 122 | +class SceneGraph { |
| 123 | + readonly niceColor = niceColor; |
| 124 | +} |
| 125 | + |
| 126 | +@Component({ |
| 127 | + standalone: true, |
| 128 | + templateUrl: 'index.html', |
| 129 | + imports: [NgtCanvas, NgtsStats], |
| 130 | +}) |
| 131 | +export default class KinematicCube { |
| 132 | + readonly scene = SceneGraph; |
| 133 | +} |
0 commit comments