Darbs ar ainas grafu

Darbs ar ainas grafu

Katrs 3D skats Aspose.3D priekš Java ir organizēts kā koka Node objekti. Scene nodrošina vienu sakni — getRootNode() — un katrs ģeometrijas, materiāla un transformācijas gabals atrodas zem šīs saknes kā bērna vai pēcnācēja mezgls.


Ainas izveide un piekļuve saknes mezglam

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

import com.aspose.threed.Scene;
import com.aspose.threed.Node;

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

Bērna mezglu pievienošana

Izsauc createChildNode() jebkuram mezglam, lai pievienotu bērnu. Metodei ir trīs bieži izmantotas pārsnieguma versijas:

import com.aspose.threed.*;

Scene scene = new Scene();
Node root = scene.getRootNode();

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

// 2. Named node with an entity
Mesh mesh = new Mesh("box");
mesh.getControlPoints().add(new Vector4(0, 0, 0));
mesh.getControlPoints().add(new Vector4(1, 0, 0));
mesh.getControlPoints().add(new Vector4(1, 1, 0));
mesh.getControlPoints().add(new Vector4(0, 1, 0));
mesh.createPolygon(0, 1, 2, 3);
Node meshNode = root.createChildNode("box", mesh);

// 3. Named node with entity and material
PbrMaterial mat = new PbrMaterial("red");
mat.setAlbedo(new Vector4(0.8f, 0.2f, 0.2f, 1.0f));
Node decorated = pivot.createChildNode("red_box", mesh, mat);

Lai pievienotu mezglu, kas tika konstruēts atsevišķi, izmanto addChildNode():

Node 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) {
    System.out.println("Found: " + found.getName());
}

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

// Iterate all direct children
for (Node child : root.getChildNodes()) {
    System.out.println(child.getName());
}

getChild(String name) meklē tikai tiešas bērni, nevis pilns apakškoks. Izmantojiet accept() lai meklētu visu koku.


Pilna koka pārlūkošana

node.accept(NodeVisitor) pārvieto cauri mezglam un visiem tā pēcnācējiem dziļuma pirmajā secībā. Apmeklētājs atgriež true lai turpinātu vai false lai agrāk apturētu:

import com.aspose.threed.NodeVisitor;

// Print every node name in the scene
scene.getRootNode().accept(n -> {
    System.out.println(n.getName());
    return true; // false would stop traversal
});

// Stop after finding the first node that has an entity
final Node[] found = {null};
scene.getRootNode().accept(n -> {
    if (n.getEntity() != null) {
        found[0] = n;
        return false; // stop walking
    }
    return true;
});

NodeVisitor ir vienas metodes interfeiss, tāpēc tas pieņem lambda izteiksmi Java 8+.


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 = root.createChildNode("ground_plane", mesh);
ground.setVisible(false);       // hidden in viewport / renderer

Node helperNode = root.createChildNode("debug_arrow", mesh);
helperNode.setExcluded(true);   // omitted from all export operations

setVisible(false) ir attēlošanas norāde. setExcluded(true) neļauj mezglam parādīties eksportētajos failos neatkarīgi no formāta.


Vairāku vienību pievienošana vienam mezglam

Mežglam ir primārā vienība (getEntity() / setEntity()), bet var saturēt papildu vienības caur addEntity(). Tas ir noderīgi, ja dažādas režģa daļas koplieto vienu transformāciju:

Mesh body  = new Mesh("body");
Mesh wheel = new Mesh("wheel");

Node carNode = root.createChildNode("car");
carNode.addEntity(body);
carNode.addEntity(wheel);

// Retrieve all entities on this node
for (Entity ent : carNode.getEntities()) {
    System.out.println(ent.getName());
}

Mezglu apvienošana

merge() pārvieto visus bērnus, objektus un materiālus no avota mezgla uz mērķa mezglu. Avota mezgls tiek atstāts tukšs:

Node lod0 = root.createChildNode("lod0");
lod0.createChildNode("mesh_high", mesh);

Node lod1 = root.createChildNode("lod1");
lod1.createChildNode("mesh_low", mesh);

// 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

LoceklisApraksts
scene.getRootNode()Aizsākuma mezgls ainas kokā; vienmēr ir pieejams pēc new Scene()
node.createChildNode(name)Izveidot nosauktu bērna mezglu bez entity
node.createChildNode(name, entity)Izveidot nosauktu bērna mezglu ar entity
node.createChildNode(name, entity, material)Izveidot nosauktu bērna mezglu ar entity un material
node.addChildNode(node)Pievienot atsevišķi konstruētu 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.getChildNodes()List<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.getEntities()List<Entity> no visām vienībām šajā mezglā
node.setVisible(bool)Rādīt vai paslēpt mezglu
node.setExcluded(bool)Iekļaut vai izslēgt mezglu no eksportēšanas
node.merge(other)Pārvietot visus bērnus un vienības no other šajā mezglā
 Latviešu