|
| 1 | +import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, effect, inject, Signal } from '@angular/core'; |
| 2 | +import { Router } from '@angular/router'; |
| 3 | +import { injectStore, NgtArgs, NgtRouterOutlet } 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]="true" [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]="true"> |
| 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 | + name="theRock" |
| 44 | + [castShadow]="true" |
| 45 | + [receiveShadow]="true" |
| 46 | + [geometry]="gltf.nodes.defaultMaterial.geometry" |
| 47 | + [material]="gltf.materials['08___Default']" |
| 48 | + (click)="onRockClicked()" |
| 49 | + /> |
| 50 | + </ngt-group> |
| 51 | + </ngt-group> |
| 52 | + </ngt-group> |
| 53 | + } |
| 54 | +
|
| 55 | + <ngts-camera-controls |
| 56 | + [options]="{ makeDefault: true, minDistance: 12, maxDistance: 12, minPolarAngle: 0, maxPolarAngle: Math.PI / 2 }" |
| 57 | + /> |
| 58 | +
|
| 59 | + <ngt-icosahedron-geometry #geometry attach="none" /> |
| 60 | + @for (menu of menus; track menu.id) { |
| 61 | + <ngt-group [position]="[15 * Math.cos(menu.angle), 0, 15 * Math.sin(menu.angle)]" [name]="'group-' + menu.id"> |
| 62 | + <ngt-mesh |
| 63 | + cursor |
| 64 | + [name]="menu.name" |
| 65 | + [position]="[0, 5, 0]" |
| 66 | + [castShadow]="true" |
| 67 | + [receiveShadow]="true" |
| 68 | + [geometry]="geometry" |
| 69 | + (click)="onColoredRockClicked(menu)" |
| 70 | + > |
| 71 | + <ngt-mesh-phong-material [color]="menu.color" [side]="FrontSide" /> |
| 72 | + </ngt-mesh> |
| 73 | + </ngt-group> |
| 74 | + } |
| 75 | +
|
| 76 | + <ngt-router-outlet /> |
| 77 | + `, |
| 78 | + imports: [NgtRouterOutlet, NgtArgs, NgtsCameraControls, Cursor], |
| 79 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 80 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 81 | + host: { class: 'rocks' }, |
| 82 | +}) |
| 83 | +export default class Rocks { |
| 84 | + protected readonly Math = Math; |
| 85 | + protected readonly FrontSide = FrontSide; |
| 86 | + protected readonly DoubleSide = DoubleSide; |
| 87 | + |
| 88 | + protected readonly menus = menus; |
| 89 | + |
| 90 | + private router = inject(Router); |
| 91 | + private rockStore = inject(RockStore); |
| 92 | + private store = injectStore(); |
| 93 | + |
| 94 | + private scene = this.store.select('scene'); |
| 95 | + private controls = this.store.select('controls') as Signal<CameraControls>; |
| 96 | + |
| 97 | + protected gltf = injectGLTF<RockGLTF>(() => './rock2/scene.gltf'); |
| 98 | + |
| 99 | + constructor() { |
| 100 | + effect(() => { |
| 101 | + const controls = this.controls(); |
| 102 | + if (!controls) return; |
| 103 | + |
| 104 | + const gltf = this.gltf(); |
| 105 | + if (!gltf) return; |
| 106 | + |
| 107 | + const scene = this.scene(); |
| 108 | + const rock = this.rockStore.selectedRock(); |
| 109 | + |
| 110 | + const obj = rock ? scene.getObjectByName(rock.name) : gltf.scene; |
| 111 | + if (obj) { |
| 112 | + void controls.fitToBox(obj, true, { |
| 113 | + paddingTop: 5, |
| 114 | + }); |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + onColoredRockClicked(menu: (typeof menus)[number]) { |
| 120 | + this.rockStore.selectedRock.set(menu); |
| 121 | + this.router.navigate([menu.path]); |
| 122 | + } |
| 123 | + |
| 124 | + onRockClicked() { |
| 125 | + this.rockStore.selectedRock.set(null); |
| 126 | + this.router.navigate(['/routed-rocks/rocks']); |
| 127 | + } |
| 128 | +} |
0 commit comments