@kirknock/node-openxr-babylonjs 0.3.3 → 0.3.6

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
@@ -11,6 +11,21 @@ bundles' `BABYLON` global, `BABYLON.Debug` and `BABYLON.GLTF2.Loader.Extensions`
11
11
  included, built from the ES modules for snippet-style code). Imports only
12
12
  `@babylonjs/*` (peer dependencies), so it runs in a browser unchanged.
13
13
 
14
+ `babylon-fixes` fixes bugs in Babylon.js itself until a Babylon.js release has
15
+ them. Each one is described in `src/babylon-fixes.ts`:
16
+
17
+ ```js
18
+ import * as core from "@babylonjs/core";
19
+ import { installBabylonFixes } from "@kirknock/node-openxr-babylonjs/babylon-fixes.js";
20
+ installBabylonFixes(core); // once, before engines render
21
+ ```
22
+
23
+ - **`setAlphaMode(-1)` blends additively on WebGPU** (9.33.2, still in 9.38.1).
24
+ Restoring a saved alpha mode that was never set (−1, as IBL shadows'
25
+ voxelization does) turned on blending without blend factors, which WebGPU
26
+ maps to ONE + ONE, so IBL shadows washed the frame out to white. With the
27
+ fix, a negative mode means `ALPHA_DISABLE`.
28
+
14
29
  ## Install
15
30
 
16
31
  ```bash
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Fixes for bugs in Babylon.js itself (not in the node-openxr platform),
3
+ * applied to the app's copy of @babylonjs/core until Babylon.js has them.
4
+ *
5
+ * import * as core from "@babylonjs/core";
6
+ * installBabylonFixes(core);
7
+ *
8
+ * Call it once, after @babylonjs/core is loaded and before engines render.
9
+ * Each fix patches a prototype, so it reaches every engine, including those
10
+ * created earlier. Calling it again does nothing.
11
+ *
12
+ * ## setAlphaMode(-1) turns blending on without blend factors (Babylon.js 9.33.2, still in 9.38.1)
13
+ *
14
+ * An engine's alpha mode starts as -1, "never set" (AbstractEngine._alphaMode
15
+ * is Array(8).fill(-1)), and getAlphaMode() returns it as it is. Code that
16
+ * saves the mode and restores it afterwards therefore calls setAlphaMode(-1)
17
+ * on an engine where nothing set a mode before. One such place is
18
+ * IblShadowsVoxelRenderer._copyMipMap, after every voxelization of the IBL
19
+ * shadows:
20
+ *
21
+ * const previousAlphaMode = this._engine.getAlphaMode(); // -1
22
+ * this._engine.setAlphaMode(Constants.ALPHA_DISABLE);
23
+ * ... copy the voxel grid's mips ...
24
+ * this._engine.setAlphaMode(previousAlphaMode); // setAlphaMode(-1)
25
+ *
26
+ * setAlphaMode() treats every mode other than ALPHA_DISABLE (0) as "blending
27
+ * on" (alphaBlend = mode !== 0), and AlphaState.setAlphaMode() sets blend
28
+ * factors only for the known modes, so after -1 blending is on and the factors
29
+ * are still null.
30
+ *
31
+ * - **WebGPU:** WebGPUCacheRenderPipeline._GetAphaBlendFactor maps a null
32
+ * factor to "one", so every following pass blends ONE + ONE. Babylon's IBL
33
+ * shadows then wash the frame out to white: the voxel tracing output
34
+ * saturates to "no shadow", the shadow accumulation (a half-float texture
35
+ * that isn't cleared) grows by one every frame, and the materials multiply
36
+ * their lighting by it. Babylon's tests "IBL Voxel Shadowing" (three of them)
37
+ * and "IBL Screen-space Shadowing" (two) fail by 85-88 % of pixels.
38
+ * - **WebGL:** nothing shows, because gl.blendFunc keeps its default (ONE,
39
+ * ZERO), which replaces the target as if blending were off.
40
+ *
41
+ * The fix: setAlphaMode() takes a negative mode as ALPHA_DISABLE, so that
42
+ * restoring a mode that was never set leaves blending off, as it was before.
43
+ * Every save-and-restore of getAlphaMode() is covered, not only _copyMipMap.
44
+ * With it the five tests pass on WebGPU (0.00-1.29 %).
45
+ *
46
+ * Remove the fix once the Babylon.js version in use has its own.
47
+ */
48
+ type Namespace = Record<string, unknown>;
49
+ /** Applies the fixes above to `core`, the @babylonjs/core module. Returns the names of the fixes applied. */
50
+ export declare function installBabylonFixes(core: Namespace): string[];
51
+ export {};
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Fixes for bugs in Babylon.js itself (not in the node-openxr platform),
3
+ * applied to the app's copy of @babylonjs/core until Babylon.js has them.
4
+ *
5
+ * import * as core from "@babylonjs/core";
6
+ * installBabylonFixes(core);
7
+ *
8
+ * Call it once, after @babylonjs/core is loaded and before engines render.
9
+ * Each fix patches a prototype, so it reaches every engine, including those
10
+ * created earlier. Calling it again does nothing.
11
+ *
12
+ * ## setAlphaMode(-1) turns blending on without blend factors (Babylon.js 9.33.2, still in 9.38.1)
13
+ *
14
+ * An engine's alpha mode starts as -1, "never set" (AbstractEngine._alphaMode
15
+ * is Array(8).fill(-1)), and getAlphaMode() returns it as it is. Code that
16
+ * saves the mode and restores it afterwards therefore calls setAlphaMode(-1)
17
+ * on an engine where nothing set a mode before. One such place is
18
+ * IblShadowsVoxelRenderer._copyMipMap, after every voxelization of the IBL
19
+ * shadows:
20
+ *
21
+ * const previousAlphaMode = this._engine.getAlphaMode(); // -1
22
+ * this._engine.setAlphaMode(Constants.ALPHA_DISABLE);
23
+ * ... copy the voxel grid's mips ...
24
+ * this._engine.setAlphaMode(previousAlphaMode); // setAlphaMode(-1)
25
+ *
26
+ * setAlphaMode() treats every mode other than ALPHA_DISABLE (0) as "blending
27
+ * on" (alphaBlend = mode !== 0), and AlphaState.setAlphaMode() sets blend
28
+ * factors only for the known modes, so after -1 blending is on and the factors
29
+ * are still null.
30
+ *
31
+ * - **WebGPU:** WebGPUCacheRenderPipeline._GetAphaBlendFactor maps a null
32
+ * factor to "one", so every following pass blends ONE + ONE. Babylon's IBL
33
+ * shadows then wash the frame out to white: the voxel tracing output
34
+ * saturates to "no shadow", the shadow accumulation (a half-float texture
35
+ * that isn't cleared) grows by one every frame, and the materials multiply
36
+ * their lighting by it. Babylon's tests "IBL Voxel Shadowing" (three of them)
37
+ * and "IBL Screen-space Shadowing" (two) fail by 85-88 % of pixels.
38
+ * - **WebGL:** nothing shows, because gl.blendFunc keeps its default (ONE,
39
+ * ZERO), which replaces the target as if blending were off.
40
+ *
41
+ * The fix: setAlphaMode() takes a negative mode as ALPHA_DISABLE, so that
42
+ * restoring a mode that was never set leaves blending off, as it was before.
43
+ * Every save-and-restore of getAlphaMode() is covered, not only _copyMipMap.
44
+ * With it the five tests pass on WebGPU (0.00-1.29 %).
45
+ *
46
+ * Remove the fix once the Babylon.js version in use has its own.
47
+ */
48
+ /** Babylon's ALPHA_DISABLE (Constants.ALPHA_DISABLE). */
49
+ const ALPHA_DISABLE = 0;
50
+ /** Marks a setAlphaMode() this module has already wrapped. */
51
+ const FIXED = Symbol.for("node-openxr-babylonjs.setAlphaModeNegativeIsDisable");
52
+ /**
53
+ * The engine classes whose prototype chains hold a setAlphaMode(): ThinEngine's
54
+ * for WebGL, ThinWebGPUEngine's for WebGPU (not exported, so reached through
55
+ * WebGPUEngine), NullEngine's own.
56
+ */
57
+ const ENGINE_CLASSES = ["ThinEngine", "Engine", "WebGPUEngine", "NullEngine", "AbstractEngine"];
58
+ /** Applies the fixes above to `core`, the @babylonjs/core module. Returns the names of the fixes applied. */
59
+ export function installBabylonFixes(core) {
60
+ const applied = [];
61
+ const seen = new Set();
62
+ for (const name of ENGINE_CLASSES) {
63
+ const engineClass = core[name];
64
+ for (let proto = engineClass?.prototype; proto && proto !== Object.prototype; proto = Object.getPrototypeOf(proto)) {
65
+ if (seen.has(proto) || !Object.prototype.hasOwnProperty.call(proto, "setAlphaMode"))
66
+ continue;
67
+ seen.add(proto);
68
+ const original = proto.setAlphaMode;
69
+ if (typeof original !== "function" || original[FIXED])
70
+ continue;
71
+ const fixed = function (mode, ...rest) {
72
+ // -1 is "never set", what getAlphaMode() returns on a new engine: restoring it means no blending.
73
+ return original.call(this, mode < 0 ? ALPHA_DISABLE : mode, ...rest);
74
+ };
75
+ fixed[FIXED] = true;
76
+ proto.setAlphaMode = fixed;
77
+ applied.push(`setAlphaMode(-1) is ALPHA_DISABLE (${proto.constructor.name})`);
78
+ }
79
+ }
80
+ return applied;
81
+ }
82
+ //# sourceMappingURL=babylon-fixes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"babylon-fixes.js","sourceRoot":"","sources":["../src/babylon-fixes.ts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a,CAAC,CAAC,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;YACvE,CAAC,CAAC;YACF,KAAK,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;YACnB,KAAwC,CAAC,YAAY,GAAG,KAAK,CAAC;YAC/D,OAAO,CAAC,IAAI,CAAC,sCAAsC,KAAK,CAAC,WAAW,CAAC,IAAI,GAAG,CAAC,CAAC;QAChF,CAAC;IACH,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kirknock/node-openxr-babylonjs",
3
- "version": "0.3.3",
3
+ "version": "0.3.6",
4
4
  "type": "module",
5
5
  "description": "Plain browser-style Babylon.js WebXR helpers (engine, default XR experience, asset loading and slideshow, floor, controller visuals and logging). Imports only @babylonjs/*.",
6
6
  "license": "MIT",