|
| 1 | +import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, signal } from '@angular/core'; |
| 2 | +import { NgtArgs, NgtEuler, NgtVector3 } from 'angular-three'; |
| 3 | +import { NgtsOrthographicCamera } from 'angular-three-soba/cameras'; |
| 4 | +import { NgtsOrbitControls } from 'angular-three-soba/controls'; |
| 5 | +import { NgtsPivotControls, OnDragParameters } from 'angular-three-soba/gizmos'; |
| 6 | +import { injectGLTF, injectTexture } from 'angular-three-soba/loaders'; |
| 7 | +import { NgtsDecal } from 'angular-three-soba/misc'; |
| 8 | +import { NgtsAccumulativeShadows, NgtsRandomizedLights } from 'angular-three-soba/staging'; |
| 9 | +import { Euler, Mesh, MeshStandardMaterial, Quaternion, Vector3 } from 'three'; |
| 10 | +import { GLTF } from 'three-stdlib'; |
| 11 | + |
| 12 | +type CupGLTF = GLTF & { |
| 13 | + nodes: { coffee_cup_top_16oz: Mesh }; |
| 14 | + materials: { ['13 - Default']: MeshStandardMaterial }; |
| 15 | +}; |
| 16 | +@Component({ |
| 17 | + selector: 'app-cup', |
| 18 | + template: ` |
| 19 | + @if (gltf(); as gltf) { |
| 20 | + <ngt-mesh |
| 21 | + castShadow |
| 22 | + [geometry]="gltf.nodes.coffee_cup_top_16oz.geometry" |
| 23 | + [material]="gltf.materials['13 - Default']" |
| 24 | + [dispose]="null" |
| 25 | + [position]="[0, -1, 0]" |
| 26 | + [scale]="2" |
| 27 | + > |
| 28 | + <ngt-value [rawValue]="1" attach="material.aoMapIntensity" /> |
| 29 | + <ngt-group [position]="[0, 0.75, 0.5]"> |
| 30 | + <ngts-pivot-controls |
| 31 | + [options]="{ activeAxes: [true, true, false], scale: 0.55 }" |
| 32 | + (dragged)="onDrag($event)" |
| 33 | + /> |
| 34 | + </ngt-group> |
| 35 | + <ngts-decal |
| 36 | + [options]="{ map: texture(), position: position(), rotation: rotation(), scale: scale(), depthTest: true }" |
| 37 | + /> |
| 38 | + </ngt-mesh> |
| 39 | + } |
| 40 | + `, |
| 41 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 42 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 43 | + imports: [NgtsPivotControls, NgtsDecal], |
| 44 | +}) |
| 45 | +export class Cup { |
| 46 | + protected position = signal<NgtVector3>([0, 0.75, 0.3]); |
| 47 | + protected rotation = signal<NgtEuler>([0, 0, 0]); |
| 48 | + protected scale = signal<NgtVector3>([0.6, 0.6, 0.6]); |
| 49 | + |
| 50 | + protected gltf = injectGLTF<CupGLTF>(() => './coffee-transformed.glb'); |
| 51 | + protected texture = injectTexture(() => './1200px-Starbucks_Logo_ab_2011.svg.png'); |
| 52 | + |
| 53 | + private p = new Vector3(); |
| 54 | + private s = new Vector3(); |
| 55 | + private q = new Quaternion(); |
| 56 | + |
| 57 | + onDrag({ l }: OnDragParameters) { |
| 58 | + l.decompose(this.p, this.q, this.s); |
| 59 | + const rotation = new Euler().setFromQuaternion(this.q); |
| 60 | + this.position.set([this.p.x, this.p.y + 0.75, this.p.z + 0.3]); |
| 61 | + this.rotation.set([rotation.x, rotation.y, rotation.z]); |
| 62 | + this.scale.set([0.6 * this.s.x, 0.6 * this.s.y, 0.6 * this.s.z]); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +@Component({ |
| 67 | + selector: 'app-scene-graph', |
| 68 | + template: ` |
| 69 | + <ngts-orthographic-camera [options]="{ makeDefault: true, position: [0, 10, 100], zoom: 140 }" /> |
| 70 | +
|
| 71 | + <ngt-color attach="background" *args="['#027946']" /> |
| 72 | +
|
| 73 | + <ngt-ambient-light [intensity]="0.5 * Math.PI" /> |
| 74 | + <ngt-directional-light [intensity]="0.5 * Math.PI" [position]="[10, 10, 10]" /> |
| 75 | +
|
| 76 | + <app-cup /> |
| 77 | +
|
| 78 | + <ngts-accumulative-shadows |
| 79 | + [options]="{ temporal: true, frames: 100, alphaTest: 0.85, opacity: 1, scale: 25, position: [0, -1, 0] }" |
| 80 | + > |
| 81 | + <ngts-randomized-lights |
| 82 | + [options]="{ amount: 8, radius: 10, ambient: 0.7, position: [10, 10, -5], bias: 0.01, size: 10 }" |
| 83 | + /> |
| 84 | + </ngts-accumulative-shadows> |
| 85 | +
|
| 86 | + <ngts-orbit-controls [options]="{ makeDefault: true }" /> |
| 87 | + `, |
| 88 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 89 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 90 | + imports: [NgtsOrthographicCamera, NgtArgs, Cup, NgtsAccumulativeShadows, NgtsRandomizedLights, NgtsOrbitControls], |
| 91 | + host: { class: 'starbucks-soba-experience' }, |
| 92 | +}) |
| 93 | +export class SceneGraph { |
| 94 | + protected readonly Math = Math; |
| 95 | +} |
0 commit comments