كيفية تحميل النماذج 3D في Java

كيفية تحميل النماذج 3D في Java

Aspose.3D FOSS for Java يوفر API بسيط لفتح ملفات 3D دون أي إدمانات أصلية. Scene يمكنك المشي في الهرم العقدي وقراءة بيانات الهندسة الخام لكل مسحوق في المشهد.

خطوة بخطوة دليل

الخطوة 1: إضافة الاعتماد على Maven

إضافة إدمان Aspose.3D FOSS إلى الخاص بك pom.xml.لا تتطلب مكتبات أصلية إضافية.

<dependency>
  <groupId>org.aspose</groupId>
  <artifactId>aspose-3d-foss</artifactId>
  <version>26.5.0</version>
</dependency>

الخطوة 2: استيراد الفصول المطلوبة

وَالْمُتَّقِينَ Scene فئة هي حاوية المستوى العلوي لجميع البيانات 3D. استيرادها جنبا إلى جنب مع Node, Mesh,، وأي فئة محددة للشكل-خيار التحميل التي تحتاجها.

import com.aspose.threed.Scene;
import com.aspose.threed.Node;
import com.aspose.threed.Mesh;
import com.aspose.threed.Entity;
import com.aspose.threed.ObjLoadOptions;
import com.aspose.threed.GltfLoadOptions;
import com.aspose.threed.StlLoadOptions;

كل الطبقات العامة تحت com.aspose.threed حزمة .


الخطوة الثالثة: تحميل ملف

استخدم الستاتية Scene.fromFile() طريقة فتح أي تنسيق مدعوم.يتم الكشف عن النسخة تلقائيًا من ملحق الملفات.

// Automatic format detection from the file extension
Scene scene = Scene.fromFile("model.obj");

بدلاً من ذلك، قم بإنشاء Scene الحالة والاتصال open().هذا مفيد عند رغبتك في إجراء خيارات التحميل أو التعامل مع الأخطاء صراحة:

Scene scene = new Scene();
scene.open("model.obj");

كلا النهجين يدعم OBJ، STL (البنائية و ASCII)، glTF 2.0 / GLB، و FBX الملفات.


الخطوة 4: عبور نواة المشهد

المشهد المزدحم هو شجرة من Node الأجسام التي تُدخل في scene.getRootNode().استخدام getChildNodes() إتقانها مرارا وتكرارا، وإستشارة جميع العقد:

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

public class SceneWalker {
    public static void main(String[] args) throws Exception {
        Scene scene = Scene.fromFile("model.obj");
        walkNode(scene.getRootNode(), 0);
    }

    static void walkNode(Node node, int depth) {
        String indent = "  ".repeat(depth);
        System.out.println(indent + "Node: " + node.getName());
        for (Node child : node.getChildNodes()) {
            walkNode(child, depth + 1);
        }
    }
}

كل واحد منهم Node يمكن أن تحمل صفر أو أكثر. Entity الأجسام (المساحات، الكاميرات، المصابيح). node.getEntities() فحص كل كيان مرفق إلى عقد أو استخدام node.getEntity() - استعادة الكيان الرئيسي.


الخطوة 5: الوصول إلى Vertex و Polygon Data

إدخال كيان النود إلى Mesh و اتصلت getControlPoints() في مواقف Vertex و getPolygons() في قائمة مؤشرات الوجه:

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

Scene scene = Scene.fromFile("model.obj");

for (Node node : scene.getRootNode().getChildNodes()) {
    Entity entity = node.getEntity();
    if (entity instanceof Mesh) {
        Mesh mesh = (Mesh) entity;
        System.out.printf("Mesh '%s': %d vertices, %d polygons%n",
            node.getName(),
            mesh.getControlPoints().size(),
            mesh.getPolygonCount());

        // First vertex position (Vector4: x, y, z, w)
        if (!mesh.getControlPoints().isEmpty()) {
            var v = mesh.getControlPoints().get(0);
            System.out.printf("  First vertex: (%.4f, %.4f, %.4f)%n", v.x, v.y, v.z);
        }

        // First polygon: array of control-point indices
        if (!mesh.getPolygons().isEmpty()) {
            int[] poly = mesh.getPolygons().get(0);
            System.out.println("  First polygon indices: " + java.util.Arrays.toString(poly));
        }
    }
}

mesh.getControlPoints() عودة أ List<Vector4> أين x, y, z تحمل موقفها و w هو المنسق المتجانس (عادة 1.0).

mesh.getPolygons() عودة أ List<int[]> حيث يكون كل شريط مجموعة محددة من مؤشرات نقطة التحكم لواجهة واحدة.


الخطوة 6: تطبيق خيارات الشحن المحددة للشكل

للحصول على التحكم في كيفية تفسير الملف، انتقل إلى إمكانية تحميل الخيارات. Scene.fromFile() أو scene.open().

ملفات OBJ - ObjLoadOptions:

import com.aspose.threed.Scene;
import com.aspose.threed.ObjLoadOptions;

ObjLoadOptions options = new ObjLoadOptions();
options.setFlipCoordinateSystem(true);  // Convert right-hand Y-up to Z-up
options.setScale(0.01);                  // Convert centimetres to metres
options.setEnableMaterials(true);        // Load the .mtl material file alongside
options.setNormalizeNormal(true);        // Normalize all normals to unit length

Scene scene = Scene.fromFile("model.obj", options);

ملفات GLTF / GLB - GltfLoadOptions:

import com.aspose.threed.Scene;
import com.aspose.threed.GltfLoadOptions;

GltfLoadOptions options = new GltfLoadOptions();
options.setFlipCoordinateSystem(true);  // Flip the coordinate system if needed

Scene scene = Scene.fromFile("model.glb", options);

ملفات STL - StlLoadOptions:

import com.aspose.threed.Scene;
import com.aspose.threed.StlLoadOptions;

StlLoadOptions options = new StlLoadOptions();
options.setFlipCoordinateSystem(true);
options.setRecalculateNormal(true);  // Recompute normals from face geometry

Scene scene = Scene.fromFile("model.stl", options);

تنسيقات الاستيراد المساعدة

شكلالتمديدملاحظات
OBJ.objWavefront OBJ؛ اختياري .mtl الملفات؛ إمكانية مع ObjLoadOptions.setEnableMaterials(true)
STL.stlASCII و الوضع الثنائي يتم الكشف عنه تلقائياً
GLTF.gltf, .glbglTF 2.0؛ متغيرات GLB الثنائية و JSON مدعومة على حد سواء
FBX.fbxدعم FBX الثنائي

مشكلات و تصحيحات مشتركة

IOException على تحميل - التحقق من مسار الملف صحيح والملف موجود.استخدم المسارات المطلقة أثناء التطوير للقضاء على عدم الوضوح في توجيه العمل.

NullPointerException الوصول إلى الكيان - ليس كل عقدة تحمل الهندسة الجيومترية. node.getEntity() instanceof Mesh قبل أن يُنقذ أو يَتَمَكَّن node.getEntities() - التعامل مع العقدات مع العديد من الأشياء المرفقة.

نظام التنسيق غير متوافق - إذا ظهرت النموذج المحمول متقلبة أو مدفوعة، استخدم setFlipCoordinateSystem(true) على طبقة الحمل المناسبة (ObjLoadOptions, GltfLoadOptions,أو StlLoadOptions).

المشهد يضرب ولكن getChildNodes() هو فارغ بعض الملفات تخزين الهندسة تحت المشاهد الفرعية بدلا من عقدة الجذر. scene.getSubScenes() فحص النقاط الجذرية لكل مشهد.


الأسئلة المتكررة (FAQ)

ما هي النماذج التي يمكنني تحميلها؟?

OBJ، STL (البنائية و ASCII)، glTF 2.0 / GLB، و FBX. يتم الكشف عن النموذج تلقائيا على أساس امتداد الملف عند الاتصال Scene.fromFile().

هل يمكنني تحميلها من سلك؟?

نعم . scene.open(InputStream) و Scene.fromStream(InputStream) كلاهما يوافق على Java InputStream.يمكنك أيضًا إجراء FileFormat معيار عندما لا يحمل التدفق تمديد.

هل المكتبة آمنة؟?

كل واحد منهم Scene الحالة مستقلة ولا تشارك حالة متغيرة مع الحالات الأخرى.

كيف أقرأ عدد البوليغونات دون إيتراة كل وجه؟?

مكالمة mesh.getPolygonCount() من أجل حساب كامل مباشر.


انظر أيضا

 العربية