示例#1
0
        private bool TryGetBuilderVert(Vector3Int pos, out Vert vert)
        {
            vert = null;

            var dx = pos.x - voxelData.gridSize[0];
            var dy = pos.y - voxelData.gridSize[1];
            var dz = pos.z - voxelData.gridSize[2];

            if (dx < 0 && dy < 0 && dz < 0)
            {
                return(voxelData.builderVerts.TryGetValue(pos, out vert));
            }

            var neighborOffset = new Vector3Int(Math.Max(dx + 1, 0), Math.Max(dy + 1, 0), Math.Max(dz + 1, 0));
            var lNeighborData  = neighborData[neighborOffset];

            if (lNeighborData.builderVerts.Count == 0)
            {
                return(false);
            }

            var nPos = (Vector3Int.one - neighborOffset) * pos;

            return(lNeighborData.builderVerts.TryGetValue(nPos, out vert));
        }
示例#2
0
        private void LevelVert(Vert vert)
        {
            if (!vert.needsLeveling)
            {
                return;
            }

            var sum   = 0f;
            var count = 0;

            foreach (var link in vert.links)
            {
                if (TryGetBuilderVert(link, out var linkedVert))
                {
                    if (linkedVert.needsLeveling)
                    {
                        continue;
                    }

                    sum += linkedVert.position.y;
                    count++;
                }
            }

            if (count == 0)
            {
                return;
            }

            var pos = vert.position;

            pos.y              = sum / count;
            vert.position      = pos;
            vert.needsLeveling = false;
        }
示例#3
0
        private MeshData ProcessFinalMesh(MeshData intermediateMesh)
        {
            var doRemove  = settings.removeSmallSurfaces;
            var threshold = settings.smallSurfaceTriangleThreshold;

            if (threshold < 1)
            {
                doRemove = false;
            }

            var result = new MeshData();

            var indexReplacementDict = new Dictionary <int, int>();
            var vertDict             = new Dictionary <int, Vert>();
            var count = 0;

            var tmpVerts = new List <Vert>();
            var stack    = new Stack <Vert>();

            foreach (var vert in voxelData.builderVerts)
            {
                var i = count++;
                vert.Value.assignedIndex = i;

                var pos = vert.Value.position;
                if (pos.x == 0 || pos.z == 0)
                {
                    vert.Value.chunkEdge = true;
                }

                vertDict.Add(i, vert.Value);
                foreach (var index in vert.Value.indices)
                {
                    indexReplacementDict[index] = vert.Value.assignedIndex;
                }
            }

            foreach (var neighbor in neighborData)
            {
                foreach (var vert in neighbor.Value.builderVerts)
                {
                    var i = count++;
                    vert.Value.assignedIndex = i;
                    vert.Value.chunkEdge     = true;
                    vertDict.Add(i, vert.Value);
                    foreach (var index in vert.Value.indices)
                    {
                        indexReplacementDict[index] = vert.Value.assignedIndex;
                    }
                }
            }

            if (doRemove)
            {
                var safety = 1024;
                while (safety-- > 0)
                {
                    Vert start = null;

                    foreach (var vert in vertDict)
                    {
                        if (vert.Value.connectedCount == 0)
                        {
                            start = vert.Value;
                            break;
                        }
                    }

                    if (start == null)
                    {
                        break;
                    }

                    tmpVerts.Clear();
                    stack.Clear();
                    stack.Push(start);
                    var chunkEdge = false;

                    while (stack.Count > 0)
                    {
                        var vert = stack.Pop();
                        tmpVerts.Add(vert);
                        vert.connectedCount = -1;
                        if (vert.chunkEdge)
                        {
                            chunkEdge = true;
                        }

                        foreach (var link in vert.links)
                        {
                            if (TryGetBuilderVert(link, out var linkedVert))
                            {
                                if (linkedVert.connectedCount == 0)
                                {
                                    stack.Push(linkedVert);
                                }
                            }
                        }
                    }

                    // Verts from neighboring chunks are not counted - always assume that triangles adjacent to chunk
                    // edges are above removal threshold.
                    var vertCount = tmpVerts.Count;
                    if (chunkEdge)
                    {
                        vertCount += threshold;
                    }

                    foreach (var vert in tmpVerts)
                    {
                        vert.connectedCount = vertCount;
                    }
                }

                if (safety == 0)
                {
                    Debug.LogWarning("Failed to remove small meshes, emergency break.");
                }
            }

            foreach (var index in intermediateMesh.indices)
            {
                var vert = vertDict[indexReplacementDict[index]];
                if (doRemove && vert.connectedCount < threshold)
                {
                    continue;
                }

                result.verts.Add(vert.position);
                result.indices.Add(index);
            }

            return(result);
        }