示例#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);
        }
示例#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;
        }
示例#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();
        }
示例#4
0
        private void Awake()
        {
            Singleton = this;

            var texture = currentPalette.texture;

            paletteWidth  = texture.width;
            paletteHeight = texture.height;

            HashSet <PaletteColor> uniqueColors = new HashSet <PaletteColor>();

            for (int y = 0; y < texture.height; y++)
            {
                for (int x = 0; x < texture.width; x++)
                {
                    var color = texture.GetPixel(x, y);
                    uniqueColors.Add(new PaletteColor(x, y, color.r, color.g, color.b));
                }
            }

            colors = uniqueColors.ToArray();
        }
示例#5
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;
            }
        }
示例#6
0
        private static void ExportObj(string path, Mesh mesh)
        {
            var meshName = Path.GetFileNameWithoutExtension(path).Replace(" ", "");

            Vector3 minVertex = Vector3.positiveInfinity;
            Vector3 maxVertex = Vector3.negativeInfinity;

            //we calculate the min and max vertices so that later on we can centerize the mesh
            foreach (var vertex in mesh.vertices)
            {
                if (vertex.x < minVertex.x)
                {
                    minVertex.x = vertex.x;
                }
                if (vertex.y < minVertex.y)
                {
                    minVertex.y = vertex.y;
                }
                if (vertex.z < minVertex.z)
                {
                    minVertex.z = vertex.z;
                }
                if (vertex.x > maxVertex.x)
                {
                    maxVertex.x = vertex.x;
                }
                if (vertex.y > maxVertex.y)
                {
                    maxVertex.y = vertex.y;
                }
                if (vertex.z > maxVertex.z)
                {
                    maxVertex.z = vertex.z;
                }
            }

            string data = "";

            data += $"mtllib ./{Path.GetFileNameWithoutExtension(path)}.mtl\n";
            data += $"o {meshName}\n";
            data += $"\n";
            data += $"g {meshName}\n";
            data += $"\n";

            List <Vector3> distinctVertices = new List <Vector3>();
            //a map which links the old vertice's positions to the new ones
            Dictionary <int, int> verticesMap = new Dictionary <int, int>();

            for (int i = 0; i < mesh.vertexCount; i++)
            {
                var vertex = mesh.vertices[i];

                int distinctIndex = distinctVertices.IndexOf(vertex);

                if (distinctIndex == -1)
                {
                    verticesMap[i] = distinctVertices.Count;
                    distinctVertices.Add(vertex);

                    data += $"v {vertex.x - (minVertex.x + maxVertex.x) / 2} {vertex.y - (minVertex.y + maxVertex.y) / 2} {vertex.z - (minVertex.z + maxVertex.z) / 2}\n";
                }
                else
                {
                    verticesMap[i] = distinctIndex;
                }
            }

            data += $"\n";

            List <Vector2>        distinctUvs = new List <Vector2>();
            Dictionary <int, int> uvsMap      = new Dictionary <int, int>();

            for (int i = 0; i < mesh.uv.Length; i++)
            {
                var uv = mesh.uv[i];

                int distinctIndex = distinctUvs.IndexOf(uv);

                if (distinctIndex == -1)
                {
                    uvsMap[i] = distinctUvs.Count;
                    distinctUvs.Add(uv);

                    data += $"vt {uv.x} {uv.y}\n";
                }
                else
                {
                    uvsMap[i] = distinctIndex;
                }
            }

            data += $"\n";

            List <Vector3>        distinctNormals = new List <Vector3>();
            Dictionary <int, int> normalsMap      = new Dictionary <int, int>();

            for (int i = 0; i < mesh.normals.Length; i++)
            {
                var normal = mesh.normals[i];

                int distinctIndex = distinctNormals.IndexOf(normal);

                if (distinctIndex == -1)
                {
                    normalsMap[i] = distinctNormals.Count;
                    distinctNormals.Add(normal);

                    data += $"vn {normal.x} {normal.y} {normal.z}\n";
                }
                else
                {
                    normalsMap[i] = distinctIndex;
                }
            }

            data += $"\n";
            data += $"g {meshName}\n";
            data += $"usemtl DefaultColorPalette\n";
            for (int i = 0; i < mesh.triangles.Length / 3; i++)
            {
                data += string.Format("f {0}/{3}/{6} {1}/{4}/{7} {2}/{5}/{8}\n",
                                      verticesMap[mesh.triangles[i * 3]] + 1, verticesMap[mesh.triangles[i * 3 + 1]] + 1, verticesMap[mesh.triangles[i * 3 + 2]] + 1,
                                      uvsMap[mesh.triangles[i * 3]] + 1, uvsMap[mesh.triangles[i * 3 + 1]] + 1, uvsMap[mesh.triangles[i * 3 + 2]] + 1,
                                      normalsMap[mesh.triangles[i * 3]] + 1, normalsMap[mesh.triangles[i * 3 + 1]] + 1, normalsMap[mesh.triangles[i * 3 + 2]] + 1);
            }

            File.WriteAllText(path, data);
            File.WriteAllText(Path.GetDirectoryName(path) + "/" + Path.GetFileNameWithoutExtension(path) + ".mtl", GetMaterialData());
            File.WriteAllBytes(Path.GetDirectoryName(path) + "/DefaultColorPalette.png", ((Texture2D)ColorPaletteManager.GetPaletteTexture()).EncodeToJPG());
        }
示例#7
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;
        }
示例#8
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));
        }
示例#9
0
 public int GetAsIndex()
 {
     return(x + y * ColorPaletteManager.GetPaletteWidth());
 }