العمل مع مخطط المشهد

العمل مع مخطط المشهد

العمل مع مخطط المشهد

كل مشهد ثلاثي الأبعاد في 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() للبحث في الشجرة بأكملها.


تصفح الشجرة بالكامل

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 هي واجهة ذات طريقة واحدة، لذا تقبل لامبدا في 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 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 إلى هذه العقدة
 العربية