This repository has been archived by the owner on Feb 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,171 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { NgModule } from '@angular/core'; | ||
import { NgtCanvasComponent } from './canvas.component'; | ||
import { NgtObject3dControllerDirective } from './three/object-3d.controller'; | ||
|
||
@NgModule({ | ||
imports: [CommonModule], | ||
declarations: [NgtCanvasComponent], | ||
exports: [NgtCanvasComponent], | ||
declarations: [NgtCanvasComponent, NgtObject3dControllerDirective], | ||
exports: [NgtCanvasComponent, NgtObject3dControllerDirective], | ||
}) | ||
export class NgtCoreModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
Directive, | ||
EventEmitter, | ||
NgZone, | ||
OnDestroy, | ||
Output, | ||
} from '@angular/core'; | ||
import * as THREE from 'three'; | ||
import type { NgtAnimationReady } from '../models'; | ||
import { AnimationStore } from '../stores/animation.store'; | ||
|
||
@Directive() | ||
export abstract class NgtAnimationLoopParticipant<TObject = unknown> | ||
implements OnDestroy | ||
{ | ||
@Output() animateReady = new EventEmitter<NgtAnimationReady<TObject>>(); | ||
|
||
private animateTeardown?: () => void; | ||
|
||
protected constructor( | ||
protected animationStore: AnimationStore, | ||
protected ngZone: NgZone | ||
) {} | ||
|
||
protected participate(animateObject: TObject) { | ||
this.ngZone.runOutsideAngular(() => { | ||
if (this.animateReady.observed) { | ||
if (animateObject instanceof THREE.Object3D) { | ||
this.animateTeardown = this.animationStore.registerAnimation( | ||
animateObject as THREE.Object3D, | ||
(obj, state) => { | ||
this.animateReady.emit({ | ||
animateObject: obj as unknown as TObject, | ||
renderState: state, | ||
}); | ||
} | ||
); | ||
} else { | ||
this.animateTeardown = this.animationStore.registerAnimation( | ||
(state) => { | ||
this.animateReady.emit({ | ||
animateObject, | ||
renderState: state, | ||
}); | ||
} | ||
); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.ngZone.runOutsideAngular(() => { | ||
if (this.animateTeardown) { | ||
this.animateTeardown(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { | ||
Directive, | ||
Input, | ||
NgZone, | ||
OnChanges, | ||
OnDestroy, | ||
OnInit, | ||
Optional, | ||
} from '@angular/core'; | ||
import * as THREE from 'three'; | ||
import type { AnyConstructor } from '../models'; | ||
import { NgtGeometry } from './geometry'; | ||
|
||
@Directive() | ||
export abstract class NgtAttribute< | ||
TAttribute extends THREE.BufferAttribute = THREE.BufferAttribute | ||
> implements OnInit, OnChanges, OnDestroy | ||
{ | ||
@Input() attach?: THREE.BuiltinShaderAttributeName; | ||
|
||
abstract attributeType: AnyConstructor<TAttribute>; | ||
|
||
protected constructor( | ||
protected ngZone: NgZone, | ||
@Optional() protected geometryDirective?: NgtGeometry | ||
) {} | ||
|
||
private _extraArgs: unknown[] = []; | ||
|
||
protected set extraArgs(v: unknown[]) { | ||
this._extraArgs = v; | ||
this.ngZone.runOutsideAngular(() => { | ||
this.init(); | ||
}); | ||
} | ||
|
||
private _attribute?: TAttribute; | ||
|
||
ngOnChanges() { | ||
this.ngZone.runOutsideAngular(() => { | ||
if (this.attribute) { | ||
this.attribute.needsUpdate = true; | ||
} | ||
}); | ||
} | ||
|
||
ngOnInit() { | ||
this.ngZone.runOutsideAngular(() => { | ||
if (!this.attribute) { | ||
this.init(); | ||
} | ||
}); | ||
} | ||
|
||
private init() { | ||
if (this.geometryDirective && this.attach) { | ||
this._attribute = new this.attributeType(...this._extraArgs); | ||
if (this.attribute) { | ||
this.geometryDirective.geometry.setAttribute( | ||
this.attach, | ||
this.attribute | ||
); | ||
} | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
this.ngZone.runOutsideAngular(() => { | ||
if (this.geometryDirective && this.attach) { | ||
this.geometryDirective.geometry.deleteAttribute(this.attach); | ||
} | ||
}); | ||
} | ||
|
||
get attribute(): TAttribute | undefined { | ||
return this._attribute; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Directive, Input, NgZone, OnInit, Optional } from '@angular/core'; | ||
import * as THREE from 'three'; | ||
import type { AnyConstructor } from '../models'; | ||
import { NgtGeometry } from './geometry'; | ||
|
||
@Directive() | ||
export abstract class NgtCurve< | ||
TCurve extends THREE.Curve<THREE.Vector> = THREE.Curve<THREE.Vector> | ||
> implements OnInit | ||
{ | ||
@Input() divisions?: number; | ||
|
||
abstract curveType: AnyConstructor<TCurve>; | ||
|
||
private _extraArgs: unknown[] = []; | ||
|
||
protected set extraArgs(v: unknown[]) { | ||
this._extraArgs = v; | ||
this.ngZone.runOutsideAngular(() => { | ||
this.init(); | ||
}); | ||
} | ||
|
||
protected constructor( | ||
protected ngZone: NgZone, | ||
@Optional() protected geometryDirective?: NgtGeometry | ||
) {} | ||
|
||
private _curve?: TCurve; | ||
|
||
ngOnInit() { | ||
this.ngZone.runOutsideAngular(() => { | ||
if (!this.curve) { | ||
this.init(); | ||
} | ||
}); | ||
} | ||
|
||
private init() { | ||
this._curve = new this.curveType(...this._extraArgs); | ||
if (this.curve && this.geometryDirective) { | ||
const points = this.curve.getPoints(this.divisions); | ||
this.geometryDirective.geometry.setFromPoints( | ||
points as unknown as THREE.Vector3[] | THREE.Vector2[] | ||
); | ||
} | ||
} | ||
|
||
get curve(): TCurve | undefined { | ||
return this._curve; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { | ||
Directive, | ||
Input, | ||
NgZone, | ||
OnDestroy, | ||
OnInit, | ||
SkipSelf, | ||
} from '@angular/core'; | ||
import * as THREE from 'three'; | ||
import type { AnyConstructor } from '../models'; | ||
import { InstancesStore } from '../stores/instances.store'; | ||
|
||
@Directive() | ||
export abstract class NgtGeometry< | ||
TGeometry extends THREE.BufferGeometry = THREE.BufferGeometry | ||
> implements OnInit, OnDestroy | ||
{ | ||
@Input() ngtId?: string; | ||
|
||
protected constructor( | ||
@SkipSelf() protected instancesStore: InstancesStore, | ||
protected ngZone: NgZone | ||
) {} | ||
|
||
abstract geometryType: AnyConstructor<TGeometry>; | ||
|
||
private _extraArgs: unknown[] = []; | ||
protected set extraArgs(v: unknown[]) { | ||
this._extraArgs = v; | ||
this.ngZone.runOutsideAngular(() => { | ||
this.init(); | ||
}); | ||
} | ||
|
||
ngOnInit() { | ||
this.ngZone.runOutsideAngular(() => { | ||
if (!this.geometry) { | ||
this.init(); | ||
} | ||
}); | ||
} | ||
|
||
private init() { | ||
this._geometry = new this.geometryType(...this._extraArgs); | ||
|
||
this.instancesStore.saveGeometry({ | ||
id: this.ngtId, | ||
geometry: this._geometry, | ||
}); | ||
} | ||
|
||
private _geometry!: TGeometry; | ||
get geometry(): TGeometry { | ||
return this._geometry; | ||
} | ||
|
||
ngOnDestroy() { | ||
this.ngZone.runOutsideAngular(() => { | ||
if (this.geometry) { | ||
this.instancesStore.removeGeometry(this.ngtId || this.geometry.uuid); | ||
this.geometry.dispose(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Directive, OnChanges, OnInit } from '@angular/core'; | ||
import * as THREE from 'three'; | ||
import type { AnyConstructor } from '../models'; | ||
import { NgtObject3d } from './object-3d'; | ||
|
||
@Directive() | ||
export abstract class NgtHelper<THelper extends THREE.Object3D> | ||
extends NgtObject3d<THelper> | ||
implements OnInit, OnChanges | ||
{ | ||
abstract helperType: AnyConstructor<THelper>; | ||
|
||
private _extraArgs: unknown[] = []; | ||
|
||
private _helper!: THelper; | ||
|
||
protected set extraArgs(v: unknown[]) { | ||
this._extraArgs = v; | ||
this.ngZone.runOutsideAngular(() => { | ||
this.init(); | ||
}); | ||
} | ||
|
||
ngOnChanges() { | ||
super.ngOnChanges(); | ||
this.inputChangeHandler(); | ||
} | ||
|
||
inputChangeHandler = () => { | ||
if (!this.object3d) { | ||
this.init(); | ||
} | ||
}; | ||
|
||
ngOnInit() { | ||
this.inputChangeHandler(); | ||
} | ||
|
||
protected initObject() { | ||
try { | ||
this._helper = new this.helperType(...this._extraArgs); | ||
} catch (e) { | ||
console.log('Failed to initialize Helper'); | ||
} | ||
} | ||
|
||
get object3d(): THelper { | ||
return this._helper; | ||
} | ||
} |
Oops, something went wrong.