コード例 #1
0
    private void RecreateUpdatedChunkMeshes()
    {
        foreach (var chunk in chunks)
        {
            if (chunk.shouldUpdateMesh)
            {
                voxelMesh.TriangulateChunkMesh(chunk);
                chunk.shouldUpdateMesh = false;
            }

            if (!useColliders || !chunk.shouldUpdateCollider)
            {
                continue;
            }
            if (Vector3.Distance(p, chunk.transform.position) < colliderRadius)
            {
                chunkCollider.Generate2DCollider(chunk, chunkResolution);
                chunk.shouldUpdateCollider = false;
            }
        }
    }
コード例 #2
0
    private void EditVoxels(Vector3 point)
    {
        chunkPos = new Vector3(Mathf.Floor(point.x / voxelResolution), Mathf.Floor(point.y / voxelResolution));
        diff     = new Vector2Int((int)Mathf.Abs(point.x - (chunkPos * voxelResolution).x),
                                  (int)Mathf.Abs(point.y - (chunkPos * voxelResolution).y));

        xStart = (diff.x - radiusIndex - 1) / voxelResolution;
        if (xStart <= -chunkResolution)
        {
            xStart = -chunkResolution + 1;
        }

        xEnd = (diff.x + radiusIndex) / voxelResolution;
        if (xEnd >= chunkResolution)
        {
            xEnd = chunkResolution - 1;
        }

        yStart = (diff.y - radiusIndex - 1) / voxelResolution;
        if (yStart <= -chunkResolution)
        {
            yStart = -chunkResolution + 1;
        }

        yEnd = (diff.y + radiusIndex) / voxelResolution;
        if (yEnd >= chunkResolution)
        {
            yEnd = chunkResolution - 1;
        }

        activeStencil = stencils[stencilIndex];
        activeStencil.Initialize(fillTypeIndex, radiusIndex);

        var        voxelYOffset = yEnd * voxelResolution;
        Vector2Int checkChunk;

        updateChunkPositions.Clear();

        var result = false;

        for (var y = yStart - 1; y < yEnd + 1; y++)
        {
            var voxelXOffset = xEnd * voxelResolution;
            for (var x = xStart - 1; x < xEnd + 1; x++)
            {
                activeStencil.SetCenter(diff.x - voxelXOffset, diff.y - voxelYOffset);

                checkChunk = new Vector2Int((int)Mathf.Floor((point.x + voxelXOffset) / voxelResolution), (int)Mathf.Floor((point.y + voxelYOffset) / voxelResolution));

                if (existingChunks.ContainsKey(checkChunk))
                {
                    var currentChunk = existingChunks[checkChunk];
                    var tempRes      = currentChunk.Apply(activeStencil);
                    if (!result && tempRes)
                    {
                        result = true;
                    }
                }
                voxelXOffset -= voxelResolution;
            }
            voxelYOffset -= voxelResolution;
        }

        if (result)
        {
            checkChunk = new Vector2Int((int)Mathf.Floor((point.x) / voxelResolution), (int)Mathf.Floor((point.y) / voxelResolution));

            EditChunkAndNeighbors(checkChunk, new Vector3(Mathf.Floor(point.x), Mathf.Floor(point.y)));

            updateChunkPositions.Sort(SortByPosition);
            foreach (var chunk in from pos in updateChunkPositions where existingChunks.ContainsKey(pos) select existingChunks[pos])
            {
                voxelMesh.TriangulateChunkMesh(chunk);
                chunkCollider.Generate2DCollider(chunk, chunkResolution);
            }
        }
    }
コード例 #3
0
    private void EditVoxels(Vector3 point, bool isBreaking)
    {
        var chunkPos = new Vector3(Mathf.Floor(point.x / voxelMap.voxelResolution), Mathf.Floor(point.y / voxelMap.voxelResolution));
        var diff     = new Vector2Int((int)Mathf.Abs(point.x - (chunkPos * voxelMap.voxelResolution).x),
                                      (int)Mathf.Abs(point.y - (chunkPos * voxelMap.voxelResolution).y));

        var xStart = (diff.x - radiusIndex - 1) / voxelMap.voxelResolution;

        if (xStart <= -voxelMap.chunkResolution)
        {
            xStart = -voxelMap.chunkResolution + 1;
        }

        var xEnd = (diff.x + radiusIndex) / voxelMap.voxelResolution;

        if (xEnd >= voxelMap.chunkResolution)
        {
            xEnd = voxelMap.chunkResolution - 1;
        }

        var yStart = (diff.y - radiusIndex - 1) / voxelMap.voxelResolution;

        if (yStart <= -voxelMap.chunkResolution)
        {
            yStart = -voxelMap.chunkResolution + 1;
        }

        var yEnd = (diff.y + radiusIndex) / voxelMap.voxelResolution;

        if (yEnd >= voxelMap.chunkResolution)
        {
            yEnd = voxelMap.chunkResolution - 1;
        }

        SetupStencil(isBreaking);

        var        voxelYOffset = yEnd * voxelMap.voxelResolution;
        Vector2Int checkChunkPos;

        updateChunkPositions.Clear();

        var isEditing = false;

        if ((isBreaking && activeStencil.fillType == 0) || (!isBreaking && activeStencil.fillType != 0))
        {
            for (var y = yStart - 1; y < yEnd + 1; y++)
            {
                var voxelXOffset = xEnd * voxelMap.voxelResolution;
                for (var x = xStart - 1; x < xEnd + 1; x++)
                {
                    activeStencil.SetCenter(diff.x - voxelXOffset, diff.y - voxelYOffset);

                    checkChunkPos = new Vector2Int((int)Mathf.Floor((point.x + voxelXOffset) / voxelMap.voxelResolution), (int)Mathf.Floor((point.y + voxelYOffset) / voxelMap.voxelResolution));

                    if (voxelMap.existingChunks.ContainsKey(checkChunkPos))
                    {
                        var currentChunk = voxelMap.existingChunks[checkChunkPos];
                        var tempRes      = currentChunk.Apply(activeStencil);
                        if (!isEditing && tempRes)
                        {
                            isEditing = true;
                        }
                    }
                    voxelXOffset -= voxelMap.voxelResolution;
                }
                voxelYOffset -= voxelMap.voxelResolution;
            }
        }

        if (isEditing)
        {
            checkChunkPos = new Vector2Int((int)Mathf.Floor((point.x) / voxelMap.voxelResolution), (int)Mathf.Floor((point.y) / voxelMap.voxelResolution));

            EditChunkAndNeighbors(checkChunkPos, new Vector3(Mathf.Floor(point.x), Mathf.Floor(point.y)));

            updateChunkPositions.Sort(SortByPosition);

            //TODO: update so it only checks the relevant one
            // foreach (var item in updateChunkPositions) {
            //     Debug.Log(item);
            // }

            foreach (var chunk in from pos in updateChunkPositions where voxelMap.existingChunks.ContainsKey(pos) select voxelMap.existingChunks[pos])
            {
                voxelMesh.TriangulateChunkMesh(chunk);
                chunkCollider.Generate2DCollider(chunk, voxelMap.chunkResolution);
            }
        }
    }