예제 #1
0
    protected override void OnCreateManager()
    {
        //Comment this in Build version
        // UnityEditor.SceneView.FocusWindowIfItsOpen(typeof(UnityEditor.SceneView));

        randomGenerater = new Unity.Mathematics.Random(1);

        modelPrefab = Resources.Load <GameObject>(modelName);
        modelMesh   = ((MeshFilter)modelPrefab.GetComponentInChildren(typeof(MeshFilter))).sharedMesh;

        VoxelUtility.Init(modelMesh, voxelSize, bottomColor, topColor);
        VoxelUtility.Describe();

        float3[] f3Vertex    = modelMesh.vertices.ToList().ConvertAll(input => new float3(input.x, input.y, input.z)).ToArray();
        var      meshVertexs = new NativeArray <float3>(f3Vertex, Allocator.TempJob);

        int3[] i3Triangle = new int3[modelMesh.triangles.Length / 3];

        int[] tri = modelMesh.triangles;
        for (int i = 0; i != i3Triangle.Length; ++i)
        {
            i3Triangle[i].x = tri[i * 3];
            i3Triangle[i].y = tri[i * 3 + 1];
            i3Triangle[i].z = tri[i * 3 + 2];
        }

        var meshTriangles = new NativeArray <int3>(i3Triangle, Allocator.TempJob);

        hashMap = new NativeHashMap <int, bool>(VoxelUtility.TotalGridsCount, Allocator.TempJob);

        var HashTriangleToVoxelJob = new HashTriangleToVoxel {
            vertexs   = meshVertexs,
            triangles = meshTriangles,
            voxels    = hashMap.ToConcurrent(),
        };

        JobHandle toSurfaceVoxelHandle = HashTriangleToVoxelJob.Schedule(i3Triangle.Length, 64);

        toSurfaceVoxelHandle.Complete();
        surfacePosArray = hashMap.GetKeyArray(Allocator.Persistent); // Length = # surface voxels

        /*End of step a */
        NativeHashMap <int, bool> surfaceHashMap = new NativeHashMap <int, bool>(VoxelUtility.TotalGridsCount * 3, Allocator.TempJob);
        var HashVoxelCommonFaceJob = new HashSurfaceVoxelUnCommonFace {
            surfaceVoxels      = hashMap,
            surfaceVoxelsIndex = surfacePosArray,
            surfaceFaces       = surfaceHashMap.ToConcurrent()
        };
        JobHandle toSurfaceFaceHandle = HashVoxelCommonFaceJob.Schedule(surfacePosArray.Length, 64, toSurfaceVoxelHandle);

        toSurfaceFaceHandle.Complete();

        Debug.Log("# Face Before removal: NumFace = " + surfaceHashMap.Length + " \nNumV= " + hashMap.Length);
        RemoveOneConsecutiveSurface(ref surfaceHashMap);
        Debug.Log("Faces Count After removal: " + surfaceHashMap.Length);

        /*End of step c*/
        int3 totalGridsCount = VoxelUtility.CalculateTotalGrids;
        NativeHashMap <int, bool> volumeHashMap = new NativeHashMap <int, bool>(VoxelUtility.TotalGridsCount, Allocator.TempJob);
        var castRayToVolumeJob = new CastRayToVolume {
            surfaceFaces   = surfaceHashMap,
            volumeVoxels   = volumeHashMap.ToConcurrent(),
            totalGridCount = totalGridsCount
        };
        JobHandle castRayHandle = castRayToVolumeJob.Schedule(totalGridsCount.x * totalGridsCount.y, 64, toSurfaceFaceHandle);

        castRayHandle.Complete();

        volumePosArray = volumeHashMap.GetKeyArray(Allocator.Persistent);
        Debug.Log("Surface voxels Count: " + surfacePosArray.Length);
        Debug.Log("Volume voxels Count: " + volumePosArray.Length);

        meshVertexs.Dispose();
        meshTriangles.Dispose();
        hashMap.Dispose();
        surfaceHashMap.Dispose();
        volumeHashMap.Dispose();

        if (randomOrder)
        {
            Shuffle(surfacePosArray);
            Shuffle(volumePosArray);
        }
        voxelPrefab = Resources.Load <GameObject>("Voxel");

        newGroup = GetEntityQuery(new EntityQueryDesc
        {
            All     = new [] { ComponentType.ReadWrite <Voxel>(), ComponentType.ReadWrite <LocalToWorld>(), ComponentType.ReadWrite <NewVoxel>() },
            Options = EntityQueryOptions.Default
        });

        movingGroup = GetEntityQuery(new EntityQueryDesc
        {
            All     = new [] { ComponentType.ReadWrite <Voxel>(), ComponentType.ReadWrite <LocalToWorld>(), ComponentType.ReadWrite <MovingVoxel>() },
            Options = EntityQueryOptions.Default
        });

        finishedGroup = GetEntityQuery(new EntityQueryDesc
        {
            All     = new [] { ComponentType.ReadWrite <Voxel>(), ComponentType.ReadWrite <LocalToWorld>() },
            None    = new ComponentType[] { ComponentType.ReadWrite <MovingVoxel>(), ComponentType.ReadWrite <NewVoxel>() },
            Options = EntityQueryOptions.Default
        });
    }