Darbs ar ainas grafu

Darbs ar ainas grafu

Katrs 3D skats Aspose.3D priekš .NET ir organizēts kā koka Node objekti. Scene.RootNode ir šī koka sakne, un katrs ģeometrijas, materiāla un transformācijas elements atrodas zem tās kā bērns vai pēcnācējs.


Ainas izveide un piekļuve saknes mezglam

Scene tiek automātiski inicializēts ar saknes mezglu, kas saucās "RootNode":

using Aspose.ThreeD;

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

Bērna mezglu pievienošana

Izsauciet CreateChildNode() jebkuram mezglam, lai pievienotu bērnu. Metodei ir trīs bieži izmantoti pārlādējumi:

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);

Lai pievienotu atsevišķi konstruētu mezglu, izmantojiet AddChildNode():

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

Bērna mezglu vaicāšana

Atrast tiešu bērnu pēc nosaukuma vai indeksa, vai iterēt visus tiešos bērnus:

// 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) meklē tikai tiešos bērnus, nevis visu apakškoku. Izmantojiet rekursīvu palīgu vai Accept() lai meklētu visu koku.


Pilna koka pārlūkošana

Pilnam dziļuma pirmkārtas pārlūkojumam iterējiet ChildNodes rekursīvi:

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);

Ātrai iziešanas pārlūkošanai, node.Accept(NodeVisitor) ir pieejams un izsauc apmeklētāju uz katru pēcnācēju, līdz apmeklētājs atgriež false.


Grupas izmantošana saistītu objektu organizēšanai

Group ir konteineru vienība, kas loģiski organizē mezglus, nepievienojot ģeometriju. Pievienojiet to mezglam, kura bērni pārstāv loģisku vienību:

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));

Pārvietošana roomNode pārvērš visu mēbeles kopā, jo bērni manto tās Transform.


Redzamības un eksporta izslēgšanas pārvaldība

Mezgli var tikt paslēpti vai izslēgti no eksportēšanas, neizņemot tos no hierarhijas:

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 ir attēlošanas norāde. Excluded = true neļauj mezglam parādīties eksportētajos failos neatkarīgi no formāta.


Vairāku vienību pievienošana vienam mezglam

Mezglam ir primārs vienība (Entity īpašība), bet var saturēt papildu vienības caur AddEntity(). Tas ir noderīgi, kad dažādas režģa daļas koplieto vienu transformāciju:

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);

Mezglu apvienošana

Merge() pārvieto visus bērnus, vienības un materiālus no avota mezgla uz mērķa mezglu. Avota mezgls paliek tukšs:

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

Nākamie soļi


API ātrais atsauces pārskats

DalībnieksApraksts
scene.RootNodeAinas koka sakne; vienmēr pieejama pēc new Scene()
node.CreateChildNode(name)Izveidot nosauktu bērna mezglu bez vienības
node.CreateChildNode(name, entity)Izveidot nosauktu bērna mezglu ar vienību
node.CreateChildNode(name, entity, material)Izveidot nosauktu bērna mezglu ar vienību un materiālu
node.AddChildNode(node)Pievienot atsevišķi izveidotu Node
node.GetChild(name)Atrast tiešu bērnu pēc nosaukuma; atgriež null ja nav atrasts
node.GetChild(index)Iegūt tiešo bērnu pie dotā indeksa
node.ChildNodesIList<Node> no visiem tiešajiem bērniem
node.Accept(visitor)Pārlūkot šo mezglu un visus pēcnācējus dziļuma pirmajā kārtībā
node.AddEntity(entity)Pievienot papildu vienību mezglam
node.EntitiesIList<Entity> no visām vienībām šajā mezglā
node.VisibleRādīt vai paslēpt mezglu
node.ExcludedIekļaut vai izslēgt mezglu no eksporta
node.Merge(other)Pārvietot visus bērnus un vienības no other uz šo mezglu
new Group(name)Konteinera vienība loģiskai bērnu mezglu grupēšanai
 Latviešu