示例#1
0
    private void CleanVerts(MarchingCubeMesh current)
    {
        int lastRealIndex = 0;

        for (int i = 0; i < verts.Length; i++)
        {
            if (verts[i].x >= 0)
            {
                verts[lastRealIndex] = verts[i];
                lastRealIndex++;
            }
        }

        //Make smaller
        current.optimizedVerts = new Vector3[lastRealIndex];
        current.optimizedTris  = new int[lastRealIndex];
        Array.Copy(verts, current.optimizedVerts, lastRealIndex);
        Array.Copy(tris, current.optimizedTris, lastRealIndex);

        //Apply mesh
        current.meshFilter.sharedMesh.Clear();
        current.meshFilter.sharedMesh.vertices  = current.optimizedVerts;
        current.meshFilter.sharedMesh.triangles = current.optimizedTris;
        current.meshFilter.sharedMesh.RecalculateNormals();
        current.meshCollider.sharedMesh = current.meshFilter.sharedMesh;
    }
示例#2
0
    public void InitializeIsoSurfaceSphere(Vector3 brushPoint, float brushRadius, Func <Vector3, float> initDef)
    {
        int        halfExtend = Mathf.FloorToInt(brushRadius / 8f) + 2;
        Vector3Int Chunk      = new Vector3Int((int)(brushPoint.x / 8f), (int)(brushPoint.y / 8f), (int)(brushPoint.z / 8f));

        for (int x = -halfExtend; x <= halfExtend; x++)
        {
            for (int y = -halfExtend; y <= halfExtend; y++)
            {
                for (int z = -halfExtend; z <= halfExtend; z++)
                {
                    Vector3Int chuckID = Chunk + new Vector3Int(x, y, z);
                    if (!meshGrid.ContainsKey(chuckID))
                    {
                        MarchingCubeMesh mesh = Instantiate(Prefab8X8, chuckID * 8, Quaternion.identity, transform);
                        meshGrid.Add(chuckID, mesh);
                        SetPointCloud(initDef, new Vector3(chuckID.x, chuckID.y, chuckID.z) * 8f);
                        marchShader.MarchCloud(ref pointCloud, ref verts, ref tris);
                        mesh.gameObject.layer = LayerMask.NameToLayer(GeoLayer);
                        CleanVerts(mesh);
                    }
                }
            }
        }
    }