Skip to content

Commit

Permalink
KHR_node_visibility (#15754)
Browse files Browse the repository at this point in the history
* Adding KHR_node_visibility

* vis test

* update dynamic
  • Loading branch information
RaananW authored Oct 30, 2024
1 parent a18574e commit 01f82d0
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { AbstractMesh } from "core/Meshes/abstractMesh";
import type { GLTFLoader } from "../glTFLoader";
import type { IGLTFLoaderExtension } from "../glTFLoaderExtension";
import { registerGLTFExtension, unregisterGLTFExtension } from "../glTFLoaderExtensionRegistry";

const NAME = "KHR_node_visibility";

declare module "../../glTFFileLoader" {
// eslint-disable-next-line jsdoc/require-jsdoc
export interface GLTFLoaderExtensionOptions {
/**
* Defines options for the KHR_node_visibility extension.
*/
// NOTE: Don't use NAME here as it will break the UMD type declarations.
["KHR_node_visibility"]: {};
}
}

/**
* Loader extension for KHR_node_visibility
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
export class KHR_node_visibility implements IGLTFLoaderExtension {
/**
* The name of this extension.
*/
public readonly name = NAME;
/**
* Defines whether this extension is enabled.
*/
public enabled: boolean;

private _loader: GLTFLoader;

/**
* @internal
*/
constructor(loader: GLTFLoader) {
this._loader = loader;
this.enabled = loader.isExtensionUsed(NAME);
}

public async onReady(): Promise<void> {
this._loader.gltf.nodes?.forEach((node) => {
node._primitiveBabylonMeshes?.forEach((mesh) => {
mesh.inheritVisibility = true;
});
// When the JSON Pointer is used we need to change both the transform node and the primitive meshes to the new value.
if (node.extensions?.KHR_node_visibility) {
if (node.extensions?.KHR_node_visibility.visible === false) {
if (node._babylonTransformNode) {
(node._babylonTransformNode as AbstractMesh).isVisible = false;
}
node._primitiveBabylonMeshes?.forEach((mesh) => {
mesh.isVisible = false;
});
}
}
});
}

public dispose() {
(this._loader as any) = null;
}
}

unregisterGLTFExtension(NAME);
registerGLTFExtension(NAME, true, (loader) => new KHR_node_visibility(loader));
5 changes: 5 additions & 0 deletions packages/dev/loaders/src/glTF/2.0/Extensions/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,9 @@ export function registerBuiltInGLTFExtensions() {
const { MSFT_sRGBFactors } = await import("./MSFT_sRGBFactors");
return new MSFT_sRGBFactors(loader);
});

registerGLTFExtension("KHR_node_visibility", true, async (loader) => {
const { KHR_node_visibility } = await import("./KHR_node_visibility");
return new KHR_node_visibility(loader);
});
}
1 change: 1 addition & 0 deletions packages/dev/loaders/src/glTF/2.0/Extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export * from "./MSFT_lod";
export * from "./MSFT_minecraftMesh";
export * from "./MSFT_sRGBFactors";
export * from "./KHR_interactivity";
export * from "./KHR_node_visibility";
export * from "./ExtrasAsMetadata";
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/tools/tests/test/visualization/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,11 @@
"playgroundId": "#WGZLGJ#3009",
"referenceImage": "gltfTextureLinearInterpolation.png"
},
{
"title": "GLTF Node visibility test",
"playgroundId": "#80DN99#3",
"referenceImage": "gltfNodeVisibility.png"
},
{
"title": "Asset Containers",
"playgroundId": "#P3U079#19",
Expand Down

0 comments on commit 01f82d0

Please sign in to comment.