Jump to content

Blender exporter for colliders


Go to solution Solved by Josh,

Recommended Posts

  • Solution
Posted

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;
	}

 

  • Like 1
  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Posted
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?

Posted

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.

  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...