Làm việc với Đồ thị Cảnh

Làm việc với Đồ thị Cảnh

Làm việc với Đồ thị Cảnh

Mỗi cảnh 3D trong Aspose.3D cho Java được tổ chức dưới dạng một cây của Node các đối tượng. Scene cung cấp một gốc duy nhất — getRootNode() — và mọi phần tử hình học, vật liệu và biến đổi đều nằm dưới gốc đó như một nút con hoặc nút hậu duệ.


Tạo Scene và Truy cập Nút Gốc

Scene tự động khởi tạo với một nút gốc có tên "RootNode":

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

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

Thêm các Nút Con

Call createChildNode() trên bất kỳ nút nào để thêm một nút con. Phương thức này có ba overload thường được sử dụng:

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

Để gắn một nút được tạo riêng biệt, sử dụng addChildNode():

Node detached = new Node("standalone");
root.addChildNode(detached);

Truy vấn các Nút Con

Tìm một nút con trực tiếp theo tên hoặc theo chỉ mục, hoặc lặp qua tất cả các nút con trực tiếp:

// 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) chỉ tìm kiếm các nút con trực tiếp, không phải toàn bộ cây con. Sử dụng accept() để tìm kiếm toàn bộ cây.


Duyệt toàn bộ Cây

node.accept(NodeVisitor) duyệt một nút và tất cả các nút hậu duệ của nó theo thứ tự sâu trước. Trình duyệt trả về true để tiếp tục hoặc false để dừng sớm:

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 là một giao diện đơn phương thức, vì vậy nó chấp nhận một lambda trong Java 8+.


Kiểm soát Khả năng Hiển thị và Loại trừ Khi Xuất

Các nút có thể được ẩn hoặc loại trừ khỏi việc xuất mà không bị xóa khỏi cây phân cấp:

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) là một gợi ý hiển thị. setExcluded(true) ngăn node xuất hiện trong các tệp được xuất ra bất kể định dạng.


Gắn nhiều thực thể vào một nút

Một node có một chính thực thể (getEntity() / setEntity()), nhưng có thể chứa các thực thể bổ sung thông qua addEntity(). Điều này hữu ích khi các mảnh lưới khác nhau chia sẻ một biến đổi duy nhất:

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

Hợp nhất các nút

merge() di chuyển tất cả các nút con, thực thể và vật liệu từ một nút nguồn sang nút đích. Nút nguồn sẽ bị để trống:

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

Các bước tiếp theo


Tham chiếu nhanh API

Thành viênMô tả
scene.getRootNode()Gốc của cây cảnh; luôn hiện hữu sau new Scene()
node.createChildNode(name)Tạo một nút con có tên mà không có thực thể
node.createChildNode(name, entity)Tạo một nút con có tên với một thực thể
node.createChildNode(name, entity, material)Tạo một nút con có tên với thực thể và vật liệu
node.addChildNode(node)Gắn một đối tượng được xây dựng riêng Node
node.getChild(name)Tìm một nút con trực tiếp theo tên; trả về null nếu không tìm thấy
node.getChild(index)Lấy phần tử con trực tiếp tại một chỉ số nhất định
node.getChildNodes()List<Node> của tất cả các phần tử con trực tiếp
node.accept(visitor)Duyệt nút này và tất cả các nút con theo chiều sâu
node.addEntity(entity)Đính kèm một thực thể bổ sung vào nút
node.getEntities()List<Entity> của tất cả các thực thể trên nút này
node.setVisible(bool)Hiển thị hoặc ẩn nút
node.setExcluded(bool)Bao gồm hoặc loại trừ nút khỏi việc xuất
node.merge(other)Di chuyển tất cả các phần tử con và thực thể từ other vào nút này
 Tiếng Việt