Unigine::NodeLayer Class
Header: | #include <UnigineNodes.h> |
Inherits: | Node |
A layer node is a zero-sized node that has no visual representation and enables to save all its child nodes into a separate .node file. Layer nodes should be used to split the world into several logical parts and save each of them in a .node file so that each layer and its child nodes can be edited without affecting the source .world file.
Creating a Layer
To create a layer node, perform as follows:
- Create an instance of the NodeLayer class.
- Release script ownership so that the node can be added to UnigineEditor.
- Add the node to UnigineEditor (wherein, node ownership will be passed to the editor automatically).
You can also specify such settings as a name, transformation and so on.
#include <UnigineEditor.h>
#include <UnigineNodes.h>
using namespace Unigine;
int AppWorldLogic::init() {
// create a layer node
NodeLayerPtr layer = NodeLayer::create();
// set a name
layer->setName("LayerNode");
// release script ownership
layer->release();
// add the layer node to the editor
Editor::get()->addNode(layer->getNode());
return 1;
}
Editing a Layer
Editing a layer node includes adding, deleting, modifying its child nodes. For example, you can add new nodes as follows:
// AppWorldLogic.cpp
#include <UnigineEditor.h>
#include <UnigineNodes.h>
using namespace Unigine;
using namespace Math;
int AppWorldLogic::init() {
// create a layer node
NodeLayerPtr layer = NodeLayer::create("unigine_project/nodes/layer_0.node");
// set a name for the node
layer->setName("layer_0");
// release script ownership
layer->release();
// create child nodes
for (int y = -10; y <= 10; y++)
{
for (int x = -10; x <= 10; x++)
{
// create a mesh
MeshPtr mesh = Mesh::create();
mesh->addBoxSurface("box_0", vec3(1.0f));
// create a node (e.g. an instance of the ObjectMeshDynamic class)
ObjectMeshDynamicPtr node = ObjectMeshDynamic::create(mesh);
// assign a material and a property to the node
for (int i = 0; i < node->getNumSurfaces(); i++)
{
node->setMaterial("mesh_base", i);
node->setProperty("surface_base", i);
}
// set node transformation
node->setWorldTransform(translate(Vec3(x * 2, y * 2, 0.0f)));
// set a name for the node
node->setName(String::format("mesh_%d_%d", x + 10, y + 10).get());
// release script ownership
node->release();
// add the node as a child to the layer node
layer->addWorldChild(node->getNode());
}
}
// add the layer node to UnigineEditor
Editor::get()->addNode(layer->getNode());
return 1;
}
Saving a Layer
Changes made in the child nodes of the layer node can be saved on the disk by saving the world. The nodes will be saved in the separate .node file specified for the layer, not in the .world file.
#include <UnigineConsole.h>
#include <UnigineEditor.h>
#include <UnigineString.h>
using namespace Unigine;
using namespace Math;
int AppWorldLogic::init() {
// create a layer node
NodeLayerPtr layer = NodeLayer::create("unigine_project/nodes/layer_0.node");
// set a name for the node
layer->setName("layer_0");
// release script ownership
layer->release();
// create child nodes
for (int y = -10; y <= 10; y++)
{
for (int x = -10; x <= 10; x++)
{
// create a mesh
MeshPtr mesh = Mesh::create();
mesh->addBoxSurface("box_0", vec3(1.0f));
// create a node (e.g. an instance of the ObjectMeshDynamic class)
ObjectMeshDynamicPtr node = ObjectMeshDynamic::create(mesh);
// assign a material and a property to the node
for (int i = 0; i < node->getNumSurfaces(); i++)
{
node->setMaterial("mesh_base", i);
node->setProperty("surface_base", i);
}
// set node transformation
node->setWorldTransform(translate(Vec3(x * 2, y * 2, 0.0f)));
// set a name for the node
node->setName(String::format("mesh_%d_%d", x + 10, y + 10).get());
// release script ownership
node->release();
// add the node as a child to the layer node
layer->addWorldChild(node->getNode());
}
}
// add the layer node to UnigineEditor
Editor::get()->addNode(layer->getNode());
// save the world
Console::get()->run("world_save unigine_project");
return 1;
}
Instead of saving the world, you can manually save child nodes of the layer into the .node file by using the World::saveNodes() function:
// declare array of nodes
Vector<NodePtr> nodes;
// append child nodes of the layer to the declared array
for (int i = 0; i < layer->getNumChildren(); i++) {
nodes.append(layer->getChild(i));
}
// save nodes to the .node file
if (World::get()->saveNodes(layer->getNodeName(), nodes) == 0) {
Log::error("Layer hasn't been saved\n");
}
See Also
- Article on Layer
NodeLayer Class
Members
static NodeLayerPtr create(const char * name)
Constructor. Creates a node layer with specified file name to store it.Arguments
- const char * name - Name of the layer.
Ptr<NodeLayer> cast(const Ptr<Node> & node)
Casts a NodeLayer out of the Node instance.Arguments
- const Ptr<Node> & node - Pointer to Node.
Return value
Pointer to NodeLayer.void setNodeName(const char * name)
Updates the name of the node layer: a path to a .node file where child nodes of the layer are stored.Arguments
- const char * name - Path to a .node file.