การทํางานกับ Scene Graph
การทํางานกับ Scene Graph
ทุกภาพยนตร์ 3D ใน Aspose.3D สําหรับ Java จะจัดขึ้นเป็นต้นไม้ของ Node วัตถุ. Scene ให้มีรากเดียว - getRootNode() - และทุกชิ้นของจิเมตริกวัสดุและเปลี่ยนชีวิตภายใต้รากนี้เป็นเด็กหรือลูกค้าระดับ.
สร้างสถานการณ์และเข้าถึงรากนิวส์
Scene เริ่มต้นโดยอัตโนมัติด้วยรากคิวที่เรียกว่า "RootNode":
import com.aspose.threed.Scene;
import com.aspose.threed.Node;
Scene scene = new Scene();
Node root = scene.getRootNode(); // always "RootNode"เพิ่มนิวเด็ก
โทรศัพท์ createChildNode() บน node ทุกที่เพิ่มเด็ก วิธีการมีสาม overloads ที่ใช้กันทั่วไป:
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);เพื่อเชื่อมต่อคิวส์ที่สร้างขึ้นแยกต่างหากใช้ addChildNode():
Node detached = new Node("standalone");
root.addChildNode(detached);การค้นหาเด็กนิวส์
ค้นหาเด็กโดยตรงตามชื่อหรือตัวเลข หรืออธิบายทุกเด็กที่อยู่:
// 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)ค้นหาเฉพาะ ท้องกันเด็ก, ไม่ใช่การตกแต่งเต็มรูปแบบ. หมายเหตุว่าaccept()การเข้าชมเฉพาะที่เรียกว่า node; iterategetChildNodes()การค้นหาอีกครั้งเพื่อค้นพบต้นไม้ทั้งหมด.
ผ่านต้นไม้เต็มรูปแบบ
node.accept(NodeVisitor) การเข้าชมเพียงที่เรียกว่าคอย - มันไม่กลับมาเข้าไปในเด็ก เพื่อผ่านต้นไม้เต็มไป getChildNodes() กลับกลับ ผู้เข้าชมจะกลับมา true เพื่อดําเนินการหรือ false เพื่อหยุดชั่วคราว:
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 เป็นอินเตอร์เฟซแบบเดียวดังนั้นจึงยอมรับ lambda ใน Java 8+.
การควบคุมความเห็นและขจัดการส่งออก
โนดสามารถซ่อนหรือยกเลิกการส่งออกโดยไม่ต้องถูกลบจากไอร์เรกชัน:
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) เป็นตัวอักษรแสดงผล. setExcluded(true) ป้องกันการปรากฏตัวของคิวส์ในไฟล์ที่ส่งออกโดยไม่คํานึงถึงรูปแบบ.
การเชื่อมต่อหลาย entities ไปยังหนึ่งคิวส์
คู่มือมี A หลัก องค์ประกอบ (getEntity() / setEntity()), แต่สามารถเก็บรวบรวมหน่วยอื่น ๆ โดยการเข้าถึง addEntity().นี้เป็นประโยชน์เมื่อชิ้นส่วนของเมซที่แตกต่างกันแบ่งปันการแปลงเดียว:
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());
}หมายเลขรุ่น: Nodes
merge() หมายเลขที่กําหนดเอง: การเคลื่อนย้ายเด็กและวัตถุทั้งหมดจากจุดแหล่งข้อมูลไปยังจุดเป้าหมาย ปิดจุดประสงค์จะถูกปล่อยให้ว่างเปล่า:
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 emptyAPI การอ้างอิงอย่างรวดเร็ว
| สมาชิก | ลักษณะ |
|---|---|
scene.getRootNode() | รากของต้นไม้ที่เกิดเหตุ; มักมีอยู่หลังจาก new Scene() |
node.createChildNode(name) | สร้างรหัสเด็กที่มีชื่อโดยไม่มีหน่วยงาน |
node.createChildNode(name, entity) | สร้างหดเด็กที่เรียกว่าด้วยหน่วยงาน |
node.createChildNode(name, entity, material) | สร้างหดเด็กที่เรียกว่าด้วยวัตถุและวัสดุ |
node.addChildNode(node) | การสร้างที่แยกต่างหาก Node |
node.getChild(name) | ค้นหาเด็กโดยตรงตามชื่อ; กลับมา null หากไม่พบ |
node.getChild(index) | รับเด็กโดยตรงในตัวเลขที่กําหนด |
node.getChildNodes() | List<Node> ของเด็กทั้งหมดโดยตรง |
node.accept(visitor) | เยี่ยมชมช่องนี้เท่านั้น (ไม่ให้กลับไปที่เด็ก) |
node.addEntity(entity) | เพิ่มหน่วยเพิ่มเติมไปยังคอยด์ |
node.getEntities() | List<Entity> ของทุก entities บน node นี้ |
node.setVisible(bool) | แสดงหรือซ่อนคอยด์ |
node.setExcluded(bool) | รวมหรือยกเลิกคิวส์จากการส่งออก |
node.merge(other) | การย้ายเด็กและองค์กรทั้งหมดจาก other ใน node นี้ |