Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new block with a specified color value index
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="z"></param>
        /// <param name="color"></param>
        public static void AddCube(int x, int y, int z, int color)
        {
            if (Singleton == null)
            {
                return;
            }

            int chunkX   = Mathf.FloorToInt(x / 16f);
            int chunkY   = Mathf.FloorToInt(y / 16f);
            int chunkZ   = Mathf.FloorToInt(z / 16f);
            int chunkKey = Singleton.Vector3ToInt(chunkX, chunkY, chunkZ);

            ChunkController chunk = Singleton.GetChunkByBlock(x, y, z);

            if (chunk == null)
            {
                chunk = Singleton.CreateChunk(chunkX, chunkY, chunkZ);
                Singleton.chunks.Add(chunkKey, chunk);
            }

            int paletteWidth = ColorPaletteManager.GetPaletteWidth();
            int colorX       = color % paletteWidth;
            int colorY       = Mathf.FloorToInt(color / paletteWidth);

            chunk.AddCube(Mathf.Abs(x - chunkX * 16), Mathf.Abs(y - chunkY * 16), Mathf.Abs(z - chunkZ * 16), colorX, colorY);
        }
Ejemplo n.º 2
0
        public void UpdateMesh()
        {
            mesh.Clear();
            meshManipulation.Clear();

            var controllerPosition = transform.position;

            foreach (var blockData in blocks.Values)
            {
                int x = blockData.x - (int)controllerPosition.x;
                int y = blockData.y - (int)controllerPosition.y;
                int z = blockData.z - (int)controllerPosition.z;

                List <Vector3> faces = new List <Vector3>();

                if (!DoesCubeExist(x, y, z + 1))
                {
                    faces.Add(Vector3.forward);
                }
                if (!DoesCubeExist(x, y, z - 1))
                {
                    faces.Add(Vector3.back);
                }
                if (!DoesCubeExist(x + 1, y, z))
                {
                    faces.Add(Vector3.right);
                }
                if (!DoesCubeExist(x - 1, y, z))
                {
                    faces.Add(Vector3.left);
                }
                if (!DoesCubeExist(x, y + 1, z))
                {
                    faces.Add(Vector3.up);
                }
                if (!DoesCubeExist(x, y - 1, z))
                {
                    faces.Add(Vector3.down);
                }

                int paletteWidth = ColorPaletteManager.GetPaletteWidth();

                meshManipulation.AddCubeQuads(new Vector3(x, y, z), faces.ToArray(), blockData.color % paletteWidth, Mathf.FloorToInt(blockData.color / (float)paletteWidth) + 1);
            }

            mesh.vertices = meshManipulation.GetVerticesArray();
            mesh.SetTriangles(meshManipulation.GetTrianglesArray(), 0);
            mesh.uv = meshManipulation.GetUVsArray();

            mesh.RecalculateNormals();
            mesh.RecalculateBounds();

            meshCollider.enabled = false;
            meshCollider.enabled = true;
        }
Ejemplo n.º 3
0
        public void AddCube(int x, int y, int z, int uvTileX, int uvTileY)
        {
            if (x < 0 || x >= modelData.GetLength(0) || y < 0 || y >= modelData.GetLength(1) || z < 0 || z >= modelData.GetLength(2))
            {
                return;
            }

            modelData[x, y, z] = uvTileX + uvTileY * ColorPaletteManager.GetPaletteWidth();
            cubesCount++;

            UpdateAdjacentChunk(x, y, z);

            UpdateMesh();
        }
Ejemplo n.º 4
0
        private void Update()
        {
            if (EventSystem.current.IsPointerOverGameObject())
            {
                return;
            }

            var currentTool = GetCurrentTool();

            if (currentTool == Tool.Select)
            {
                if (Input.GetKeyDown(KeyCode.Mouse0))
                {
                    RaycastHit hit;
                    var        ray = camera.ScreenPointToRay(Input.mousePosition);

                    if (Physics.Raycast(ray, out hit))
                    {
                        int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / -2);
                        int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / -2);
                        int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / -2);

                        if (ModelManager.GetCube(x, y, z) && !CubeSelectionController.DoesCubeExist(x, y, z) && TransformController.GetSelectedTranformComponents() == 0)
                        {
                            int color = ModelManager.GetCubeColor(x, y, z);

                            //CubeSelectionController.ResetCubePositions();
                            CubeSelectionController.AddCube(x, y, z, color);

                            ModelManager.RemoveCube(x, y, z);

                            TransformController.UpdateVisibleTools();
                        }
                    }
                    else
                    {
                        var controllerPosition = CubeSelectionController.GetPosition();
                        foreach (var cube in CubeSelectionController.GetAllCubes())
                        {
                            ModelManager.AddCube(cube.x + (int)controllerPosition.x, cube.y + (int)controllerPosition.y, cube.z + (int)controllerPosition.z, cube.color);
                        }

                        CubeSelectionController.Clear();

                        CubeSelectionController.ResetPosition();

                        TransformController.UpdateVisibleTools();
                    }
                }
            }
            else if (currentTool == Tool.Create)
            {
                if (Input.GetKey(KeyCode.Mouse0) && Input.GetKey(KeyCode.LeftShift) && Time.time - lastRapidTool > 0.15f)
                {
                    lastRapidTool = Time.time;

                    RaycastHit hit;
                    var        ray = camera.ScreenPointToRay(Input.mousePosition);

                    if (Physics.Raycast(ray, out hit))
                    {
                        int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / 2);
                        int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / 2);
                        int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / 2);

                        ModelManager.AddCube(x, y, z);

                        UndoManager.AddAction(new CreateBlockAction(x, y, z, ColorpickerController.GetClosestColor().GetAsIndex()));
                    }
                }
                else if (Input.GetKeyDown(KeyCode.Mouse0))
                {
                    RaycastHit hit;
                    var        ray = camera.ScreenPointToRay(Input.mousePosition);

                    if (Physics.Raycast(ray, out hit))
                    {
                        int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / 2);
                        int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / 2);
                        int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / 2);

                        ModelManager.AddCube(x, y, z);

                        UndoManager.AddAction(new CreateBlockAction(x, y, z, ColorpickerController.GetClosestColor().GetAsIndex()));
                    }
                    else
                    {
                        var point = ray.GetPoint(5);
                        int x     = Mathf.FloorToInt(point.x);
                        int y     = Mathf.FloorToInt(point.y);
                        int z     = Mathf.FloorToInt(point.z);

                        ModelManager.AddCube(x, y, z);

                        UndoManager.AddAction(new CreateBlockAction(x, y, z, ColorpickerController.GetClosestColor().GetAsIndex()));
                    }
                }
            }
            else if (currentTool == Tool.Destroy)
            {
                if (Input.GetKey(KeyCode.Mouse0) && Input.GetKey(KeyCode.LeftShift) && Time.time - lastRapidTool > 0.15f)
                {
                    lastRapidTool = Time.time;

                    RaycastHit hit;
                    var        ray = camera.ScreenPointToRay(Input.mousePosition);

                    if (Physics.Raycast(ray, out hit))
                    {
                        int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / -2);
                        int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / -2);
                        int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / -2);

                        int color = ModelManager.GetCubeColor(x, y, z);
                        ModelManager.RemoveCube(x, y, z);

                        UndoManager.AddAction(new RemoveBlockAction(x, y, z, color));
                    }
                }
                else if (Input.GetKeyDown(KeyCode.Mouse0))
                {
                    RaycastHit hit;
                    var        ray = camera.ScreenPointToRay(Input.mousePosition);

                    if (Physics.Raycast(ray, out hit))
                    {
                        int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / -2);
                        int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / -2);
                        int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / -2);

                        int color = ModelManager.GetCubeColor(x, y, z);
                        ModelManager.RemoveCube(x, y, z);

                        UndoManager.AddAction(new RemoveBlockAction(x, y, z, color));
                    }
                }
            }
            else if (currentTool == Tool.Paint)
            {
                if (Input.GetKey(KeyCode.Mouse0))
                {
                    RaycastHit hit;
                    var        ray = camera.ScreenPointToRay(Input.mousePosition);

                    if (Physics.Raycast(ray, out hit))
                    {
                        int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / -2);
                        int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / -2);
                        int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / -2);

                        int color            = ModelManager.GetCubeColor(x, y, z);
                        int colorpickerValue = ColorpickerController.GetClosestColor().GetAsIndex();

                        if (color != colorpickerValue)
                        {
                            UndoManager.AddAction(new PaintBlockAction(x, y, z, color, ColorpickerController.GetClosestColor().GetAsIndex()));

                            ModelManager.AddCube(x, y, z);
                        }
                    }
                }
            }
            else if (currentTool == Tool.Colorpicker)
            {
                if (Input.GetKeyDown(KeyCode.Mouse0))
                {
                    RaycastHit hit;
                    var        ray = camera.ScreenPointToRay(Input.mousePosition);

                    if (Physics.Raycast(ray, out hit))
                    {
                        int x = Mathf.FloorToInt(hit.point.x + hit.normal.x / -2);
                        int y = Mathf.FloorToInt(hit.point.y + hit.normal.y / -2);
                        int z = Mathf.FloorToInt(hit.point.z + hit.normal.z / -2);

                        int color        = ModelManager.GetCubeColor(x, y, z);
                        int paletteWidth = ColorPaletteManager.GetPaletteWidth();
                        ColorpickerController.SetColor(color % paletteWidth, Mathf.FloorToInt(color / paletteWidth));
                    }
                }
            }

            if (Input.GetKeyDown(KeyCode.Mouse1))
            {
                CursorController.AddUser("mainLook");
                isLooking = true;
            }
            if (Input.GetKeyUp(KeyCode.Mouse1))
            {
                CursorController.RemoveUser("mainLook");
                isLooking = false;
            }

            if (Input.GetKeyDown(KeyCode.LeftShift))
            {
                movementSpeed = 0.35f;
            }
            if (Input.GetKeyUp(KeyCode.LeftShift))
            {
                movementSpeed = 0.175f;
            }
        }
Ejemplo n.º 5
0
        public void UpdateMesh()
        {
            mesh.Clear();
            meshManipulation.Clear();

            for (int x = 0; x < modelData.GetLength(0); x++)
            {
                for (int y = 0; y < modelData.GetLength(1); y++)
                {
                    for (int z = 0; z < modelData.GetLength(2); z++)
                    {
                        if (!DoesCubeExist(x, y, z))
                        {
                            continue;
                        }

                        List <Vector3> faces = new List <Vector3>();

                        if (!DoesCubeExist(x, y, z + 1))
                        {
                            faces.Add(Vector3.forward);
                        }
                        if (!DoesCubeExist(x, y, z - 1))
                        {
                            faces.Add(Vector3.back);
                        }
                        if (!DoesCubeExist(x + 1, y, z))
                        {
                            faces.Add(Vector3.right);
                        }
                        if (!DoesCubeExist(x - 1, y, z))
                        {
                            faces.Add(Vector3.left);
                        }
                        if (!DoesCubeExist(x, y + 1, z))
                        {
                            faces.Add(Vector3.up);
                        }
                        if (!DoesCubeExist(x, y - 1, z))
                        {
                            faces.Add(Vector3.down);
                        }

                        var colorValue   = modelData[x, y, z];
                        int paletteWidth = ColorPaletteManager.GetPaletteWidth();
                        int uvY          = Mathf.FloorToInt(colorValue / (float)paletteWidth);

                        meshManipulation.AddCubeQuads(new Vector3(x, y, z), faces.ToArray(), colorValue % paletteWidth, uvY);
                    }
                }
            }

            mesh.vertices = meshManipulation.GetVerticesArray();
            mesh.SetTriangles(meshManipulation.GetTrianglesArray(), 0);
            mesh.uv = meshManipulation.GetUVsArray();

            mesh.RecalculateNormals();
            mesh.RecalculateBounds();

            meshCollider.enabled = false;
            meshCollider.enabled = true;
        }
Ejemplo n.º 6
0
        public void AddQuad(Vector3 origin, Vector3 normal, int uvTileX, int uvTileY)
        {
            int count = vertices.Count;

            if (normal == Vector3.forward)
            {
                vertices.Add(origin + new Vector3(0, 0, 0));
                vertices.Add(origin + new Vector3(1, 0, 0));
                vertices.Add(origin + new Vector3(1, 1, 0));
                vertices.Add(origin + new Vector3(0, 1, 0));

                AddTriangle(count, 2, 0, 1);
                AddTriangle(count, 2, 3, 0);
            }
            else if (normal == Vector3.back)
            {
                vertices.Add(origin + new Vector3(0, 0, 0));
                vertices.Add(origin + new Vector3(1, 0, 0));
                vertices.Add(origin + new Vector3(1, 1, 0));
                vertices.Add(origin + new Vector3(0, 1, 0));

                AddTriangle(count, 2, 1, 0);
                AddTriangle(count, 2, 0, 3);
            }
            else if (normal == Vector3.right)
            {
                vertices.Add(origin + new Vector3(0, 0, 0));
                vertices.Add(origin + new Vector3(0, 0, 1));
                vertices.Add(origin + new Vector3(0, 1, 1));
                vertices.Add(origin + new Vector3(0, 1, 0));

                AddTriangle(count, 3, 1, 0);
                AddTriangle(count, 2, 1, 3);
            }
            else if (normal == Vector3.left)
            {
                vertices.Add(origin + new Vector3(0, 0, 0));
                vertices.Add(origin + new Vector3(0, 0, 1));
                vertices.Add(origin + new Vector3(0, 1, 1));
                vertices.Add(origin + new Vector3(0, 1, 0));

                AddTriangle(count, 3, 0, 1);
                AddTriangle(count, 2, 3, 1);
            }
            else if (normal == Vector3.up)
            {
                vertices.Add(origin + new Vector3(0, 0, 0));
                vertices.Add(origin + new Vector3(1, 0, 0));
                vertices.Add(origin + new Vector3(1, 0, 1));
                vertices.Add(origin + new Vector3(0, 0, 1));

                AddTriangle(count, 0, 2, 1);
                AddTriangle(count, 0, 3, 2);
            }
            else if (normal == Vector3.down)
            {
                vertices.Add(origin + new Vector3(0, 0, 0));
                vertices.Add(origin + new Vector3(1, 0, 0));
                vertices.Add(origin + new Vector3(1, 0, 1));
                vertices.Add(origin + new Vector3(0, 0, 1));

                AddTriangle(count, 0, 1, 2);
                AddTriangle(count, 0, 2, 3);
            }

            float materialTextureWidth  = ColorPaletteManager.GetPaletteWidth();
            float materialTextureHeight = ColorPaletteManager.GetPaletteHeight();

            uvs.Add(new Vector2(uvTileX / materialTextureWidth, uvTileY / materialTextureHeight));
            uvs.Add(new Vector2(uvTileX / materialTextureWidth, uvTileY / materialTextureHeight));
            uvs.Add(new Vector2((uvTileX + 1) / materialTextureWidth, (uvTileY + 1) / materialTextureHeight));
            uvs.Add(new Vector2((uvTileX + 1) / materialTextureWidth, (uvTileY + 1) / materialTextureHeight));
        }
Ejemplo n.º 7
0
 public int GetAsIndex()
 {
     return(x + y * ColorPaletteManager.GetPaletteWidth());
 }