Пример #1
0
Файл: mc.cs Проект: abarabone/mc
        CommandBuffer createCommandBuffer(MeshResources res, Material mat)
        {
            var cb = new CommandBuffer();

            cb.name = "marching cubes drawer";

            cb.DispatchCompute(this.setGridCubeIdShader, 0, res.ArgsBufferForDispatch, 0);

            cb.DrawMeshInstancedIndirect(res.mesh, 0, mat, 0, res.ArgsBufferForInstancing);

            return(cb);
        }
Пример #2
0
Файл: mc.cs Проект: abarabone/mc
        unsafe void Awake()
        {
            this.gridData      = new NativeList <CubeUtility.GridInstanceData>(this.maxDrawGridLength, Allocator.Persistent);
            this.cubeInstances = new NativeList <CubeInstance>(1000000, Allocator.Persistent);
            //this.cubeInstances = new NativeQueue<CubeInstance>( Allocator.Persistent );

            this.meshResources = new MeshResources(this.MarchingCubeAsset, this.maxDrawGridLength);
            setResources();

            var res = this.meshResources;
            var cb  = createCommandBuffer(res, this.Material);

            Camera.main.AddCommandBuffer(CameraEvent.BeforeSkybox, cb);

            initCubes();
            createHitMesh();
        }
Пример #3
0
        unsafe void Awake()
        {
            this.gridPositions = new NativeList <float4>(this.maxDrawGridLength, Allocator.Persistent);
            this.nearGrids     = new NativeList <int4>(this.maxDrawGridLength * 2, Allocator.Persistent);
            this.cubeInstances = new NativeList <CubeInstance>(1000000, Allocator.Persistent);
            //this.cubeInstances = new NativeQueue<CubeInstance>( Allocator.Persistent );

            this.meshResources = new MeshResources(this.MarchingCubeAsset, this.maxDrawGridLength);
            var res = this.meshResources;

            this.setGridCubeIdShader.SetBuffer(0, "src_instances", res.instancesBuffer);
            this.setGridCubeIdShader.SetBuffer(0, "dst_grid_cubeids", res.gridCubeIdBuffer);

            this.Material.SetBuffer("BaseVtxList", res.baseVtxsBuffer);
            this.Material.SetBuffer("IdxList", res.idxListsBuffer);
            this.Material.SetBuffer("Instances", res.instancesBuffer);
            this.Material.SetBuffer("GridPositions", res.gridPositionBuffer);
            this.Material.SetBuffer("near_gridids_prev_and_next", res.nearGridIdBuffer);
            this.Material.SetBuffer("Normals", res.triNormalsBuffer);
            this.Material.SetBuffer("grid_cubeids", res.gridCubeIdBuffer);
            this.Material.SetBuffer("cube_normals", res.cubeNormalBuffer);
            res.cubeNormalBuffer.SetData(this.MarchingCubeAsset.CubeIdAndVertexIndicesList.SelectMany(x => x.normalsForVertex).ToArray());

            this.cubeGrids = new CubeGridArrayUnsafe(8, 3, 8);
            this.cubeGrids.FillCubes(new int3(-1, 2, -1), new int3(11, 11, 11), isFillAll: true);
            this.cubeGrids.FillCubes(new int3(2, 1, 3), new int3(1, 2, 1), isFillAll: true);

            var c = this.cubeGrids[0, 0, 0];

            (*c.p)[1, 1, 1] = 1;
            c[31, 1, 1]     = 1;
            c[31, 31, 31]   = 1;
            c[1, 31, 1]     = 1;
            for (var iy = 0; iy < 15; iy++)
            {
                for (var iz = 0; iz < 15; iz++)
                {
                    for (var ix = 0; ix < 13; ix++)
                    {
                        c[5 + ix, 5 + iy, 5 + iz] = 1;
                    }
                }
            }
            //this.job = this.cubeGrids.BuildCubeInstanceData( this.gridPositions, this.nearGrids, this.cubeInstances );

            this.job.Complete();
            nearGrids.AsArray().ForEach(x => Debug.Log(x));

            res.instancesBuffer.SetData(this.cubeInstances.AsArray());
            res.gridPositionBuffer.SetData(this.gridPositions.AsArray());
            res.nearGridIdBuffer.SetData(this.nearGrids.AsArray());
            this.setGridCubeIdShader.Dispatch(0, this.cubeInstances.Length >> 6, 1, 1);
            Debug.Log($"{cubeInstances.Length} / {res.instancesBuffer.count}");


            var cb = createCommandBuffer(res, this.Material);

            Camera.main.AddCommandBuffer(CameraEvent.BeforeSkybox, cb);


            var glen = this.cubeGrids.GridLength;

            this.cubeGridColliders = new MeshCollider[glen.x, glen.y, glen.z];

            this.idxLists = this.MarchingCubeAsset.CubeIdAndVertexIndicesList.Select(x => x.vertexIndices).ToArray();
            this.vtxList  = this.MarchingCubeAsset.BaseVertexList.Select(x => new float3(x.x, x.y, x.z)).ToArray();
            //var idxLists = this.MarchingCubeAsset.CubeIdAndVertexIndicesList.Select( x => x.vertexIndices ).ToArray();
            //var vtxList = this.MarchingCubeAsset.BaseVertexList.Select( x => new float3( x.x, x.y, x.z ) ).ToArray();
            var q =
                from x in this.cubeInstances.ToArray()
                let gridId = CubeUtility.FromCubeInstance(x.instance).gridId
                             group x by gridId
            ;

            foreach (var cubeId in q)
            {
                var gridid  = (int)cubeId.Key;
                var gridpos = this.gridPositions[gridid];
                var igrid   = ((int4)gridpos >> 5) * new int4(1, -1, -1, 0);

                if (igrid.x < 0 || igrid.y < 0 || igrid.z < 0)
                {
                    continue;
                }

                var collider = this.cubeGridColliders[igrid.x, igrid.y, igrid.z];
                this.cubeGridColliders[igrid.x, igrid.y, igrid.z] = this.BuildMeshCollider(gridpos.xyz, collider, cubeId);
            }
        }