Problem drawing a mesh with Graphics.DrawMeshNow

Hello all. I’m making a block based building system for a mech fighting game but I seem to have hit a snag. You see I am trying to take all the blocks the player makes and assemble them into a single mesh to save memory, the problem is that when I try to draw this mesh I get an error that says “size <= 0”. That’s all it says, does not tell me what “size” is referring to. Although it does point to Graphics.DrawMeshNow as being the problem. Anyways, here is all the code relevant to the problem.

var VertexMasterArray = new Array();
var NormalMasterArray = new Array();
var TriangleMasterArray = new Array();

        function Update(){
		if (Input.GetKeyDown (KeyCode.V)) {
		var BigMesh : Mesh = new Mesh();
		var VBlocks = GameObject.FindGameObjectsWithTag("Block");
		VertexMasterArray.Clear();
		NormalMasterArray.Clear();
		TriangleMasterArray.Clear();
		BigMesh.Clear();
		
		for (var i = 0; i < VBlocks.length; i++) {
			var mesh : Mesh = VBlocks*.GetComponent(MeshFilter).mesh;*
  •   		var Vvertices : Vector3[] = mesh.vertices;*
    
  •   		for(var i2=0; i2<Vvertices.length;i2++){*
    

_ VertexMasterArray.Push(Vvertices[i2]+VBlocks*.transform.position);_
_
}_
_
}_
_
BigMesh.vertices = VertexMasterArray;*_

* for (i = 0; i < VBlocks.length; i++) {*
_ mesh = VBlocks*.GetComponent(MeshFilter).mesh;
var Vnormals : Vector3 = mesh.normals;
for(i2=0; i2<Vnormals.length;i2++){
NormalMasterArray.Push(Vnormals[i2]);
}
}
BigMesh.normals = NormalMasterArray;*_

* for (i = 0; i< VBlocks.length; i++) {*
_ mesh = VBlocks*.GetComponent(MeshFilter).mesh;
var VTriangles : int = mesh.triangles;
for(i2=0; i2<VTriangles.length;i2++){
TriangleMasterArray.Push(VTriangles[i2]+24i);

* }
}
BigMesh.triangles = TriangleMasterArray;*_

* BigMesh.RecalculateNormals();*
* BigMesh.RecalculateBounds();*
* BigMesh.Optimize();*

* VertexMasterArray.Clear();*
* VertexMasterArray = BigMesh.vertices;*
* NormalMasterArray.Clear();*
* NormalMasterArray = BigMesh.normals;*
* TriangleMasterArray.Clear();*
* TriangleMasterArray = BigMesh.triangles;*

* print(“Mesh Built”);*
* }*

* if (Input.GetKey(KeyCode.C)) {*
* var rotation = Quaternion.identity;*
* // Assign a rotation 30 degrees around the y axis*
* rotation.eulerAngles = Vector3(0, 0, 0);*
* var BigMesh2 : Mesh = new Mesh();*
* BigMesh2.vertices = VertexMasterArray;*
* BigMesh2.triangles = TriangleMasterArray;*
* BigMesh2.normals = NormalMasterArray;*
* BigMesh2.RecalculateNormals();*
* BigMesh2.RecalculateBounds();*
* BigMesh2.Optimize();*
* Graphics.DrawMeshNow(BigMesh2,Vector3(0,20,0),rotation);*
* }*
}
I’m pretty much at a loss as how to proceed next.

To sum up some things that should kept in mind:

  • Vertices of a Mesh are in local space. To transform the vertices into world space you need to use Transform.TransformPoint so the position, rotation and scale is used to transform the point.
  • Same thing for normals but since they are directions you have to use Transform.TransformDirection so only the rotation (and scale i think) is (are) used.
  • Your whole thing will only work when each block-mesh have exactly 24 vertices (i guess you use unity cubes? :wink: )
  • Not sure if you need them but you don’t transfer the UVs so you can’t use textures on your mesh
  • Your whole mesh creation should be a seperate function so you can call it once in start to make sure there is always something to render when you press C.
  • The 3 VBlock loops should be combined into one, that reduces the GetComponent accesses by factor 3.
  • Why are you re-creating the already built and optimised mesh each time you want to draw it? Just save your bigmesh outside instead of your 3 (slow)arrays.
  • Using DrawMeshNow in Update is pretty much useless since right after Update the camera will clear the buffers. You should use it in OnPostRender to draw it after all cameras has been drawn.
  • Finally just to have it mentioned: You need Unity pro to use the Graphics class