วิธีการโหลดรูปแบบ 3D ใน Java
Aspose.3D FOSS for Java ให้ API ที่เรียบง่ายสําหรับการเปิดไฟล์ 3D โดยไม่ต้องมีส่วนร่วมในประเทศ หลังจากโหลดไฟล์ลงไปสู่ระบบที่อยู่อาศัยของเว็บไซต์ของคุณ Scene object คุณสามารถเดินไปของไดเรกทอรีนิวส์และอ่านข้อมูลภูมิศาสตร์ดิบสําหรับแต่ละม้าในสถานที่.
คู่มือขั้นตอน
ขั้นตอนที่ 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 บรรจุภัณฑ์.
ขั้นตอนที่ 3: ดาวน์โหลดไฟล์
ใช้สเตียรอยด์ Scene.fromFile() วิธีการเปิดรูปแบบใด ๆ ที่ได้รับการสนับสนุน บิสตจักรจะตรวจจับรูปแบบโดยอัตโนมัติจากไฟล์ขยาย.
// Automatic format detection from the file extension
Scene scene = Scene.fromFile("model.obj");ตัวเลือกการสร้าง A Scene กรณีและโทรศัพท์ open().นี้เป็นประโยชน์เมื่อคุณต้องการที่จะผ่านตัวเลือกโหลดหรือจัดการข้อผิดพลาดอย่างชัดเจน:
Scene scene = new Scene();
scene.open("model.obj");ทั้งสองวิธีการสนับสนุน OBJ, STL (ไบารีและ ASCII), glTF 2.0 / GLB และไฟล์ FBX.
ขั้นตอนที่ 4: Traverse ฉาก โนด
ฉากที่โหลดเป็นต้นไม้ของ Node วัตถุที่สืบใน scene.getRootNode().ใช้ getChildNodes() เพื่อยึดกลับและเยี่ยมชม nodes ทั้งหมด:
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 และข้อมูลโพลีกอน
การวาง entity ของ node เพื่อ 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() กลับ A List<Vector4> ที่ไหน x, y, z ใช้ตําแหน่งและ w เป็นการจัดเรียงแบบ homogeneous (ปกติ 1.0).
mesh.getPolygons() กลับ A 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 | .obj | Wavefront OBJ; ตัวเลือก .mtl ไฟล์วัสดุ; สามารถใช้กับ ObjLoadOptions.setEnableMaterials(true) |
| STL | .stl | ASCII และโหมดไบนาเรียจะถูกตรวจพบโดยอัตโนมัติ |
| กลตฟ | .gltf, .glb | glTF 2.0; ตัวเลือกไบนารี GLB และ JSON ทั้งสองได้รับการสนับสนุน |
| FBX | .fbx | บินเนียร์ FBX สนับสนุน |
ปัญหาทั่วไปและข้อกําหนด
IOException บล็อก — ตรวจสอบเส้นทางไฟล์ที่ถูกต้องและไฟล์มีอยู่ ใช้เส้นทางการ absolute ในระหว่างการพัฒนาเพื่อกําจัดความไม่แน่นอนในการทํางานของไดเรกทอรี.
NullPointerException entity การเข้าถึง - ไม่ทุกคิวส์มีจิเมตริกเสมอที่ดูแลด้วย node.getEntity() instanceof Mesh ก่อนที่จะ casting หรือ iterate node.getEntities() การจัดการคอยล์ที่มีวัตถุหลายแบบที่เชื่อมต่อ.
ระบบการจัดเรียงความผิดปกติ — หากรูปแบบที่โหลดปรากฏขึ้นลอยหรือหมุนใช้ setFlipCoordinateSystem(true) ในระดับการโหลดตัวเลือกที่เหมาะสม (ObjLoadOptions, GltfLoadOptions,หรือ StlLoadOptions).
ฉากโหลด แต่ getChildNodes() นอกลวง — บางไฟล์เก็บจิเมตริกภายใต้สถานการณ์ที่อยู่เบื้องต้นแทนที่จะรากคอยด์ ตรวจสอบ scene.getSubScenes() และตรวจสอบรากของแต่ละ sub-scene.
คําถามที่พบบ่อย (FAQ)
รูปแบบใดที่ฉันสามารถโหลดได้หรือไม่?
OBJ, STL (ไบารีและ ASCII), glTF 2.0 / GLB และ FBX การตรวจจับรูปแบบจะถูกสร้างขึ้นโดยอัตโนมัติตามการขยายไฟล์เมื่อคุณโทร Scene.fromFile().
ฉันสามารถโหลดได้จาก Stream ได้หรือไม่?
ใช่. scene.open(InputStream) และ Scene.fromStream(InputStream) ทั้งสองยอมรับ Java InputStream.คุณยังสามารถผ่าน A FileFormat พารามิเตอร์เมื่อไหลไม่ได้รับการขยายตัว.
บีบอิสระมีความปลอดภัยหรือไม่?
ทุกคน Scene ตัวอย่างเป็นอิสระและไม่แบ่งปันสถานะที่เปลี่ยนแปลงได้กับตัวอย่างอื่น ๆ.
ฉันจะอ่านคํานวณโพลีกอนได้อย่างไรโดยไม่ต้องยึดหน้าแต่ละใบหน้า?
โทรศัพท์ mesh.getPolygonCount() สําหรับคํานวณแบบเต็มที่โดยตรง.