public static void ScheduleRenderLibFiveMesh(LFTree tree, Bounds bounds, float resolution, ref RenderJobPayload payload) { UnityEngine.Profiling.Profiler.BeginSample("Schedule Render Mesh"); libfive_region3 bound = new libfive_region3(); bound.X.lower = bounds.min.x; bound.Y.lower = bounds.min.y; bound.Z.lower = bounds.min.z; bound.X.upper = bounds.max.x; bound.Y.upper = bounds.max.y; bound.Z.upper = bounds.max.z; RenderLibFiveMeshJob curRenderJob; unsafe { curRenderJob = new RenderLibFiveMeshJob() { treeToRender = tree.tree, bounds = bound, resolution = resolution, libFiveMeshPtr = payload.libFiveMeshPtr }; } payload.handle = curRenderJob.Schedule(payload.handle); JobHandle.ScheduleBatchedJobs(); UnityEngine.Profiling.Profiler.EndSample(); }
/// <summary> /// Renders a tree to a set of triangles /// /// R is a region that will be subdivided into an octree. For clean /// triangles, it should be near-cubical, but that isn't a hard requirement. /// /// res should be approximately half the model's smallest feature size; /// subdivision halts when all sides of the region are below it. /// </summary> public void RenderMesh(Mesh meshToFill, Bounds bounds, float resolution = 12.0f, float vertexSplittingAngle = 180f) { libfive_region3 bound = new libfive_region3(); bound.X.lower = bounds.min.x; bound.Y.lower = bounds.min.y; bound.Z.lower = bounds.min.z; bound.X.upper = bounds.max.x; bound.Y.upper = bounds.max.y; bound.Z.upper = bounds.max.z; IntPtr libFiveMeshPtr = libfive.libfive_tree_render_mesh(tree, bound, resolution); //Marshal the mesh into vertex and triangle arrays and assign to unity mesh libfive_mesh libFiveMesh = (libfive_mesh)Marshal.PtrToStructure(libFiveMeshPtr, typeof(libfive_mesh)); float[] vertexFloats = new float[libFiveMesh.vert_count * 3]; int[] triangleIndices = new int[libFiveMesh.tri_count * 3]; Marshal.Copy(libFiveMesh.verts, vertexFloats, 0, vertexFloats.Length); Marshal.Copy(libFiveMesh.tris, triangleIndices, 0, triangleIndices.Length); List <Vector3> vertices = new List <Vector3>(vertexFloats.Length * 2 / 3); for (int i = 0; i < vertexFloats.Length / 3; i++) { vertices.Add(new Vector3( vertexFloats[(i * 3)], vertexFloats[(i * 3) + 1], vertexFloats[(i * 3) + 2])); } //The original indices were UInt32, so cast to and from those since Unity still needs them as ints for (int i = 0; i < triangleIndices.Length; i++) { triangleIndices[i] = (int)((UInt32)triangleIndices[i]); } meshToFill.Clear(); meshToFill.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; meshToFill.SetVertices(vertices); meshToFill.SetTriangles(triangleIndices, 0); meshToFill.RecalculateBounds(); meshToFill.RecalculateNormals(vertexSplittingAngle); libfive.libfive_mesh_delete(libFiveMeshPtr); }