SpiderPig Posted March 20 Share Posted March 20 @Josh I'm writing a blender exporter for my own model format and I want it to also export a mesh as a collider but I don't know the formatting of the .collider file. Is this something you could share? Quote Link to comment Share on other sites More sharing options...
Solution Josh Posted March 20 Solution Share Posted March 20 Sure. It's a JSON string followed by byte 0, and then any binary data follows that. All offsets are relative to the start of the binary data, not its absolute position in the file. bool Collider::Save(const WString& path, const SaveFlags flags) { auto binstream = CreateBufferStream(); auto stream = WriteFile(path); if (stream == NULL) return false; nlohmann::json j3 = nlohmann::json::object(); if (shapeid == COLLIDER_COMPOUND) { j3["collider"]["parts"] = nlohmann::json::array(); for (int n = 0; n < this->subshapes.size(); ++n) { nlohmann::json subshape; switch (subshapes[n]->shapeid) { case COLLIDER_BOX: subshape["shape"] = "BOX"; break; case COLLIDER_CYLINDER: subshape["shape"] = "CYLINDER"; break; case COLLIDER_SPHERE: subshape["shape"] = "SPHERE"; break; case COLLIDER_CONE: subshape["shape"] = "CONE"; break; case COLLIDER_MESH: subshape["shape"] = "MESH"; j3["collider"]["optimized"] = subshapes[n]->optimizemesh; break; case COLLIDER_CONVEXHULL: subshape["shape"] = "CONVEXHULL"; j3["collider"]["tolerance"] = subshapes[n]->tolerance; break; } if (subshapes[n]->shapeid == COLLIDER_CONVEXHULL) { subshape["vertices"] = subshapes[n]->finalvertices.size(); subshape["data"] = binstream->GetSize(); for (const auto v : subshapes[n]->finalvertices) { binstream->WriteFloat(v.x); binstream->WriteFloat(v.y); binstream->WriteFloat(v.z); } } else if (subshapes[n]->shapeid == COLLIDER_MESH) { subshape["faces"] = subshapes[n]->meshfaces.size(); subshape["data"] = binstream->GetSize(); for (const auto& face : subshapes[n]->meshfaces) { binstream->WriteInt(face.size()); for (const auto& pos : face) { binstream->WriteFloat(pos.x); binstream->WriteFloat(pos.y); binstream->WriteFloat(pos.z); } } } else { subshape["size"] = { subshapes[n]->size.x, subshapes[n]->size.y, subshapes[n]->size.z }; subshape["offset"] = { subshapes[n]->position.x, subshapes[n]->position.y, subshapes[n]->position.z }; subshape["rotation"] = { subshapes[n]->rotation.x, subshapes[n]->rotation.y, subshapes[n]->rotation.z }; } j3["collider"]["parts"].push_back(subshape); } } else { j3["collider"]["parts"] = nlohmann::json::array(); j3["collider"]["parts"].push_back({}); switch (shapeid) { case COLLIDER_BOX: j3["collider"]["parts"][0]["shape"] = "BOX"; break; case COLLIDER_CYLINDER: j3["collider"]["parts"][0]["shape"] = "CYLINDER"; break; case COLLIDER_CHAMFERCYLINDER: j3["collider"]["parts"][0]["shape"] = "CHAMFERCYLINDER"; break; case COLLIDER_CAPSULE: j3["collider"]["parts"][0]["shape"] = "CAPSULE"; break; case COLLIDER_SPHERE: j3["collider"]["parts"][0]["shape"] = "SPHERE"; break; case COLLIDER_CONE: j3["collider"]["parts"][0]["shape"] = "CONE"; break; case COLLIDER_MESH: j3["collider"]["optimized"] = optimizemesh; j3["collider"]["parts"][0]["shape"] = "MESH"; break; case COLLIDER_CONVEXHULL: j3["collider"]["tolerance"] = tolerance; j3["collider"]["parts"][0]["shape"] = "CONVEXHULL"; break; } if (shapeid == COLLIDER_MESH) { j3["collider"]["parts"][0]["faces"] = meshfaces.size(); j3["collider"]["parts"][0]["data"] = binstream->GetSize(); for (const auto& face : meshfaces) { binstream->WriteInt(face.size()); for (const auto& pos : face) { binstream->WriteFloat(pos.x); binstream->WriteFloat(pos.y); binstream->WriteFloat(pos.z); } } } else if (shapeid == COLLIDER_CONVEXHULL) { j3["collider"]["parts"][0]["vertices"] = finalvertices.size(); j3["collider"]["parts"][0]["data"] = binstream->GetSize(); for (const auto v : finalvertices) { binstream->WriteFloat(v.x); binstream->WriteFloat(v.y); binstream->WriteFloat(v.z); } } else { j3["collider"]["parts"][0]["size"] = { size.x, size.y, size.z }; j3["collider"]["parts"][0]["offset"] = { position.x, position.y, position.z }; j3["collider"]["parts"][0]["rotation"] = { rotation.x, rotation.y, rotation.z }; } } if (not properties.empty()) j3["extras"] = properties; stream->WriteString(j3.dump(1, ' '), false); if (binstream->GetSize()) { stream->WriteByte(0); stream->Write(binstream->data->Data(), binstream->GetSize()); } stream->Close(); return true; } 1 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
SpiderPig Posted March 20 Author Share Posted March 20 Thankyou. This will speed up development for me by exporting straight from blender. 🙂 Quote Link to comment Share on other sites More sharing options...
reepblue Posted March 20 Share Posted March 20 1 hour ago, SpiderPig said: Thankyou. This will speed up development for me by exporting straight from blender. 🙂 If you can start with a simple .collider export extension for blender, I'm sure people would appreciate it! 3 Quote Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
SpiderPig Posted March 21 Author Share Posted March 21 That's a good idea. I'll do that. Quote Link to comment Share on other sites More sharing options...
SpiderPig Posted March 21 Author Share Posted March 21 for (const auto& face : meshfaces) { binstream->WriteInt(face.size()); for (const auto& pos : face) { binstream->WriteFloat(pos.x); binstream->WriteFloat(pos.y); binstream->WriteFloat(pos.z); } } Is 'face : meshfaces' each polygon? What is face.size()? How many vertices per face? Quote Link to comment Share on other sites More sharing options...
Josh Posted March 22 Share Posted March 22 face.size is the number of vertices in that face. Each face can have any number of vertices, so it can combine quads, triangles, or anything else with three or more vertices. Most of the time this will just be triangles. 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.