|
| 1 | +import { NgTemplateOutlet } from '@angular/common'; |
| 2 | +import { |
| 3 | + ChangeDetectionStrategy, |
| 4 | + Component, |
| 5 | + computed, |
| 6 | + contentChild, |
| 7 | + CUSTOM_ELEMENTS_SCHEMA, |
| 8 | + ElementRef, |
| 9 | + input, |
| 10 | + signal, |
| 11 | + Signal, |
| 12 | + TemplateRef, |
| 13 | + viewChild, |
| 14 | +} from '@angular/core'; |
| 15 | +import { injectBeforeRender, NgtArgs, NgtEuler, NgtHTML, NgtVector3 } from 'angular-three'; |
| 16 | +import { NgtsOrbitControls } from 'angular-three-soba/controls'; |
| 17 | +import { injectGLTF } from 'angular-three-soba/loaders'; |
| 18 | +import { NgtsHTML, NgtsHTMLContent } from 'angular-three-soba/misc'; |
| 19 | +import { NgtsContactShadows, NgtsEnvironment } from 'angular-three-soba/staging'; |
| 20 | +import { Group, Vector3 } from 'three'; |
| 21 | + |
| 22 | +@Component({ |
| 23 | + selector: 'app-marker', |
| 24 | + template: ` |
| 25 | + <ngt-group #group> |
| 26 | + <ngts-html [options]="{ transform: true, occlude: true, position: position(), rotation: rotation() }"> |
| 27 | + <div [htmlContent]="{ containerStyle: containerStyle() }" (occluded)="isOccluded.set($event)"> |
| 28 | + <ng-content /> |
| 29 | + </div> |
| 30 | + </ngts-html> |
| 31 | + </ngt-group> |
| 32 | + `, |
| 33 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 34 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 35 | + imports: [NgtsHTML, NgtsHTMLContent], |
| 36 | +}) |
| 37 | +export class Marker { |
| 38 | + position = input<NgtVector3>([0, 0, 0]); |
| 39 | + rotation = input<NgtEuler>([0, 0, 0]); |
| 40 | + |
| 41 | + private groupRef = viewChild.required<ElementRef<Group>>('group'); |
| 42 | + |
| 43 | + protected isOccluded = signal(false); |
| 44 | + private isInRange = signal(false); |
| 45 | + |
| 46 | + private isVisible = computed(() => !this.isOccluded() && this.isInRange()); |
| 47 | + protected containerStyle = computed(() => ({ |
| 48 | + transition: 'all 0.2s', |
| 49 | + opacity: this.isVisible() ? '1' : '0', |
| 50 | + transform: `scale(${this.isVisible() ? 1 : 0.25})`, |
| 51 | + })); |
| 52 | + |
| 53 | + constructor() { |
| 54 | + const v = new Vector3(); |
| 55 | + injectBeforeRender(({ camera }) => { |
| 56 | + const range = camera.position.distanceTo(this.groupRef().nativeElement.getWorldPosition(v)) <= 10; |
| 57 | + if (range !== this.isInRange()) this.isInRange.set(range); |
| 58 | + }); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +@Component({ |
| 63 | + selector: 'app-marker-icon', |
| 64 | + template: ` |
| 65 | + @if (withText()) { |
| 66 | + <div style="position: absolute; font-size: 10px; letter-spacing: -0.5px; left: 17.5px">north</div> |
| 67 | + } |
| 68 | + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="size-5" [class]="color()"> |
| 69 | + <path |
| 70 | + fill-rule="evenodd" |
| 71 | + d="m9.69 18.933.003.001C9.89 19.02 10 19 10 19s.11.02.308-.066l.002-.001.006-.003.018-.008a5.741 5.741 0 0 0 .281-.14c.186-.096.446-.24.757-.433.62-.384 1.445-.966 2.274-1.765C15.302 14.988 17 12.493 17 9A7 7 0 1 0 3 9c0 3.492 1.698 5.988 3.355 7.584a13.731 13.731 0 0 0 2.273 1.765 11.842 11.842 0 0 0 .976.544l.062.029.018.008.006.003ZM10 11.25a2.25 2.25 0 1 0 0-4.5 2.25 2.25 0 0 0 0 4.5Z" |
| 72 | + clip-rule="evenodd" |
| 73 | + /> |
| 74 | + </svg> |
| 75 | + `, |
| 76 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 77 | +}) |
| 78 | +export class MarkerIcon extends NgtHTML { |
| 79 | + color = input<string>('text-orange-500'); |
| 80 | + withText = input(false); |
| 81 | +} |
| 82 | + |
| 83 | +@Component({ |
| 84 | + selector: 'app-model', |
| 85 | + template: ` |
| 86 | + @if (gltf(); as gltf) { |
| 87 | + <ngt-group [rotation]="[-Math.PI / 2, 0, Math.PI]" [position]="position()" [dispose]="null"> |
| 88 | + <ngt-mesh [geometry]="gltf.nodes['URF-Height_Lampd_Ice_0'].geometry" [material]="gltf.materials.Lampd_Ice" /> |
| 89 | + <ngt-mesh [geometry]="gltf.nodes['URF-Height_watr_0'].geometry" [material]="gltf.materials.watr"> |
| 90 | + <ngt-value [rawValue]="0" attach="material.roughness" /> |
| 91 | + </ngt-mesh> |
| 92 | + <ngt-mesh [geometry]="gltf.nodes['URF-Height_Lampd_0'].geometry" [material]="gltf.materials.Lampd"> |
| 93 | + <ngt-value [rawValue]="'lightgreen'" attach="material.color" /> |
| 94 | +
|
| 95 | + <app-marker [position]="[0, 1.3, 0]" [rotation]="[0, Math.PI / 2, 0]"> |
| 96 | + <app-marker-icon color="text-orange-500" /> |
| 97 | + </app-marker> |
| 98 | +
|
| 99 | + <ngt-group [position]="[0, 0, 1.3]" [rotation]="[0, 0, Math.PI]"> |
| 100 | + <app-marker [rotation]="[0, Math.PI / 2, Math.PI / 2]"> |
| 101 | + <app-marker-icon color="text-red-500" [withText]="true" /> |
| 102 | + </app-marker> |
| 103 | + </ngt-group> |
| 104 | + </ngt-mesh> |
| 105 | + </ngt-group> |
| 106 | +
|
| 107 | + <ng-container [ngTemplateOutlet]="content()" /> |
| 108 | + } |
| 109 | + `, |
| 110 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 111 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 112 | + imports: [Marker, MarkerIcon, NgTemplateOutlet], |
| 113 | +}) |
| 114 | +export class Model { |
| 115 | + protected Math = Math; |
| 116 | + |
| 117 | + position = input<NgtVector3>([0, 0, 0]); |
| 118 | + |
| 119 | + protected content = contentChild.required(TemplateRef); |
| 120 | + |
| 121 | + protected gltf = injectGLTF(() => './earth.gltf') as Signal<any>; |
| 122 | +} |
| 123 | + |
| 124 | +@Component({ |
| 125 | + selector: 'app-lowpoly-earth-scene-graph', |
| 126 | + template: ` |
| 127 | + <ngt-color *args="['#ececec']" attach="background" /> |
| 128 | + <ngt-ambient-light [intensity]="0.5" /> |
| 129 | + <app-model [position]="[0, 0.25, 0]"> |
| 130 | + <ngts-contact-shadows |
| 131 | + * |
| 132 | + [options]="{ frames: 1, scale: 5, position: [0, -1, 0], far: 1, blur: 5, color: '#204080' }" |
| 133 | + /> |
| 134 | + </app-model> |
| 135 | + <ngts-environment [options]="{ preset: 'warehouse' }" /> |
| 136 | + @if (!asRenderTexture()) { |
| 137 | + <ngts-orbit-controls [options]="{ autoRotate: true }" /> |
| 138 | + } |
| 139 | + `, |
| 140 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 141 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 142 | + imports: [Model, NgtsEnvironment, NgtsContactShadows, NgtsOrbitControls, NgtArgs], |
| 143 | +}) |
| 144 | +export class SceneGraph { |
| 145 | + protected Math = Math; |
| 146 | + |
| 147 | + asRenderTexture = input(false); |
| 148 | +} |
| 149 | + |
| 150 | +injectGLTF.preload(() => './earth.gltf'); |
0 commit comments