示例#1
0
文件: mc.cs 项目: abarabone/mc
        void createHitMesh()
        {
            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.gridData[gridid].Position;  // スケールを1としている
                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);
            }
        }
示例#2
0
文件: mc.cs 项目: abarabone/mc
        public MeshCollider BuildMeshCollider(float3 gridpos, MeshCollider mc, IEnumerable <CubeInstance> cubeInstances)
        {
            if (!cubeInstances.Any())
            {
                return(mc);
            }

            var gridid = CubeUtility.FromCubeInstance(cubeInstances.First().instance).gridId;

            if (mc == null)
            {
                var igrid = ((int3)gridpos >> 5) * new int3(1, -1, -1);
                var go    = new GameObject($"grid {igrid.x} {igrid.y} {igrid.z}");
                go.transform.position = gridpos;

                mc            = go.AddComponent <MeshCollider>();
                mc.sharedMesh = new Mesh();
            }
            mc.enabled = false;

            var(i, v) = CubeUtility.MakeCollisionMeshData(cubeInstances, this.idxLists, this.vtxList);
            using (i)
                using ( v )
                {
                    var mesh = mc.sharedMesh;
                    mesh.Clear();
                    mesh.MarkDynamic();
                    mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
                    mesh.SetVertices(v.AsArray());
                    mesh.SetIndices(i.AsArray(), MeshTopology.Triangles, 0);

                    mc.sharedMesh = mesh;
                }
            mc.enabled = true;
            return(mc);
        }
示例#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);
            }
        }