Instancing: sharing a mesh (or other node attribute)
 
 
 

In the CubeCreator tutorial program, every time the user adds a cube, CubeCreator creates a new KFbxMesh object and a new KFbxNode object containing that mesh. That’s two objects for each cube, not to mention all the layer objects, layer element objects, and so forth. If the user needs hundreds or thousands of cubes, the memory requirements could be significant.

Imagine that you need a program where all cubes looked alike, but you needed many thousands of them. You could save memory by creating one KFbxMesh object when the program starts up. Then, every time you needed a new cube, you create a new KFbxNode object, then point that node to the one mesh. This is called instancing.

The Instances sample illusrates how to create instances of the same cube mesh. In general, you can save memory by having many node objects share one node attribute object (i.e., one object of any subclass of KFbxNodeAttribute). The following function taken from the Instances sample shows how to create instances of the same cube:

// Create a cube instance with the given mesh as node attribute, and add it to the scene.
KFbxNode* CreateCubeInstance(KFbxScene* pScene, const char* pName, KFbxMesh* pFirstCube)
{
    // create a KFbxNode
    KFbxNode* lNode = KFbxNode::Create(pScene,pName);

    // set the node attribute
    lNode->SetNodeAttribute(pFirstCube);

    // rescale the cube
    lNode->LclScaling.Set(KFbxVector4(0.3, 0.3, 0.3));

        // Add node to the scene
    pScene->GetRootNode()->AddChild(lNode);

    // return the KFbxNode
    return lNode;
}

Each node is an instance of the mesh, NURBS, or other scene element. If you export your scene to an FBX file, instancing also reduces the file size. You can also save memory by having multiple nodes share textures, materials, animation curves, etc.