|
| 1 | +import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, ElementRef, viewChild } from '@angular/core'; |
| 2 | +import { injectBeforeRender, NgtArgs, NgtThreeElements } from 'angular-three'; |
| 3 | +import { NgtsOrbitControls } from 'angular-three-soba/controls'; |
| 4 | +import * as THREE from 'three'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + selector: 'app-scene-graph', |
| 8 | + template: ` |
| 9 | + <ngt-color *args="['#201919']" attach="background" /> |
| 10 | +
|
| 11 | + <ngt-ambient-light [intensity]="0.5" /> |
| 12 | + <ngt-spot-light [position]="[0, 8, 4]" [intensity]="Math.PI" [decay]="0" [angle]="2" /> |
| 13 | +
|
| 14 | + <ngt-group #group> |
| 15 | + @for (x of positions; track $index) { |
| 16 | + @for (y of positions; track $index) { |
| 17 | + @for (z of positions; track $index) { |
| 18 | + <ngt-mesh |
| 19 | + [position]="[x, y, z]" |
| 20 | + (pointerenter)="$event.stopPropagation(); onPointerEnter(material)" |
| 21 | + (pointerleave)="$event.stopPropagation(); onPointerLeave(material)" |
| 22 | + > |
| 23 | + <ngt-box-geometry /> |
| 24 | + <ngt-mesh-standard-material #material color="#efefef" [roughness]="0.5" [metalness]="0.5" /> |
| 25 | + </ngt-mesh> |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + </ngt-group> |
| 30 | +
|
| 31 | + <ngts-orbit-controls /> |
| 32 | + `, |
| 33 | + imports: [NgtsOrbitControls, NgtArgs], |
| 34 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 35 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 36 | +}) |
| 37 | +export class SceneGraph { |
| 38 | + protected readonly Math = Math; |
| 39 | + |
| 40 | + private groupRef = viewChild.required<ElementRef<THREE.Group>>('group'); |
| 41 | + |
| 42 | + protected readonly positions = [-2.5, 0, 2.5]; |
| 43 | + |
| 44 | + constructor() { |
| 45 | + injectBeforeRender(({ delta }) => { |
| 46 | + this.groupRef().nativeElement.rotation.y += delta * 0.25; |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + onPointerEnter(material: NgtThreeElements['ngt-mesh-standard-material']) { |
| 51 | + (material as THREE.MeshStandardMaterial).color.set('mediumpurple'); |
| 52 | + } |
| 53 | + |
| 54 | + onPointerLeave(material: NgtThreeElements['ngt-mesh-standard-material']) { |
| 55 | + (material as THREE.MeshStandardMaterial).color.set('#efefef'); |
| 56 | + } |
| 57 | +} |
0 commit comments