การทำงานกับกราฟฉาก
การทำงานกับกราฟฉาก
ทุกฉาก 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"การเพิ่มโหนดลูก
Call createChildNode() บนโหนดใดก็ได้เพื่อเพิ่มโหนดลูก วิธีนี้มีการโอเวอร์โหลดสามแบบที่ใช้บ่อย:
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.accept(NodeVisitor) เดินผ่านโหนดและโหนดสืบทอดทั้งหมดในลำดับแบบลึกก่อนกว้าง ตัวเยี่ยมชมจะคืนค่า 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) ป้องกันไม่ให้โหนดปรากฏในไฟล์ที่ส่งออกโดยไม่คำนึงถึงรูปแบบ.
การแนบหลายเอนทิตีไปยังโหนดเดียว
โหนดมี หลัก entity (getEntity() / setEntity()), แต่สามารถบรรจุ entities เพิ่มเติมผ่าน 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());
}การรวมโหนด
merge() ย้ายลูกทั้งหมด, entities, และ materials จากโหนดต้นทางไปยังโหนดเป้าหมาย โหนดต้นทางจะเหลือว่างเปล่า:
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ขั้นตอนต่อไป
- การใช้ Transforms — ปรับตำแหน่ง, หมุน, และสเกลโหนดใด ๆ ด้วย
Transform - การสร้างและทำงานกับ Meshes — สร้าง polygon geometry และแนบไปยัง nodes
อ้างอิงอย่างรวดเร็วของ API
| สมาชิก | คำอธิบาย |
|---|---|
scene.getRootNode() | รากของ scene tree; มักจะปรากฏหลังจาก new Scene() |
node.createChildNode(name) | สร้างโหนดลูกที่มีชื่อโดยไม่มี entity |
node.createChildNode(name, entity) | สร้างโหนดลูกที่มีชื่อพร้อม entity |
node.createChildNode(name, entity, material) | สร้างโหนดลูกที่มีชื่อพร้อม 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> ของเอนทิตีทั้งหมดบนโหนดนี้ |
node.setVisible(bool) | แสดงหรือซ่อนโหนด |
node.setExcluded(bool) | รวมหรือยกเว้นโหนดจากการส่งออก |
node.merge(other) | ย้ายลูกทั้งหมดและเอนทิตีจาก other ไปยังโหนดนี้ |