Bekerja dengan Grafik Adegan

Bekerja dengan Grafik Adegan

Bekerja dengan Grafik Adegan

Setiap adegan 3D dalam Aspose.3D untuk Java diatur sebagai pohon dari Node objek. Scene menyediakan satu akar — getRootNode() — dan setiap bagian geometri, material, dan transformasi berada di bawah akar itu sebagai node anak atau keturunan.


Membuat Scene dan Mengakses Node Akar

Scene diinisialisasi secara otomatis dengan node akar bernama "RootNode":

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

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

Menambahkan Node Anak

Call createChildNode() pada node mana pun untuk menambahkan anak. Metode ini memiliki tiga overload yang umum digunakan:

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

Untuk melampirkan node yang dibangun secara terpisah, gunakan addChildNode():

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

Menanyakan Node Anak

Temukan anak langsung berdasarkan nama atau indeks, atau iterasi semua anak langsung:

// 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) mencari hanya anak langsung, bukan seluruh subtree. Gunakan accept() untuk mencari seluruh pohon.


Menelusuri Seluruh Pohon

node.accept(NodeVisitor) menelusuri sebuah node dan semua keturunannya dalam urutan depth-first. Pengunjung mengembalikan true untuk melanjutkan atau false untuk menghentikan lebih awal:

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 adalah antarmuka satu-metode, sehingga menerima lambda di Java 8+.


Mengontrol Visibilitas dan Pengecualian Ekspor

Node dapat disembunyikan atau dikecualikan dari ekspor tanpa dihapus dari hierarki:

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) adalah petunjuk tampilan. setExcluded(true) mencegah node muncul dalam file yang diekspor terlepas dari format.


Melampirkan Beberapa Entitas ke Satu Node

Sebuah node memiliki utama entity (getEntity() / setEntity()), tetapi dapat menampung entitas tambahan melalui addEntity(). Ini berguna ketika potongan mesh yang berbeda berbagi satu transformasi:

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

Menggabungkan Node

merge() memindahkan semua anak, entitas, dan material dari node sumber ke node target. Node sumber dibiarkan kosong:

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

Langkah Selanjutnya


Referensi Cepat API

AnggotaDeskripsi
scene.getRootNode()Akar dari pohon scene; selalu ada setelah new Scene()
node.createChildNode(name)Buat node anak bernama tanpa entitas
node.createChildNode(name, entity)Buat node anak bernama dengan entitas
node.createChildNode(name, entity, material)Buat node anak bernama dengan entitas dan material
node.addChildNode(node)Lampirkan yang dibangun secara terpisah Node
node.getChild(name)Temukan anak langsung berdasarkan nama; mengembalikan null jika tidak ditemukan
node.getChild(index)Dapatkan anak langsung pada indeks tertentu
node.getChildNodes()List<Node> dari semua anak langsung
node.accept(visitor)Jelajahi node ini dan semua keturunan secara depth-first
node.addEntity(entity)Lampirkan entitas tambahan ke node
node.getEntities()List<Entity> dari semua entitas pada node ini
node.setVisible(bool)Tampilkan atau sembunyikan node
node.setExcluded(bool)Sertakan atau kecualikan node dari ekspor
node.merge(other)Pindahkan semua anak dan entitas dari other ke node ini
 Bahasa Indonesia