|
1 | | -import { CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, Component } from '@angular/core'; |
2 | | -import { NgtArgs } from 'angular-three'; |
3 | | -import { TheatreSheetObject } from 'angular-three-theatre'; |
| 1 | +import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, signal, viewChild } from '@angular/core'; |
| 2 | +import { injectBeforeRender, NgtArgs } from 'angular-three'; |
| 3 | +import { NgtsRoundedBox } from 'angular-three-soba/abstractions'; |
| 4 | +import { NgtsPerspectiveCamera } from 'angular-three-soba/cameras'; |
| 5 | +import { TheatreSheetObject, TheatreSheetObjectTransform } from 'angular-three-theatre'; |
4 | 6 |
|
5 | 7 | @Component({ |
6 | 8 | selector: 'app-scene-graph', |
7 | 9 | template: ` |
| 10 | + <ngts-perspective-camera #camera [options]="{ makeDefault: true, position: [5, 5, 5], fov: 75, far: 1000 }" /> |
| 11 | +
|
8 | 12 | <ngt-ambient-light |
9 | 13 | #ambientLight |
10 | 14 | *sheetObject="'Ambient Light'" |
11 | 15 | [sync]="ambientLight" |
12 | 16 | [syncProps]="['intensity', 'color']" |
13 | 17 | /> |
14 | 18 |
|
15 | | - <ng-template sheetObject="Directional Light"> |
16 | | - <theatre-transform> |
17 | | - <ngt-directional-light |
18 | | - #directionalLight |
19 | | - castShadow |
20 | | - [sync]="directionalLight" |
21 | | - [syncProps]="['intensity', 'color']" |
| 19 | + <theatre-transform *sheetObject="'Directional Light'"> |
| 20 | + <ngt-directional-light |
| 21 | + #directionalLight |
| 22 | + castShadow |
| 23 | + [sync]="directionalLight" |
| 24 | + [syncProps]="['intensity', 'color']" |
| 25 | + /> |
| 26 | + </theatre-transform> |
| 27 | +
|
| 28 | + <theatre-transform |
| 29 | + #boxTransform |
| 30 | + *sheetObject="'Box'; select as select; deselect as deselect" |
| 31 | + [options]="{ mode: boxTransformMode() }" |
| 32 | + > |
| 33 | + <ngts-rounded-box [options]="{ castShadow: true }" (click)="select()" (pointermissed)="deselect()"> |
| 34 | + <ngt-mesh-standard-material |
| 35 | + #boxMaterial |
| 36 | + transparent |
| 37 | + [sync]="boxMaterial" |
| 38 | + [syncProps]="['color', 'roughness', 'metalness', 'side', 'opacity']" |
22 | 39 | /> |
23 | | - </theatre-transform> |
24 | | - </ng-template> |
25 | | -
|
26 | | - <ng-template sheetObject="Box" let-select="select" let-deselect="deselect"> |
27 | | - <theatre-transform> |
28 | | - <ngt-mesh castShadow (click)="select()" (pointermissed)="deselect()"> |
29 | | - <ngt-box-geometry /> |
30 | | - <ngt-mesh-standard-material |
31 | | - #boxMaterial |
32 | | - transparent |
33 | | - [sync]="boxMaterial" |
34 | | - [syncProps]="['color', 'roughness', 'metalness', 'side', 'opacity']" |
35 | | - /> |
36 | | - </ngt-mesh> |
37 | | - </theatre-transform> |
38 | | - </ng-template> |
39 | | -
|
40 | | - <ngt-mesh receiveShadow [position.y]="-1" [rotation.x]="-Math.PI / 2"> |
41 | | - <ngt-circle-geometry *args="[1.4, 48]" /> |
| 40 | + </ngts-rounded-box> |
| 41 | + </theatre-transform> |
| 42 | +
|
| 43 | + <ngt-mesh receiveShadow [position.y]="-1" [rotation.x]="-Math.PI / 2" [scale]="10"> |
| 44 | + <ngt-circle-geometry /> |
42 | 45 | <ngt-mesh-standard-material /> |
43 | 46 | </ngt-mesh> |
44 | 47 | `, |
45 | 48 | schemas: [CUSTOM_ELEMENTS_SCHEMA], |
46 | | - imports: [NgtArgs, TheatreSheetObject], |
| 49 | + imports: [NgtArgs, TheatreSheetObject, NgtsRoundedBox, NgtsPerspectiveCamera], |
47 | 50 | changeDetection: ChangeDetectionStrategy.OnPush, |
48 | | - host: { class: 'experience-basic-theatre' }, |
| 51 | + host: { class: 'experience-basic-theatre', '(document:keyup)': 'onKeyup($event)' }, |
49 | 52 | }) |
50 | 53 | export class SceneGraph { |
51 | 54 | protected readonly Math = Math; |
| 55 | + |
| 56 | + private boxTransform = viewChild.required('boxTransform', { read: TheatreSheetObjectTransform }); |
| 57 | + private modes = ['translate', 'rotate', 'scale'] as const; |
| 58 | + protected boxTransformMode = signal<(typeof this.modes)[number]>('translate'); |
| 59 | + |
| 60 | + protected onKeyup(event: KeyboardEvent) { |
| 61 | + if (event.key === 'Shift' && event.location === KeyboardEvent.DOM_KEY_LOCATION_LEFT) { |
| 62 | + // cycle through modes |
| 63 | + this.boxTransformMode.set( |
| 64 | + this.modes[(this.modes.indexOf(this.boxTransformMode()) + 1) % this.modes.length], |
| 65 | + ); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + constructor() { |
| 70 | + injectBeforeRender(({ camera }) => { |
| 71 | + const boxSheetObject = this.boxTransform().sheetObject(); |
| 72 | + const position = boxSheetObject.value['position']; |
| 73 | + camera.lookAt(position.x, position.y, position.z); |
| 74 | + }); |
| 75 | + } |
52 | 76 | } |
0 commit comments