|
| 1 | +import { RouteMeta } from '@analogjs/router'; |
| 2 | +import { CUSTOM_ELEMENTS_SCHEMA, Component, Directive } from '@angular/core'; |
| 3 | +import { NgtArgs, NgtCanvas, injectBeforeRender } from 'angular-three'; |
| 4 | +import { NgtcPhysics } from 'angular-three-cannon'; |
| 5 | +import { injectSphere } from 'angular-three-cannon/services'; |
| 6 | +import { NgtpEffectComposer } from 'angular-three-postprocessing'; |
| 7 | +import { NgtpBloom } from 'angular-three-postprocessing/effects'; |
| 8 | +import { injectNgtsTextureLoader } from 'angular-three-soba/loaders'; |
| 9 | +import { NgtsEnvironment, NgtsSky } from 'angular-three-soba/staging'; |
| 10 | +import * as THREE from 'three'; |
| 11 | + |
| 12 | +export const routeMeta: RouteMeta = { |
| 13 | + title: 'Object Clump w/ Physics', |
| 14 | +}; |
| 15 | + |
| 16 | +@Directive({ selector: 'clump-pointer', standalone: true }) |
| 17 | +class Pointer { |
| 18 | + readonly pointerBody = injectSphere(() => ({ type: 'Kinematic', args: [3], position: [0, 0, 0] })); |
| 19 | + |
| 20 | + constructor() { |
| 21 | + injectBeforeRender(({ pointer, viewport }) => { |
| 22 | + this.pointerBody.api().position.set((pointer.x * viewport.width) / 2, (pointer.y * viewport.height) / 2, 0); |
| 23 | + }); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +const mat = new THREE.Matrix4(); |
| 28 | +const vec = new THREE.Vector3(); |
| 29 | + |
| 30 | +@Component({ |
| 31 | + selector: 'object-clump', |
| 32 | + standalone: true, |
| 33 | + templateUrl: 'object-clump.html', |
| 34 | + imports: [NgtArgs], |
| 35 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 36 | +}) |
| 37 | +class ObjectClump { |
| 38 | + readonly count = 40; |
| 39 | + readonly texture = injectNgtsTextureLoader(() => 'cross.jpg'); |
| 40 | + |
| 41 | + readonly sphereBody = injectSphere<THREE.InstancedMesh>(() => ({ |
| 42 | + args: [1], |
| 43 | + mass: 1, |
| 44 | + angularDamping: 0.1, |
| 45 | + linearDamping: 0.65, |
| 46 | + position: [ |
| 47 | + THREE.MathUtils.randFloatSpread(20), |
| 48 | + THREE.MathUtils.randFloatSpread(20), |
| 49 | + THREE.MathUtils.randFloatSpread(20), |
| 50 | + ], |
| 51 | + })); |
| 52 | + |
| 53 | + onBeforeRender(object: THREE.InstancedMesh) { |
| 54 | + for (let i = 0; i < this.count; i++) { |
| 55 | + // Get current whereabouts of the instanced sphere |
| 56 | + object.getMatrixAt(i, mat); |
| 57 | + // Normalize the position and multiply by a negative force. |
| 58 | + // This is enough to drive it towards the center-point. |
| 59 | + this.sphereBody |
| 60 | + .api() |
| 61 | + .at(i) |
| 62 | + .applyForce(vec.setFromMatrixPosition(mat).normalize().multiplyScalar(-50).toArray(), [0, 0, 0]); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +@Component({ |
| 68 | + standalone: true, |
| 69 | + templateUrl: 'scene.html', |
| 70 | + imports: [NgtArgs, NgtcPhysics, NgtsEnvironment, NgtsSky, NgtpEffectComposer, NgtpBloom, ObjectClump, Pointer], |
| 71 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 72 | +}) |
| 73 | +class SceneGraph {} |
| 74 | + |
| 75 | +@Component({ |
| 76 | + standalone: true, |
| 77 | + templateUrl: 'index.html', |
| 78 | + imports: [NgtCanvas], |
| 79 | +}) |
| 80 | +export default class Clump { |
| 81 | + readonly scene = SceneGraph; |
| 82 | +} |
0 commit comments