public List <List <ClusterInfo> > GetClusterDistances(UnsafeList <Cluster> clusters, Vector3[] verts) { Vector3 *positions = (Vector3 *)UnsafeUtility.Malloc(sizeof(Vector3) * clusters.length, 16, Allocator.Temp); List <List <ClusterInfo> > distances = new List <List <ClusterInfo> >(); ClusterDistancesDatas.positions = positions; ClusterDistancesDatas.distances = distances; ClusterDistancesDatas.verts = verts; ClusterDistancesDatas.clusters = clusters; for (int i = 0; i < clusters.length; ++i) { ClusterDistancesDatas.distances.Add(new List <ClusterInfo>(ClusterDistancesDatas.clusters.length)); Cluster *c = (Cluster *)ClusterDistancesDatas.clusters[i]; ClusterDistancesDatas.positions[i] = ClusterDistancesDatas.verts[c->x] + ClusterDistancesDatas.verts[c->y] + ClusterDistancesDatas.verts[c->z] + ClusterDistancesDatas.verts[c->w]; ClusterDistancesDatas.positions[i] /= 4f; } JobHandle getSortDistanceJob = (new GetSortedDistance()).Schedule(clusters.length, 1); getSortDistanceJob.Complete(); ClusterDistancesDatas.positions = null; ClusterDistancesDatas.distances = null; ClusterDistancesDatas.verts = null; UnsafeUtility.Free(positions, Allocator.Temp); return(distances); }
public void GenerateAsync(bool listCommand = true) { clusterBuffer = new NativeArray <Cluster>(property.clusterCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); pointsBuffer = new NativeArray <Point>(property.clusterCount * PipelineBaseBuffer.CLUSTERCLIPCOUNT, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); Cluster * clusterData = clusterBuffer.Ptr(); Point * verticesData = pointsBuffer.Ptr(); const string infosPath = "Assets/BinaryData/MapInfos/"; const string pointsPath = "Assets/BinaryData/MapPoints/"; MStringBuilder sb = new MStringBuilder(pointsPath.Length + property.name.Length + ".mpipe".Length); allStrings[0] = infosPath; allStrings[1] = property.name; allStrings[2] = ".mpipe"; sb.Combine(allStrings); // FileStream fileStream = new FileStream(sb.str, FileMode.Open, FileAccess.Read); using (FileStream reader = new FileStream(sb.str, FileMode.Open, FileAccess.Read)) { int length = (int)reader.Length; byte[] bytes = GetByteArray(length); reader.Read(bytes, 0, length); fixed(byte *b = bytes) { UnsafeUtility.MemCpy(clusterData, b, length); } } allStrings[0] = pointsPath; sb.Combine(allStrings); using (FileStream reader = new FileStream(sb.str, FileMode.Open, FileAccess.Read)) { int length = (int)reader.Length; byte[] bytes = GetByteArray(length); reader.Read(bytes, 0, length); fixed(byte *b = bytes) { UnsafeUtility.MemCpy(verticesData, b, length); } } LoadingCommandQueue commandQueue = LoadingThread.commandQueue; if (listCommand) { lock (commandQueue) { commandQueue.Queue(GenerateRun()); } } }
private void OnDisable() { testMesh = GetComponent <MeshFilter>().sharedMesh; Vector3[] vert = testMesh.vertices; Vector3[] nor = testMesh.normals; Vector4[] tangents = testMesh.tangents; Vector2[] uv0 = testMesh.uv; int[] tris = testMesh.triangles; UnsafeList <Cluster> v = GetAllCluster(tris, vert); var clusterDistance = GetClusterDistances(v, vert); List <Cluster[]> clusterGroups = GetClusters(clusterDistance, v); uint infoByteSize = (uint)(clusterGroups.Count * ObjectInfo.SIZE); ObjectInfo * infos = (ObjectInfo *)UnsafeUtility.Malloc(infoByteSize, 16, Allocator.Temp); uint pointsByteSize = (uint)(clusterGroups.Count * 64 * Point.SIZE); Point * points = (Point *)UnsafeUtility.Malloc(pointsByteSize, 16, Allocator.Temp); for (int i = 0, pointsCount = 0; i < clusterGroups.Count; ++i) { Cluster *group = (Cluster *)UnsafeUtility.Malloc(UnsafeUtility.SizeOf <Cluster>() * clusterGroups[i].Length, 16, Allocator.Temp); for (int a = 0; a < clusterGroups[i].Length; ++a) { group[a] = clusterGroups[i][a]; } int * decodedGroup = (int *)group; ObjectInfo info; info.position = Vector3.zero; info.extent = Vector3.zero; Vector3 *allPositions = (Vector3 *)UnsafeUtility.Malloc(sizeof(Vector3) * 64, 16, Allocator.Temp); for (int a = 0; a < 64; ++a) { int vertID = decodedGroup[a]; Point p; p.vertex = vert[vertID]; p.vertex = transform.localToWorldMatrix.MultiplyPoint(p.vertex); allPositions[a] = p.vertex; info.position += p.vertex; p.normal = nor[vertID]; p.normal = transform.localToWorldMatrix.MultiplyVector(p.normal); p.tangent = tangents[vertID]; Vector3 tangentXYZ = new Vector3(p.tangent.x, p.tangent.y, p.tangent.z); tangentXYZ = transform.localToWorldMatrix.MultiplyVector(tangentXYZ); p.tangent.x = tangentXYZ.x; p.tangent.y = tangentXYZ.y; p.tangent.z = tangentXYZ.z; p.texcoord = uv0[vertID]; points[pointsCount] = p; pointsCount++; } info.position /= 64f; for (int a = 0; a < 64; ++a) { Vector3 point = allPositions[a]; Vector3 dir = point - info.position; dir = dir.Abs(); info.extent = dir.Max(info.extent); } infos[i] = info; byte[] pointsBytes = new byte[pointsByteSize]; byte[] infosBytes = new byte[infoByteSize]; fixed(byte *pointStartPtr = &pointsBytes[0]) { UnsafeUtility.MemCpy(pointStartPtr, points, pointsByteSize); } fixed(byte *infoStartPtr = &infosBytes[0]) { UnsafeUtility.MemCpy(infoStartPtr, infos, infoByteSize); } ByteArrayToFile("Assets/Resources/MapPoints.bytes", pointsBytes); ByteArrayToFile("Assets/Resources/MapInfos.bytes", infosBytes); UnsafeUtility.Free(allPositions, Allocator.Temp); UnsafeUtility.Free(group, Allocator.Temp); UnsafeUtility.Free(points, Allocator.Temp); UnsafeUtility.Free(infos, Allocator.Temp); } }