|
| 1 | +import { NgIf } from '@angular/common'; |
| 2 | +import { Component, computed, CUSTOM_ELEMENTS_SCHEMA, effect, Input } from '@angular/core'; |
| 3 | +import { |
| 4 | + applyProps, |
| 5 | + extend, |
| 6 | + injectNgtRef, |
| 7 | + is, |
| 8 | + signalStore, |
| 9 | + type NgtEuler, |
| 10 | + type NgtMesh, |
| 11 | + type NgtRef, |
| 12 | + type NgtVector3, |
| 13 | +} from 'angular-three'; |
| 14 | +import * as THREE from 'three'; |
| 15 | +import { AxesHelper, BoxGeometry, Mesh, MeshNormalMaterial } from 'three'; |
| 16 | +import { DecalGeometry } from 'three-stdlib'; |
| 17 | + |
| 18 | +extend({ Mesh, BoxGeometry, MeshNormalMaterial, AxesHelper }); |
| 19 | + |
| 20 | +export type NgtsDecalState = { |
| 21 | + debug: boolean; |
| 22 | + mesh?: NgtRef<THREE.Mesh>; |
| 23 | + position: NgtVector3; |
| 24 | + rotation: NgtEuler | number; |
| 25 | + scale: NgtVector3; |
| 26 | + map?: THREE.Texture; |
| 27 | + polygonOffsetFactor: number; |
| 28 | + depthTest: boolean; |
| 29 | +}; |
| 30 | + |
| 31 | +declare global { |
| 32 | + interface HTMLElementTagNameMap { |
| 33 | + /** |
| 34 | + * @extends ngt-mesh |
| 35 | + */ |
| 36 | + 'ngts-decal': NgtsDecalState & NgtMesh; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function vecToArray(vec: number[] | NgtVector3 | NgtEuler | number = [0, 0, 0]) { |
| 41 | + if (Array.isArray(vec)) { |
| 42 | + return vec; |
| 43 | + } |
| 44 | + |
| 45 | + if (vec instanceof THREE.Vector3 || vec instanceof THREE.Euler) { |
| 46 | + return [vec.x, vec.y, vec.z]; |
| 47 | + } |
| 48 | + |
| 49 | + return [vec, vec, vec]; |
| 50 | +} |
| 51 | + |
| 52 | +@Component({ |
| 53 | + selector: 'ngts-decal', |
| 54 | + standalone: true, |
| 55 | + template: ` |
| 56 | + <ngt-mesh [ref]="decalRef" ngtCompound> |
| 57 | + <ngt-value [rawValue]="true" attach="material.transparent" /> |
| 58 | + <ngt-value [rawValue]="true" attach="material.polygonOffset" /> |
| 59 | + <ngt-value [rawValue]="polygonOffsetFactor()" attach="material.polygonOffsetFactor" /> |
| 60 | + <ngt-value [rawValue]="depthTest()" attach="material.depthTest" /> |
| 61 | + <ngt-value [rawValue]="map()" attach="material.map" /> |
| 62 | + <ng-content /> |
| 63 | +
|
| 64 | + <ngt-mesh *ngIf="debug()" [ref]="helperRef"> |
| 65 | + <ngt-box-geometry /> |
| 66 | + <ngt-mesh-normal-material [wireframe]="true" /> |
| 67 | + <ngt-axes-helper /> |
| 68 | + </ngt-mesh> |
| 69 | + </ngt-mesh> |
| 70 | + `, |
| 71 | + imports: [NgIf], |
| 72 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 73 | +}) |
| 74 | +export class NgtsDecal { |
| 75 | + private inputs = signalStore<NgtsDecalState>({ |
| 76 | + debug: false, |
| 77 | + depthTest: false, |
| 78 | + polygonOffsetFactor: -10, |
| 79 | + position: [0, 0, 0], |
| 80 | + rotation: [0, 0, 0], |
| 81 | + scale: 1, |
| 82 | + }); |
| 83 | + |
| 84 | + @Input() decalRef = injectNgtRef<Mesh>(); |
| 85 | + |
| 86 | + @Input({ alias: 'debug' }) set _debug(debug: boolean) { |
| 87 | + this.inputs.set({ debug }); |
| 88 | + } |
| 89 | + |
| 90 | + @Input({ alias: 'mesh' }) set _mesh(mesh: NgtRef<THREE.Mesh>) { |
| 91 | + this.inputs.set({ mesh }); |
| 92 | + } |
| 93 | + |
| 94 | + @Input({ alias: 'position' }) set _position(position: NgtVector3) { |
| 95 | + this.inputs.set({ position }); |
| 96 | + } |
| 97 | + |
| 98 | + @Input({ alias: 'rotation' }) set _rotation(rotation: NgtEuler | number) { |
| 99 | + this.inputs.set({ rotation }); |
| 100 | + } |
| 101 | + |
| 102 | + @Input({ alias: 'scale' }) set _scale(scale: NgtVector3) { |
| 103 | + this.inputs.set({ scale }); |
| 104 | + } |
| 105 | + |
| 106 | + @Input({ alias: 'map' }) set _map(map: THREE.Texture) { |
| 107 | + this.inputs.set({ map }); |
| 108 | + } |
| 109 | + |
| 110 | + @Input({ alias: 'polygonOffsetFactor' }) set _polygonOffsetFactor(polygonOffsetFactor: number) { |
| 111 | + this.inputs.set({ polygonOffsetFactor }); |
| 112 | + } |
| 113 | + |
| 114 | + @Input({ alias: 'depthTest' }) set _depthTest(depthTest: boolean) { |
| 115 | + this.inputs.set({ depthTest }); |
| 116 | + } |
| 117 | + |
| 118 | + private mesh = this.inputs.select('mesh'); |
| 119 | + private __position = this.inputs.select('position'); |
| 120 | + private __rotation = this.inputs.select('rotation'); |
| 121 | + private __scale = this.inputs.select('scale'); |
| 122 | + private position = computed(() => vecToArray(this.__position())); |
| 123 | + private rotation = computed(() => vecToArray(this.__rotation())); |
| 124 | + private scale = computed(() => vecToArray(this.__scale())); |
| 125 | + |
| 126 | + helperRef = injectNgtRef<Mesh>(); |
| 127 | + |
| 128 | + debug = this.inputs.select('debug'); |
| 129 | + depthTest = this.inputs.select('depthTest'); |
| 130 | + polygonOffsetFactor = this.inputs.select('polygonOffsetFactor'); |
| 131 | + map = this.inputs.select('map'); |
| 132 | + |
| 133 | + constructor() { |
| 134 | + this.processDecal(); |
| 135 | + } |
| 136 | + |
| 137 | + private processDecal() { |
| 138 | + effect((onCleanup) => { |
| 139 | + const decal = this.decalRef.nativeElement; |
| 140 | + if (!decal) return; |
| 141 | + |
| 142 | + const [mesh, position, rotation, scale, helper] = [ |
| 143 | + this.mesh(), |
| 144 | + this.position(), |
| 145 | + this.rotation(), |
| 146 | + this.scale(), |
| 147 | + this.helperRef.untracked, |
| 148 | + ]; |
| 149 | + |
| 150 | + const parent = mesh ? (is.ref(mesh) ? mesh.nativeElement : mesh) : decal.parent; |
| 151 | + if (!(parent instanceof Mesh)) { |
| 152 | + throw new Error('[NGT] ngts-decal must have a ngt-mesh as parent or specify its "mesh" input'); |
| 153 | + } |
| 154 | + |
| 155 | + const state = { |
| 156 | + position: new THREE.Vector3(), |
| 157 | + rotation: new THREE.Euler(), |
| 158 | + scale: new THREE.Vector3(1, 1, 1), |
| 159 | + }; |
| 160 | + |
| 161 | + if (parent) { |
| 162 | + applyProps(state, { position, scale }); |
| 163 | + |
| 164 | + // Zero out the parents matrix world for this operation |
| 165 | + const matrixWorld = parent.matrixWorld.clone(); |
| 166 | + parent.matrixWorld.identity(); |
| 167 | + |
| 168 | + if (!rotation || typeof rotation === 'number') { |
| 169 | + const o = new THREE.Object3D(); |
| 170 | + |
| 171 | + o.position.copy(state.position); |
| 172 | + o.lookAt(parent.position); |
| 173 | + if (typeof rotation === 'number') o.rotateZ(rotation); |
| 174 | + applyProps(state, { rotation: o.rotation }); |
| 175 | + } else { |
| 176 | + applyProps(state, { rotation }); |
| 177 | + } |
| 178 | + |
| 179 | + decal.geometry = new DecalGeometry(parent, state.position, state.rotation, state.scale); |
| 180 | + if (helper) { |
| 181 | + applyProps(helper, state); |
| 182 | + // Prevent the helpers from blocking rays |
| 183 | + helper.traverse((child) => (child.raycast = () => null)); |
| 184 | + } |
| 185 | + // Reset parents matix-world |
| 186 | + parent.matrixWorld = matrixWorld; |
| 187 | + |
| 188 | + onCleanup(() => { |
| 189 | + decal.geometry.dispose(); |
| 190 | + }); |
| 191 | + } |
| 192 | + }); |
| 193 | + } |
| 194 | +} |
0 commit comments