|
| 1 | +import { |
| 2 | + ChangeDetectionStrategy, |
| 3 | + Component, |
| 4 | + CUSTOM_ELEMENTS_SCHEMA, |
| 5 | + effect, |
| 6 | + ElementRef, |
| 7 | + input, |
| 8 | + signal, |
| 9 | + viewChild, |
| 10 | +} from '@angular/core'; |
| 11 | +import { Meta } from '@storybook/angular'; |
| 12 | +import { injectBeforeRender, NgtArgs, NgtThreeElements } from 'angular-three'; |
| 13 | +import { injectHelper } from 'angular-three-soba/abstractions'; |
| 14 | +import { NgtsOrbitControls } from 'angular-three-soba/controls'; |
| 15 | +import { NgtsBVH } from 'angular-three-soba/performances'; |
| 16 | +import * as THREE from 'three'; |
| 17 | +import { MeshBVHHelper } from 'three-mesh-bvh'; |
| 18 | +import { storyDecorators, storyFunction } from '../setup-canvas'; |
| 19 | + |
| 20 | +@Component({ |
| 21 | + selector: 'torus-bvh', |
| 22 | + template: ` |
| 23 | + <ngts-bvh> |
| 24 | + <ngt-mesh |
| 25 | + #mesh |
| 26 | + [position.z]="positionZ()" |
| 27 | + (pointerover)="color.set('#ffff00')" |
| 28 | + (pointerout)="color.set('#ff0000')" |
| 29 | + > |
| 30 | + <ngt-torus-knot-geometry *args="[1, 0.4, 250, 50]" /> |
| 31 | + <ngt-mesh-basic-material [color]="color()" /> |
| 32 | + </ngt-mesh> |
| 33 | + </ngts-bvh> |
| 34 | + `, |
| 35 | + imports: [NgtsBVH, NgtArgs], |
| 36 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 37 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 38 | +}) |
| 39 | +class TorusBVH { |
| 40 | + positionZ = input(0); |
| 41 | + |
| 42 | + private meshRef = viewChild.required<ElementRef<THREE.Mesh>>('mesh'); |
| 43 | + |
| 44 | + protected color = signal('#ff0000'); |
| 45 | + |
| 46 | + constructor() { |
| 47 | + injectHelper(this.meshRef, () => MeshBVHHelper); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +const pointDist = 5; |
| 52 | +const raycaster = new THREE.Raycaster(); |
| 53 | +const origVec = new THREE.Vector3(); |
| 54 | +const dirVec = new THREE.Vector3(); |
| 55 | + |
| 56 | +@Component({ |
| 57 | + selector: 'raycast-obj', |
| 58 | + template: ` |
| 59 | + <ngt-group #obj> |
| 60 | + <ngt-mesh #original> |
| 61 | + <ngt-sphere-geometry *args="[0.1, 20, 20]" /> |
| 62 | + <ngt-mesh-basic-material /> |
| 63 | + </ngt-mesh> |
| 64 | + <ngt-mesh #hit> |
| 65 | + <ngt-sphere-geometry *args="[0.1, 20, 20]" /> |
| 66 | + <ngt-mesh-basic-material /> |
| 67 | + </ngt-mesh> |
| 68 | + <ngt-mesh #cylinder> |
| 69 | + <ngt-cylinder-geometry *args="[0.01, 0.01]" /> |
| 70 | + <ngt-mesh-basic-material transparent [opacity]="0.25" /> |
| 71 | + </ngt-mesh> |
| 72 | + </ngt-group> |
| 73 | + `, |
| 74 | + imports: [NgtArgs], |
| 75 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 76 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 77 | +}) |
| 78 | +class RaycastObj { |
| 79 | + group = input.required<NgtThreeElements['ngt-group']>(); |
| 80 | + |
| 81 | + private objRef = viewChild.required<ElementRef<THREE.Group>>('obj'); |
| 82 | + private originalRef = viewChild.required<ElementRef<THREE.Mesh>>('original'); |
| 83 | + private hitRef = viewChild.required<ElementRef<THREE.Mesh>>('hit'); |
| 84 | + private cylinderRef = viewChild.required<ElementRef<THREE.Mesh>>('cylinder'); |
| 85 | + |
| 86 | + constructor() { |
| 87 | + effect(() => { |
| 88 | + const [obj, originalMesh, hitMesh] = [ |
| 89 | + this.objRef().nativeElement, |
| 90 | + this.originalRef().nativeElement, |
| 91 | + this.hitRef().nativeElement, |
| 92 | + this.cylinderRef().nativeElement, |
| 93 | + ]; |
| 94 | + hitMesh.scale.multiplyScalar(0.5); |
| 95 | + originalMesh.position.set(pointDist, 0, 0); |
| 96 | + obj.rotation.x = Math.random() * 10; |
| 97 | + obj.rotation.y = Math.random() * 10; |
| 98 | + }); |
| 99 | + |
| 100 | + const xDir = Math.random() - 0.5; |
| 101 | + const yDir = Math.random() - 0.5; |
| 102 | + |
| 103 | + injectBeforeRender(({ delta }) => { |
| 104 | + const [obj, originalMesh, hitMesh, cylinderMesh, group] = [ |
| 105 | + this.objRef().nativeElement, |
| 106 | + this.originalRef().nativeElement, |
| 107 | + this.hitRef().nativeElement, |
| 108 | + this.cylinderRef().nativeElement, |
| 109 | + this.group(), |
| 110 | + ]; |
| 111 | + |
| 112 | + obj.rotation.x += xDir * delta; |
| 113 | + obj.rotation.y += yDir * delta; |
| 114 | + |
| 115 | + originalMesh.updateMatrixWorld(); |
| 116 | + origVec.setFromMatrixPosition(originalMesh.matrixWorld); |
| 117 | + dirVec.copy(origVec).multiplyScalar(-1).normalize(); |
| 118 | + |
| 119 | + raycaster.set(origVec, dirVec); |
| 120 | + raycaster.firstHitOnly = true; |
| 121 | + const res = raycaster.intersectObject(group as THREE.Group, true); |
| 122 | + const length = res.length ? res[0].distance : pointDist; |
| 123 | + |
| 124 | + hitMesh.position.set(pointDist - length, 0, 0); |
| 125 | + cylinderMesh.position.set(pointDist - length / 2, 0, 0); |
| 126 | + cylinderMesh.scale.set(1, length, 1); |
| 127 | + cylinderMesh.rotation.z = Math.PI / 2; |
| 128 | + }); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +@Component({ |
| 133 | + selector: 'debug-raycast', |
| 134 | + template: ` |
| 135 | + @for (i of amount; track $index) { |
| 136 | + <raycast-obj [group]="group()" /> |
| 137 | + } |
| 138 | + `, |
| 139 | + imports: [RaycastObj], |
| 140 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 141 | +}) |
| 142 | +class DebugRaycast { |
| 143 | + group = input.required<NgtThreeElements['ngt-group']>(); |
| 144 | + protected amount = Array.from({ length: 80 }, (_, index) => index); |
| 145 | +} |
| 146 | + |
| 147 | +@Component({ |
| 148 | + template: ` |
| 149 | + <ngt-group #group> |
| 150 | + <torus-bvh [positionZ]="-2" /> |
| 151 | + <torus-bvh [positionZ]="0" /> |
| 152 | + <torus-bvh [positionZ]="2" /> |
| 153 | + </ngt-group> |
| 154 | +
|
| 155 | + <debug-raycast [group]="group" /> |
| 156 | + <ngts-orbit-controls [options]="{ enablePan: false, zoomSpeed: 0.5 }" /> |
| 157 | + `, |
| 158 | + imports: [TorusBVH, DebugRaycast, NgtsOrbitControls], |
| 159 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 160 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 161 | + host: { class: 'default-bvh-story' }, |
| 162 | +}) |
| 163 | +class DefaultBVHStory {} |
| 164 | + |
| 165 | +export default { |
| 166 | + title: 'Performances/BVH', |
| 167 | + decorators: storyDecorators(), |
| 168 | +} as Meta; |
| 169 | + |
| 170 | +export const Default = storyFunction(DefaultBVHStory, { |
| 171 | + controls: false, |
| 172 | +}); |
0 commit comments