angular-three 2.0.0-beta.251 → 2.0.0-beta.252

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +222 -4
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,7 +1,225 @@
1
- # core
1
+ # `angular-three`
2
2
 
3
- This library was generated with [Nx](https://nx.dev).
3
+ A custom Renderer for Angular 18+ that uses Three.js to render 3D scenes.
4
4
 
5
- ## Running unit tests
5
+ ## Compatibility
6
6
 
7
- Run `nx test core` to execute the unit tests.
7
+ Angular Three v2 is still in beta and aims to be compatible with Angular 17+.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install angular-three@beta ngxtension three
13
+ # yarn add angular-three@beta ngxtension three
14
+ # pnpm add angular-three@beta ngxtension three
15
+ ```
16
+
17
+ > Make sure to install `@types/three` as well
18
+
19
+
20
+ ## Usage
21
+
22
+ ```typescript
23
+ import { extend } from 'angular-three';
24
+ import { Mesh, BoxGeometry } from 'three';
25
+
26
+ extend({
27
+ Mesh, // makes ngt-mesh available
28
+ BoxGeometry, // makes ngt-box-geometry available
29
+ /* ... */
30
+ MyMesh: Mesh, // makes ngt-my-mesh available
31
+ });
32
+
33
+ // alternatively for demo purposes, you can use the following
34
+ // extend(THREE);
35
+ // This includes the entire THREE.js namespace
36
+
37
+ @Component({
38
+ // This Component is rendered in the Custom Renderer
39
+ standalone: true,
40
+ template: `
41
+ <ngt-mesh>
42
+ <ngt-box-geometry />
43
+ </ngt-mesh>
44
+ `,
45
+ schemas: [CUSTOM_ELEMENTS_SCHEMA], // required
46
+ changeDetection: ChangeDetectionStrategy.OnPush,
47
+ })
48
+ export class SceneGraph {}
49
+
50
+ @Component({
51
+ // This Component is rendered normally in Angular.
52
+ selector: 'app-my-experience',
53
+ standalone: true,
54
+ template: `
55
+ <ngt-canvas [sceneGraph]="SceneGraph" />
56
+ `,
57
+ imports: [NgtCanvas]
58
+ })
59
+ export class MyExperience {
60
+ SceneGraph = SceneGraph;
61
+ }
62
+ ```
63
+
64
+ > The Component that renders `NgtCanvas` (`MyExperience` in this case) controls the dimensions of the canvas so make sure to style it accordingly.
65
+
66
+ ### Inputs
67
+
68
+ - `sceneGraph: Type<any>`: **required**. This is the Component that renders your 3D Scene graph. It must be a standalone Component.
69
+ - `gl?: NgtGLOptions`: This input allows you to configure the WebGL renderer used by Angular Three. You can provide a THREE.js renderer instance, properties for the default renderer, or a function that returns a renderer based on the canvas element.
70
+ - `size?: NgtSize`: Specifies the dimensions of the renderer. If omitted, the component will automatically measure the canvas dimensions.
71
+ - `shadows?: boolean | 'basic' | 'percentage' | 'soft' | 'variance' | Partial<WebGLShadowMap>`: Enables or disables shadows in the scene. You can provide a boolean value to toggle shadows on or off, or use specific strings to control the shadow type. Additionally, you can pass partial WebGLShadowMap options for fine-tuning.
72
+ - `legacy?: boolean`: Disables three r139 color management when set to true.
73
+ - `linear?: boolean`: Switches off automatic sRGB color space and gamma correction when set to true.
74
+ - `flat?: boolean`: Uses THREE.NoToneMapping instead of THREE.ACESFilmicToneMapping when set to true.
75
+ - `orthographic?: boolean`: Creates an orthographic camera instead of a perspective camera when set to true.
76
+ - `frameloop?: 'always' | 'demand' | 'never'`: Controls the rendering mode. 'always' renders continuously, 'demand' renders only on state changes, and 'never' gives you manual control over rendering.
77
+ - `performance?: Partial<Omit<NgtPerformance, 'regress'>>`: Allows you to configure performance options for adaptive performance.
78
+ - `dpr?: NgtDpr`: Sets the target pixel ratio. You can provide a single number or a range [min, max].
79
+ - `raycaster?: Partial<Raycaster>`: Configures the default raycaster used for interaction.
80
+ - `scene?: Scene | Partial<Scene>`: Provides a THREE.js scene instance or properties to create a default scene.
81
+ - `camera?: NgtCamera | Partial<NgtObject3DNode<Camera>>`: Provides a THREE.js camera instance or properties to create a default camera. You can also set the manual property to true to take control of the camera projection.
82
+ - `events?: (store: NgtSignalStore<NgtState>) => NgtEventManager<HTMLElement>`: Allows you to customize the event manager for handling pointer events.
83
+ - `eventSource?: HTMLElement | ElementRef<HTMLElement>`: Specifies the target element where events are subscribed. By default, it's the div wrapping the canvas.
84
+ - `eventPrefix?: 'offset' | 'client' | 'page' | 'layer' | 'screen'`: Sets the event prefix used for canvas pointer events.
85
+ - `lookAt?: Vector3 | Parameters<Vector3['set']>`: Defines the default coordinate for the camera to look at.
86
+
87
+ ### Outputs
88
+
89
+ - `created`: Emitted when the canvas is created.
90
+ - `pointerMissed`: Emitted when a pointer event is not captured by any element (aka clicking on the canvas)
91
+
92
+ ## Intellisense support
93
+
94
+ Since Angular Three is a custom Renderer, the elements are not recognized by the Angular Language Service.
95
+
96
+ ### Jetbrains IDE
97
+
98
+ The consumers can add `web-types` property to the workspace's `package.json` and set the value to `node_modules/angular-three/web-types.json`.
99
+
100
+ ```json
101
+ {
102
+ "web-types": "node_modules/angular-three/web-types.json"
103
+ }
104
+ ```
105
+
106
+ ### VSCode
107
+
108
+ Similarly, there's `node_modules/angular-three/metadata.json` file that can be used to provide intellisense support for VSCode users.
109
+
110
+ The consumers can enable it via `html.customData` in their `settings.json` file.
111
+
112
+ ```json
113
+ {
114
+ "html.customData": [
115
+ "node_modules/angular-three/metadata.json"
116
+ ]
117
+ }
118
+ ```
119
+
120
+ ## Input Bindings
121
+
122
+ Input bindings for `ngt-*` elements work the same way as they do in Angular.
123
+
124
+ > You can consult THREE.js documentation on what is available on the entities
125
+
126
+ ```html
127
+ <ngt-mesh [position]="[x, y, z]" [rotation]="[x, y, z]">
128
+ <ngt-mesh-basic-material color="hotpink" />
129
+ </ngt-mesh>
130
+ ```
131
+
132
+ ## Events
133
+
134
+ Angular Three Custom Renderer supports the following events on applicable objects (`ngt-mesh`, `ngt-group` etc...)
135
+
136
+ ```
137
+ 'click',
138
+ 'contextmenu',
139
+ 'dblclick',
140
+ 'pointerup',
141
+ 'pointerdown',
142
+ 'pointerover',
143
+ 'pointerout',
144
+ 'pointerenter',
145
+ 'pointerleave',
146
+ 'pointermove',
147
+ 'pointermissed',
148
+ 'pointercancel',
149
+ 'wheel',
150
+ ```
151
+
152
+ In addition, there are 2 special events that the consumers can listen to;
153
+
154
+ - `attached`: when the element is attached to its parent
155
+ - `updated`: when the element properties are updated
156
+
157
+ ## Constructor Arguments
158
+
159
+ In THREE.js, there are some entities that require the consumers to dispose and recreate them if their parameters change; like the Geometries.
160
+
161
+ To handle this, Angular Three exports a `NgtArgs` structural directive that always accepts an Array of values. The consumers can consult THREE.js documentations to know what values are applicable for what entities and their order.
162
+
163
+ ```html
164
+ <!-- for example, new BoxGeometry(width, height, depth) -->
165
+ <ngt-box-geometry *args="[width, height, depth]" />
166
+ ```
167
+
168
+ `NgtArgs`, as a structural directive, ensures to create a new instance of the entity when the value changes
169
+
170
+ ## Parameters
171
+
172
+ Beside the normal properties that `ngt-*` elements can accept for Input bindings, the consumers can also pass a `parameters` object to a special property `[parameters]` on the elements. This parameters object will be used to apply the properties on the entity.
173
+
174
+ ```html
175
+ <!-- instead of <ngt-mesh [position]="[x, y, z]" [scale]="scale" /> -->
176
+ <ngt-mesh [parameters]="{ position: [x, y, z], scale }" />
177
+ ```
178
+
179
+ ## Element Queries
180
+
181
+ The consumers can query for the THREE.js entities like they would do in normal HTML Angular Template.
182
+
183
+ ```ts
184
+ @Component({
185
+ template: `
186
+ <ngt-mesh #mesh></ngt-mesh>
187
+ `
188
+ })
189
+ export class Box {
190
+ mesh = viewChild.required<ElementRef<Mesh>>('mesh');
191
+ // notice that it is an ElementRef of THREE.Mesh instead of an HTMLElement
192
+ }
193
+ ```
194
+
195
+ ## Animation Loop
196
+
197
+ In order to participate in the animation loop, use `injectBeforeRender` inject function
198
+
199
+ ```ts
200
+ @Component({/*...*/})
201
+ export class Box {
202
+ mesh = viewChild.required<ElementRef<Mesh>>('mesh');
203
+
204
+ constructor() {
205
+ injectBeforeRender(() => {
206
+ // runs every frame
207
+ const mesh = this.mesh().nativeElement;
208
+ mesh.rotation.x += 0.01;
209
+ });
210
+ }
211
+ }
212
+ ```
213
+
214
+ ## Store
215
+
216
+ Angular Three keeps track of its state via an internal store. The consumers can access this store via the `injectStore` inject function
217
+
218
+ ```ts
219
+ export class Box {
220
+ store = injectStore();
221
+ viewport = this.store.select('viewport'); // Signal<NgtViewport>
222
+ camera = this.store.select('camera'); // Signal<NgtCamera> - the default camera
223
+ /* many more properties */
224
+ }
225
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "angular-three",
3
- "version": "2.0.0-beta.251",
3
+ "version": "2.0.0-beta.252",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },