|
| 1 | +import { ChangeDetectionStrategy, Component, computed, CUSTOM_ELEMENTS_SCHEMA, input } from '@angular/core'; |
| 2 | +import { is, NgtArgs, NgtThreeElements } from 'angular-three'; |
| 3 | +import { NgtsOrbitControls } from 'angular-three-soba/controls'; |
| 4 | +import { injectGLTF } from 'angular-three-soba/loaders'; |
| 5 | +import { NgtsAccumulativeShadows, NgtsCenter, NgtsEnvironment, NgtsRandomizedLights } from 'angular-three-soba/staging'; |
| 6 | +import * as THREE from 'three'; |
| 7 | +import { FlakesTexture, GLTF } from 'three-stdlib'; |
| 8 | +import suziGLTF from './suzi.gltf'; |
| 9 | + |
| 10 | +interface SuziGLTF extends GLTF { |
| 11 | + materials: { |
| 12 | + default: THREE.MeshStandardMaterial; |
| 13 | + }; |
| 14 | +} |
| 15 | + |
| 16 | +@Component({ |
| 17 | + selector: 'app-suzi', |
| 18 | + template: ` |
| 19 | + <ngt-primitive *args="[scene()]" [parameters]="options()" /> |
| 20 | + `, |
| 21 | + imports: [NgtArgs], |
| 22 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 23 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 24 | +}) |
| 25 | +export class Suzi { |
| 26 | + options = input<Partial<NgtThreeElements['ngt-group']>>({}); |
| 27 | + protected gltf = injectGLTF<SuziGLTF>(() => suziGLTF); |
| 28 | + |
| 29 | + protected scene = computed(() => { |
| 30 | + const gltf = this.gltf(); |
| 31 | + if (!gltf) return null; |
| 32 | + |
| 33 | + const { scene, materials } = gltf; |
| 34 | + |
| 35 | + scene.traverse((obj) => { |
| 36 | + if (is.three<THREE.Mesh>(obj, 'isMesh')) { |
| 37 | + obj.receiveShadow = obj.castShadow = true; |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + materials.default.color.set('orange'); |
| 42 | + materials.default.roughness = 0; |
| 43 | + materials.default.normalMap = new THREE.CanvasTexture( |
| 44 | + // @ts-expect-error - three-stdlib type |
| 45 | + new FlakesTexture(), |
| 46 | + THREE.UVMapping, |
| 47 | + THREE.RepeatWrapping, |
| 48 | + THREE.RepeatWrapping, |
| 49 | + ); |
| 50 | + materials.default.normalMap.repeat.set(40, 40); |
| 51 | + materials.default.normalScale.set(0.1, 0.1); |
| 52 | + |
| 53 | + return scene; |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +@Component({ |
| 58 | + selector: 'app-scene-graph', |
| 59 | + template: ` |
| 60 | + <ngt-color *args="['goldenrod']" attach="background" /> |
| 61 | +
|
| 62 | + <ngt-group [position]="[0, -0.5, 0]"> |
| 63 | + <ngts-center [options]="{ top: true }"> |
| 64 | + <app-suzi [options]="{ rotation: [-0.63, 0, 0], scale: 2 }" /> |
| 65 | + </ngts-center> |
| 66 | +
|
| 67 | + <ngts-center [options]="{ top: true, position: [-2, 0, 1] }"> |
| 68 | + <ngt-mesh castShadow> |
| 69 | + <ngt-sphere-geometry *args="[0.25, 64, 64]" /> |
| 70 | + <ngt-mesh-standard-material color="lightblue" /> |
| 71 | + </ngt-mesh> |
| 72 | + </ngts-center> |
| 73 | +
|
| 74 | + <ngts-center [options]="{ top: true, position: [2.5, 0, 1] }"> |
| 75 | + <ngt-mesh castShadow [rotation]="[0, Math.PI / 4, 0]"> |
| 76 | + <ngt-box-geometry *args="[0.5, 0.5, 0.5]" /> |
| 77 | + <ngt-mesh-standard-material color="indianred" /> |
| 78 | + </ngt-mesh> |
| 79 | + </ngts-center> |
| 80 | +
|
| 81 | + <ngts-accumulative-shadows |
| 82 | + [options]="{ |
| 83 | + temporal: true, |
| 84 | + frames: 100, |
| 85 | + color: 'orange', |
| 86 | + colorBlend: 2, |
| 87 | + toneMapped: true, |
| 88 | + alphaTest: 0.75, |
| 89 | + opacity: 2, |
| 90 | + scale: 12, |
| 91 | + }" |
| 92 | + > |
| 93 | + <ngts-randomized-lights |
| 94 | + [options]="{ intensity: Math.PI, amount: 8, radius: 4, ambient: 0.5, position: [5, 5, -10], bias: 0.001 }" |
| 95 | + /> |
| 96 | + </ngts-accumulative-shadows> |
| 97 | + </ngt-group> |
| 98 | +
|
| 99 | + <ngts-orbit-controls [options]="{ minPolarAngle: 0, maxPolarAngle: Math.PI / 2 }" /> |
| 100 | + <ngts-environment [options]="{ preset: 'city' }" /> |
| 101 | + `, |
| 102 | + |
| 103 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 104 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 105 | + imports: [ |
| 106 | + NgtsEnvironment, |
| 107 | + NgtsOrbitControls, |
| 108 | + NgtsAccumulativeShadows, |
| 109 | + NgtsRandomizedLights, |
| 110 | + NgtsCenter, |
| 111 | + NgtArgs, |
| 112 | + Suzi, |
| 113 | + ], |
| 114 | +}) |
| 115 | +export class SceneGraph { |
| 116 | + protected readonly Math = Math; |
| 117 | +} |
0 commit comments