与舞台图形合作

与舞台图形合作

每个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() 该方法有三种常用的过载:

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() 只访问所谓的节点; iterate getChildNodes() 再一次,寻找整个树。.


穿越全树

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 它是一个单方法界面,因此它接受了Java 8+中的Lambda。.


控制可见性和出口排斥

节点可以隐藏或排除出口,而不被从层次上删除:

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) 这是一个展示指示。. setExcluded(true) 防止节点在出口文件中出现,无论格式如何。.


将多个实体连接到一个节点

一个节点有一个 首要 主体(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());
}

微笑的诺德

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 empty


API 快速参考

成员描述
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> 所有本节点的实体
node.setVisible(bool)显示或隐藏节点
node.setExcluded(bool)包括或排除从出口中节点
node.merge(other)将所有儿童和实体从 other 到这个节点
 中文