Skip to content

Commit cb9d206

Browse files
committed
refactor(soba): update cameras with new renderer
1 parent 054a010 commit cb9d206

File tree

5 files changed

+73
-72
lines changed

5 files changed

+73
-72
lines changed

apps/docs/src/content/docs/index.mdx

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,30 @@ import FloatingCodeBlock from '../../components/ui/floating-code-block.astro';
2323
import SimpleScene from '../../components/scenes/simple-scene/simple-scene';
2424

2525
<section class="min-h-screen w-full flex justify-center items-center">
26-
<div class="relative h-full w-full grid grid-cols-8 gap-4">
26+
<div class="h-full w-full grid grid-cols-8 gap-4">
2727
<div class="col-span-3">
28-
<h1>
29-
Declarative <span class="text-accent-600 dark:text-accent-200">scene graphs</span>
30-
</h1>
31-
32-
<p>
33-
Angular Three allows the users to declaratively build a scene graph using the familiar Angular template syntax. This
34-
approach enables Angular developers to leverage familiar skills and tools of Angular template to build 3D scenes.
35-
</p>
36-
37-
<div class="h-[200px] w-full">
38-
<SimpleScene client:only />
39-
</div>
40-
28+
<h1>
29+
Declarative <span class="text-accent-600 dark:text-accent-200">scene graphs</span>
30+
</h1>
31+
<p>
32+
Angular Three allows the users to use every feature of THREE.js in a declarative way via Angular{' '}
33+
<strong>template syntax</strong>. Scale your 3D experiences with ease by leveraging Angular's batteries-included
34+
APIs like <strong>Signal</strong>, and more
35+
</p>
36+
<div class="h-[200px] w-full">
37+
<SimpleScene client:only />
38+
</div>
4139
</div>
4240
<FloatingCodeBlock
43-
class="col-span-5"
44-
snippets={[
45-
{ title: 'scene-graph.ts', name: 'simple-scene' },
46-
{ title: 'canvas.ts', name: 'simple-scene-canvas' },
47-
]}
41+
class="col-span-5"
42+
snippets={[
43+
{ title: 'scene-graph.ts', name: 'simple-scene' },
44+
{ title: 'canvas.ts', name: 'simple-scene-canvas' },
45+
]}
4846
/>
49-
5047
</div>
48+
</section>
5149

50+
<section class="min-h-screen w-full flex justify-center items-center">
51+
<div class="h-full w-full grid grid-cols-8 gap-4"></div>
5252
</section>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Directive, Signal } from '@angular/core';
2-
import { Texture } from 'three';
2+
import * as THREE from 'three';
33

44
@Directive({ selector: 'ng-template[cameraContent]' })
55
export class NgtsCameraContent {
6-
static ngTemplateContextGuard(_: NgtsCameraContent, ctx: unknown): ctx is { $implicit: Signal<Texture> } {
6+
static ngTemplateContextGuard(_: NgtsCameraContent, ctx: unknown): ctx is { $implicit: Signal<THREE.Texture> } {
77
return true;
88
}
99
}

libs/soba/cameras/src/lib/cube-camera.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import {
1313
untracked,
1414
viewChild,
1515
} from '@angular/core';
16-
import { extend, injectBeforeRender, injectStore, merge, NgtArgs, NgtGroup, omit, pick } from 'angular-three';
16+
import { extend, injectBeforeRender, injectStore, merge, NgtArgs, NgtThreeElements, omit, pick } from 'angular-three';
1717
import { assertInjector } from 'ngxtension/assert-injector';
1818
import { mergeInputs } from 'ngxtension/inject-inputs';
19-
import { CubeCamera, Fog, FogExp2, Group, HalfFloatType, Texture, WebGLCubeRenderTarget } from 'three';
19+
import * as THREE from 'three';
20+
import { Group } from 'three';
2021
import { NgtsCameraContent } from './camera-content';
2122

2223
export interface CubeCameraOptions {
@@ -27,16 +28,14 @@ export interface CubeCameraOptions {
2728
/** Camera far, 1000 */
2829
far?: number;
2930
/** Custom environment map that is temporarily set as the scenes background */
30-
envMap?: Texture;
31+
envMap?: THREE.Texture;
3132
/** Custom fog that is temporarily set as the scenes fog */
32-
fog?: Fog | FogExp2;
33+
fog?: THREE.Fog | THREE.FogExp2;
3334
}
3435

3536
export function injectCubeCamera(options: () => CubeCameraOptions, { injector }: { injector?: Injector } = {}) {
3637
return assertInjector(injectCubeCamera, injector, () => {
3738
const store = injectStore();
38-
const gl = store.select('gl');
39-
const scene = store.select('scene');
4039

4140
// backfill the options with default values
4241
const mergedOptions = merge(options, { resolution: 256, near: 0.1, far: 1000 }, 'backfill');
@@ -45,8 +44,8 @@ export function injectCubeCamera(options: () => CubeCameraOptions, { injector }:
4544
const far = pick(mergedOptions, 'far');
4645

4746
const fbo = computed(() => {
48-
const fbo = new WebGLCubeRenderTarget(resolution());
49-
fbo.texture.type = HalfFloatType;
47+
const fbo = new THREE.WebGLCubeRenderTarget(resolution());
48+
fbo.texture.type = THREE.HalfFloatType;
5049
return fbo;
5150
});
5251

@@ -55,26 +54,26 @@ export function injectCubeCamera(options: () => CubeCameraOptions, { injector }:
5554
onCleanup(() => _fbo.dispose());
5655
});
5756

58-
const camera = computed(() => new CubeCamera(near()!, far()!, fbo()));
57+
const cubeCamera = computed(() => new THREE.CubeCamera(near()!, far()!, fbo()));
5958
const update = () => {
60-
const [_scene, _gl, _camera, { envMap, fog }] = [scene(), gl(), camera(), untracked(mergedOptions)];
61-
let originalFog: Fog | FogExp2;
62-
let originalBackground: Texture;
63-
64-
originalFog = _scene.fog as Fog | FogExp2;
65-
originalBackground = _scene.background as Texture;
66-
_scene.background = envMap || originalBackground;
67-
_scene.fog = fog || originalFog;
68-
_camera.update(_gl, _scene);
69-
_scene.fog = originalFog;
70-
_scene.background = originalBackground;
59+
const [scene, gl, camera, { envMap, fog }] = [store.scene(), store.gl(), cubeCamera(), untracked(mergedOptions)];
60+
let originalFog: THREE.Fog | THREE.FogExp2;
61+
let originalBackground: THREE.Texture;
62+
63+
originalFog = scene.fog as THREE.Fog | THREE.FogExp2;
64+
originalBackground = scene.background as THREE.Texture;
65+
scene.background = envMap || originalBackground;
66+
scene.fog = fog || originalFog;
67+
camera.update(gl, scene);
68+
scene.fog = originalFog;
69+
scene.background = originalBackground;
7170
};
7271

73-
return { fbo, camera, update };
72+
return { fbo, camera: cubeCamera, update };
7473
});
7574
}
7675

77-
export interface NgtsCubeCameraOptions extends Partial<NgtGroup>, CubeCameraOptions {
76+
export interface NgtsCubeCameraOptions extends Partial<NgtThreeElements['ngt-group']>, CubeCameraOptions {
7877
frames: number;
7978
}
8079

@@ -101,15 +100,15 @@ const defaultOptions: NgtsCubeCameraOptions = {
101100
})
102101
export class NgtsCubeCamera {
103102
options = input(defaultOptions, { transform: mergeInputs(defaultOptions) });
104-
parameters = omit(this.options, ['fog', 'near', 'far', 'envMap', 'resolution', 'frames']);
103+
protected parameters = omit(this.options, ['fog', 'near', 'far', 'envMap', 'resolution', 'frames']);
105104

106105
private cubeCamera = injectCubeCamera(pick(this.options, ['near', 'far', 'envMap', 'fog', 'resolution']));
107106

108-
camera = this.cubeCamera.camera;
109-
texture = pick(this.cubeCamera.fbo, 'texture');
107+
protected camera = this.cubeCamera.camera;
108+
protected texture = pick(this.cubeCamera.fbo, 'texture');
110109

111-
groupRef = viewChild.required<ElementRef<Group>>('group');
112-
cameraContent = contentChild(NgtsCameraContent, { read: TemplateRef });
110+
groupRef = viewChild.required<ElementRef<THREE.Group>>('group');
111+
protected cameraContent = contentChild(NgtsCameraContent, { read: TemplateRef });
113112

114113
constructor() {
115114
extend({ Group });

libs/soba/cameras/src/lib/orthographic-camera.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import {
1212
untracked,
1313
viewChild,
1414
} from '@angular/core';
15-
import { NgtOrthographicCamera, extend, injectBeforeRender, injectStore, omit, pick } from 'angular-three';
15+
import { NgtThreeElements, extend, injectBeforeRender, injectStore, omit, pick } from 'angular-three';
1616
import { injectFBO } from 'angular-three-soba/misc';
1717
import { mergeInputs } from 'ngxtension/inject-inputs';
18-
import { Color, Group, OrthographicCamera, Texture } from 'three';
18+
import * as THREE from 'three';
19+
import { Group, OrthographicCamera } from 'three';
1920
import { NgtsCameraContent } from './camera-content';
2021

21-
export interface NgtsOrthographicCameraOptions extends Partial<NgtOrthographicCamera> {
22+
export interface NgtsOrthographicCameraOptions extends Partial<NgtThreeElements['ngt-orthographic-camera']> {
2223
/** Registers the camera as the system default, fiber will start rendering with it */
2324
makeDefault?: boolean;
2425
/** Making it manual will stop responsiveness and you have to calculate aspect ratio yourself. */
@@ -28,7 +29,7 @@ export interface NgtsOrthographicCameraOptions extends Partial<NgtOrthographicCa
2829
/** Resolution of the FBO, 256 */
2930
resolution: number;
3031
/** Optional environment map for functional use */
31-
envMap?: Texture;
32+
envMap?: THREE.Texture;
3233
}
3334

3435
const defaultOptions: NgtsOrthographicCameraOptions = {
@@ -74,16 +75,16 @@ export class NgtsOrthographicCamera {
7475
'right',
7576
]);
7677

77-
content = contentChild(TemplateRef);
78-
cameraContent = contentChild(NgtsCameraContent, { read: TemplateRef });
78+
protected content = contentChild(TemplateRef);
79+
protected cameraContent = contentChild(NgtsCameraContent, { read: TemplateRef });
7980

80-
cameraRef = viewChild.required<ElementRef<OrthographicCamera>>('camera');
81-
groupRef = viewChild.required<ElementRef<Group>>('group');
81+
cameraRef = viewChild.required<ElementRef<THREE.OrthographicCamera>>('camera');
82+
groupRef = viewChild.required<ElementRef<THREE.Group>>('group');
8283

8384
private store = injectStore();
8485

85-
private camera = this.store.select('camera');
86-
protected size = this.store.select('size');
86+
private camera = this.store.camera;
87+
private size = this.store.size;
8788

8889
protected left = computed(() => this.options().left ?? this.size().width / -2);
8990
protected right = computed(() => this.options().right ?? this.size().width / 2);
@@ -122,7 +123,7 @@ export class NgtsOrthographicCamera {
122123
});
123124

124125
let count = 0;
125-
let oldEnvMap: Color | Texture | null = null;
126+
let oldEnvMap: THREE.Color | THREE.Texture | null = null;
126127
injectBeforeRender(({ gl, scene }) => {
127128
const [{ frames, envMap }, group, camera, fbo] = [
128129
this.options(),

libs/soba/cameras/src/lib/perspective-camera.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import {
1111
untracked,
1212
viewChild,
1313
} from '@angular/core';
14-
import { NgtPerspectiveCamera, extend, injectBeforeRender, injectStore, omit, pick } from 'angular-three';
14+
import { NgtThreeElements, extend, injectBeforeRender, injectStore, omit, pick } from 'angular-three';
1515
import { injectFBO } from 'angular-three-soba/misc';
1616
import { mergeInputs } from 'ngxtension/inject-inputs';
17-
import { Color, Group, PerspectiveCamera, Texture } from 'three';
17+
import * as THREE from 'three';
18+
import { Group, PerspectiveCamera } from 'three';
1819
import { NgtsCameraContent } from './camera-content';
1920

20-
export interface NgtsPerspectiveCameraOptions extends Partial<NgtPerspectiveCamera> {
21+
export interface NgtsPerspectiveCameraOptions extends Partial<NgtThreeElements['ngt-perspective-camera']> {
2122
/** Registers the camera as the system default, fiber will start rendering with it */
2223
makeDefault?: boolean;
2324
/** Making it manual will stop responsiveness and you have to calculate aspect ratio yourself. */
@@ -27,7 +28,7 @@ export interface NgtsPerspectiveCameraOptions extends Partial<NgtPerspectiveCame
2728
/** Resolution of the FBO, 256 */
2829
resolution: number;
2930
/** Optional environment map for functional use */
30-
envMap?: Texture;
31+
envMap?: THREE.Texture;
3132
}
3233

3334
const defaultOptions: NgtsPerspectiveCameraOptions = {
@@ -56,16 +57,16 @@ export class NgtsPerspectiveCamera {
5657
options = input(defaultOptions, { transform: mergeInputs(defaultOptions) });
5758
protected parameters = omit(this.options, ['envMap', 'makeDefault', 'manual', 'frames', 'resolution']);
5859

59-
content = contentChild(TemplateRef);
60-
cameraContent = contentChild(NgtsCameraContent, { read: TemplateRef });
60+
protected content = contentChild(TemplateRef);
61+
protected cameraContent = contentChild(NgtsCameraContent, { read: TemplateRef });
6162

62-
cameraRef = viewChild.required<ElementRef<PerspectiveCamera>>('camera');
63-
groupRef = viewChild.required<ElementRef<Group>>('group');
63+
cameraRef = viewChild.required<ElementRef<THREE.PerspectiveCamera>>('camera');
64+
groupRef = viewChild.required<ElementRef<THREE.Group>>('group');
6465

6566
private store = injectStore();
6667

67-
private camera = this.store.select('camera');
68-
private size = this.store.select('size');
68+
private camera = this.store.camera;
69+
private size = this.store.size;
6970

7071
private manual = pick(this.options, 'manual');
7172
private makeDefault = pick(this.options, 'makeDefault');
@@ -96,9 +97,9 @@ export class NgtsPerspectiveCamera {
9697
this.store.update({ camera: this.cameraRef().nativeElement });
9798
onCleanup(() => this.store.update(() => ({ camera: oldCam })));
9899
});
99-
/**/
100+
100101
let count = 0;
101-
let oldEnvMap: Color | Texture | null = null;
102+
let oldEnvMap: THREE.Color | THREE.Texture | null = null;
102103
injectBeforeRender(({ gl, scene }) => {
103104
const [{ frames, envMap }, group, camera, fbo] = [
104105
this.options(),

0 commit comments

Comments
 (0)