|
| 1 | +import { CUSTOM_ELEMENTS_SCHEMA, Component, Injector, Input, computed, effect, inject } from '@angular/core'; |
| 2 | +import { NgtArgs, NgtStore, injectNgtRef } from 'angular-three'; |
| 3 | +import * as THREE from 'three'; |
| 4 | +import { Line2, LineGeometry, LineMaterial, LineSegments2, LineSegmentsGeometry } from 'three-stdlib'; |
| 5 | +import { NgtsLineInputs, NgtsLineState } from './line-input'; |
| 6 | + |
| 7 | +declare global { |
| 8 | + interface HTMLElementTagNameMap { |
| 9 | + 'ngts-line': NgtsLineState; |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +@Component({ |
| 14 | + selector: 'ngts-line', |
| 15 | + standalone: true, |
| 16 | + template: ` |
| 17 | + <ngt-primitive *args="[line()]" [ref]="lineRef" ngtCompound> |
| 18 | + <ngt-primitive *args="[lineGeometry()]" attach="geometry" /> |
| 19 | + <ngt-primitive |
| 20 | + *args="[lineMaterial]" |
| 21 | + attach="material" |
| 22 | + [color]="lineMaterialParameters().color" |
| 23 | + [vertexColors]="lineMaterialParameters().vertexColors" |
| 24 | + [resolution]="lineMaterialParameters().resolution" |
| 25 | + [linewidth]="lineMaterialParameters().linewidth" |
| 26 | + [alphaToCoverage]="lineMaterialParameters().alphaToCoverage" |
| 27 | + [dashed]="lineMaterialParameters().dashed" |
| 28 | + [dashScale]="lineMaterialParameters().dashScale" |
| 29 | + [dashSize]="lineMaterialParameters().dashSize" |
| 30 | + [dashOffset]="lineMaterialParameters().dashOffset" |
| 31 | + [gapSize]="lineMaterialParameters().gapSize" |
| 32 | + [wireframe]="lineMaterialParameters().wireframe" |
| 33 | + [worldUnits]="lineMaterialParameters().worldUnits" |
| 34 | + /> |
| 35 | + </ngt-primitive> |
| 36 | + `, |
| 37 | + imports: [NgtArgs], |
| 38 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 39 | +}) |
| 40 | +export class NgtsLine extends NgtsLineInputs { |
| 41 | + @Input() lineRef = injectNgtRef<LineSegments2 | Line2>(); |
| 42 | + |
| 43 | + @Input() set points( |
| 44 | + points: Array<THREE.Vector3 | THREE.Vector2 | [number, number, number] | [number, number] | number> |
| 45 | + ) { |
| 46 | + this.set({ points }); |
| 47 | + } |
| 48 | + |
| 49 | + @Input() set segments(segments: boolean) { |
| 50 | + this.set({ segments }); |
| 51 | + } |
| 52 | + |
| 53 | + readonly #injector = inject(Injector); |
| 54 | + readonly #store = inject(NgtStore); |
| 55 | + |
| 56 | + readonly #resolution = computed(() => { |
| 57 | + const size = this.#store.select('size'); |
| 58 | + const resolution = this.select('resolution'); |
| 59 | + |
| 60 | + return resolution() ? resolution() : [size().width, size().height]; |
| 61 | + }); |
| 62 | + |
| 63 | + readonly #pointValues = computed(() => { |
| 64 | + const points = this.select('points'); |
| 65 | + return points().map((p) => { |
| 66 | + const isArray = Array.isArray(p); |
| 67 | + return p instanceof THREE.Vector3 |
| 68 | + ? [p.x, p.y, p.z] |
| 69 | + : p instanceof THREE.Vector2 |
| 70 | + ? [p.x, p.y, 0] |
| 71 | + : isArray && p.length === 3 |
| 72 | + ? [p[0], p[1], p[2]] |
| 73 | + : isArray && p.length === 2 |
| 74 | + ? [p[0], p[1], 0] |
| 75 | + : p; |
| 76 | + }); |
| 77 | + }); |
| 78 | + readonly #vertexColors = computed(() => { |
| 79 | + const vertexColors = this.select('vertexColors'); |
| 80 | + return (vertexColors() || []).map((c) => (c instanceof THREE.Color ? c.toArray() : c)); |
| 81 | + }); |
| 82 | + |
| 83 | + readonly lineGeometry = computed(() => { |
| 84 | + const segments = this.select('segments'); |
| 85 | + const pointValues = this.#pointValues(); |
| 86 | + const vertexColors = this.#vertexColors(); |
| 87 | + |
| 88 | + const geometry = segments() ? new LineSegmentsGeometry() : new LineGeometry(); |
| 89 | + geometry.setPositions(pointValues.flat()); |
| 90 | + |
| 91 | + if (vertexColors.length) { |
| 92 | + geometry.setColors(vertexColors.flat()); |
| 93 | + } |
| 94 | + |
| 95 | + return geometry; |
| 96 | + }); |
| 97 | + readonly lineMaterial = new LineMaterial(); |
| 98 | + readonly line = computed(() => { |
| 99 | + const segments = this.select('segments'); |
| 100 | + return segments() ? new LineSegments2() : new Line2(); |
| 101 | + }); |
| 102 | + |
| 103 | + readonly lineMaterialParameters = computed(() => { |
| 104 | + const color = this.select('color'); |
| 105 | + const vertexColors = this.select('vertexColors'); |
| 106 | + const resolution = this.#resolution(); |
| 107 | + const linewidth = this.select('lineWidth'); |
| 108 | + const alphaToCoverage = this.select('alphaToCoverage'); |
| 109 | + const dashed = this.select('dashed'); |
| 110 | + const dashScale = this.select('dashScale'); |
| 111 | + const dashSize = this.select('dashSize'); |
| 112 | + const dashOffset = this.select('dashOffset'); |
| 113 | + const gapSize = this.select('gapSize'); |
| 114 | + const wireframe = this.select('wireframe'); |
| 115 | + const worldUnits = this.select('worldUnits'); |
| 116 | + |
| 117 | + return { |
| 118 | + color: color(), |
| 119 | + vertexColors: Boolean(vertexColors()), |
| 120 | + resolution, |
| 121 | + linewidth: linewidth(), |
| 122 | + alphaToCoverage: alphaToCoverage(), |
| 123 | + dashed: dashed(), |
| 124 | + dashScale: dashScale() ?? this.lineMaterial.dashScale, |
| 125 | + dashSize: dashSize() ?? this.lineMaterial.dashSize, |
| 126 | + dashOffset: dashOffset() ?? this.lineMaterial.dashOffset, |
| 127 | + gapSize: gapSize() ?? this.lineMaterial.gapSize, |
| 128 | + wireframe: wireframe() ?? this.lineMaterial.wireframe, |
| 129 | + worldUnits: worldUnits() ?? this.lineMaterial.worldUnits, |
| 130 | + }; |
| 131 | + }); |
| 132 | + |
| 133 | + constructor() { |
| 134 | + super(); |
| 135 | + this.set({ segments: false }); |
| 136 | + this.#disposeGeometry(); |
| 137 | + this.#computeLineDistances(); |
| 138 | + } |
| 139 | + |
| 140 | + #computeLineDistances() { |
| 141 | + const trigger = computed(() => { |
| 142 | + const points = this.select('points'); |
| 143 | + const lineGeometry = this.lineGeometry(); |
| 144 | + const line = this.line(); |
| 145 | + const children = this.lineRef.children('nonObjects'); |
| 146 | + return { points: points(), lineGeometry, line, children: children() }; |
| 147 | + }); |
| 148 | + effect( |
| 149 | + () => { |
| 150 | + const { line, children } = trigger(); |
| 151 | + if (children.length) { |
| 152 | + line.computeLineDistances(); |
| 153 | + } |
| 154 | + }, |
| 155 | + { injector: this.#injector } |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + #disposeGeometry() { |
| 160 | + effect( |
| 161 | + (onCleanup) => { |
| 162 | + const lineGeometry = this.lineGeometry(); |
| 163 | + onCleanup(() => lineGeometry.dispose()); |
| 164 | + }, |
| 165 | + { injector: this.#injector } |
| 166 | + ); |
| 167 | + } |
| 168 | +} |
0 commit comments