Toggle visibility of a group of X3D / glTF nodes

Free Modular Shed with interior - model by denniswoo1993 from https://sketchfab.com/3d-models/free-modular-shed-with-interior-30b1eb71bb0e4b889160f8283e1750c1

We support now visible field on various X3D nodes:

We also support very similar glTF extension KHR_node_visibility. The logic of this glTF extension directly maps to X3D visible field.

The main use-case is to toggle visibility of a group of visual things at run-time. From Pascal code in Castle Game Engine, you can find the TTransformNode that interests you using e.g. the TCastleSceneCore.Node method and then toggle the Visible boolean property. If you don’t know what transform nodes are available (e.g. because you load model in other format, like glTF) then inspect it, by first saving it to X3D using e.g. our Castle Model Viewer, and then open X3D in any text editor.

As a testcase, open x3d/visible_toggling.x3dv in our demo-models repository, https://github.com/castle-engine/demo-models/ . We recommend to just get the whole repository (git clone ...) to have all files. It shows spheres (some visible, some hidden by default) and allows toggling “visible” at runtime (using KeySensor.controlKey routed to “visible” fields).

For more notes see API docs on TAbstractGroupingNode.Visible.

TODO:

  • Both the X3D and glTF features specifications’ say to also disable light and fog nodes within a group that you made invisible. We don’t implement this yet — for now, the Visible field only affects the visibility of shapes. If you use this feature, be aware that we will toggle lights and fog in the future too.

  • In case of glTF, it would be great to also support KHR_animation_pointer. This is a bit like X3D routes: allows to animate almost every part of the glTF, in particular it allows to animate the visibility defined by KHR_node_visibility.

If you enjoy this, please support our work on Patreon!

Our screenshot shows house 3D model by denniswoo1993. Not really related to the feature described here, just something pretty arranged in our editor:)

3 Likes

Hi

Just sharing my method

I use TTransformNode.Scale := (0,0,0) to hide a mesh.

For example, during reload animation:
Each weapon magazine has a bone, so I can hide the mag by setting the bone scale to (0,0,0) and build an acceptable reload animation in Pascal. At one moment I hide it and show the mag in hand, and at another moment I inverse that.

Simple and works fine. This method can also give the same result as using the Visible property.

1 Like

That’s a cool trick:) Indeed, setting scale to zero is in many ways similar to hiding. Thought note that

  • The object scaled this way is still rendered (eats rendering time). It will just “collapse” to a single point.
  • Physics engine, collisions, bounding box calculations try to account for the zero-sized object. In the end, all calculations should resolve to “nothing” for such object, but these calculations are still done:)
  • This also affects collisions (which may, or may not, be what someone wanted). Using X3D nodes, the more standard approach is to use TCollisionNode, which has a proxy “use this for collisions”, this allows to enable/disable/control collisions independently from visibility.
1 Like