Skip to content

Commit 0ac4b0a

Browse files
committed
fix: rename scene input to sceneGraph to differentiate it with THREE Scene
1 parent 3627823 commit 0ac4b0a

File tree

4 files changed

+118
-38
lines changed

4 files changed

+118
-38
lines changed

apps/demo/src/app/test/scene.component.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

apps/demo/src/app/test/test.component.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1-
import { Component } from '@angular/core';
2-
import { NgtCanvas } from 'angular-three';
3-
import { Scene } from './scene.component';
1+
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
2+
import { NgtBeforeRenderEvent, NgtCanvas } from 'angular-three';
3+
4+
@Component({
5+
selector: 'demo-test-scene',
6+
standalone: true,
7+
template: `
8+
<ngt-mesh
9+
(click)="active = !active"
10+
(pointerover)="hover = true"
11+
(pointerout)="hover = false"
12+
[scale]="active ? 1.5 : 1"
13+
(beforeRender)="onBeforeRender($any($event))"
14+
>
15+
<ngt-box-geometry />
16+
<ngt-mesh-basic-material [color]="hover ? 'goldenrod' : 'darkred'" />
17+
</ngt-mesh>
18+
`,
19+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
20+
})
21+
export class Scene {
22+
active = false;
23+
hover = false;
24+
25+
onBeforeRender({ object }: NgtBeforeRenderEvent<THREE.Mesh>) {
26+
object.rotation.x += 0.01;
27+
object.rotation.y += 0.01;
28+
}
29+
}
430

531
@Component({
632
selector: 'demo-test',
733
standalone: true,
8-
template: `<ngt-canvas [scene]="Scene" />`,
34+
template: `<ngt-canvas [sceneGraph]="Scene" />`,
935
imports: [NgtCanvas],
1036
})
1137
export default class DemoTest {

apps/documentation/docs/getting-started/first-scene.mdx

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,96 @@ sidebar_label: First Scene
66

77
This tutorial will help us setup our first NGT scene and introduce us to some of its core concepts.
88

9+
:::note
10+
11+
- We are using Inline Template syntax for this tutorial
12+
- We will put everything in `app.component.ts` for simplicity
13+
14+
:::
15+
16+
## Create a root component for our Scene graph
17+
18+
Let's start by creating a Component as the root of our Scene graph. We'll put the component in `app.component.ts` for now
19+
20+
```ts title="app.component.ts"
21+
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
22+
23+
@Component({
24+
standalone: true,
25+
template: ``,
26+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
27+
})
28+
export class SceneGraph {}
29+
30+
@Component({
31+
/*...*/
32+
})
33+
export class AppComponent {}
34+
```
35+
36+
- The `template` is intentionally left empty, we'll fill it out later.
37+
- The `selector` is intentionally left out because we'll not render our `SceneGraph` component on the template.
38+
- `CUSTOM_ELEMENTS_SCHEMA` is required because Angular does not support custom schemas so our Custom Elements won't work without it.
39+
940
## Set up the Canvas
1041

11-
The scene graph in NGT starts with `<ngt-canvas>`. Let's start by rendering `<ngt-canvas>` on our `app.component.ts`.
42+
The scene graph in NGT starts with `<ngt-canvas>`. Let's render `<ngt-canvas>` on our `app.component.ts` template.
1243
Make sure to import `NgtCanvas` from `angular-three`
1344

14-
:::note
45+
```ts title="app.component.ts"
46+
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
47+
// highlight-next-line
48+
import { NgtCanvas } from 'angular-three';
49+
50+
@Component({
51+
/*...*/
52+
})
53+
export class SceneGraph {}
54+
55+
@Component({
56+
selector: 'app-root',
57+
standalone: true,
58+
// highlight-start
59+
template: `<ngt-canvas [sceneGraph]="SceneGraph" />`,
60+
imports: [NgtCanvas],
61+
// highlight-end
62+
})
63+
export class AppComponent {
64+
// highlight-next-line
65+
readonly SceneGraph = SceneGraph;
66+
}
67+
```
68+
69+
- `<ngt-canvas>` has a **required** input `[sceneGraph]` which accepts a Component class. We pass our `SceneGraph` component into that input.
70+
71+
`ngt-canvas` sets up the following:
72+
73+
- A `WebGLRenderer`, a default `Scene`, and a default `PerspectiveCamera`
74+
- A render loop that renders our scene evere frame outside of Angular Zone
75+
- A `window:resize` listener that updates our Renderer and Camera when the viewport is resized
76+
77+
`ngt-canvas` renders the `SceneGraph` input in a _detached_ environment from Angular Change Detection. It also provides the custom Angular Renderer
78+
to render THREE.js entities instead of DOM elements.
79+
80+
Next, we'll set some basic styles in `styles.css`
81+
82+
```css title="style.css"
83+
html,
84+
body {
85+
height: 100%;
86+
width: 100%;
87+
margin: 0;
88+
}
89+
```
90+
91+
:::tip
92+
93+
`ngt-canvas` is designed to fit the parent container so we can control our 3D scene by adjusting the parent's dimensions.
1594

16-
We are using Inline Template syntax for this tutorial
95+
```html
96+
<div class="canvas-container">
97+
<ngt-canvas [sceneGraph]="..." />
98+
</div>
99+
```
17100

18101
:::

libs/angular-three/src/lib/canvas.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class NgtCanvas extends NgtRxStore<NgtCanvasInputs> implements OnInit, On
9393
return this.get('eventSource') !== this.host.nativeElement ? 'none' : 'auto';
9494
}
9595

96-
@Input() scene!: Type<any>;
96+
@Input() sceneGraph!: Type<any>;
9797
@Input() compoundPrefixes: string[] = [];
9898

9999
@Input() set linear(linear: boolean) {
@@ -242,7 +242,7 @@ export class NgtCanvas extends NgtRxStore<NgtCanvasInputs> implements OnInit, On
242242
],
243243
this.envInjector
244244
);
245-
this.glRef = this.glAnchor.createComponent(this.scene, { environmentInjector: this.glEnvInjector });
245+
this.glRef = this.glAnchor.createComponent(this.sceneGraph, { environmentInjector: this.glEnvInjector });
246246
this.glRef.changeDetectorRef.detach();
247247

248248
// here, we override the detectChanges to also call detectChanges on the ComponentRef

0 commit comments

Comments
 (0)