|
| 1 | +import { |
| 2 | + CUSTOM_ELEMENTS_SCHEMA, |
| 3 | + Component, |
| 4 | + Injector, |
| 5 | + Input, |
| 6 | + NgZone, |
| 7 | + OnInit, |
| 8 | + computed, |
| 9 | + effect, |
| 10 | + forwardRef, |
| 11 | + inject, |
| 12 | + signal, |
| 13 | + untracked, |
| 14 | +} from '@angular/core'; |
| 15 | +import { |
| 16 | + NgtArgs, |
| 17 | + NgtRef, |
| 18 | + createInjectionToken, |
| 19 | + extend, |
| 20 | + injectBeforeRender, |
| 21 | + injectNgtRef, |
| 22 | + is, |
| 23 | + signalStore, |
| 24 | + type NgtColor, |
| 25 | + type NgtVector3, |
| 26 | +} from 'angular-three'; |
| 27 | +import * as THREE from 'three'; |
| 28 | +import { Line2, LineMaterial, LineSegmentsGeometry } from 'three-stdlib'; |
| 29 | +import { SegmentObject } from './segment-object'; |
| 30 | + |
| 31 | +export type NgtsSegmentState = { |
| 32 | + start?: NgtVector3; |
| 33 | + end?: NgtVector3; |
| 34 | + color?: NgtColor; |
| 35 | +}; |
| 36 | + |
| 37 | +export type NgtsSegmentsState = { |
| 38 | + limit: number; |
| 39 | + lineWidth: number; |
| 40 | +}; |
| 41 | + |
| 42 | +declare global { |
| 43 | + interface HTMLElementTagNameMap { |
| 44 | + 'ngt-segment-object': SegmentObject; |
| 45 | + 'ngts-segment': NgtsSegmentState; |
| 46 | + 'ngts-segments': NgtsSegmentsState; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +const normPos = (pos: NgtsSegmentState['start']): SegmentObject['start'] => |
| 51 | + pos instanceof THREE.Vector3 |
| 52 | + ? pos |
| 53 | + : new THREE.Vector3(...((typeof pos === 'number' ? [pos, pos, pos] : pos) as [number, number, number])); |
| 54 | + |
| 55 | +extend({ SegmentObject }); |
| 56 | + |
| 57 | +export const [injectNgtsSegmentsApi, provideNgtsSegmentsApi] = createInjectionToken( |
| 58 | + (segments: NgtsSegments) => segments.api, |
| 59 | + { isRoot: false, deps: [forwardRef(() => NgtsSegments)] }, |
| 60 | +); |
| 61 | + |
| 62 | +@Component({ |
| 63 | + selector: 'ngts-segment', |
| 64 | + standalone: true, |
| 65 | + template: ` |
| 66 | + <ngt-segment-object [ref]="segmentRef" [color]="color()" [start]="normalizedStart()" [end]="normalizedEnd()" /> |
| 67 | + `, |
| 68 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 69 | +}) |
| 70 | +export class NgtsSegment implements OnInit { |
| 71 | + private inputs = signalStore<NgtsSegmentState>({}); |
| 72 | + |
| 73 | + @Input() segmentRef = injectNgtRef<SegmentObject>(); |
| 74 | + |
| 75 | + @Input({ alias: 'start' }) set _start(start: NgtVector3) { |
| 76 | + this.inputs.set({ start }); |
| 77 | + } |
| 78 | + |
| 79 | + @Input({ alias: 'end' }) set _end(end: NgtVector3) { |
| 80 | + this.inputs.set({ end }); |
| 81 | + } |
| 82 | + |
| 83 | + @Input({ alias: 'color' }) set _color(color: NgtColor) { |
| 84 | + this.inputs.set({ color }); |
| 85 | + } |
| 86 | + |
| 87 | + private segmentsApi = injectNgtsSegmentsApi(); |
| 88 | + private injector = inject(Injector); |
| 89 | + private zone = inject(NgZone); |
| 90 | + |
| 91 | + private start = this.inputs.select('start'); |
| 92 | + private end = this.inputs.select('end'); |
| 93 | + |
| 94 | + normalizedStart = computed(() => normPos(this.start())); |
| 95 | + normalizedEnd = computed(() => normPos(this.end())); |
| 96 | + color = this.inputs.select('color'); |
| 97 | + |
| 98 | + ngOnInit() { |
| 99 | + effect( |
| 100 | + (onCleanup) => { |
| 101 | + const cleanup = this.zone.runOutsideAngular(() => { |
| 102 | + const api = this.segmentsApi(); |
| 103 | + return api.subscribe(this.segmentRef); |
| 104 | + }); |
| 105 | + onCleanup(cleanup); |
| 106 | + }, |
| 107 | + { injector: this.injector }, |
| 108 | + ); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +@Component({ |
| 113 | + selector: 'ngts-segments', |
| 114 | + standalone: true, |
| 115 | + template: ` |
| 116 | + <ngt-primitive *args="[line]" [ref]="segmentsRef"> |
| 117 | + <ngt-primitive *args="[geometry]" attach="geometry" /> |
| 118 | + <ngt-primitive |
| 119 | + *args="[material]" |
| 120 | + attach="material" |
| 121 | + ngtCompound |
| 122 | + [vertexColors]="true" |
| 123 | + [resolution]="resolution" |
| 124 | + [linewidth]="lineWidth()" |
| 125 | + /> |
| 126 | + <ng-content /> |
| 127 | + </ngt-primitive> |
| 128 | + `, |
| 129 | + imports: [NgtArgs], |
| 130 | + providers: [provideNgtsSegmentsApi()], |
| 131 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 132 | +}) |
| 133 | +export class NgtsSegments { |
| 134 | + private inputs = signalStore<NgtsSegmentsState>({ limit: 1000, lineWidth: 1 }); |
| 135 | + |
| 136 | + @Input() segmentsRef = injectNgtRef<Line2>(); |
| 137 | + |
| 138 | + @Input({ alias: 'limit' }) set _limit(limit: number) { |
| 139 | + this.inputs.set({ limit }); |
| 140 | + } |
| 141 | + |
| 142 | + @Input({ alias: 'lineWidth' }) set _lineWidth(lineWidth: number) { |
| 143 | + this.inputs.set({ lineWidth }); |
| 144 | + } |
| 145 | + |
| 146 | + private segments = signal<NgtRef<SegmentObject>[]>([]); |
| 147 | + private limit = this.inputs.select('limit'); |
| 148 | + |
| 149 | + private positions = computed(() => Array(this.limit() * 6).fill(0)); |
| 150 | + private colors = computed(() => Array(this.limit() * 6).fill(0)); |
| 151 | + |
| 152 | + resolution = new THREE.Vector2(512, 512); |
| 153 | + lineWidth = this.inputs.select('lineWidth'); |
| 154 | + line = new Line2(); |
| 155 | + material = new LineMaterial(); |
| 156 | + geometry = new LineSegmentsGeometry(); |
| 157 | + |
| 158 | + api = computed(() => ({ |
| 159 | + subscribe: (segmentRef: NgtRef<SegmentObject>) => { |
| 160 | + untracked(() => { |
| 161 | + this.segments.update((s) => [...s, segmentRef]); |
| 162 | + }); |
| 163 | + |
| 164 | + return () => { |
| 165 | + untracked(() => { |
| 166 | + this.segments.update((s) => s.filter((segment) => segment !== segmentRef)); |
| 167 | + }); |
| 168 | + }; |
| 169 | + }, |
| 170 | + })); |
| 171 | + |
| 172 | + constructor() { |
| 173 | + this.beforeRender(); |
| 174 | + } |
| 175 | + |
| 176 | + private beforeRender() { |
| 177 | + injectBeforeRender(() => { |
| 178 | + const [segments, limit, positions, colors] = [ |
| 179 | + this.segments(), |
| 180 | + this.limit(), |
| 181 | + this.positions(), |
| 182 | + this.colors(), |
| 183 | + ]; |
| 184 | + for (let i = 0; i < limit; i++) { |
| 185 | + const segmentRef = segments[i]; |
| 186 | + const segment = is.ref(segmentRef) ? segmentRef.nativeElement : segmentRef; |
| 187 | + if (segment) { |
| 188 | + positions[i * 6 + 0] = segment.start.x; |
| 189 | + positions[i * 6 + 1] = segment.start.y; |
| 190 | + positions[i * 6 + 2] = segment.start.z; |
| 191 | + |
| 192 | + positions[i * 6 + 3] = segment.end.x; |
| 193 | + positions[i * 6 + 4] = segment.end.y; |
| 194 | + positions[i * 6 + 5] = segment.end.z; |
| 195 | + |
| 196 | + colors[i * 6 + 0] = segment.color.r; |
| 197 | + colors[i * 6 + 1] = segment.color.g; |
| 198 | + colors[i * 6 + 2] = segment.color.b; |
| 199 | + |
| 200 | + colors[i * 6 + 3] = segment.color.r; |
| 201 | + colors[i * 6 + 4] = segment.color.g; |
| 202 | + colors[i * 6 + 5] = segment.color.b; |
| 203 | + } |
| 204 | + } |
| 205 | + this.geometry.setColors(colors); |
| 206 | + this.geometry.setPositions(positions); |
| 207 | + this.line.computeLineDistances(); |
| 208 | + }); |
| 209 | + } |
| 210 | +} |
0 commit comments