|
| 1 | +import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, effect, inject } from '@angular/core'; |
| 2 | +import { Router, RouterOutlet } from '@angular/router'; |
| 3 | +import { injectStore, NgtArgs } from 'angular-three'; |
| 4 | +import { NgtsCameraControls } from 'angular-three-soba/controls'; |
| 5 | +import { injectGLTF } from 'angular-three-soba/loaders'; |
| 6 | +import CameraControls from 'camera-controls'; |
| 7 | +import { DoubleSide, FrontSide, Mesh, MeshStandardMaterial } from 'three'; |
| 8 | +import { GLTF } from 'three-stdlib'; |
| 9 | +import { menus } from './constants'; |
| 10 | +import { Cursor } from './cursor'; |
| 11 | +import { RockStore } from './store'; |
| 12 | + |
| 13 | +interface RockGLTF extends GLTF { |
| 14 | + nodes: { defaultMaterial: Mesh }; |
| 15 | + materials: { '08___Default': MeshStandardMaterial }; |
| 16 | +} |
| 17 | + |
| 18 | +@Component({ |
| 19 | + template: ` |
| 20 | + <ngt-fog *args="['white', 15, 50]" attach="fog" /> |
| 21 | +
|
| 22 | + <ngt-grid-helper *args="[50, 10]" /> |
| 23 | +
|
| 24 | + <ngt-mesh receiveShadow [rotation]="[Math.PI / 2, 0, 0]"> |
| 25 | + <ngt-plane-geometry *args="[100, 100]" /> |
| 26 | + <ngt-mesh-phong-material color="white" [side]="DoubleSide" [depthWrite]="false" /> |
| 27 | + </ngt-mesh> |
| 28 | +
|
| 29 | + <ngt-hemisphere-light [position]="10" [intensity]="Math.PI * 0.2" /> |
| 30 | +
|
| 31 | + <ngt-point-light [position]="10" [decay]="0" castShadow> |
| 32 | + <ngt-vector2 *args="[1024, 1024]" attach="shadow.mapSize" /> |
| 33 | + <ngt-value [rawValue]="4" attach="shadow.radius" /> |
| 34 | + <ngt-value [rawValue]="-0.0005" attach="shadow.bias" /> |
| 35 | + </ngt-point-light> |
| 36 | +
|
| 37 | + @if (gltf(); as gltf) { |
| 38 | + <ngt-group [position]="[0, 2.6, 0]" [scale]="3"> |
| 39 | + <ngt-group [rotation]="[-Math.PI / 2, 0, 0]"> |
| 40 | + <ngt-group [rotation]="[Math.PI / 2, 0, 0]"> |
| 41 | + <ngt-mesh |
| 42 | + cursor |
| 43 | + castShadow |
| 44 | + receiveShadow |
| 45 | + [geometry]="gltf.nodes.defaultMaterial.geometry" |
| 46 | + [material]="gltf.materials['08___Default']" |
| 47 | + (click)="router.navigate(['/routed-rocks/rocks'])" |
| 48 | + /> |
| 49 | + </ngt-group> |
| 50 | + </ngt-group> |
| 51 | + </ngt-group> |
| 52 | + } |
| 53 | +
|
| 54 | + <ngts-camera-controls |
| 55 | + [options]="{ makeDefault: true, minDistance: 12, maxDistance: 12, minPolarAngle: 0, maxPolarAngle: Math.PI / 2 }" |
| 56 | + /> |
| 57 | +
|
| 58 | + <ngt-icosahedron-geometry #geometry attach="none" /> |
| 59 | + @for (menu of menus; track menu.id) { |
| 60 | + <ngt-group [name]="menu.name" [position]="[15 * Math.cos(menu.angle), 0, 15 * Math.sin(menu.angle)]"> |
| 61 | + <ngt-mesh |
| 62 | + cursor |
| 63 | + castShadow |
| 64 | + receiveShadow |
| 65 | + [position]="[0, 5, 0]" |
| 66 | + [geometry]="geometry" |
| 67 | + (click)="router.navigate([menu.path])" |
| 68 | + > |
| 69 | + <ngt-mesh-phong-material [color]="menu.color" [side]="FrontSide" /> |
| 70 | +
|
| 71 | + <!-- NOTE: we can use ng-template for this use-case as well. --> |
| 72 | + <!-- @let templateRefs = coloredRockTemplateRefs(); --> |
| 73 | + <!-- @let template = templateRefs[menu.slug]; --> |
| 74 | + <!----> |
| 75 | + <!-- @if (template) { --> |
| 76 | + <!-- <ng-container [ngTemplateOutlet]="template" [ngTemplateOutletContext]="{ $implicit: menu }" /> --> |
| 77 | + <!-- } --> |
| 78 | + </ngt-mesh> |
| 79 | + </ngt-group> |
| 80 | + } |
| 81 | +
|
| 82 | + <router-outlet /> |
| 83 | + `, |
| 84 | + imports: [RouterOutlet, NgtArgs, NgtsCameraControls, Cursor], |
| 85 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 86 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 87 | + host: { class: 'rocks' }, |
| 88 | +}) |
| 89 | +export default class Rocks { |
| 90 | + protected readonly Math = Math; |
| 91 | + protected readonly FrontSide = FrontSide; |
| 92 | + protected readonly DoubleSide = DoubleSide; |
| 93 | + |
| 94 | + protected readonly menus = menus; |
| 95 | + |
| 96 | + protected router = inject(Router); |
| 97 | + private rockStore = inject(RockStore); |
| 98 | + private store = injectStore(); |
| 99 | + |
| 100 | + protected gltf = injectGLTF<RockGLTF>(() => './rock2/scene.gltf'); |
| 101 | + |
| 102 | + constructor() { |
| 103 | + effect(() => { |
| 104 | + const controls = this.store.controls() as CameraControls; |
| 105 | + if (!controls) return; |
| 106 | + |
| 107 | + const gltf = this.gltf(); |
| 108 | + if (!gltf) return; |
| 109 | + |
| 110 | + const scene = this.store.scene(); |
| 111 | + const rock = this.rockStore.selectedRock(); |
| 112 | + |
| 113 | + const obj = rock ? scene.getObjectByName(rock.name) : gltf.scene; |
| 114 | + if (obj) { |
| 115 | + void controls.fitToBox(obj, true, { paddingTop: 5 }); |
| 116 | + } |
| 117 | + }); |
| 118 | + } |
| 119 | +} |
0 commit comments