@@ -8,34 +8,47 @@ import {
8
8
Directive ,
9
9
effect ,
10
10
ElementRef ,
11
+ Injector ,
11
12
input ,
12
13
output ,
13
- Signal ,
14
14
TemplateRef ,
15
15
viewChild ,
16
16
} from '@angular/core' ;
17
- import { extend , hasListener , injectBeforeRender , injectStore , NgtPortal , NgtPortalContent , pick } from 'angular-three' ;
17
+ import {
18
+ extend ,
19
+ hasListener ,
20
+ injectBeforeRender ,
21
+ injectStore ,
22
+ NgtPortal ,
23
+ NgtPortalAutoRender ,
24
+ NgtPortalContent ,
25
+ pick ,
26
+ } from 'angular-three' ;
18
27
import { NgtsOrthographicCamera } from 'angular-three-soba/cameras' ;
19
28
import CameraControls from 'camera-controls' ;
20
29
import { mergeInputs } from 'ngxtension/inject-inputs' ;
21
- import { Group , Matrix4 , Object3D , Quaternion , Scene , Vector3 } from 'three' ;
30
+ import * as THREE from 'three' ;
31
+ import { Group } from 'three' ;
22
32
import { OrbitControls } from 'three-stdlib' ;
23
33
24
34
const turnRate = 2 * Math . PI ; // turn rate in angles per second
25
- const dummy = new Object3D ( ) ;
26
- const matrix = new Matrix4 ( ) ;
27
- const [ q1 , q2 ] = [ new Quaternion ( ) , new Quaternion ( ) ] ;
28
- const target = new Vector3 ( ) ;
29
- const targetPosition = new Vector3 ( ) ;
35
+ const dummy = new THREE . Object3D ( ) ;
36
+ const matrix = new THREE . Matrix4 ( ) ;
37
+ const [ q1 , q2 ] = [ new THREE . Quaternion ( ) , new THREE . Quaternion ( ) ] ;
38
+ const target = new THREE . Vector3 ( ) ;
39
+ const targetPosition = new THREE . Vector3 ( ) ;
30
40
31
41
@Directive ( { selector : 'ng-template[gizmoHelperContent]' } )
32
42
export class NgtsGizmoHelperContent {
33
- static ngTemplateContextGuard ( _ : NgtsGizmoHelperContent , ctx : unknown ) : ctx is { container : Object3D } {
43
+ static ngTemplateContextGuard (
44
+ _ : NgtsGizmoHelperContent ,
45
+ ctx : unknown ,
46
+ ) : ctx is { container : THREE . Object3D ; injector : Injector } {
34
47
return true ;
35
48
}
36
49
}
37
50
38
- type ControlsProto = { update ( delta ?: number ) : void ; target : Vector3 } ;
51
+ type ControlsProto = { update ( delta ?: number ) : void ; target : THREE . Vector3 } ;
39
52
40
53
export interface NgtsGizmoHelperOptions {
41
54
alignment :
@@ -66,16 +79,15 @@ const defaultOptions: NgtsGizmoHelperOptions = {
66
79
67
80
<ngt-portal
68
81
[container]="scene"
69
- [autoRender]="true"
70
- [autoRenderPriority]="_renderPriority"
82
+ [autoRender]="_renderPriority"
71
83
[state]="{ events: { priority: _renderPriority + 1 } }"
72
84
>
73
- <ng-template portalContent let-injector="injector" let-container="container" >
85
+ <ng-template portalContent let-injector="injector">
74
86
<ngts-orthographic-camera [options]="{ makeDefault: true, position: [0, 0, 200] }" />
75
87
<ngt-group #gizmo [position]="[x(), y(), 0]">
76
88
<ng-container
77
89
[ngTemplateOutlet]="content()"
78
- [ngTemplateOutletContext]="{ container, injector }"
90
+ [ngTemplateOutletContext]="{ container: scene , injector }"
79
91
[ngTemplateOutletInjector]="injector"
80
92
/>
81
93
</ngt-group>
@@ -84,7 +96,7 @@ const defaultOptions: NgtsGizmoHelperOptions = {
84
96
` ,
85
97
schemas : [ CUSTOM_ELEMENTS_SCHEMA ] ,
86
98
changeDetection : ChangeDetectionStrategy . OnPush ,
87
- imports : [ NgtPortal , NgtPortalContent , NgtsOrthographicCamera , NgTemplateOutlet ] ,
99
+ imports : [ NgtPortal , NgtPortalAutoRender , NgtPortalContent , NgtsOrthographicCamera , NgTemplateOutlet ] ,
88
100
} )
89
101
export class NgtsGizmoHelper {
90
102
options = input ( defaultOptions , { transform : mergeInputs ( defaultOptions ) } ) ;
@@ -94,41 +106,38 @@ export class NgtsGizmoHelper {
94
106
protected margin = pick ( this . options , 'margin' ) ;
95
107
protected alignment = pick ( this . options , 'alignment' ) ;
96
108
97
- protected scene = new Scene ( ) ;
109
+ protected scene = new THREE . Scene ( ) ;
98
110
99
111
protected content = contentChild . required ( NgtsGizmoHelperContent , { read : TemplateRef } ) ;
100
112
101
- private gizmoRef = viewChild < ElementRef < Group > > ( 'gizmo' ) ;
113
+ private gizmoRef = viewChild < ElementRef < THREE . Group > > ( 'gizmo' ) ;
102
114
private virtualCameraRef = viewChild ( NgtsOrthographicCamera ) ;
103
115
104
116
private store = injectStore ( ) ;
105
- private size = this . store . select ( 'size' ) ;
106
- private mainCamera = this . store . select ( 'camera' ) ;
107
- private defaultControls = this . store . select ( 'controls' ) as unknown as Signal < ControlsProto > ;
108
- private invalidate = this . store . select ( 'invalidate' ) ;
109
117
110
118
protected x = computed ( ( ) => {
111
119
const alignment = this . alignment ( ) ;
112
120
if ( alignment . endsWith ( '-center' ) ) return 0 ;
113
121
114
- const [ { width } , [ marginX ] ] = [ this . size ( ) , this . margin ( ) ] ;
122
+ const [ width , [ marginX ] ] = [ this . store . size . width ( ) , this . margin ( ) ] ;
115
123
return alignment . endsWith ( '-left' ) ? - width / 2 + marginX : width / 2 - marginX ;
116
124
} ) ;
117
125
protected y = computed ( ( ) => {
118
126
const alignment = this . alignment ( ) ;
119
127
if ( alignment . startsWith ( 'center-' ) ) return 0 ;
120
128
121
- const [ { height } , [ marginY ] ] = [ this . size ( ) , this . margin ( ) ] ;
129
+ const [ height , [ marginY ] ] = [ this . store . size . height ( ) , this . margin ( ) ] ;
122
130
return alignment . startsWith ( 'top-' ) ? height / 2 - marginY : - height / 2 + marginY ;
123
131
} ) ;
124
132
125
133
private animating = false ;
126
134
private radius = 0 ;
127
- private focusPoint = new Vector3 ( 0 , 0 , 0 ) ;
128
- private defaultUp = new Vector3 ( 0 , 0 , 0 ) ;
135
+ private focusPoint = new THREE . Vector3 ( 0 , 0 , 0 ) ;
136
+ private defaultUp = new THREE . Vector3 ( 0 , 0 , 0 ) ;
129
137
130
138
constructor ( ) {
131
139
extend ( { Group } ) ;
140
+
132
141
effect ( ( ) => {
133
142
this . updateDefaultUpEffect ( ) ;
134
143
} ) ;
@@ -141,7 +150,11 @@ export class NgtsGizmoHelper {
141
150
142
151
if ( ! virtualCamera || ! gizmo ) return ;
143
152
144
- const [ defaultControls , mainCamera , invalidate ] = [ this . defaultControls ( ) , this . mainCamera ( ) , this . invalidate ( ) ] ;
153
+ const [ defaultControls , mainCamera , invalidate ] = [
154
+ this . store . snapshot . controls as unknown as ControlsProto ,
155
+ this . store . snapshot . camera ,
156
+ this . store . snapshot . invalidate ,
157
+ ] ;
145
158
146
159
// Animate step
147
160
if ( this . animating ) {
@@ -179,8 +192,12 @@ export class NgtsGizmoHelper {
179
192
} ) ;
180
193
}
181
194
182
- tweenCamera ( direction : Vector3 ) {
183
- const [ defaultControls , invalidate , mainCamera ] = [ this . defaultControls ( ) , this . invalidate ( ) , this . mainCamera ( ) ] ;
195
+ tweenCamera ( direction : THREE . Vector3 ) {
196
+ const [ defaultControls , invalidate , mainCamera ] = [
197
+ this . store . snapshot . controls as unknown as ControlsProto ,
198
+ this . store . snapshot . invalidate ,
199
+ this . store . snapshot . camera ,
200
+ ] ;
184
201
185
202
this . animating = true ;
186
203
if ( defaultControls ) {
@@ -202,7 +219,7 @@ export class NgtsGizmoHelper {
202
219
}
203
220
204
221
private updateDefaultUpEffect ( ) {
205
- const mainCamera = this . mainCamera ( ) ;
222
+ const mainCamera = this . store . camera ( ) ;
206
223
this . defaultUp . copy ( mainCamera . up ) ;
207
224
}
208
225
0 commit comments