Lavorare con il grafico della scena
Lavorare con il grafico della scena
Ogni scena 3D in Aspose.3D per Java è organizzata come un albero di Node degli oggetti. Scene Un singolo racconto - getRootNode() e ogni pezzo di geometria, materiale e trasforma la vita sotto quella radice come un bambino o nodo discendente.
Creare la scena e accedere al nodo di radice
Scene inizializza automaticamente con un nodo root chiamato "RootNode":
import com.aspose.threed.Scene;
import com.aspose.threed.Node;
Scene scene = new Scene();
Node root = scene.getRootNode(); // always "RootNode"Aggiungi i nodi per bambini
Chiamate createChildNode() Il metodo ha tre sovraccarico comunemente utilizzato:
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);Per aggiungere un nodo che è stato costruito separatamente, utilizzare addChildNode():
Node detached = new Node("standalone");
root.addChildNode(detached);Chiedere i nodi per bambini
Trova un bambino diretto per nome o per indice, oppure iterate tutti i bambini diretti:
// 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)Cercate solo Diretto Bambini,Non è un’intera sottomarina, ma unaaccept()Visita solo il cosiddetto nodo; iterategetChildNodes()ricursivamente per cercare l’intero albero.
Passeggiando l’albero pieno
node.accept(NodeVisitor) Visita solo il cosiddetto nodo – non si ricorre nei bambini. per attraversare l’albero intero, iterate getChildNodes() Il visitatore torna true per continuare o false Per fermare presto:
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 è un’interfaccia di singolo metodo, quindi accetta una lambda in Java 8+.
Controllo della visibilità e esclusione delle esportazioni
I nodi possono essere nascosti o esclusi dall’esportazione senza essere rimossi dalla gerarchia:
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 operationssetVisible(false) È un’indicazione di mostra. setExcluded(true) impedisce che il nodo appare nei file esportati indipendentemente dal formato.
Aggiungere molte entità a un nodo
Un nodo ha un primario Il soggetto (getEntity() / setEntity()(e) può essere utilizzato da altre entità attraverso addEntity().Questo è utile quando diversi pezzi di mesh condividono una sola trasformazione:
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());
}Mergere Node
merge() spostare tutti i bambini, le entità e i materiali da un nodo di fonte al node di destinazione.:
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 emptyIl rapido riferimento di API
| Membro | Descrizione |
|---|---|
scene.getRootNode() | La radice dell’albero della scena; sempre presente dopo new Scene() |
node.createChildNode(name) | Creare un nodo di bambino con nome senza entità |
node.createChildNode(name, entity) | Creare un nodo di bambino con un’entità |
node.createChildNode(name, entity, material) | Creare un nodo di bambino con entità e materiale |
node.addChildNode(node) | Un’altra costruzione separata Node |
node.getChild(name) | Trova un bambino diretto per nome; ritorna null Se non si trova |
node.getChild(index) | Ottieni il bambino diretto su un dato indice |
node.getChildNodes() | List<Node> di tutti i bambini diretti |
node.accept(visitor) | Visita solo questo nodo (non si ricorre ai bambini) |
node.addEntity(entity) | Aggiungi un’entità aggiuntiva al nodo |
node.getEntities() | List<Entity> di tutte le entità su questo nodo |
node.setVisible(bool) | Mostra o nascondi il nodo |
node.setExcluded(bool) | Includere o escludire il nodo dall’esportazione |
node.merge(other) | trasferisce tutti i bambini e le entità da other In questo nodo |