What is a scene graph in games?
The basic structure of a scene graph
A scene graph uses a tree data structure. A tree like in real life has a root and branches that lead to lots of leaves. At each point you’ll find a node. These nodes represent different parts of what’s rendered. The root node is the main parent node with every other node being a child.
As shown here, the Render node is the root and has two immediate children from Character and Terrain. The character node also has child nodes for its vehicle, hat, and weapon.
Types of Nodes
3D model
A 3D model node displays an entire character or breaks down into each of its parts.
Sprite
A sprite node displays a 2D graphic
Effect
An effect node, such as a glowing aura, applies itself to its parent node and it’s parent’s children.
Logic
A logic node can have a script that affects the properties of its parent.
Vector
A vector node represents the position of its parent with it’s x, y, and z coordinates.
Light
A light node describes how a graphical node reacts to light.
Scene graphs in games
A scene graph is used for 2D & 3D rendering.
In 2D rendering, the graph structure is used to control the order of how each node is drawn. For instance, a child node gets drawn on top of its parent and then the parent gets drawn on top of its parent. In games, this structure is used to draw user interfaces such as menus or character name plates.
In 3D rendering, a rendering engine uses the graph to apply specific transforms on the 3d model. These transforms include when a 3D object moves (translation) or rotates. The scene graph structure makes it more efficient and easy to apply these sorts of functions. To go deeper, these transforms use linear algebra with matrices.
A more naive approach would instead use a list of 3D models. This simple approach works fine for smaller games but not so much for those with lots of models.
A game engine may also use the visitor design pattern when traversing each node within the tree.
Let’s continue your journey and learn about the Entity Component System design pattern!