Skip to content

Scene Structure

Karn Kaul edited this page Oct 26, 2022 · 1 revision

Introduction

facade models GLTF very closely, using a similar scene and node hierarchy. This page describes the various components involved in a scene and its render.

Id

Much like how GLTF data references other data via indices, facade models that in a strongly typed manner via Id<Type>, which is just a glorified std::size_t. In most cases this represents a direct index into an array (std::vector) stored within a Scene, and as a result, scenes do not support removal of individual resources.

Node

Models a GTLF Node, representing node data and a tree of nodes.

Transform

Represents the node's local 3D transformation. Its global transformation is computed by recursively multiplying all parent transforms with its own. The 4x4 matrix is cached and only updated when the Transform is "dirty".

Instance

This is a facade feature that exploits Vulkan's instanced rendering: if Node::instances is not empty, each transform in that vector is treated as an individual instance during a draw call.

Component

Each node can have zero or more "components" attached: these can be any T, stored uniquely (one T instance attachable per Node). The scene owning the nodes attaches Id<Mesh> and Id<Camera> to nodes which have them, which can be queried and mutated by user code. Any other (moveable) types can be attached to any node as well.

Children

Each node owns all its child nodes:

      Parent Node
  ---------|---------
  |        |        |
 Node     Node    Node

Scene

Models a GLTF Asset, aka a list of GLTF Scenes and all the data referenced by them. Most assets have zero or one scenes, a facade scene stores the data for all GLTF scenes but keeps only one's node hierarchy active at any time.

Storage

All GLTF scenes within a facade scene reference into the same shared data, which is modelled by Scene::Storage.

Camera

Contains perspective or orthographic projection parameters. The view matrix is built off the transform of the owning node (per the GLTF spec).

Sampler

Describes how to sample textures in shaders. The scene will add a default sampler if none exist in a loaded GLTF asset.

Material

Describes the render parameters for a PBR (Physically Based Rendering) workflow, including Ids of referenced textures.

StaticMesh

Owns geometry (vertex and index buffers) uploaded to the GPU, can be used in a draw call.

Image

Decompressed bytestream for 4-channel (RGBA) image data.

Texture

Owns a Vulkan image and image view uploaded to the GPU, can be bound to descriptor sets from a pipeline.

Mesh

Models a GLTF Mesh: an ordered vector of primitives.

Primitive

Contains an Id<StaticMesh> and an optional Id<Material>, describes the geometry and shader parameters for a draw call.

Graph

Tree

Models a GLTF scene: an ordered vector of all its root nodes.

Node

Models a GLTF node in a GLTF scene; the owning scene attaches mesh and camera Ids as read from the GLTF source data. Id<Node>s are monotonically increasing, assigned by the scene, unlike the others which are straight indices. A node can be searched for using its Id: the scene will perform a depth first search into the tree. Users should NOT store pointers to nodes as they are not address stable; store the Id instead and retrieve the node when desired.

Id

The Id of the Mesh attached to a node, if any.

Id

The Id of the Camera attached to a node, if any.

Render

  1. Write global uniform / storage buffers (view and lights)
  2. Call recursive function for each root node, passing identity as the parent transform
    1. Compute node's model matrix (parent * node)
    2. If node has a mesh attached, for each primitive:
      1. Obtain material, get or build pipeline
      2. Bind globals to set 0
      3. Have material update its sets; LitMaterial:
        1. Update textures to set 1
        2. Write data to set 2
      4. Obtain static mesh
      5. Write instance matrices to scratch vertex buffer
      6. Bind geometry and instance vertex buffers
      7. Perform instanced draw