@galacean/effects-core 2.8.0-alpha.2 → 2.8.0-alpha.3

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.
package/README.md CHANGED
@@ -1,156 +1,300 @@
1
1
  # Galacean Effects Core
2
2
 
3
+ The core runtime library for Galacean Effects, responsible for asset loading, scene management, and rendering.
4
+
3
5
  ## Basic Concepts
4
- The **Composition** in Galacean Effects is the unit of animation playback. The abstract class `Composition` manages the process from data parsing (JSON -> VFXItem -> mesh) to the creation, updating, and destruction of rendered frames (`renderFrame`) and render passes (`renderPass`).
5
-
6
- Each composition utilizes animation data from various types of elements (`VFXItem`) and their corresponding components (`Component`), including camera properties, multiple layers, particles, and interactive elements.
7
-
8
- When a composition is created, it loads various data assets, creates elements (`VFXItem`) along with their corresponding components (`Component`), and initializes animation texture maps (`Texture`), `renderFrame`, and `renderPass`.
9
-
10
- At the beginning of the lifecycle, the corresponding mesh will be added to the default `renderPass` by the composition. During the course of the lifecycle, the data within the mesh, such as `Geometry` and `Material`, will be updated.
11
-
12
- When post-processing is needed, the mesh will be broken down into the appropriate `renderPass`. After the lifecycle ends, the corresponding mesh will be removed from the `renderFrame`.
13
-
14
- To play the animation, the engine retrieves the mesh from the `renderFrame` and adds it to the scene, continuously calls the update function of the `Composition` during the rendering loop to `update` the data.
15
-
16
- ## Process
17
- ### 1. Resource Loading and Creation
18
- - Asset Download [AssetManager](./src/asset-manager.ts): Before playing the animation, JSON data along with binary resources (`processBins`) and image resources (`processImages`) are downloaded. Upon completion of image downloads, parameters for creating textures (`Texture`) are returned. In addition to basic resource downloading functionality, the following features are supported:
19
- 1. Selective downloading of resources based on rendering levels.
20
- 2. After loading the image, image/text replacement is performed according to the configuration, and the modified image is saved as an `imageData` object by drawing on a Canvas.
21
- 3. Enable the gl extension `KHR_parallel_shader_compile` to compile shaders after resource loading is completed.
22
- - Asset Creation [engine](./src/engine.ts)
23
- The scene data loaded from the network needs to be mounted onto the `engine` object (`addPackageDatas`) to create instances through the `engine` object.
24
- 1. Texture Creation [Texture](./src/texture/texture.ts): The static methods `create` and `createWithData` in the `Texture` abstract class are used to create real texture objects based on the parameters returned above. The current texture objects may be based on the creation types enumerated in `TextureSourceType`.
25
- 2. Element Creation [VFXItem](./src/vfx-item.ts): Call `engine.createVFXItems()` to create VFXItem instances.
26
-
27
- ### 2. Animation Playback
28
- - [Composition](./src/composition.ts): The Composition manages the data processing and rendering settings for animation playback. The engine needs to obtain the mesh through `composition.renderFrame` and add the retrieved meshes to the scene.
29
- 1. The constructor will invoke the following functions, which do not need to be called manually when integrating:
30
- - Plugin system `pluginSystem.initializeComposition()`
31
- - `composition.createRenderFrame()`: Creation and initialization of `renderFrame`
32
- - `composition.reset()`: Animation data parsing and initialization of the rendering instance state such as Mesh
33
- - `composition.play()`: Start playback of the composition
34
- 2. `update` method: This method is used to call `renderFrame` to add/modify/delete meshes, driving the update of `VFXItem` and refreshing vertex data, uniform variable values, etc. The following functions will be called and need to be implemented:
35
- - `updateVideo`: Update video frames for video playback
36
- - `getRendererOptions`: Return an empty `Texture` created with the data
37
- - `reloadTexture/offloadTexture`: Handle the `reload` and `offload` of textures
38
- 3. The meshes or rendering objects added to the scene are obtained through `renderFrame`. The interface can be freely designed in `Composition` according to the needs of the engine.
39
- 4. `dispose` method: At the end of the composition lifecycle, this method will be called based on the termination behavior, executing the composition destruction callback of `VFXItem`, and will also destroy objects such as meshes and textures.
40
-
41
- - [RenderFrame](./src/render/render-frame.ts): The `RenderFrame` can be understood as the rendering data object corresponding to each frame of the composition. In addition to managing the `renderPass`, it also stores the camera properties and common uniform variable table (semantics) associated with the composition. The meshes corresponding to different types of elements are added and removed using `addMeshToDefaultRenderPass` and `removeMeshFromDefaultRenderPass` methods of `renderFrame`. The mesh is added to the appropriate position in the `renderPass` based on its `priority` property.
42
- 1. `addMeshToDefaultRenderPass/removeMeshFromDefaultRenderPass`:
43
- - For compositions without filter elements, the engine can manage all meshes through the `defRenderPass`, or it can directly place the passed-in mesh into its own scene. The engine can also organize and manage the meshes as required.
44
- - For compositions with filter elements involving post-processing, the effects-core will call the `splitDefaultRenderPassByMesh` function to split the `renderPass` using the splitting parameters. In this case, the engine needs to iterate over `renderFrame._renderPasses` to retrieve meshes and add them to the scene.
45
- - When adding a mesh, the common uniforms used by the material can be obtained through `mesh.material.uniformSemantics`, including matrices related to MVP transformations and the attachments used.
46
- 2. `setEditorTransformUniform`: This method is used to set the translation/scaling transformation of an element after model transformation. The engine may not necessarily understand this concept but can set the value to `semantics[EDITOR_TRANSFORM]`.
47
- - [RenderPass](./src/render/render-pass.ts): The meshes added to the scene can be obtained through `renderPass.meshes`. The render pass `renderPass` contains the meshes for the current pass, the operations for clearing the buffer before and after rendering, and attachments related to color, depth, and stencil. The `delegate` property is used to specify the callbacks before and after rendering for the `renderPass`, as defined in [filters](./src/filters). The engine needs to execute these callbacks before actually rendering the meshes to ensure the correct operation of the filters.
48
- - [Mesh](./src/render/mesh.ts): Each `VFXItem` calls the `Mesh.create()` function during initialization, passing in parameters such as geometry and material, and sets/retrieves the rendering order for the current mesh using `priority`.
49
- 1. The static `create` method is used to create a new `Mesh` object that the engine can render. The engine needs to add geometry, material, and other objects to the mesh here.
50
- - The primitive type to be rendered can be obtained from `geometry.mode`.
51
- 2. The `setter` and `getter` functions for `priority` are used to set the rendering order of the current mesh. Meshes with lower `priority` values should be drawn before those with higher values.
52
- 3. `setVisible/getVisible` sets the visibility of the mesh.
53
-
54
- > Tips
55
- >
56
- > - To access methods on the element component, you can use `VFXItem.getComponent(XXXComponent)`.
57
- > - To obtain the mesh corresponding to the current `VFXItem`, you can use `VFXItem.content.mesh` to retrieve it.
58
-
59
- ### 3. [Geometry](./src/render/geometry.ts)
60
- Each `VFXItem` calls the `Geometry.create()` function during initialization, passing in the drawing type, vertex data, and index data of the element. During each frame update, new vertex data is passed to the attribute data.
61
- 1. The static create method: It processes the passed attribute data. If the data contains the dataSource property, it indicates that the attribute shares a buffer with the data source.
62
- - `size`, `offset`, and `stride` are also passed in. If the data length is 0 and the engine does not allow dynamic modification of the GPU cache length, an initialization array should be created using the `maxVertex` parameter.
63
- 2. `setAttributeData/getAttributeData`: Sets/retrieves attribute data for the specified attribute name.
64
- 3. `setAttributeSubData`: Sets partial attribute updates.
65
- 4. `getIndexData/setIndexData`: Sets/retrieves index data.
66
- 5. `setDrawCount/getDrawCount`: Sets/retrieves the draw count.
67
-
68
- Attributes involved:
69
- #### Sprite
6
+
7
+ The **Composition** is the fundamental unit of animation playback in Galacean Effects. The `Composition` class manages the entire lifecycle from data parsing (JSON -> VFXItem -> RendererComponent) to the creation, updating, and destruction of render frames (`RenderFrame`) and render passes (`RenderPass`).
8
+
9
+ ### Core Architecture
10
+
11
+ ```
12
+ Engine
13
+ ├── Composition
14
+ │ ├── VFXItem (Element)
15
+ │ │ ├── Component
16
+ │ │ │ └── RendererComponent
17
+ │ │ └── Transform
18
+ │ ├── RenderFrame
19
+ │ │ └── RenderPass
20
+ │ └── Camera
21
+ └── Resource Management
22
+ ├── Texture
23
+ ├── Material
24
+ └── Geometry
25
+ ```
26
+
27
+ Each composition contains different types of elements (`VFXItem`) and their components (`Component`), including:
28
+ - Camera component
29
+ - Sprite (layer) component
30
+ - Particle system
31
+ - Text component
32
+ - Interactive component
33
+
34
+ When a composition is created, it completes:
35
+ 1. Loading of data assets
36
+ 2. Creation of elements (`VFXItem`) and their components
37
+ 3. Loading and creation of textures (`Texture`)
38
+ 4. Initialization of `RenderFrame` and `RenderPass`
39
+
40
+ ## Core Modules
41
+
42
+ ### 1. Engine [Engine](./src/engine.ts)
43
+
44
+ `Engine` is the core entry point, responsible for managing all GPU resources and the lifecycle of compositions.
45
+
46
+ ```typescript
47
+ import { Engine } from '@galacean/effects-core';
48
+
49
+ // Create engine
50
+ const engine = Engine.create(canvas, {
51
+ fps: 60,
52
+ pixelRatio: window.devicePixelRatio,
53
+ });
54
+ ```
55
+
56
+ Main features:
57
+ - Manages the renderer (`Renderer`)
58
+ - Manages GPU resources (textures, materials, geometries)
59
+ - Manages creation and destruction of compositions
60
+ - Provides a ticker (`Ticker`) to drive the render loop
61
+ - Handles interactive events
62
+
63
+ ### 2. Asset Management [AssetManager](./src/asset-manager.ts)
64
+
65
+ Responsible for loading all resources needed for effects:
66
+
67
+ ```typescript
68
+ import { AssetManager } from '@galacean/effects-core';
69
+
70
+ const assetManager = new AssetManager(options);
71
+ ```
72
+
73
+ Supported features:
74
+ 1. Loading JSON and binary resources
75
+ 2. Loading image and video resources
76
+ 3. Selective resource downloading based on render level
77
+ 4. Image/text template replacement
78
+ 5. Font loading (via `AssetManager.loadFontFamily`)
79
+
80
+ ### 3. Composition [Composition](./src/composition.ts)
81
+
82
+ Manages data processing and rendering for animation playback:
83
+
84
+ ```typescript
85
+ const composition = new Composition(props, scene);
86
+
87
+ // Playback control
88
+ composition.play();
89
+ composition.pause();
90
+ composition.resume();
91
+
92
+ // Update
93
+ composition.update(deltaTime);
94
+
95
+ // Dispose
96
+ composition.dispose();
97
+ ```
98
+
99
+ Main properties:
100
+ - `renderFrame`: The rendering data object for the current frame
101
+ - `rootItem`: The root element of the composition
102
+ - `camera`: The composition camera
103
+ - `speed`: Playback speed
104
+
105
+ ### 4. VFX Element [VFXItem](./src/vfx-item.ts)
106
+
107
+ The base class for all effect elements, supporting component-based architecture:
108
+
109
+ ```typescript
110
+ // Get component
111
+ const component = item.getComponent(SpriteComponent);
112
+
113
+ // Iterate children
114
+ item.children.forEach(child => {
115
+ // ...
116
+ });
117
+
118
+ // Transform operations
119
+ item.transform.setPosition(x, y, z);
70
120
  ```
71
- 1. aPoint: Float32Array - Vertex data
72
- 2. aIndex: Float32Array - Shared buffer with aPoint
73
- 3. Index data: Uint16Array
121
+
122
+ Main properties:
123
+ - `transform`: Position, rotation, scale transforms
124
+ - `components`: Component list
125
+ - `children`: Child element list
126
+
127
+ ### 5. Component System [Component](./src/components/component.ts)
128
+
129
+ Components are functional units attached to VFXItem:
130
+
131
+ ```typescript
132
+ abstract class Component extends EffectsObject {
133
+ item: VFXItem; // Owner element
134
+ enabled: boolean; // Whether enabled
135
+
136
+ onAwake() {} // Initialization
137
+ onEnable() {} // On enable
138
+ onDisable() {} // On disable
139
+ onStart() {} // Before first update
140
+ onUpdate(dt: number) {} // Per-frame update
141
+ onLateUpdate(dt: number) {} // Late update
142
+ onDestroy() {} // On destroy
143
+ }
74
144
  ```
75
145
 
76
- #### Particle
146
+ ### 6. Renderer Component [RendererComponent](./src/components/renderer-component.ts)
147
+
148
+ The base class for all rendering components, responsible for adding renderable objects to render passes:
149
+
150
+ ```typescript
151
+ class RendererComponent extends Component {
152
+ material: Material; // Material
153
+ materials: Material[]; // Material list
154
+ priority: number; // Render priority
155
+
156
+ render(renderer: Renderer): void {} // Render method
157
+ }
77
158
  ```
78
- 1. aPos: Float32Array
79
- 2. aVel: Float32Array - Shared buffer with aPos
80
- 3. aDirX: Float32Array - Shared buffer with aPos
81
- 4. aDirY: Float32Array - Shared buffer with aPos
82
- 5. aRot: Float32Array - Shared buffer with aPos
83
- 6. aSeed: Float32Array - Shared buffer with aRot
84
- 7. aColor: Float32Array - Shared buffer with aRot
85
- 8. aOffset: Float32Array
86
- 9. aSprite: Float32Array
87
- 10. Index data: Uint16Array
159
+
160
+ When a component is enabled, it is automatically added to the default render pass of `RenderFrame`.
161
+
162
+ ### 7. Render Frame [RenderFrame](./src/render/render-frame.ts)
163
+
164
+ The rendering data object for each frame, managing render passes and global uniforms:
165
+
166
+ ```typescript
167
+ interface RenderFrameOptions {
168
+ camera: Camera,
169
+ renderer: Renderer,
170
+ globalVolume?: PostProcessVolume,
171
+ postProcessingEnabled?: boolean,
172
+ }
88
173
  ```
89
174
 
90
- #### Particle-trail
175
+ Main features:
176
+ - Manages `RenderPass` list
177
+ - Stores camera properties
178
+ - Manages global uniform variables (`GlobalUniforms`)
179
+ - Supports post-processing (Bloom, ToneMapping)
180
+
181
+ Main methods:
182
+ - `addMeshToDefaultRenderPass(mesh: RendererComponent)`: Add renderer component to default render pass
183
+ - `removeMeshFromDefaultRenderPass(mesh: RendererComponent)`: Remove renderer component from default render pass
184
+
185
+ ### 8. Render Pass [RenderPass](./src/render/render-pass.ts)
186
+
187
+ Manages a group of objects to be rendered:
188
+
189
+ ```typescript
190
+ interface RenderPassClearAction {
191
+ clearColor?: vec4,
192
+ colorAction?: TextureLoadAction,
193
+ clearDepth?: number,
194
+ depthAction?: TextureLoadAction,
195
+ }
91
196
  ```
92
- 1. aColor: Float32Array
93
- 2. aSeed: Float32Array - Shared buffer with aColor
94
- 3. aInfo: Float32Array - Shared buffer with aColor
95
- 4. aPos: Float32Array - Shared buffer with aColor
96
- 5. aTime: Float32Array
97
- 6. aDir: Float32Array
98
- 7. aTrailStart: Float32Array
99
- 8. aTrailStartIndex: Float32Array
197
+
198
+ Features:
199
+ - Manages render object list
200
+ - Configures color, depth, stencil attachments
201
+ - Sets clear behavior
202
+
203
+ ### 9. Geometry [Geometry](./src/render/geometry.ts)
204
+
205
+ Abstract class for managing vertex and index data:
206
+
207
+ ```typescript
208
+ const geometry = Geometry.create(engine, {
209
+ attributes: {
210
+ aPosition: { size: 3, data: positions },
211
+ aTexCoord: { size: 2, data: texCoords },
212
+ },
213
+ indices: { data: indices },
214
+ mode: WebGLRenderingContext.TRIANGLES,
215
+ });
216
+
217
+ // Update data
218
+ geometry.setAttributeData('aPosition', newPositions);
219
+ geometry.setAttributeSubData('aPosition', offset, partialData);
220
+ geometry.setIndexData(newIndices);
221
+ geometry.setDrawCount(count);
100
222
  ```
101
223
 
102
- ### 4. [Material](./src/material/material.ts)
103
- Each `VFXItem` calls the `Material.create()` function during initialization, passing the shader and uniform semantics. The states and uniform data of the material are not passed in the constructor parameters but are set through functions after material creation.
104
- 1. Static `create` method: It needs to handle the provided shader text and set the `uniformSemantics`.
105
- 2. Implementation of `setter/getter` methods for states: The constant type passed is `glContext`, which may need to be converted to constants defined by the engine.
106
- 3. `set[dataType]/get[dataType]` methods for uniforms: effects-core will invoke the corresponding methods based on the type of the uniform to set data.
224
+ ### 10. Material [Material](./src/material/material.ts)
225
+
226
+ Abstract class for managing shaders and render states:
107
227
 
108
- > ⚠️ Note:
109
- > **The related UBO calls are deprecated, and `material-data-block` does not need to be implemented.**
228
+ ```typescript
229
+ const material = Material.create(engine, {
230
+ shader: shaderSource,
231
+ uniformValues: {
232
+ uColor: [1, 0, 0, 1],
233
+ },
234
+ });
110
235
 
111
- Uniforms involved and their types:
112
- #### Sprite
236
+ // Set render states
237
+ material.blending = true;
238
+ material.depthTest = true;
239
+ material.depthMask = true;
113
240
  ```
114
- 1. uMainData: mat4
115
- 2. uTexParams: vec4
116
- 3. uTexOffset: vec4
117
- 4. uSampler\[i]: sampler2D
118
- 5. uSamplerPre: sampler2D
119
- 6. uFeatherSampler: sampler2D
241
+
242
+ Render state properties:
243
+ - `blending`: Color blending switch
244
+ - `blendFunction`: Blend function
245
+ - `depthTest`: Depth test switch
246
+ - `depthMask`: Depth write switch
247
+ - `stencilTest`: Stencil test switch
248
+ - `culling`: Back-face culling switch
249
+
250
+ ### 11. Texture [Texture](./src/texture/texture.ts)
251
+
252
+ Abstract class for managing GPU texture resources:
253
+
254
+ ```typescript
255
+ // From image
256
+ const texture = await Texture.fromImage(url, engine);
257
+
258
+ // From video
259
+ const texture = await Texture.fromVideo(url, engine);
260
+
261
+ // From data
262
+ const texture = Texture.create(engine, {
263
+ sourceType: TextureSourceType.data,
264
+ data: {
265
+ width: 256,
266
+ height: 256,
267
+ data: pixelData,
268
+ },
269
+ });
120
270
  ```
121
271
 
122
- #### Particle
272
+ ## Plugin System
273
+
274
+ Supports extending functionality through plugins:
275
+
276
+ ```typescript
277
+ import { registerPlugin } from '@galacean/effects-core';
278
+
279
+ registerPlugin('custom', CustomLoader);
123
280
  ```
124
- 1. uSprite: vec4
125
- 2. uParams: vec4
126
- 3. uAcceleration: vec4
127
- 4. uGravityModifierValue: vec4
128
- 5. uOpacityOverLifetimeValue: vec4
129
- 6. uRXByLifeTimeValue: vec4
130
- 7. uRYByLifeTimeValue: vec4
131
- 8. uRZByLifeTimeValue: vec4
132
- 9. uLinearXByLifetimeValue: vec4
133
- 10. uLinearYByLifetimeValue: vec4
134
- 11. uLinearZByLifetimeValue: vec4
135
- 12. uSpeedLifetimeValue: vec4
136
- 13. uOrbXByLifetimeValue: vec4
137
- 14. uOrbYByLifetimeValue: vec4
138
- 15. uOrbZByLifetimeValue: vec4
139
- 16. uSizeByLifetimeValue: vec4
140
- 17. uSizeYByLifetimeValue:vec4
141
- 18. uColorParams: vec4
142
- 19. uFSprite: vec4
143
- 20. uPreviewColor: vec4
144
- 21. uVCurveValues: vec4Array
145
- 22. uFCurveValues: vec4
146
- 23. uFinalTarget: vec3
147
- 24. uForceCurve: vec4
148
- 25. uOrbCenter: vec3
149
- 26. uTexOffset: vec2
150
- 27. uPeriodValue: vec4
151
- 28. uMovementValue: vec4
152
- 29. uStrengthValue: vec4
153
- 30. uWaveParams: vec4
281
+
282
+ Built-in plugins:
283
+ - `sprite`: Layer rendering
284
+ - `particle`: Particle system
285
+ - `text`: Text rendering
286
+ - `interact`: Interaction handling
287
+ - `camera`: Camera control
288
+
289
+ ## Installation
290
+
291
+ ```bash
292
+ npm install @galacean/effects-core
154
293
  ```
155
294
 
295
+ ## Dependencies
296
+
297
+ - `@galacean/effects-specification`: Data specification definitions
298
+ - `@galacean/effects-math`: Math library
299
+
156
300
  ## [API Documentation](https://www.galacean.com/effects/api/effects-core)
@@ -1,3 +1,4 @@
1
+ import * as spec from '@galacean/effects-specification';
1
2
  import type { Engine } from '../engine';
2
3
  import { MeshComponent } from './mesh-component';
3
4
  /**
@@ -8,5 +9,5 @@ export declare class EffectComponent extends MeshComponent {
8
9
  constructor(engine: Engine);
9
10
  onStart(): void;
10
11
  onUpdate(dt: number): void;
11
- fromData(data: unknown): void;
12
+ fromData(data: spec.EffectComponentData): void;
12
13
  }
@@ -1,3 +1,5 @@
1
+ import type { Engine } from '../engine';
2
+ import type { Maskable } from '../material/types';
1
3
  import type { BoundingBoxTriangle, HitTestTriangleParams } from '../plugins';
2
4
  import { MeshCollider } from '../plugins';
3
5
  import type { Geometry } from '../render/geometry';
@@ -6,7 +8,7 @@ import { RendererComponent } from './renderer-component';
6
8
  /**
7
9
  * Mesh 组件
8
10
  */
9
- export declare class MeshComponent extends RendererComponent {
11
+ export declare class MeshComponent extends RendererComponent implements Maskable {
10
12
  /**
11
13
  * 渲染的 Geometry
12
14
  */
@@ -15,7 +17,11 @@ export declare class MeshComponent extends RendererComponent {
15
17
  * 用于点击测试的碰撞器
16
18
  */
17
19
  protected meshCollider: MeshCollider;
20
+ private readonly maskManager;
21
+ constructor(engine: Engine);
18
22
  render(renderer: Renderer): void;
23
+ drawStencilMask(renderer: Renderer): void;
19
24
  getHitTestParams: (force?: boolean) => HitTestTriangleParams | void;
20
25
  getBoundingBox(): BoundingBoxTriangle | void;
26
+ fromData(data: any): void;
21
27
  }