rhodonite 0.22.0 → 0.22.2

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 (33) hide show
  1. package/README.md +4 -4
  2. package/VERSION-FILE +1 -1
  3. package/dist/esm/foundation/components/Light/LightComponent.d.ts +7 -0
  4. package/dist/esm/foundation/components/SceneGraph/SceneGraphComponent.d.ts +28 -1
  5. package/dist/esm/foundation/core/ComponentRepository.d.ts +7 -0
  6. package/dist/esm/foundation/core/Config.d.ts +34 -0
  7. package/dist/esm/foundation/math/Transform3D.d.ts +26 -2
  8. package/dist/esm/foundation/renderer/lightCluster/LightClusterBuilder.d.ts +7 -1
  9. package/dist/esm/foundation/renderer/lightCluster/LightClusterLayout.d.ts +11 -0
  10. package/dist/esm/foundation/renderer/lightCluster/LightClusterSystem.d.ts +22 -0
  11. package/dist/esm/foundation/system/Engine.d.ts +13 -2
  12. package/dist/esm/index.js +394 -227
  13. package/dist/esm/webgl/WebGLStrategyWebGL2.d.ts +9 -5
  14. package/dist/esm/webgpu/WebGpuGpuTimer.d.ts +18 -7
  15. package/dist/esm/webgpu/WebGpuLightClusterBuilder.d.ts +4 -1
  16. package/dist/esm/webgpu/convertFloat32ToFloat16Bits.d.ts +16 -0
  17. package/dist/esm/webgpu/packRgbaRowsForUpload.d.ts +23 -0
  18. package/dist/esmdev/foundation/components/Light/LightComponent.d.ts +7 -0
  19. package/dist/esmdev/foundation/components/SceneGraph/SceneGraphComponent.d.ts +28 -1
  20. package/dist/esmdev/foundation/core/ComponentRepository.d.ts +7 -0
  21. package/dist/esmdev/foundation/core/Config.d.ts +34 -0
  22. package/dist/esmdev/foundation/math/Transform3D.d.ts +26 -2
  23. package/dist/esmdev/foundation/renderer/lightCluster/LightClusterBuilder.d.ts +7 -1
  24. package/dist/esmdev/foundation/renderer/lightCluster/LightClusterLayout.d.ts +11 -0
  25. package/dist/esmdev/foundation/renderer/lightCluster/LightClusterSystem.d.ts +22 -0
  26. package/dist/esmdev/foundation/system/Engine.d.ts +13 -2
  27. package/dist/esmdev/index.js +954 -128
  28. package/dist/esmdev/webgl/WebGLStrategyWebGL2.d.ts +9 -5
  29. package/dist/esmdev/webgpu/WebGpuGpuTimer.d.ts +18 -7
  30. package/dist/esmdev/webgpu/WebGpuLightClusterBuilder.d.ts +4 -1
  31. package/dist/esmdev/webgpu/convertFloat32ToFloat16Bits.d.ts +16 -0
  32. package/dist/esmdev/webgpu/packRgbaRowsForUpload.d.ts +23 -0
  33. package/package.json +1 -1
package/README.md CHANGED
@@ -17,6 +17,7 @@ https://librn.com/
17
17
  * Component-oriented
18
18
  * Blittable Memory Architecture (Original GPU data storage system with floating point texture)
19
19
  * Physically-based Rendering with Image-based Lighting
20
+ * Clustered Forward Rendering
20
21
  * Node-based Shader System
21
22
  * Node-based Raymarching System (with integration to polygon rendering)
22
23
  * Support loading the following 3D model files: [glTF2](https://github.com/KhronosGroup/glTF), glTF1, [VRM](https://vrm.dev/en/)
@@ -27,7 +28,6 @@ https://librn.com/
27
28
 
28
29
  ## What's the "Blittable Memory Architecture"
29
30
 
30
-
31
31
  With the Blittable Memory Architecture, Rhodonite stores almost all of its data in a large pre-allocated ArrayBuffer. Data storage for matrix or vector classes in Rhodonite's component classes and materials are assigned from the memory pool, which means most of the data are on that memory pool, transferred to the GPU every frame as a floating-point texture.
32
32
  This architecture allows all shaders always to access a vast amount of data.
33
33
 
@@ -39,10 +39,10 @@ You can try our library via https://editor.librn.com/.
39
39
  This viewer supports glTF/VRM/efkpkg(Effekseer package) files Drag & Drop to display.
40
40
  (Drag & Drop all files if glTF data consists of multiple files.)
41
41
 
42
- Poly model
43
- ![poly](./assets/images/screenshot_poly.jpg)
42
+ Clustered Forward Rendering
43
+ ![poly](./assets/images/screenshot_clustered_forward.jpg)
44
44
 
45
- Advanced PBR
45
+ Advanced PBR with Image-based Lighting
46
46
  ![PBR](./assets/images/screenshot_pbr.jpg)
47
47
 
48
48
  VRM avatar model and VRMA animation
package/VERSION-FILE CHANGED
@@ -1,2 +1,2 @@
1
- v0.22.0-1-ge32bbaa78-dirty
1
+ v0.22.2-1-g4a2246a4e-dirty
2
2
  main
@@ -41,6 +41,7 @@ export declare class LightComponent extends Component {
41
41
  private __updateCount;
42
42
  private __lastUpdateCount;
43
43
  private __lastTransformUpdateCount;
44
+ private __lastRangeScale;
44
45
  /** The number of changes of all light components, per engine. */
45
46
  private static __updateCountMap;
46
47
  /**
@@ -295,6 +296,12 @@ export declare class LightComponent extends Component {
295
296
  * to avoid unnecessary recalculations when nothing has changed.
296
297
  */
297
298
  $logic(): void;
299
+ /**
300
+ * Returns the intensity factor that compensates the shortened range. The factors are the measured brightness
301
+ * balance of the Sponza scene: 0.9 -> 1.16 and 0.8 -> 1.35. Values in between interpolate; below 0.8 the last
302
+ * slope continues. A constant factor compensates the range window approximately, not exactly.
303
+ */
304
+ private static __getIntensityScale;
298
305
  /**
299
306
  * Returns the range used for shading. Point and spot lights without a range get an automatic range
300
307
  * when `Config.lightAutoRangeThreshold` is greater than 0.
@@ -31,6 +31,12 @@ export declare class SceneGraphComponent extends Component {
31
31
  private __isGizmoChild;
32
32
  private __physicsComponentCountInSubtree;
33
33
  private static __hierarchyVersion;
34
+ /**
35
+ * Validity epoch of the per-node `__aabbDirtyChainEpoch` marks. Advancing it invalidates all marks at
36
+ * once; this is required whenever an AABB-dirty flag is cleared or a parent-child link changes, since
37
+ * either can break the "this node and all ancestors are AABB-dirty" guarantee of a mark.
38
+ */
39
+ private static __aabbDirtyChainEpoch;
34
40
  private _worldMatrix;
35
41
  private _worldMatrixRest;
36
42
  private _normalMatrix;
@@ -47,6 +53,16 @@ export declare class SceneGraphComponent extends Component {
47
53
  private __worldMergedAABBWithSkeletal;
48
54
  private __worldMergedAABB;
49
55
  private __isWorldAABBDirty;
56
+ /**
57
+ * Epoch at which this node was last visited by {@link setWorldAABBDirtyParentRecursively}.
58
+ * While it equals the static epoch, this node and all of its ancestors are known to be AABB-dirty,
59
+ * so an upward propagation reaching this node can stop here.
60
+ */
61
+ private __aabbDirtyChainEpoch;
62
+ /** `__worldMatrixVersion` when logicForce last ran; while unchanged, $logic has nothing to update. */
63
+ private __settledWorldMatrixVersion;
64
+ /** Component creation count when logicForce last ran (a mesh may have been attached since). */
65
+ private __settledComponentCreationCount;
50
66
  private _isVisible;
51
67
  private _isBillboard;
52
68
  private __aabbGizmo?;
@@ -55,6 +71,8 @@ export declare class SceneGraphComponent extends Component {
55
71
  private __rotationGizmo?;
56
72
  private __scaleGizmo?;
57
73
  private __jointGizmo?;
74
+ /** True when any gizmo of this node has been set up: skips the per-frame gizmo checks of plain nodes. */
75
+ private __hasGizmos;
58
76
  private __transformGizmoSpace;
59
77
  private __latestPrimitivePositionAccessorVersion;
60
78
  toMakeWorldMatrixTheSameAsLocalMatrix: boolean;
@@ -221,6 +239,10 @@ export declare class SceneGraphComponent extends Component {
221
239
  * @returns The hierarchy version counter
222
240
  */
223
241
  static get hierarchyVersion(): number;
242
+ /**
243
+ * Advances the hierarchy version after a parent-child relationship changed.
244
+ */
245
+ private static __bumpHierarchyVersion;
224
246
  static get componentTID(): 4;
225
247
  /**
226
248
  * Gets the component type identifier for this instance.
@@ -255,7 +277,12 @@ export declare class SceneGraphComponent extends Component {
255
277
  */
256
278
  setWorldMatrixDirtyRecursively(): void;
257
279
  /**
258
- * Recursively marks the world AABB as dirty up the parent hierarchy.
280
+ * Marks the world AABB of this node and all of its ancestors as dirty.
281
+ *
282
+ * The walk stops at the first node already marked in the current epoch: that node and all of its
283
+ * ancestors are known to be dirty. Many nodes of the same subtree changing in one frame (e.g. every
284
+ * joint of an animated skeleton) therefore cost O(number of distinct ancestors) in total instead of
285
+ * O(changed nodes * depth).
259
286
  */
260
287
  setWorldAABBDirtyParentRecursively(): void;
261
288
  /**
@@ -15,7 +15,14 @@ export declare class ComponentRepository {
15
15
  private __renderingComponentTIDs;
16
16
  static readonly invalidComponentSID = -1;
17
17
  private __engine;
18
+ private __componentCreationCount;
18
19
  constructor(engine: Engine);
20
+ /**
21
+ * Number of components created in this repository so far, of any type.
22
+ * Consumers compare it with a remembered value to detect that components were attached to entities.
23
+ * @internal
24
+ */
25
+ get _componentCreationCount(): number;
19
26
  /**
20
27
  * Registers a component class with the repository.
21
28
  * This method associates a component class with its unique ComponentTID for later instantiation.
@@ -30,6 +30,7 @@ type ConfigInitDescription = {
30
30
  clusteredForwardMinLocalLightNumber?: number;
31
31
  maxLightListLength?: number;
32
32
  lightAutoRangeThreshold?: number;
33
+ lightRangeScale?: number;
33
34
  };
34
35
  /**
35
36
  * Where the light clusters of Clustered Forward rendering are built.
@@ -174,6 +175,39 @@ export declare class Config {
174
175
  * updated next.
175
176
  */
176
177
  lightAutoRangeThreshold: number;
178
+ /**
179
+ * Scales the effective range of the point and spot lights, and raises their intensity to compensate.
180
+ * A shorter range reaches fewer clusters, so fewer fragments evaluate the light in Clustered Forward rendering.
181
+ *
182
+ * @remarks
183
+ * `1` (default) applies no adjustment. The measured presets are:
184
+ *
185
+ * | lightRangeScale | Intensity | GPU frame time (measured) |
186
+ * |-----------------|-----------|----------------------------|
187
+ * | 1 | x1 | 5.5-6.4 ms |
188
+ * | 0.9 | x1.16 | 4.2-4.5 ms (-20 to -24%) |
189
+ * | 0.8 | x1.35 | 3.1-3.2 ms (-43 to -50%) |
190
+ *
191
+ * The measurement is a Sponza scene with 1000 animated point lights (WebGPU, 1920x1080, clustered forward).
192
+ * Intermediate values interpolate the measured intensity factors: `1 -> 1`, `0.9 -> 1.16`, `0.8 -> 1.35`,
193
+ * and below 0.8 the last slope continues. A constant factor cannot compensate the range window exactly: with
194
+ * a shorter range the falloff `(1 - (d / range)^4)^4 / d^2` is slightly dimmer at mid distances, and the lights
195
+ * fade out closer to their centers. 0.9 changes only 0.1% of the pixels by more than 32/255; 0.8 changes about
196
+ * 7% of the pixels that much (the lit area of each light becomes smaller).
197
+ *
198
+ * The engine does not change this value by itself. An application that targets a frame budget can adjust it
199
+ * at runtime (for example, from the measured GPU time and the display refresh interval).
200
+ *
201
+ * Only point and spot lights with a positive effective range are adjusted. Directional lights and lights without
202
+ * a range limit are unchanged. The values of `LightComponent.range` and `LightComponent.intensity` themselves are
203
+ * not modified: only the values uploaded to the GPU (and read by the light clusters and the shaders) are scaled,
204
+ * so all rendering paths see the same light.
205
+ */
206
+ private __lightRangeScale;
207
+ /** See {@link Config.lightRangeScale}. */
208
+ get lightRangeScale(): number;
209
+ /** See {@link Config.lightRangeScale}. */
210
+ set lightRangeScale(value: number);
177
211
  /**
178
212
  * Number of instances of each material type to be placed consecutively in memory.
179
213
  * This is on the memory layout, and the number of material instances that can be generated is not limited by this setting.
@@ -26,6 +26,13 @@ export declare class Transform3D {
26
26
  private __scale;
27
27
  private __rotation;
28
28
  private __updateCount;
29
+ /** The local matrix composed from the current position, rotation and scale. */
30
+ private __matrix;
31
+ private __isMatrixUpToDate;
32
+ /** The position, rotation and scale the cached matrix was composed from (to detect direct inner mutations). */
33
+ private __matrixPosition;
34
+ private __matrixRotation;
35
+ private __matrixScale;
29
36
  private static __tmpMatrix44_0;
30
37
  private static __tmpVector3_0;
31
38
  private static __tmpVector3_1;
@@ -126,9 +133,15 @@ export declare class Transform3D {
126
133
  get rotationInner(): Quaternion;
127
134
  /**
128
135
  * Internal method to increment the update counter when transform changes.
136
+ *
137
+ * A position-only change updates the translation column of the cached matrix instead of invalidating it.
138
+ * Animations that move nodes (the common case with many lights) then avoid the quaternion-to-matrix
139
+ * composition every frame. A rotation or scale change invalidates the matrix.
140
+ *
141
+ * @param change - What changed: `'position'` or everything (`'all'`).
129
142
  * @private
130
143
  */
131
- __updateTransform(): void;
144
+ __updateTransform(change?: 'all' | 'position'): void;
132
145
  /**
133
146
  * Sets the transform from a 4x4 transformation matrix.
134
147
  * Decomposes the matrix into position, rotation, and scale components.
@@ -146,9 +159,20 @@ export declare class Transform3D {
146
159
  * @returns A MutableMatrix44 representing the composed transformation
147
160
  */
148
161
  get matrixInner(): MutableMatrix44;
162
+ /**
163
+ * Composes the local matrix from the current position, rotation and scale and stores it in the cache.
164
+ */
165
+ private __composeMatrix;
166
+ /**
167
+ * Brings the cached matrix up to date. Direct mutations of the inner vectors (positionInner and the like)
168
+ * bypass the setters, so they are detected here: a position-only change updates the translation column, and
169
+ * a rotation or scale change recomposes the matrix.
170
+ */
171
+ private __ensureMatrixUpToDate;
149
172
  /**
150
173
  * Efficiently computes the transformation matrix and stores it in the provided matrix.
151
- * This method avoids memory allocation by reusing an existing matrix object.
174
+ * The composed matrix is cached, so repeated reads (including animated translations) do not
175
+ * recompose the rotation and scale.
152
176
  * @param mat - The target matrix to store the result in
153
177
  */
154
178
  getMatrixInnerTo(mat: MutableMatrix44): void;
@@ -79,13 +79,19 @@ export declare function buildLightClustersOnCpu(dst: Float32Array, viewOffset: n
79
79
  */
80
80
  export declare function sphereIntersectsLightCluster(dst: Float32Array, viewOffset: number, layout: LightClusterViewLayout, x: number, y: number, z: number, cx: number, cy: number, cz: number, r: number): boolean;
81
81
  /**
82
- * The reference implementation of the cluster lookup of the shaders (`getLightList`).
82
+ * The reference implementation of the cluster lookup of the shaders (`getLightList` in opticalDefinition.glsl /
83
+ * .wgsl), used by the tests and by CPU-side code that needs the cluster of a position.
84
+ *
85
+ * It reads the same header rows as the shaders, so a change to one form must be made to the other.
83
86
  *
84
87
  * @returns The cluster index, or -1 when the position uses the all-light list (behind the eye plane or overflow).
85
88
  */
86
89
  export declare function computeLightClusterIndex(dst: Float32Array, viewOffset: number, px: number, py: number, pz: number): number;
87
90
  /**
88
91
  * Reads the light entries of a cluster built by `buildLightClustersOnCpu` (or by the compute shader).
92
+ *
93
+ * This follows the same two-level lookup as the shaders: the table entry gives the vec4 offset and the entry count
94
+ * of the cluster, and the entries themselves are read from the index list from that offset.
89
95
  */
90
96
  export declare function readLightClusterEntries(dst: Float32Array, viewOffset: number, layout: LightClusterViewLayout, clusterIndex: number): number[];
91
97
  /**
@@ -58,10 +58,17 @@ export declare const LightClusterLayout: {
58
58
  export declare function getLightListCapacity(maxLightListLength: number): number;
59
59
  /**
60
60
  * Returns the array length (in vec4) of the `clusteredLightData` global property.
61
+ *
62
+ * The light entries are read 4 at a time (`entries[n >> 2][n & 3]` in the shaders), so the list is rounded up to
63
+ * whole vec4s: the last vec4 can hold fewer than 4 lights.
61
64
  */
62
65
  export declare function getLightClusterGlobalPropertyLength(maxLightListLength: number): number;
63
66
  /**
64
67
  * Packs a light component SID and a shadow slot into a light entry.
68
+ *
69
+ * The SID is stored in the low 16 bits and `shadowSlot + 1` above them, so -1 (no shadow slot) is representable
70
+ * without a sign. The shaders unpack the same way: `(entry & 0xFFFF, (entry >> 16) - 1)`.
71
+ * The value stays below 2^24, so a float32 stores it exactly.
65
72
  */
66
73
  export declare function packLightEntry(lightComponentSid: number, shadowSlot: number): number;
67
74
  /**
@@ -92,6 +99,10 @@ export declare function getLightClusterViewLayout(gridSize: readonly [number, nu
92
99
  /**
93
100
  * Splits the byte range `[start, end)` into the parts outside the excluded ranges.
94
101
  *
102
+ * The rendering strategies use this to upload the whole GPU instance data in one call while leaving out the
103
+ * regions owned by the light cluster system: the cluster tables are large, are unchanged for many frames and
104
+ * may be written by compute shaders, so they are uploaded (or not uploaded) separately as dirty ranges.
105
+ *
95
106
  * @param excluded - Ranges `[start, end)` sorted by their start
96
107
  * @returns The remaining ranges `[start, end)` in ascending order
97
108
  */
@@ -37,6 +37,17 @@ export type LightClusterGpuBuildParams = {
37
37
  *
38
38
  * The data lives in the GPU instance data, so it is uploaded by the rendering strategy. `dataVersion` changes
39
39
  * whenever this system writes the data, and the strategies upload the data when it changes.
40
+ *
41
+ * ## Reading guide (the order of the code below)
42
+ * 1. `update()` is the frame entry point, called by `Engine` after the Logic stage.
43
+ * 2. `__collectLights()` turns the light components into the all-light list (`__entries`) and the light spheres
44
+ * (`__spheres`), and detects whether anything changed since the previous frame.
45
+ * 3. `__writeGlobalData()` publishes the all-light list in the `clusteredLightData` global property.
46
+ * 4. `__collectCameras()` / `__assignViews()` choose the cameras whose fragments use clusters, allocate their
47
+ * view regions (header + cluster table + light index list) and build them (`LightClusterBuilder.ts` on the CPU,
48
+ * `WebGpuLightClusterBuilder.ts` on the GPU).
49
+ * 5. The fragment shaders (`opticalDefinition.glsl` / `.wgsl`, `getLightList`) look up the cluster of a fragment
50
+ * and read its light list. The layout of both the list and the view region is in `LightClusterLayout.ts`.
40
51
  */
41
52
  export declare class LightClusterSystem {
42
53
  private __engine;
@@ -142,6 +153,13 @@ export declare class LightClusterSystem {
142
153
  * @returns true if the lists changed since the previous frame
143
154
  */
144
155
  private __collectLights;
156
+ /**
157
+ * Writes the all-light list into the `clusteredLightData` global property.
158
+ *
159
+ * The layout is documented in LightClusterLayout.ts: (allCount, globalCount, localCount, sphereBase) followed by
160
+ * the entries, 4 per vec4. The shaders read the counts from the header: `globalCount` is how many entries they
161
+ * read from the all-light list, and the rest of the list of a clustered fragment comes from the view region.
162
+ */
145
163
  private __writeGlobalData;
146
164
  /**
147
165
  * Lists the cameras of the render passes that may draw lit materials, in the order of the render passes.
@@ -166,6 +184,10 @@ export declare class LightClusterSystem {
166
184
  private __prepareViewBuild;
167
185
  /**
168
186
  * Allocates a region in the GPU instance data. Returns undefined (and disables the clusters) when it fails.
187
+ *
188
+ * The region is ordinary GPU instance data: the WebGL2 data texture / the WebGPU storage buffer. That is why the
189
+ * shaders can read the table with the same `fetchElement` / `get_clusteredLightData` accessor as any other data,
190
+ * and why the strategies have to skip it in the generic upload (see `_getExcludedByteRanges`).
169
191
  */
170
192
  private __allocate;
171
193
  }
@@ -193,10 +193,21 @@ export declare class Engine extends RnObject {
193
193
  * @remarks
194
194
  * Undefined until a result is available, or when {@link gpuTimerEnabled} is false or the environment does
195
195
  * not support GPU timer queries. GPU results arrive asynchronously, so the value lags behind by a few
196
- * frames. In WebGPU it is the sum of the durations of the render passes; in WebGL2 it is the time the GPU
197
- * spent on all the commands issued during process().
196
+ * frames. In WebGPU it is the sum of the durations of the render passes and the compute passes (such as
197
+ * the light cluster build); in WebGL2 it is the time the GPU spent on all the commands issued during
198
+ * process().
198
199
  */
199
200
  get gpuTimeMilliseconds(): number | undefined;
201
+ /**
202
+ * The GPU time of each pass of the latest measured process() call, in milliseconds, keyed by the pass label.
203
+ *
204
+ * @remarks
205
+ * WebGPU only. Undefined until a complete result is available, when {@link gpuTimerEnabled} is false, or
206
+ * when the GPU timer is not supported. The labels are the labels of the GPU passes: the unique names of the
207
+ * render passes, `DepthPrepass`, `LightClusterBuild`, and so on. Passes with the same label are summed.
208
+ * Like {@link gpuTimeMilliseconds}, the value lags behind by a few frames.
209
+ */
210
+ get gpuPassTimesMilliseconds(): ReadonlyMap<string, number> | undefined;
200
211
  private __getWebGLGpuTimer;
201
212
  private __beginGpuTimer;
202
213
  private __endGpuTimer;