Treballant amb el Graf d'Escena

Treballant amb el Graf d'Escena

Treballant amb el graf de l’escena

Cada escena 3D a Aspose.3D for .NET està organitzada com un arbre d’objectes Node. Scene.RootNode és l’arrel d’aquest arbre i cada peça de geometria, material i transformació viu sota ella com a fill o descendent.


Creant l’Escena i Accedint al Node Arrel

Scene s’inicialitza automàticament amb un node arrel anomenat "RootNode":

using Aspose.ThreeD;

var scene = new Scene();
Node root = scene.RootNode; // always "RootNode"

Afegint nodes fills

Crida CreateChildNode() a qualsevol node per afegir un fill. El mètode té tres sobrecàrregues d’ús comú:

using Aspose.ThreeD;
using Aspose.ThreeD.Entities;

var scene = new Scene();
Node root = scene.RootNode;

// 1. Named node with no entity — useful as a pivot or transform container
Node pivot = root.CreateChildNode("pivot");

// 2. Named node with an entity
var box = new Box(2, 2, 2);
Node boxNode = root.CreateChildNode("box", box);

// 3. Named node with entity and material
var mat = new Aspose.ThreeD.Shading.PhongMaterial("red");
mat.Diffuse = new Vector4(0.8, 0.2, 0.2, 1.0);
Node decorated = pivot.CreateChildNode("red_box", box, mat);

Per adjuntar un node construït per separat, utilitzeu AddChildNode():

var detached = new Node("standalone");
root.AddChildNode(detached);

Consulta de nodes fills

Troba un fill directe per nom o per índex, o itera tots els fills directes:

// By name — returns null if no direct child has that name
Node? found = root.GetChild("box");
if (found != null)
    Console.WriteLine("Found: " + found.Name);

// By index
Node first = root.GetChild(0);

// Iterate all direct children
foreach (Node child in root.ChildNodes)
    Console.WriteLine(child.Name);

GetChild(string name) cerca només fills directes, no tot el subarbre. Utilitza un ajudant recursiu o Accept() per cercar tot l’arbre.


Recorregut de l’arbre complet

Per a un recorregut complet en profunditat, itera ChildNodes recursivament:

static void Walk(Node node, int depth = 0)
{
    Console.WriteLine(new string(' ', depth * 2) + node.Name);
    foreach (var child in node.ChildNodes)
        Walk(child, depth + 1);
}

Walk(scene.RootNode);

Per al recorregut amb sortida anticipada, node.Accept(NodeVisitor) està disponible i crida el visitador a cada descendent fins que el visitador retorna false.


Utilitzant Group per Organitzar Objectes Relacionats

Group és una entitat contenidora que organitza lògicament els nodes sense afegir geometria. Adjunta-la a un node els fills del qual representen una unitat lògica:

using Aspose.ThreeD;
using Aspose.ThreeD.Entities;

var scene = new Scene();

// A group node for all furniture in a room
var furnitureGroup = new Group("furniture");
Node roomNode = scene.RootNode.CreateChildNode("living_room", furnitureGroup);

// Child nodes under the group node
Node sofaNode  = roomNode.CreateChildNode("sofa",         new Box(3, 1, 1));
Node tableNode = roomNode.CreateChildNode("coffee_table", new Box(2, 0.5, 1));

Moure roomNode transforma tots els mobles alhora perquè els fills hereten el seu Transform.


Control de la visibilitat i exclusió d’exportació

Els nodes es poden amagar o excloure de l’exportació sense ser eliminats de la jerarquia:

Node ground = scene.RootNode.CreateChildNode("ground_plane", new Box(100, 0.1, 100));
ground.Visible = false;       // hidden in viewport / renderer

Node helperNode = scene.RootNode.CreateChildNode("debug_arrow", new Box());
helperNode.Excluded = true;   // omitted from all export operations

Visible = false és una pista de visualització. Excluded = true impedeix que el node aparegui en els fitxers exportats independentment del format.


Adjuntar diverses entitats a un node

Un node té una entitat primària (propietat Entity), però pot contenir entitats addicionals a través de AddEntity(). Això és útil quan diferents peces de malla comparteixen una única transformació:

var bodyMesh  = new Mesh("body");
var wheelMesh = new Mesh("wheel");

Node carNode = scene.RootNode.CreateChildNode("car");
carNode.AddEntity(bodyMesh);
carNode.AddEntity(wheelMesh);

// Retrieve all entities on this node
foreach (Entity ent in carNode.Entities)
    Console.WriteLine(ent.GetType().Name + ": " + ent.Name);

Fusió de nodes

Merge() mou tots els fills, entitats i materials d’un node d’origen cap al node de destinació. El node d’origen queda buit:

Node lod0 = scene.RootNode.CreateChildNode("lod0");
lod0.CreateChildNode("mesh_high", new Box(1, 1, 1));

Node lod1 = scene.RootNode.CreateChildNode("lod1");
lod1.CreateChildNode("mesh_low", new Box(1, 1, 1));

// Consolidate lod0 children into lod1
lod1.Merge(lod0);
// lod1 now has both mesh_high and mesh_low; lod0 is empty

Pròxims passos


Referència ràpida de l’API

MemberDescription
scene.RootNodeRaíz de l’arbre de escenari; sempre presentat després de new Scene()
node.CreateChildNode(name)Crea un node fill nomat amb cap entitat
node.CreateChildNode(name, entity)Crea un node fill nomat amb una entitat
node.CreateChildNode(name, entity, material)Crea un node fill nomat amb entitat i material
node.AddChildNode(node)Attaca un Node construït separadament
node.GetChild(name)Busca un fill directe per nom; retorna null si no es troba
node.GetChild(index)Obté el fill directe a l’índex especificado
node.ChildNodesIList<Node> de tots els fills directs
node.Accept(visitor)Recorre aquest node i tots els descendents en profunditat
node.AddEntity(entity)Attaca una entitat addicional al node
node.EntitiesIList<Entity> de totes les entitats d’aquest node
node.VisibleMostra o oculta el node
node.ExcludedInclou o exclou el node de l’exportació
node.Merge(other)Mou tots els fills i entitats de other a aquest node
new Group(name)Entity de contenidor per agrupar lògicament els nodes fills
 Català