Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
feat(core): allow to pass Object3d getter to appendTo
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Jan 4, 2022
1 parent 5c8f4d1 commit d4b0bf7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import { Controller, createControllerProviderFactory } from './controller';
ngt-soba-gizmo-helper,
ngt-soba-gizmo-viewport,
ngt-soba-gizmo-axis-head,
ngt-soba-transform-controls,
ngt-soba-text
`,
exportAs: 'ngtObject3dInputsController',
Expand Down Expand Up @@ -148,7 +149,7 @@ export class NgtObject3dInputsController extends Controller {
@Input() raycast?: THREE.Object3D['raycast'] | null;

@Input() appendMode: 'immediate' | 'root' | 'none' = 'immediate';
@Input() appendTo?: THREE.Object3D;
@Input() appendTo?: THREE.Object3D | (() => THREE.Object3D);

@Input() object3dInputsController?: NgtObject3dInputsController;

Expand Down
20 changes: 14 additions & 6 deletions packages/core/src/lib/controllers/object-3d.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
SkipSelf,
} from '@angular/core';
import { requestAnimationFrame } from '@rx-angular/cdk/zone-less';
import { defer, map, merge, Subscription, take } from 'rxjs';
import { BehaviorSubject, defer, map, merge, Subscription, take } from 'rxjs';
import * as THREE from 'three';
import { NgtEventsStore } from '../stores/events.store';
import { NgtStore } from '../stores/store';
Expand Down Expand Up @@ -96,7 +96,9 @@ const supportedEvents = [
],
})
export class NgtObject3dController extends Controller implements OnDestroy {
#object3d?: THREE.Object3D;
#object3d = new BehaviorSubject<THREE.Object3D | null>(null);
readonly object3d$ = this.#object3d.asObservable();

#inputChangesSubscription?: Subscription;

#initFn?: () => THREE.Object3D;
Expand Down Expand Up @@ -168,7 +170,7 @@ export class NgtObject3dController extends Controller implements OnDestroy {

init() {
this.ngZone.runOutsideAngular(() => {
this.#object3d = this.initFn();
this.#object3d.next(this.initFn());
if (this.object3d) {
this.#applyCustomProps();

Expand Down Expand Up @@ -240,7 +242,7 @@ export class NgtObject3dController extends Controller implements OnDestroy {
// Schedule this in the next frame to allow for all appendTo's to settle
requestAnimationFrame(() => {
if (this.objectInputsController.appendTo) {
this.objectInputsController.appendTo.add(this.object3d);
this.#appendTo.add(this.object3d);
return;
}

Expand Down Expand Up @@ -272,7 +274,7 @@ export class NgtObject3dController extends Controller implements OnDestroy {

#remove() {
if (this.objectInputsController.appendTo) {
this.objectInputsController.appendTo.remove(this.object3d);
this.#appendTo.remove(this.object3d);
} else if (
this.parentObject3d &&
this.objectInputsController.appendMode === 'immediate'
Expand Down Expand Up @@ -405,7 +407,13 @@ export class NgtObject3dController extends Controller implements OnDestroy {
}

get object3d(): THREE.Object3D {
return this.#object3d as THREE.Object3D;
return this.#object3d.getValue() as THREE.Object3D;
}

get #appendTo() {
return this.objectInputsController.appendTo instanceof THREE.Object3D
? this.objectInputsController.appendTo
: (this.objectInputsController.appendTo as () => THREE.Object3D)();
}

get controller(): Controller | undefined {
Expand Down
1 change: 1 addition & 0 deletions tools/generators/three/controllers/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default async function controllersGenerator(
'soba-gizmo-helper',
'soba-gizmo-viewport',
'soba-gizmo-axis-head',
'soba-transform-controls',
];
const sobaWithMaterialSelectors = ['soba-text'];
const sobaAudioSelectors = ['soba-positional-audio'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class NgtObject3dInputsController extends Controller {
@Input() raycast?: THREE.Object3D['raycast'] | null;

@Input() appendMode: 'immediate' | 'root' | 'none' = 'immediate';
@Input() appendTo?: THREE.Object3D;
@Input() appendTo?: THREE.Object3D | (() => THREE.Object3D);

@Input() object3dInputsController?: NgtObject3dInputsController;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
SkipSelf,
} from '@angular/core';
import { requestAnimationFrame } from '@rx-angular/cdk/zone-less';
import { defer, map, merge, Subscription, take } from 'rxjs';
import { BehaviorSubject, defer, map, merge, Subscription, take } from 'rxjs';
import * as THREE from 'three';
import { NgtEventsStore } from '../stores/events.store';
import { NgtStore } from '../stores/store';
Expand Down Expand Up @@ -67,7 +67,9 @@ const supportedEvents = [
],
})
export class NgtObject3dController extends Controller implements OnDestroy {
#object3d?: THREE.Object3D;
#object3d = new BehaviorSubject<THREE.Object3D | null>(null);
readonly object3d$ = this.#object3d.asObservable();

#inputChangesSubscription?: Subscription;

#initFn?: () => THREE.Object3D;
Expand Down Expand Up @@ -139,7 +141,7 @@ export class NgtObject3dController extends Controller implements OnDestroy {

init() {
this.ngZone.runOutsideAngular(() => {
this.#object3d = this.initFn();
this.#object3d.next(this.initFn());
if (this.object3d) {
this.#applyCustomProps();

Expand Down Expand Up @@ -211,7 +213,7 @@ export class NgtObject3dController extends Controller implements OnDestroy {
// Schedule this in the next frame to allow for all appendTo's to settle
requestAnimationFrame(() => {
if (this.objectInputsController.appendTo) {
this.objectInputsController.appendTo.add(this.object3d);
this.#appendTo.add(this.object3d);
return;
}

Expand Down Expand Up @@ -243,7 +245,7 @@ export class NgtObject3dController extends Controller implements OnDestroy {

#remove() {
if (this.objectInputsController.appendTo) {
this.objectInputsController.appendTo.remove(this.object3d);
this.#appendTo.remove(this.object3d);
} else if (
this.parentObject3d &&
this.objectInputsController.appendMode === 'immediate'
Expand Down Expand Up @@ -376,7 +378,13 @@ export class NgtObject3dController extends Controller implements OnDestroy {
}

get object3d(): THREE.Object3D {
return this.#object3d as THREE.Object3D;
return this.#object3d.getValue() as THREE.Object3D;
}

get #appendTo() {
return this.objectInputsController.appendTo instanceof THREE.Object3D
? this.objectInputsController.appendTo
: (this.objectInputsController.appendTo as () => THREE.Object3D)();
}

get controller(): Controller | undefined {
Expand Down

0 comments on commit d4b0bf7

Please sign in to comment.