From db89a94ab8ef6f730cbfaf4b095692b96939f883 Mon Sep 17 00:00:00 2001 From: zhouhang95 <765229842@qq.com> Date: Thu, 10 Oct 2024 16:23:59 +0800 Subject: [PATCH] import stl --- projects/FBX/CMakeLists.txt | 1 + projects/FBX/ReadFBX.cpp | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/projects/FBX/CMakeLists.txt b/projects/FBX/CMakeLists.txt index 0632230cd4..068447d99e 100644 --- a/projects/FBX/CMakeLists.txt +++ b/projects/FBX/CMakeLists.txt @@ -9,6 +9,7 @@ target_include_directories(zeno PRIVATE ../../ui/include) set(ASSIMP_BUILD_ALL_EXPORTERS_BY_DEFAULT FALSE CACHE BOOL "GUN TMD ASS" FORCE) set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT FALSE CACHE BOOL "GUN TMD ASS" FORCE) set(ASSIMP_BUILD_FBX_IMPORTER TRUE CACHE BOOL "CIHOU FBX" FORCE) +set(ASSIMP_BUILD_STL_IMPORTER TRUE CACHE BOOL "CIHOU FBX" FORCE) set(ASSIMP_BUILD_FBX_EXPORTER TRUE CACHE BOOL "CIHOU FBX" FORCE) add_subdirectory(assimp) target_link_libraries(zeno PRIVATE assimp) diff --git a/projects/FBX/ReadFBX.cpp b/projects/FBX/ReadFBX.cpp index 05fbc58573..e672ae585f 100644 --- a/projects/FBX/ReadFBX.cpp +++ b/projects/FBX/ReadFBX.cpp @@ -1340,4 +1340,64 @@ ZENDEFNODE(ReadLightFromFile, "FBX", } }); + +struct ReadSTL : zeno::INode { + virtual void apply() override { + auto filepath = get_input2("path"); + + Assimp::Importer importer; + + // Import the STL file with triangulation and joining identical vertices + const aiScene* scene = importer.ReadFile(filepath, 0); + + // Check if the import was successful + if (!scene) { + std::cerr << "Failed to load model: " << importer.GetErrorString() << std::endl; + } + + auto prim = std::make_shared(); + + // Iterate through all meshes in the scene + for (unsigned int meshIndex = 0; meshIndex < scene->mNumMeshes; ++meshIndex) { + aiMesh* mesh = scene->mMeshes[meshIndex]; + prim->verts.resize(mesh->mNumVertices); + std::cout << "Mesh " << meshIndex << ": " << mesh->mNumVertices << " vertices, " << mesh->mNumFaces << " faces." << std::endl; + + // Iterate through all vertices in the mesh + for (unsigned int vertexIndex = 0; vertexIndex < mesh->mNumVertices; ++vertexIndex) { + aiVector3D position = mesh->mVertices[vertexIndex]; + prim->verts[vertexIndex] = {position.x, position.y, position.z}; + } + + prim->loops.reserve(mesh->mNumFaces * 3); + prim->polys.resize(mesh->mNumFaces); + + int counter = 0; + + // Iterate through all faces in the mesh + for (unsigned int faceIndex = 0; faceIndex < mesh->mNumFaces; ++faceIndex) { + aiFace face = mesh->mFaces[faceIndex]; + + for (unsigned int i = 0; i < face.mNumIndices; ++i) { + + prim->loops.push_back(face.mIndices[i]); + } + + prim->polys[faceIndex] = {counter, int(face.mNumIndices)}; + counter += face.mNumIndices; + } + } + set_output("prim", prim); + } +}; +ZENDEFNODE(ReadSTL, { + { + {"readpath", "path"}, + }, + { + {"prim"}, + }, + {}, + {"BaseIO"}, +}); }