Procedurally Create A Mesh


Recommended Posts



I'm creating an application where I wish to create a mesh based on dynamic camera data that is provided by a depth sensor. I have created a similar application in Unity/Ogre/DirectX using the following process:


1) Create a Mesh of size depthImageWidth x depthImageHeight * 6 (a triangle mesh where each each depth pixel represents a triangle quad). 

2) Pass DepthImage into a custom vertex/geometry shader that updates location of vertices. The shader requires all the intrinsic/extrinsic (4x4 KRt optics) to be passed into the shader (which has to be broken down into smaller vec4s or floatArrays) while continuously passing in the depthImage (via setProceduralImageTexture I assume) to update the vertices/geometry of the mesh I create in step 1. 


So in essence, I just need to create a mesh once that is of the correct size which will get updated by the shader based on the depth sensor data. 


I've hit a bit of a snag on the first step. I can create a mesh programmatically, but when the mesh becomes too large (e.g., 32768 in the below simple sample), Unigine seems to slow to a halt. Here's a simple example (a little messy from testing) I've been using for testing:

	Unigine::MeshPtr baseMesh = Unigine::Mesh::create();
	int surfaceId = baseMesh->addSurface("baseMeshSurface");
	int surfaceTarget = 0;
	int index = 0;
	for (int i = 0; i < 32768; i++)
		baseMesh->addVertex(Unigine::vec3(0, 0, 0), surfaceId);
		baseMesh->addVertex(Unigine::vec3(index++, 0, 0), surfaceId);
		baseMesh->addVertex(Unigine::vec3(0, index++, 0), surfaceId);

	int tIndex = 0;
	for (int i = 0; i < 32768; i++)
		baseMesh->addTIndex(tIndex, surfaceTarget);
		baseMesh->addTIndex(tIndex, surfaceTarget);
		baseMesh->addTIndex(tIndex, surfaceTarget);

	int ret = baseMesh->createIndices(surfaceId);

	staticMesh = Unigine::ObjectMeshStatic::create("Static Base Mesh", 0);
	Unigine::mat4 staticMeshTForm;
	staticMesh->setMaterial("mesh_base", "*");
	staticMesh->setProperty("surface_base", "*");

	Unigine::MaterialPtr meshMaterial = staticMesh->getMaterial(0);

I've switched out the mesh type with every option I can find in the c++ API:

	dynamicMesh = Unigine::ObjectMeshDynamic::create(1);
	Unigine::mat4 dynamicMeshTForm;
	dynamicMesh->setMaterial("mesh_base", "*");
	dynamicMesh->setProperty("surface_base", "*");

along with alternate versions of the create/createMesh functions. Saving out a mesh to a fbx file and loading it into Unigine with the fbx importer seems to have no problem displaying a large mesh. 


Any thoughts?

Link to post

Saving out the mesh (dynamicMesh->saveMesh("depthMesh.mesh") offered a clue. When I load it in the unigine mesh viewer, seems to do just fine viewing the mesh. Seems like I'm not setting a flag correctly somewhere. 

Link to post