1- import {
2- ChangeDetectionStrategy ,
3- Component ,
4- computed ,
5- CUSTOM_ELEMENTS_SCHEMA ,
6- effect ,
7- ElementRef ,
8- inject ,
9- viewChild ,
10- } from '@angular/core' ;
11- import { injectStore , NgtArgs , NgtEuler , NgtVector3 } from 'angular-three' ;
1+ import { ChangeDetectionStrategy , Component , CUSTOM_ELEMENTS_SCHEMA , inject } from '@angular/core' ;
2+ import { NgtArgs , NgtEuler , NgtParent , NgtVector3 } from 'angular-three' ;
123import { NgtsText } from 'angular-three-soba/abstractions' ;
13- import { FrontSide , Group } from 'three' ;
4+ import { FrontSide } from 'three' ;
145import { RockStore } from './store' ;
156
167@Component ( {
178 template : `
18- <ngt-group #group attach="none">
19- @if (selectedRock(); as rock) {
9+ @if (selectedRock(); as rock) {
10+ <ngt-group *parent=" rock.name">
2011 <ngt-mesh
2112 [castShadow]="true"
2213 [receiveShadow]="true"
@@ -43,10 +34,10 @@ import { RockStore } from './store';
4334 />
4435 }
4536 </ngt-group>
46- }
47- </ngt-group>
37+ </ngt-group>
38+ }
4839 ` ,
49- imports : [ NgtArgs , NgtsText ] ,
40+ imports : [ NgtArgs , NgtsText , NgtParent ] ,
5041 schemas : [ CUSTOM_ELEMENTS_SCHEMA ] ,
5142 changeDetection : ChangeDetectionStrategy . OnPush ,
5243 host : { class : 'colored-rock' } ,
@@ -64,35 +55,6 @@ export default class ColoredRock {
6455 ] as NgtVector3 ,
6556 } ) ) ;
6657
67- private groupRef = viewChild . required < ElementRef < Group > > ( 'group' ) ;
68-
6958 private rockStore = inject ( RockStore ) ;
7059 protected readonly selectedRock = this . rockStore . selectedRock ;
71-
72- private store = injectStore ( ) ;
73- private scene = this . store . select ( 'scene' ) ;
74-
75- private parent = computed ( ( ) => {
76- const selected = this . selectedRock ( ) ;
77- if ( ! selected ) return null ;
78-
79- const parent = this . scene ( ) . getObjectByName ( selected . name ) ;
80- if ( ! parent ) return null ;
81-
82- return parent ;
83- } ) ;
84-
85- constructor ( ) {
86- effect ( ( onCleanup ) => {
87- const parent = this . parent ( ) ;
88- if ( ! parent ) return ;
89-
90- const group = this . groupRef ( ) . nativeElement ;
91-
92- parent . add ( group ) ;
93- onCleanup ( ( ) => {
94- parent . remove ( group ) ;
95- } ) ;
96- } ) ;
97- }
9860}
0 commit comments