Treballant amb el gràfic d'escena

Treballant amb el gràfic d'escena

Treballant amb el gràfic d’escena

Cada escena 3D a Aspose.3D per a .NET està organitzada com un arbre de Node objectes. 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);

Consultant 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 el subarbre complet. Utilitzeu un ajudant recursiu o Accept() per cercar tot l’arbre.


Recorregut de tot l’arbre

Per a un recorregut complet en profunditat, itereu 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 a un recorregut amb sortida anticipada, node.Accept(NodeVisitor) està disponible i crida el visitor a cada descendent fins que el visitor retorni false.


Utilitzant Group per Organitzar Objectes Relacionats

Group és una entitat contenidora que organitza lògicament els nodes sense afegir geometria. Adjunteu-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));

Movent roomNode transforma tot el moble conjuntament perquè els fills hereten el seu Transform.


Controlant la visibilitat i l’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 evita que el node aparegui en els fitxers exportats independentment del format.


Adjuntar diverses entitats a un node

Un node té un primari entitat (Entity propietat), 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);

Fusionar 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

Propers passos


Referència ràpida de l’API

MembreDescripció
scene.RootNodeArrel de l’arbre d’escena; sempre present després de new Scene()
node.CreateChildNode(name)Crea un node fill anomenat sense entitat
node.CreateChildNode(name, entity)Crea un node fill anomenat amb una entitat
node.CreateChildNode(name, entity, material)Crea un node fill anomenat amb entitat i material
node.AddChildNode(node)Adjunta una construïda per separat Node
node.GetChild(name)Troba un fill directe per nom; retorna null si no es troba
node.GetChild(index)Obté el fill directe a un índex donat
node.ChildNodesIList<Node> de tots els fills directes
node.Accept(visitor)Recorre aquest node i tots els seus descendents en profunditat primer
node.AddEntity(entity)Adjunta una entitat addicional al node
node.EntitiesIList<Entity> de totes les entitats d’aquest node
node.VisibleMostra o amaga el node
node.ExcludedInclou o exclou el node de l’exportació
node.Merge(other)Mou tots els fills i entitats des de other a aquest node
new Group(name)Entitat contenidora per agrupar lògicament nodes fills
 Català