Initialize() public method

public Initialize ( bool fillType, int radius ) : void
fillType bool
radius int
return void
Example #1
0
    private void SetupStencil(bool isBreaking)
    {
        activeStencil = stencils[stencilIndex];

        if (worldManager.creativeMode)
        {
            activeStencil.Initialize(fillTypeIndex, radiusIndex);
        }
        else
        {
            var fillType = 0;
            var blocks   = BlockManager.Read().blocks;

            for (int i = 0; i < blocks.Count; i++)
            {
                if (isBreaking)
                {
                    fillType = 0;
                    break;
                }
                if (uiHotBar.currentItem != null && blocks[i].blockType.ToString() == uiHotBar.currentItem.itemType.ToString())
                {
                    fillType = i;
                    break;
                }
            }

            activeStencil.Initialize(fillType, radiusIndex);
        }
    }
Example #2
0
    void MobileControls()
    {
        if (clickState != ClickState.NONE || clickState != ClickState.UP)
        {
            Touch touch = Input.GetTouch(0);
            Ray   ray   = cam.ScreenPointToRay(touch.position);
            scale = Vector3.one * ((radiusIndex + 0.5f) * voxelSize * 2f);
            RaycastHit singleRayHitInfo;

            if (Physics.Raycast(ray, out singleRayHitInfo))
            {
                Vector3 singleRayCenter = transform.InverseTransformPoint(singleRayHitInfo.point);
                gizmoCenter = singleRayCenter;
                Vector3 center = singleRayCenter;
                //   Debug.DrawRay(ray.origin, ray.direction * 20, Color.red);

                center.x += halfSize;
                center.y += halfSize;

                if (clickState == ClickState.HOLD)
                {
                    CylinderCast(touch.position);

                    VoxelStencil activeStencil = stencils[stencilIndex];
                    activeStencil.Initialize(fillTypeIndex == 0, (radiusIndex + 0.5f) * voxelSize);
                    activeStencil.SetCenter(center.x, center.y);

                    activeLayer.EditVoxels(center, activeStencil);
                }
            }
        }
    }
Example #3
0
    private void Update()
    {
        Transform  visualization = stencilVisualizations[stencilIndex];
        RaycastHit hitInfo;

        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo) &&
            hitInfo.collider.gameObject == gameObject)
        {
            //used for stencil visualization calculations
            Vector2 center = transform.InverseTransformPoint(hitInfo.point);
            center.x += halfSize;
            center.y += halfSize;
            //----------------
            if (Input.GetMouseButton(0))
            {
                VoxelStencil activeStencil = stencils[stencilIndex];
                activeStencil.Initialize(fillTypeIndex == 0, (radiusIndex + 0.5f) * voxelSize);
                activeStencil.SetCenter(center.x, center.y);

                EditVoxels(center, activeStencil);
            }
            //used for stencil visualization calculations
            center.x -= halfSize;
            center.y -= halfSize;
            visualization.localPosition = center;
            Vector3 tempScale = Vector3.one * ((radiusIndex + 0.5f) * voxelSize * 2f);
            visualization.localScale = new Vector3(tempScale.x, 0.01f, tempScale.z);
            //----------------
            visualization.gameObject.SetActive(true);
        }
        else
        {
            visualization.gameObject.SetActive(false);
        }
    }
Example #4
0
    private void EditVoxels(Vector2 center)
    {
        VoxelStencil activeStencil = stencils[stencilIndex];

        activeStencil.Initialize(
            fillTypeIndex, (radiusIndex + 0.5f) * voxelSize);
        activeStencil.SetCenter(center.x, center.y);

        EditVoxels(center, activeStencil);
    }
Example #5
0
    void CutStart()
    {
        Vector2 center = new Vector2(chunkResolutionX * chunkSize / 2, chunkResolutionY * chunkSize);

        VoxelStencil activeStencil = stencils[1];

        activeStencil.Initialize(false, (4 + 0.5f) * voxelSize);
        activeStencil.SetCenter(center.x, center.y);

        EditVoxels(center, activeStencil);
    }
    private void EditVoxels(Vector3 point)
    {
        int centerX = (int)((point.x + halfSize) / voxelSize);
        int centerY = (int)((point.y + halfSize) / voxelSize);

        int xStart = (centerX - radiusIndex - 1) / voxelResolution;

        if (xStart < 0)
        {
            xStart = 0;
        }
        int xEnd = (centerX + radiusIndex) / voxelResolution;

        if (xEnd >= chunkResolution)
        {
            xEnd = chunkResolution - 1;
        }
        int yStart = (centerY - radiusIndex - 1) / voxelResolution;

        if (yStart < 0)
        {
            yStart = 0;
        }
        int yEnd = (centerY + radiusIndex) / voxelResolution;

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


        VoxelStencil activeStencil = stencils[stencilIndex];

        activeStencil.Initialize(fillTypeIndex == 0, radiusIndex);

        int voxelYOffset = yEnd * voxelResolution;

        for (int y = yEnd; y >= yStart; y--)
        {
            int i            = y * chunkResolution + xEnd;
            int voxelXOffset = xEnd * voxelResolution;
            for (int x = xEnd; x >= xStart; x--, i--)
            {
                activeStencil.SetCenter(centerX - voxelXOffset, centerY - voxelYOffset);
                chunks[i].Apply(activeStencil);
                voxelXOffset -= voxelResolution;
            }
            voxelYOffset -= voxelResolution;
        }
    }
Example #7
0
    void PCcontrolsCylinder()
    {
        Transform visualization = stencilVisualizations[stencilIndex];
        Ray       ray           = cam.ScreenPointToRay(Input.mousePosition);

        scale = Vector3.one * 2;// * ((radiusIndex + 0.5f) * voxelSize * 2f);
        RaycastHit singleRayHitInfo;

        if (Physics.Raycast(ray, out singleRayHitInfo))
        {
            Vector3 singleRayCenter = transform.InverseTransformPoint(singleRayHitInfo.point);
            gizmoCenter = singleRayCenter;
            Vector3 center = singleRayCenter;
            Debug.DrawRay(ray.origin, ray.direction * 20, Color.red);

            #region used for stencil visualization calculations
            center.x += halfSize;
            center.y += halfSize;
            #endregion

            if (clickState == ClickState.HOLD)
            {
                CylinderCast(Input.mousePosition);

                VoxelStencil activeStencil = stencils[stencilIndex];
                activeStencil.Initialize(fillTypeIndex == 0, 1);
                activeStencil.SetCenter(center.x, center.y);

                activeLayer.EditVoxels(center, activeStencil);
            }

            #region used for stencil visualization calculations

            center.x -= halfSize;
            center.y -= halfSize;
            center.z -= 0.30f;
            visualization.localPosition = center;
            visualization.localScale    = new Vector3(scale.x, 0.05f, scale.z);

            #endregion

            visualization.gameObject.SetActive(true);
        }
        else
        {
            visualization.gameObject.SetActive(false);
            //Debug.Log(hitInfo.collider.gameObject.name);
        }
    }
    private void EditVoxels(Vector3 point)
    {
        int centerX = (int)((point.x + _halfSize) / _voxelSize);
        int centerY = (int)((point.y + _halfSize) / _voxelSize);
        int xStart = (centerX - _radiusIndex) / VoxelResolution;
        if (xStart < 0)
        {
            xStart = 0;
        }
        int xEnd = (centerX + _radiusIndex) / VoxelResolution;
        if (xEnd >= ChunkResolution)
        {
            xEnd = ChunkResolution - 1;
        }
        int yStart = (centerY - _radiusIndex) / VoxelResolution;
        if (yStart < 0)
        {
            yStart = 0;
        }
        int yEnd = (centerY + _radiusIndex) / VoxelResolution;
        if (yEnd >= ChunkResolution)
        {
            yEnd = ChunkResolution - 1;
        }
        int chunkX = centerX / VoxelResolution;
        int chunkY = centerY / VoxelResolution;
        centerX -= chunkX * VoxelResolution;
        centerY -= chunkY * VoxelResolution;
        VoxelStencil activeStencil = new VoxelStencil();
        activeStencil.Initialize(_fillTypeIndex == 0, _radiusIndex);

        activeStencil.SetCenter(centerX,centerY);
        _chunks[chunkY * ChunkResolution + chunkX].Apply(activeStencil);

        int voxelYOffset = yStart * VoxelResolution;
        for (int y = yStart; y <= yEnd; y++)
        {
            int i = y * ChunkResolution + xStart;
            int voxelXOffset = xStart * VoxelResolution;
            for (int x = xStart; x <= xEnd; x++, i++)
            {
                activeStencil.SetCenter(centerX - voxelXOffset, centerY - voxelYOffset);
                _chunks[i].Apply(activeStencil);
                voxelXOffset += VoxelResolution;
            }
            voxelYOffset += VoxelResolution;
        }
        Debug.Log(centerX + ", " + centerY + " in chunk " + chunkX + ", " + chunkY);
    }
Example #9
0
        private void PaintVoxels(Vector3 point, VoxelStencil stencil)
        {
            int centerX = (int)((point.x + voxelMap.halfSize) / voxelMap.voxelSize);
            int centerY = (int)((point.y + voxelMap.halfSize) / voxelMap.voxelSize);

            Debug.Log(centerX + " - " + centerY + " Chunk(" + ")");
            stencil.Initialize(fillTypeIndex == 0, radiusIndex);

            int xStart = (centerX - radiusIndex - 1) / voxelMap.gridResolution;

            if (xStart < 0)
            {
                xStart = 0;
            }
            int xEnd = (centerX + radiusIndex) / voxelMap.gridResolution;

            if (xEnd >= voxelMap.resolution)
            {
                xEnd = voxelMap.resolution - 1;
            }
            int yStart = (centerY - radiusIndex - 1) / voxelMap.gridResolution;

            if (yStart < 0)
            {
                yStart = 0;
            }
            int yEnd = (centerY + radiusIndex) / voxelMap.gridResolution;

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

            int voxelYOffset = yEnd * voxelMap.gridResolution;

            for (int y = yEnd; y >= yStart; y--)
            {
                int i            = y * voxelMap.resolution + xEnd;
                int voxelXOffset = xEnd * voxelMap.gridResolution;
                for (int x = xEnd; x >= xStart; x--, i--)
                {
                    stencil.SetCenter(centerX - voxelXOffset, centerY - voxelYOffset);
                    ApplyChunkStencil(voxelMap.chunks[i], stencil);
                    voxelXOffset -= voxelMap.gridResolution;
                }
                voxelYOffset -= voxelMap.gridResolution;
            }
        }
Example #10
0
    private void EditVoxels(Vector2 center)
    {
        VoxelStencil activeStencil = stencils[stencilIndex];

        activeStencil.Initialize(fillTypeIndex == 0, (radiusIndex + 0.5f) * voxelSize);
        activeStencil.SetCenter(center.x, center.y);

        int xStart = (int)((activeStencil.XStart - voxelSize) / chunkSize);

        if (xStart < 0)
        {
            xStart = 0;
        }
        int xEnd = (int)((activeStencil.XEnd + voxelSize) / chunkSize);

        if (xEnd >= chunkResolution)
        {
            xEnd = chunkResolution - 1;
        }
        int yStart = (int)((activeStencil.YStart - voxelSize) / chunkSize);

        if (yStart < 0)
        {
            yStart = 0;
        }
        int yEnd = (int)((activeStencil.YEnd + voxelSize) / chunkSize);

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

        for (int y = yEnd; y >= yStart; y--)
        {
            int i = y * chunkResolution + xEnd;
            for (int x = xEnd; x >= xStart; x--, i--)
            {
                activeStencil.SetCenter(center.x - x * chunkSize, center.y - y * chunkSize);
                chunks[i].Apply(activeStencil);
            }
        }
    }
Example #11
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);
            }
        }
    }