This is an introduction to the concept of of Spatials, the elements of the 3D scene graph. The scene graph is a data structure that manages all objects in your 3D world. For example it keeps track of the 3D models that you load and position. When you extend a Java class from com.jme3.app.SimpleApplication, you inherit the scene graph and rootNode.
The main element of the scene graph is a Spatial called rootNode. All other Spatials are attached to the rootNode in a parent-child relationship. If you think you want to understand the scene graph better, please read Scenegraph for dummies first.
A Spatial is either a Node or a Geometry.
Spatials | ||
---|---|---|
Purpose: | A Spatial is an abstract data structure that stores transformations (translation, rotation, scale) of elements of the scene graph. A Spatial can be saved and loaded using the AssetManager. | |
com.jme3.scene.Geometry | com.jme3.scene.Node | |
Visibility: | A Geometry represents a visible 3-D object in the scene graph. | A Node is an invisible "handle" for a group of objects in the scene graph. |
Purpose: | Use Geometries to represent an object's looks: Every Geometry contains a polygon mesh and a material, specifying its shape, color, texture, and opacity/transparency. You can attach a Geometry to an Node. | Use nodes to structure and group Geometries and other Nodes. Every Node is attached to parent node, and the node can have children attached to itself. When you transform a parent node, all its children are transformed as well. |
Content: | Transformations, mesh, material. | Transformations. No mesh, no material. |
Examples: | A box, a sphere, player, a building, a piece of terrain, a vehicle, missiles, NPCs, etc… | The rootNode, the guiNode, an audio node, a custom grouping node, etc… |
Important: You never create a Spatial with Spatial s = new Spatial();
– it's abstract. Instead you create (e.g. load) a Node or Geometry object, and cast it to Spatial. You use the Spatial type in methods that accept both Nodes and Geometries as arguments.
The polygon Mesh inside a Geometry can be one of three things:
Often after you load a scene or model, you need to access a part of it as an individual Geometry in the scene graph. Maybe you want to swap a character's weapon, or you want to play a door-opening animation. First you need to know the unique name of the sub-mesh.
In the following example, the Node house
is the loaded model. The sub-meshes in the Node are called its children. The String, here door 12
, is the name of the mesh that you are searching.
Geometry submesh = (Geometry) houseScene.getChild("door 12");