I am trying to load an object which is in binary *.bytes format. It contains the points for all vertices. It is created by Vectrosity. Here is my code so far:
var textAssetFileName = "file:///sdcard/temp/cylinder.bytes";
function Start ()
{
var www = new WWW(textAssetFileName);
yield www;
var mesh : Mesh = new Mesh ();
GetComponent(MeshFilter).mesh = mesh;
var cubePoints = Vector.BytesToVector3Array (www.data);
mesh.vertices = cubePoints ;
}
But that produces the error: “The best overload for the method ‘Vector.BytesToVector3Array(byte[ ])’ is not compatible with the argument list ‘(String)’”.
And I want to create a gameObject from this as well, but this page does not explain how to do that:
http://unity3d.com/support/documentation/ScriptReference/Mesh.html
Use WWW.bytes instead of WWW.data (which is deprecated now anyway; you should use WWW.text instead).
–Eric
Thanks Eric, I managed to get most of it to work:
var textAssetFileName = "file:///sdcard/temp/cylinder.bytes";
var newObject : GameObject;
var defaultMaterial : Material;
function Start ()
{
var www = new WWW(textAssetFileName);
yield www;
var pointCloud = Vector.BytesToVector3Array (www.bytes);
newObject = new GameObject("NewObject: cylinder", MeshRenderer, MeshFilter);
var newMesh : Mesh = new Mesh();
(newObject.GetComponent(MeshFilter) as MeshFilter).mesh = newMesh;
newMesh.vertices = pointCloud;
//newMesh.triangles = PointCloudToTriangles(pointCloud);
newMesh.RecalculateBounds();
newObject.renderer.material = defaultMaterial;
newMesh.RecalculateBounds();
newObject.AddComponent(MeshCollider);
}
But now I need to create triangles from the vertices. I know such an algorithm is highly depended on the shape but I just want to triangulate a simple cube and cylinder. Any code for that floating around?
EDIT: Or maybe it is easier to just use CreatePrimitive. The cylinder I use from Vectrosity is derived from a standard Unity cylinder anyway.
3 words… Defined File Format
This statement:
var pointCloud = Vector.BytesToVector3Array (www.bytes);
would not work without the Vector class being created that would handle BytesToVector3Array(byte[ ])
A true defined file format will have sections of code for vertices and triangles at the minimum. You would also want to keep track of texture vertices and possibly normals and tangents. Furthermore, you may want to look at bone weights, bone structures and animation. These files contain alot of data, not just a point cloud.
If all you have is a point cloud, you would have to figure out how many vertices that you have on the outside of the entire thing and start making triangles to suit. (this is hard, and not the best way to do it)
Yeah, it looks like a dead end. So I settled with using a normal Unity primitive. That textasset is created out of such a primitive so it is easier to use the original anyway.