public override Mesh BuildMeshFromNodeHandle(uint nodeHandle) { Mesh collisionMesh = new Mesh(); collisionMesh.hideFlags = HideFlags.DontSave; // Get the data from Cubiquity. int[] indices = CubiquityDLL.GetIndicesMC(nodeHandle); TerrainVertex[] cubiquityVertices = CubiquityDLL.GetVerticesMC(nodeHandle); // Create the arrays which we'll copy the data to. Vector3[] vertices = new Vector3[cubiquityVertices.Length]; for (int ct = 0; ct < cubiquityVertices.Length; ct++) { // Get the vertex data from Cubiquity. vertices[ct] = new Vector3(cubiquityVertices[ct].x, cubiquityVertices[ct].y, cubiquityVertices[ct].z); vertices[ct] *= (1.0f / 256.0f); } // Assign vertex data to the meshes. collisionMesh.vertices = vertices; collisionMesh.triangles = indices; return(collisionMesh); }
public Mesh BuildMeshFromNodeHandleForTerrainVolume(uint nodeHandle) { // At some point I should read this: http://forum.unity3d.com/threads/5687-C-plugin-pass-arrays-from-C // Create rendering and possible collision meshes. Mesh renderingMesh = new Mesh(); renderingMesh.hideFlags = HideFlags.DontSave; // Get the data from Cubiquity. int[] indices = CubiquityDLL.GetIndicesMC(nodeHandle); TerrainVertex[] cubiquityVertices = CubiquityDLL.GetVerticesMC(nodeHandle); // Create the arrays which we'll copy the data to. Vector3[] renderingVertices = new Vector3[cubiquityVertices.Length]; Vector3[] renderingNormals = new Vector3[cubiquityVertices.Length]; Color32[] renderingColors = new Color32[cubiquityVertices.Length]; //Vector4[] renderingTangents = new Vector4[cubiquityVertices.Length]; Vector2[] renderingUV = new Vector2[cubiquityVertices.Length]; Vector2[] renderingUV2 = new Vector2[cubiquityVertices.Length]; for (int ct = 0; ct < cubiquityVertices.Length; ct++) { // Get the vertex data from Cubiquity. Vector3 position = new Vector3(cubiquityVertices[ct].x, cubiquityVertices[ct].y, cubiquityVertices[ct].z); Vector3 normal = new Vector3(cubiquityVertices[ct].nx, cubiquityVertices[ct].ny, cubiquityVertices[ct].nz); Color32 color = new Color32(cubiquityVertices[ct].m0, cubiquityVertices[ct].m1, cubiquityVertices[ct].m2, cubiquityVertices[ct].m3); //Vector4 tangents = new Vector4(cubiquityVertices[ct].m4 / 255.0f, cubiquityVertices[ct].m5 / 255.0f, cubiquityVertices[ct].m6 / 255.0f, cubiquityVertices[ct].m7 / 255.0f); Vector2 uv = new Vector2(cubiquityVertices[ct].m4 / 255.0f, cubiquityVertices[ct].m5 / 255.0f); Vector2 uv2 = new Vector2(cubiquityVertices[ct].m6 / 255.0f, cubiquityVertices[ct].m7 / 255.0f); // Copy it to the arrays. renderingVertices[ct] = position; renderingNormals[ct] = normal; renderingColors[ct] = color; //renderingTangents[ct] = tangents; renderingUV[ct] = uv; renderingUV2[ct] = uv2; } // Assign vertex data to the meshes. renderingMesh.vertices = renderingVertices; renderingMesh.normals = renderingNormals; renderingMesh.colors32 = renderingColors; //renderingMesh.tangents = renderingTangents; renderingMesh.uv = renderingUV; renderingMesh.uv2 = renderingUV2; renderingMesh.triangles = indices; // FIXME - Get proper bounds renderingMesh.bounds.SetMinMax(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(32.0f, 32.0f, 32.0f)); return(renderingMesh); }
public Mesh BuildMeshFromNodeHandleForTerrainVolume(uint nodeHandle) { // At some point I should read this: http://forum.unity3d.com/threads/5687-C-plugin-pass-arrays-from-C // Create rendering and possible collision meshes. Mesh renderingMesh = new Mesh(); renderingMesh.hideFlags = HideFlags.DontSave; // Get the data from Cubiquity. int[] indices = CubiquityDLL.GetIndicesMC(nodeHandle); TerrainVertex[] cubiquityVertices = CubiquityDLL.GetVerticesMC(nodeHandle); // Create the arrays which we'll copy the data to. Vector3[] renderingVertices = new Vector3[cubiquityVertices.Length]; Vector3[] renderingNormals = new Vector3[cubiquityVertices.Length]; Color32[] renderingColors = new Color32[cubiquityVertices.Length]; //Vector4[] renderingTangents = new Vector4[cubiquityVertices.Length]; Vector2[] renderingUV = new Vector2[cubiquityVertices.Length]; Vector2[] renderingUV2 = new Vector2[cubiquityVertices.Length]; for (int ct = 0; ct < cubiquityVertices.Length; ct++) { // Get and decode the position Vector3 position = new Vector3(cubiquityVertices[ct].x, cubiquityVertices[ct].y, cubiquityVertices[ct].z); position *= (1.0f / 256.0f); // Get and decode the normal // Get the materials Color32 color = new Color32(cubiquityVertices[ct].m0, cubiquityVertices[ct].m1, cubiquityVertices[ct].m2, cubiquityVertices[ct].m3); //Vector4 tangents = new Vector4(cubiquityVertices[ct].m4 / 255.0f, cubiquityVertices[ct].m5 / 255.0f, cubiquityVertices[ct].m6 / 255.0f, cubiquityVertices[ct].m7 / 255.0f); Vector2 uv = new Vector2(cubiquityVertices[ct].m4 / 255.0f, cubiquityVertices[ct].m5 / 255.0f); Vector2 uv2 = new Vector2(cubiquityVertices[ct].m6 / 255.0f, cubiquityVertices[ct].m7 / 255.0f); ushort ux = (ushort)((cubiquityVertices[ct].normal >> (ushort)8) & (ushort)0xFF); ushort uy = (ushort)((cubiquityVertices[ct].normal) & (ushort)0xFF); // Convert to floats in the range [-1.0f, +1.0f]. float ex = ux / 127.5f - 1.0f; float ey = uy / 127.5f - 1.0f; // Reconstruct the origninal vector. This is a C++ implementation // of Listing 2 of http://jcgt.org/published/0003/02/01/ float vx = ex; float vy = ey; float vz = 1.0f - Math.Abs(ex) - Math.Abs(ey); if (vz < 0.0f) { float refX = ((1.0f - Math.Abs(vy)) * (vx >= 0.0f ? +1.0f : -1.0f)); float refY = ((1.0f - Math.Abs(vx)) * (vy >= 0.0f ? +1.0f : -1.0f)); vx = refX; vy = refY; } Vector3 normal = new Vector3(vx, vy, vz); normal.Normalize(); // Copy it to the arrays. renderingVertices[ct] = position; renderingNormals[ct] = normal; renderingColors[ct] = color; //renderingTangents[ct] = tangents; renderingUV[ct] = uv; renderingUV2[ct] = uv2; } // Assign vertex data to the meshes. renderingMesh.vertices = renderingVertices; renderingMesh.normals = renderingNormals; renderingMesh.colors32 = renderingColors; //renderingMesh.tangents = renderingTangents; renderingMesh.uv = renderingUV; renderingMesh.uv2 = renderingUV2; renderingMesh.triangles = indices; // FIXME - Get proper bounds renderingMesh.bounds.SetMinMax(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(32.0f, 32.0f, 32.0f)); return(renderingMesh); }