Esempio n. 1
0
    private void DrawPalette()
    {
        Color = new byte[rows, columns];
        Image = new GameObject("Image");
        Image.transform.parent = gameObject.transform;
        Image.AddComponent <RectTransform>();
        Image.AddComponent <Image>();
        Image.GetComponent <RectTransform>().anchorMax        = new Vector2(1, 1);
        Image.GetComponent <RectTransform>().anchorMin        = new Vector2(1, 1);
        Image.GetComponent <RectTransform>().anchoredPosition = new Vector2(-128 / 2, -GetComponent <RectTransform>().rect.height / 2);
        Image.GetComponent <RectTransform>().sizeDelta        = new Vector2(GetComponent <RectTransform>().rect.width,
                                                                            GetComponent <RectTransform>().rect.height);

        Texture2D texture = new Texture2D(rows, columns);

        for (int index = 0; index < 256; index++)
        {
            int y = index % columns;
            int x = (index - y) / columns;
            texture.SetPixel(x, y, UColor.ByteToColor((byte)index));
            Color[x, columns - y - 1] = (byte)index;
        }
        texture.Apply(false);
        texture.filterMode = FilterMode.Point;

        Image.GetComponent <Image>().sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f), 1f);
        UColor.CurrentColor = Color[UColor.x, UColor.y];
    }
        private Color32 GetGroundColor()
        {
            Color32    color = new Color32();
            RaycastHit hit;

            if (Physics.SphereCast(transform.position, characterController.radius,
                                   transform.TransformDirection(Vector3.down), out hit, characterController.height / 3))
            {
                Vector3 position = hit.point;
                position += hit.normal * 0.5f;

                int x = Mathf.RoundToInt(position.x);
                int y = Mathf.RoundToInt(position.y);
                int z = Mathf.RoundToInt(position.z);

                color = UColor.ByteToColor(volume.GetComponent <MVolume>().volume.Voxel[x, y, z]);

                return(color);
            }
            return(color);
        }
Esempio n. 3
0
        private static void AddFace(int x, int y, int z, int index, byte inputColor, byte faceId,
                                    uint block01, uint block02, uint block03,
                                    uint block04, uint block05, uint block06,
                                    uint block07, uint block08, uint block09,
                                    uint block11, uint block12, uint block13,
                                    uint block14, uint block16,
                                    uint block17, uint block18, uint block19,
                                    uint block21, uint block22, uint block23,
                                    uint block24, uint block25, uint block26,
                                    uint block27, uint block28, uint block29,
                                    Vector3[] vertices, int[] triangles, Color32[] colors32)
        {
            float ao1F    = 3.5f;  //3.5  // 4
            float ao2F    = 1.5f;  //1.5  //1.2
            float lightX  = 0.20f; //0.20f   /0.15f
            float lightPy = 0;
            float lightNy = 0.5f;  //0.5f  //0.35f
            float lightZ  = 0.25f; //0.25f  //0.2


            bool    flipped = false;
            uint    empty   = 0x00000000;
            Color32 color   = UColor.ByteToColor(inputColor);

            index = index * 4;

            float xScale = x / Scale;
            float yScale = y / Scale;
            float zScale = z / Scale;
            float i      = 1 / Scale;


            if (faceId == 1)
            {
                color = new Color32((byte)(color.r - color.r * lightX), (byte)(color.g - color.g * lightX),
                                    (byte)(color.b - color.b * lightX), 255);
                Color32 ao1 = CalculateAo(color.r, color.g, color.b, ao1F);
                Color32 ao2 = CalculateAo(color.r, color.g, color.b, ao2F);

                if (block19 != empty && block06 != empty)
                {
                    vertices[index] = new Vector3(xScale + i, yScale - i, zScale); //1
                    colors32[index] = ao2;
                    flipped         = true;
                }
                else if (block19 != empty)
                {
                    vertices[index] = new Vector3(xScale + i, yScale - i, zScale); //1
                    colors32[index] = ao1;
                }
                else if (block06 != empty)
                {
                    vertices[index] = new Vector3(xScale + i, yScale - i, zScale); //1
                    colors32[index] = ao1;
                }
                else if (block09 != empty)
                {
                    vertices[index] = new Vector3(xScale + i, yScale - i, zScale); //1
                    colors32[index] = ao1;
                    flipped         = true;
                }
                else
                {
                    vertices[index] = new Vector3(xScale + i, yScale - i, zScale); //1
                    colors32[index] = color;
                }

                if (block19 != empty && block26 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale + i, yScale, zScale); //2
                    colors32[index + 1] = ao2;
                }
                else if (block19 != empty)
                {
                    vertices[index + 1] = new Vector3(xScale + i, yScale, zScale); //2
                    colors32[index + 1] = ao1;
                }
                else if (block26 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale + i, yScale, zScale); //2
                    colors32[index + 1] = ao1;
                }
                else if (block29 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale + i, yScale, zScale); //2
                    colors32[index + 1] = ao1;
                }
                else
                {
                    vertices[index + 1] = new Vector3(xScale + i, yScale, zScale); //2
                    colors32[index + 1] = color;
                }

                if (block13 != empty && block26 != empty)
                {
                    vertices[index + 2] =
                        new Vector3(xScale + i, yScale, zScale + i); //3
                    colors32[index + 2] = ao2;
                    flipped             = true;
                }
                else if (block13 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale, zScale + i); //3
                    colors32[index + 2] = ao1;
                }
                else if (block26 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale, zScale + i); //3
                    colors32[index + 2] = ao1;
                }
                else if (block23 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale, zScale + i); //3
                    colors32[index + 2] = ao1;
                    flipped             = true;
                }
                else
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale, zScale + i); //3
                    colors32[index + 2] = color;
                }

                if (block13 != empty && block06 != empty)
                {
                    vertices[index + 3] =
                        new Vector3(xScale + i, yScale - i, zScale + i); //4
                    colors32[index + 3] = ao2;
                }
                else if (block13 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale + i, yScale - i, zScale + i); //4
                    colors32[index + 3] = ao1;
                }
                else if (block06 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale + i, yScale - i, zScale + i); //4
                    colors32[index + 3] = ao1;
                }
                else if (block03 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale + i, yScale - i, zScale + i); //4
                    colors32[index + 3] = ao1;
                }
                else
                {
                    vertices[index + 3] = new Vector3(xScale + i, yScale - i, zScale + i); //4
                    colors32[index + 3] = color;
                }
            }

            if (faceId == 2)
            {
                color = new Color32((byte)(color.r - color.r * lightX), (byte)(color.g - color.g * lightX),
                                    (byte)(color.b - color.b * lightX), 255);
                Color32 ao1 = CalculateAo(color.r, color.g, color.b, ao1F);
                Color32 ao2 = CalculateAo(color.r, color.g, color.b, ao2F);

                if (block11 != empty && block04 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale - i, zScale + i); //1
                    colors32[index] = ao2;
                    flipped         = true;
                }
                else if (block11 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale - i, zScale + i); //1
                    colors32[index] = ao1;
                }
                else if (block04 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale - i, zScale + i); //1
                    colors32[index] = ao1;
                }
                else if (block01 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale - i, zScale + i); //1
                    colors32[index] = ao1;
                    flipped         = true;
                }
                else
                {
                    vertices[index] = new Vector3(xScale, yScale - i, zScale + i); //1
                    colors32[index] = color;
                }

                if (block11 != empty && block24 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale, yScale, zScale + i); //2
                    colors32[index + 1] = ao2;
                }
                else if (block11 != empty)
                {
                    vertices[index + 1] = new Vector3(xScale, yScale, zScale + i); //2
                    colors32[index + 1] = ao1;
                }
                else if (block24 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale, yScale, zScale + i); //2
                    colors32[index + 1] = ao1;
                }
                else if (block21 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale, yScale, zScale + i); //2
                    colors32[index + 1] = ao1;
                }
                else
                {
                    vertices[index + 1] = new Vector3(xScale, yScale, zScale + i); //2
                    colors32[index + 1] = color;
                }

                if (block17 != empty && block24 != empty)
                {
                    vertices[index + 2] =
                        new Vector3(xScale, yScale, zScale); //3
                    colors32[index + 2] = ao2;
                    flipped             = true;
                }
                else if (block17 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale, yScale, zScale); //3
                    colors32[index + 2] = ao1;
                }
                else if (block24 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale, yScale, zScale); //3
                    colors32[index + 2] = ao1;
                }
                else if (block27 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale, yScale, zScale); //3
                    colors32[index + 2] = ao1;
                    flipped             = true;
                }
                else
                {
                    vertices[index + 2] = new Vector3(xScale, yScale, zScale); //3
                    colors32[index + 2] = color;
                }

                if (block17 != empty && block04 != empty)
                {
                    vertices[index + 3] =
                        new Vector3(xScale, yScale - i, zScale); //4
                    colors32[index + 3] = ao2;
                }
                else if (block17 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale, yScale - i, zScale); //4
                    colors32[index + 3] = ao1;
                }
                else if (block04 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale, yScale - i, zScale); //4
                    colors32[index + 3] = ao1;
                }
                else if (block07 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale, yScale - i, zScale); //4
                    colors32[index + 3] = ao1;
                }
                else
                {
                    vertices[index + 3] = new Vector3(xScale, yScale - i, zScale); //4
                    colors32[index + 3] = color;
                }
            }

            if (faceId == 3)
            {
                color = new Color32((byte)(color.r - color.r * lightPy), (byte)(color.g - color.g * lightPy),
                                    (byte)(color.b - color.b * lightPy), 255);
                Color32 ao1 = CalculateAo(color.r, color.g, color.b, ao1F);
                Color32 ao2 = CalculateAo(color.r, color.g, color.b, ao2F);

                if (block22 != empty && block24 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale, zScale + i); //1
                    colors32[index] = ao2;
                    flipped         = true;
                }
                else if (block22 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale, zScale + i); //1
                    colors32[index] = ao1;
                }
                else if (block24 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale, zScale + i); //1
                    colors32[index] = ao1;
                }
                else if (block21 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale, zScale + i); //1
                    colors32[index] = ao1;
                    flipped         = true;
                }
                else
                {
                    vertices[index] = new Vector3(xScale, yScale, zScale + i); //1
                    colors32[index] = color;
                }

                if (block22 != empty && block26 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale + i, yScale, zScale + i); //2
                    colors32[index + 1] = ao2;
                }
                else if (block22 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale + i, yScale, zScale + i); //2
                    colors32[index + 1] = ao1;
                }
                else if (block26 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale + i, yScale, zScale + i); //2
                    colors32[index + 1] = ao1;
                }
                else if (block23 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale + i, yScale, zScale + i); //2
                    colors32[index + 1] = ao1;
                }
                else
                {
                    vertices[index + 1] = new Vector3(xScale + i, yScale, zScale + i); //2
                    colors32[index + 1] = color;
                }

                if (block28 != empty && block26 != empty)
                {
                    vertices[index + 2] =
                        new Vector3(xScale + i, yScale, zScale); //3
                    colors32[index + 2] = ao2;
                    flipped             = true;
                }
                else if (block28 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale, zScale); //3
                    colors32[index + 2] = ao1;
                }
                else if (block26 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale, zScale); //3
                    colors32[index + 2] = ao1;
                }
                else if (block29 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale, zScale); //3
                    colors32[index + 2] = ao1;
                    flipped             = true;
                }
                else
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale, zScale); //3
                    colors32[index + 2] = color;
                }

                if (block28 != empty && block24 != empty)
                {
                    vertices[index + 3] =
                        new Vector3(xScale, yScale, zScale); //4
                    colors32[index + 3] = ao2;
                }
                else if (block28 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale, yScale, zScale); //4
                    colors32[index + 3] = ao1;
                }
                else if (block24 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale, yScale, zScale); //4
                    colors32[index + 3] = ao1;
                }
                else if (block27 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale, yScale, zScale); //4
                    colors32[index + 3] = ao1;
                }
                else
                {
                    vertices[index + 3] = new Vector3(xScale, yScale, zScale); //4
                    colors32[index + 3] = color;
                }
            }

            if (faceId == 4)
            {
                color = new Color32((byte)(color.r - color.r * lightNy), (byte)(color.g - color.g * lightNy),
                                    (byte)(color.b - color.b * lightNy), 255);
                Color32 ao1 = CalculateAo(color.r, color.g, color.b, ao1F);
                Color32 ao2 = CalculateAo(color.r, color.g, color.b, ao2F);

                if (block08 != empty && block04 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale - i, zScale); //1
                    colors32[index] = ao2;
                    flipped         = true;
                }
                else if (block08 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale - i, zScale); //1
                    colors32[index] = ao1;
                }
                else if (block04 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale - i, zScale); //1
                    colors32[index] = ao1;
                }
                else if (block07 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale - i, zScale); //1
                    colors32[index] = ao1;
                    flipped         = true;
                }
                else
                {
                    vertices[index] = new Vector3(xScale, yScale - i, zScale); //1
                    colors32[index] = color;
                }

                if (block08 != empty && block06 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale + i, yScale - i, zScale); //2
                    colors32[index + 1] = ao2;
                }
                else if (block08 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale + i, yScale - i, zScale); //2
                    colors32[index + 1] = ao1;
                }
                else if (block06 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale + i, yScale - i, zScale); //2
                    colors32[index + 1] = ao1;
                }
                else if (block09 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale + i, yScale - i, zScale); //2
                    colors32[index + 1] = ao1;
                }
                else
                {
                    vertices[index + 1] = new Vector3(xScale + i, yScale - i, zScale); //2
                    colors32[index + 1] = color;
                }

                if (block02 != empty && block06 != empty)
                {
                    vertices[index + 2] =
                        new Vector3(xScale + i, yScale - i, zScale + i); //3
                    colors32[index + 2] = ao2;
                    flipped             = true;
                }
                else if (block02 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale - i, zScale + i); //3
                    colors32[index + 2] = ao1;
                }
                else if (block06 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale - i, zScale + i); //3
                    colors32[index + 2] = ao1;
                }
                else if (block03 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale - i, zScale + i); //3
                    colors32[index + 2] = ao1;
                    flipped             = true;
                }
                else
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale - i, zScale + i); //3
                    colors32[index + 2] = color;
                }

                if (block02 != empty && block04 != empty)
                {
                    vertices[index + 3] =
                        new Vector3(xScale, yScale - i, zScale + i); // 4
                    colors32[index + 3] = ao2;
                }
                else if (block02 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale, yScale - i, zScale + i); // 4
                    colors32[index + 3] = ao1;
                }
                else if (block04 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale, yScale - i, zScale + i); // 4
                    colors32[index + 3] = ao1;
                }
                else if (block01 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale, yScale - i, zScale + i); // 4
                    colors32[index + 3] = ao1;
                }
                else
                {
                    vertices[index + 3] = new Vector3(xScale, yScale - i, zScale + i); // 4
                    colors32[index + 3] = color;
                }
            }

            if (faceId == 5)
            {
                color = new Color32((byte)(color.r - color.r * lightZ), (byte)(color.g - color.g * lightZ),
                                    (byte)(color.b - color.b * lightZ), 255);
                Color32 ao1 = CalculateAo(color.r, color.g, color.b, ao1F);
                Color32 ao2 = CalculateAo(color.r, color.g, color.b, ao2F);

                if (block13 != empty && block02 != empty)
                {
                    vertices[index] = new Vector3(xScale + i, yScale - i, zScale + i); //1
                    colors32[index] = ao2;
                    flipped         = true;
                }
                else if (block13 != empty)
                {
                    vertices[index] = new Vector3(xScale + i, yScale - i, zScale + i); //1
                    colors32[index] = ao1;
                }
                else if (block02 != empty)
                {
                    vertices[index] = new Vector3(xScale + i, yScale - i, zScale + i); //1
                    colors32[index] = ao1;
                }
                else if (block03 != empty)
                {
                    vertices[index] = new Vector3(xScale + i, yScale - i, zScale + i); //1
                    colors32[index] = ao1;
                    flipped         = true;
                }
                else
                {
                    vertices[index] = new Vector3(xScale + i, yScale - i, zScale + i); //1
                    colors32[index] = color;
                }

                if (block13 != empty && block22 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale + i, yScale, zScale + i); //2
                    colors32[index + 1] = ao2;
                }
                else if (block13 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale + i, yScale, zScale + i); //2
                    colors32[index + 1] = ao1;
                }
                else if (block22 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale + i, yScale, zScale + i); //2
                    colors32[index + 1] = ao1;
                }
                else if (block23 != empty)
                {
                    vertices[index + 1] =
                        new Vector3(xScale + i, yScale, zScale + i); //2
                    colors32[index + 1] = ao1;
                }
                else
                {
                    vertices[index + 1] = new Vector3(xScale + i, yScale, zScale + i); //2
                    colors32[index + 1] = color;
                }

                if (block11 != empty && block22 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale, yScale, zScale + i); //3
                    colors32[index + 2] = ao2;
                    flipped             = true;
                }
                else if (block11 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale, yScale, zScale + i); //3
                    colors32[index + 2] = ao1;
                }
                else if (block22 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale, yScale, zScale + i); //3
                    colors32[index + 2] = ao1;
                }
                else if (block21 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale, yScale, zScale + i); //3
                    colors32[index + 2] = ao1;
                    flipped             = true;
                }
                else
                {
                    vertices[index + 2] = new Vector3(xScale, yScale, zScale + i); //3
                    colors32[index + 2] = color;
                }

                if (block11 != empty && block02 != empty)
                {
                    vertices[index + 3] =
                        new Vector3(xScale, yScale - i, zScale + i); //4
                    colors32[index + 3] = ao2;
                }
                else if (block11 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale, yScale - i, zScale + i); //4
                    colors32[index + 3] = ao1;
                }
                else if (block02 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale, yScale - i, zScale + i); //4
                    colors32[index + 3] = ao1;
                }
                else if (block01 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale, yScale - i, zScale + i); //4
                    colors32[index + 3] = ao1;
                }
                else
                {
                    vertices[index + 3] = new Vector3(xScale, yScale - i, zScale + i); //4
                    colors32[index + 3] = color;
                }
            }

            if (faceId == 6)
            {
                color = new Color32((byte)(color.r - color.r * lightZ), (byte)(color.g - color.g * lightZ),
                                    (byte)(color.b - color.b * lightZ), 255);
                Color32 ao1 = CalculateAo(color.r, color.g, color.b, ao1F);
                Color32 ao2 = CalculateAo(color.r, color.g, color.b, ao2F);

                if (block17 != empty && block08 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale - i, zScale); //1
                    colors32[index] = ao2;
                    flipped         = true;
                }
                else if (block17 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale - i, zScale); //1
                    colors32[index] = ao1;
                }
                else if (block08 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale - i, zScale); //1
                    colors32[index] = ao1;
                }
                else if (block07 != empty)
                {
                    vertices[index] = new Vector3(xScale, yScale - i, zScale); //1
                    colors32[index] = ao1;
                    flipped         = true;
                }
                else
                {
                    vertices[index] = new Vector3(xScale, yScale - i, zScale); //1
                    colors32[index] = color;
                }

                if (block17 != empty && block28 != empty)
                {
                    vertices[index + 1] = new Vector3(xScale, yScale, zScale); //2
                    colors32[index + 1] = ao2;
                }
                else if (block17 != empty)
                {
                    vertices[index + 1] = new Vector3(xScale, yScale, zScale); //2
                    colors32[index + 1] = ao1;
                }
                else if (block28 != empty)
                {
                    vertices[index + 1] = new Vector3(xScale, yScale, zScale); //2
                    colors32[index + 1] = ao1;
                }
                else if (block27 != empty)
                {
                    vertices[index + 1] = new Vector3(xScale, yScale, zScale); //2
                    colors32[index + 1] = ao1;
                }
                else
                {
                    vertices[index + 1] = new Vector3(xScale, yScale, zScale); //2
                    colors32[index + 1] = color;
                }

                if (block19 != empty && block28 != empty)
                {
                    vertices[index + 2] =
                        new Vector3(xScale + i, yScale, zScale); //3
                    colors32[index + 2] = ao2;
                    flipped             = true;
                }
                else if (block19 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale, zScale); //3
                    colors32[index + 2] = ao1;
                }
                else if (block28 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale, zScale); //3
                    colors32[index + 2] = ao1;
                }
                else if (block29 != empty)
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale, zScale); //3
                    colors32[index + 2] = ao1;
                    flipped             = true;
                }
                else
                {
                    vertices[index + 2] = new Vector3(xScale + i, yScale, zScale); //3
                    colors32[index + 2] = color;
                }

                if (block19 != empty && block08 != empty)
                {
                    vertices[index + 3] =
                        new Vector3(xScale + i, yScale - i, zScale); //4
                    colors32[index + 3] = ao2;
                }
                else if (block19 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale + i, yScale - i, zScale); //4
                    colors32[index + 3] = ao1;
                }
                else if (block08 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale + i, yScale - i, zScale); //4
                    colors32[index + 3] = ao1;
                }
                else if (block09 != empty)
                {
                    vertices[index + 3] = new Vector3(xScale + i, yScale - i, zScale); //4
                    colors32[index + 3] = ao1;
                }
                else
                {
                    vertices[index + 3] = new Vector3(xScale + i, yScale - i, zScale); //4
                    colors32[index + 3] = color;
                }
            }

            int face = index / 4;

            index = index / 4 * 6;
            if (!flipped)
            {
                triangles[index]     = face * 4;
                triangles[index + 1] = face * 4 + 1;
                triangles[index + 2] = face * 4 + 2;
                triangles[index + 3] = face * 4;
                triangles[index + 4] = face * 4 + 2;
                triangles[index + 5] = face * 4 + 3;
            }
            else
            {
                triangles[index]     = face * 4 + 3;
                triangles[index + 1] = face * 4;
                triangles[index + 2] = face * 4 + 1;
                triangles[index + 3] = face * 4 + 3;
                triangles[index + 4] = face * 4 + 1;
                triangles[index + 5] = face * 4 + 2;
            }

            flipped = false;
        }
Esempio n. 4
0
        private const float Scale = 1f; // Scale of the mesh

        public static Mesh Get(int chunkX, int chunkZ, Volume volume)
        {
            _cx = chunkX * 16;
            _cz = chunkZ * 16;

            var vertices  = new List <Vector3>();
            var colors32  = new List <Color32>();
            var triangles = new List <int>();
            var uv        = new List <Vector2>();

            var dimensions = new [] { ChunkSize.X, ChunkSize.Y, ChunkSize.Z }; // Size of each axis

            for (var d = 0; d < 3; d++)                                        // Iterate for x, y, z axis: 0 = x; 1 = y; 2 = z;
            {
                var u         = (d + 1) % 3;
                var v         = (d + 2) % 3;
                var x         = new int[3];
                var q         = new int[3]; q[d] = 1;
                var mask      = new int[dimensions[u] * dimensions[v]];
                var colorMask = new byte[dimensions[u] * dimensions[v]];

                for (x[d] = -1; x[d] < dimensions[d];) // Do something 16 times
                {
                    var n = 0;
                    for (x[v] = 0; x[v] < dimensions[v]; ++x[v])          // x
                    {
                        for (x[u] = 0; x[u] < dimensions[u]; ++x[u], ++n) // y
                        {
                            var current = (x[d] >= 0 ? Data(x[0], x[1], x[2], volume) : 0);
                            var next    = (x[d] < dimensions[d] - 1 ? Data(x[0] + q[0], x[1] + q[1], x[2] + q[2], volume) : 0);
                            if (current != next)
                            {
                                if (current > 0 && next > 0)
                                {
                                    mask[n]      = 0;
                                    colorMask[n] = 0;
                                }
                                else if (current == 0)
                                {
                                    mask[n]      = -1;
                                    colorMask[n] = (byte)next;
                                }
                                else if (next == 0)
                                {
                                    mask[n]      = 1;
                                    colorMask[n] = (byte)current;
                                }
                                else
                                {
                                    mask[n]      = 0;
                                    colorMask[n] = 0;
                                }
                            }
                            else
                            {
                                mask[n]      = 0;
                                colorMask[n] = 0;
                            }
                        }
                    } // End.

                    x[d]++;
                    n = 0;
                    for (var j = 0; j < dimensions[v]; ++j)
                    {
                        for (var i = 0; i < dimensions[u];)
                        {
                            var maskValue  = mask[n];
                            var colorValue = colorMask[n];
                            if (maskValue != 0)
                            {
                                int l, k;

                                var w = 1;
                                for (;
                                     n + w < mask.Length && mask[n + w] == maskValue && colorValue == colorMask[n + w] &&
                                     i + w < dimensions[u];
                                     ++w)
                                {
                                }

                                var h    = 1;
                                var done = false;
                                for (; j + h < dimensions[v]; ++h)
                                {
                                    for (k = 0; k < w; ++k)
                                    {
                                        if (mask[n + k + h * dimensions[u]] != maskValue ||
                                            colorMask[n + k + h * dimensions[u]] != colorValue)
                                        {
                                            done = true;
                                            break;
                                        }
                                    }

                                    if (done)
                                    {
                                        break;
                                    }
                                }

                                var xp = new Vector3();
                                xp[u] = i;
                                xp[v] = j;
                                xp[d] = x[d];
                                xp   *= Scale;
                                var du = new Vector3();
                                du[u] = w * Scale;
                                var dv = new Vector3();
                                dv[v] = h * Scale;
                                AddFace(
                                    new Vector3(xp[0], xp[1], xp[2]),
                                    new Vector3(xp[0] + du[0], xp[1] + du[1], xp[2] + du[2]),
                                    new Vector3(xp[0] + du[0] + dv[0], xp[1] + du[1] + dv[1], xp[2] + du[2] + dv[2]),
                                    new Vector3(xp[0] + dv[0], xp[1] + dv[1], xp[2] + dv[2]),
                                    UColor.ByteToColor(colorValue), d, maskValue < 0,
                                    vertices, colors32, triangles, uv);
                                for (l = 0; l < h; ++l)
                                {
                                    for (k = 0; k < w; ++k)
                                    {
                                        mask[(n + k) + l * dimensions[u]]      = 0;
                                        colorMask[(n + k) + l * dimensions[u]] = 0;
                                    }
                                }

                                i += w;
                                n += w;
                            }
                            else
                            {
                                ++i;
                                ++n;
                            }
                        }
                    }
                }
            }

            return(new Mesh
            {
                vertices = vertices.ToArray(),
                triangles = triangles.ToArray(),
                colors32 = colors32.ToArray()
            });
        }
Esempio n. 5
0
    public static Mesh Get(string name, byte team)
    {
        float Scale = 1 / 15f;

        Load(name);

        var vertices  = new List <Vector3>();
        var colors32  = new List <Color32>();
        var triangles = new List <int>();
        var uv        = new List <Vector2>();

        var dimensions = new[] { Width, Height, Depth }; // Size of each axis

        for (var d = 0; d < 3; d++)                      // Iterate for x, y, z axis: 0 = x; 1 = y; 2 = z;
        {
            var u         = (d + 1) % 3;
            var v         = (d + 2) % 3;
            var x         = new int[3];
            var q         = new int[3]; q[d] = 1;
            var mask      = new int[dimensions[u] * dimensions[v]];
            var colorMask = new byte[dimensions[u] * dimensions[v]];

            for (x[d] = -1; x[d] < dimensions[d];) // Do something 16 times
            {
                var n = 0;
                for (x[v] = 0; x[v] < dimensions[v]; ++x[v])          // x
                {
                    for (x[u] = 0; x[u] < dimensions[u]; ++x[u], ++n) // y
                    {
                        var current = (x[d] >= 0 ? Data(x[0], x[1], x[2]) : 0);
                        var next    = (x[d] < dimensions[d] - 1 ? Data(x[0] + q[0], x[1] + q[1], x[2] + q[2]) : 0);
                        if (current != next)
                        {
                            if (current > 0 && next > 0)
                            {
                                mask[n]      = 0;
                                colorMask[n] = 0;
                            }
                            else if (current == 0)
                            {
                                mask[n]      = -1;
                                colorMask[n] = (byte)next;
                            }
                            else if (next == 0)
                            {
                                mask[n]      = 1;
                                colorMask[n] = (byte)current;
                            }
                            else
                            {
                                mask[n]      = 0;
                                colorMask[n] = 0;
                            }
                        }
                        else
                        {
                            mask[n]      = 0;
                            colorMask[n] = 0;
                        }
                    }
                } // End.

                x[d]++;
                n = 0;
                for (var j = 0; j < dimensions[v]; ++j)
                {
                    for (var i = 0; i < dimensions[u];)
                    {
                        var maskValue  = mask[n];
                        var colorValue = colorMask[n];
                        if (maskValue != 0)
                        {
                            int l, k;

                            var w = 1;
                            for (;
                                 n + w < mask.Length && mask[n + w] == maskValue && colorValue == colorMask[n + w] &&
                                 i + w < dimensions[u];
                                 ++w)
                            {
                            }

                            var h    = 1;
                            var done = false;
                            for (; j + h < dimensions[v]; ++h)
                            {
                                for (k = 0; k < w; ++k)
                                {
                                    if (mask[n + k + h * dimensions[u]] != maskValue ||
                                        colorMask[n + k + h * dimensions[u]] != colorValue)
                                    {
                                        done = true;
                                        break;
                                    }
                                }

                                if (done)
                                {
                                    break;
                                }
                            }

                            var xp = new Vector3();
                            xp[u] = i;
                            xp[v] = j;
                            xp[d] = x[d];
                            xp   *= Scale;
                            var du = new Vector3();
                            du[u] = w * Scale;
                            var dv = new Vector3();
                            dv[v] = h * Scale;

                            if (colorValue == 227) // Light
                            {
                                //if (team == 0) colorValue = UColor.LightDefault;
                                //else if (team == 1) colorValue = UColor.LightGreen;
                                //else if (team == 2) colorValue = UColor.LightBlue;
                            }

                            else if (colorValue == 231) // Dark
                            {
                                //if (team == 0) colorValue = UColor.DarkDefault;
                                //else if (team == 1) colorValue = UColor.DarkGreen;
                                //else if (team == 2) colorValue = UColor.DarkBlue;
                            }

                            AddFace(
                                new Vector3(xp[0] - 0.5f, xp[1] - 0.5f, xp[2] - 0.5f),
                                new Vector3(xp[0] + du[0] - 0.5f, xp[1] + du[1] - 0.5f, xp[2] + du[2] - 0.5f),
                                new Vector3(xp[0] + du[0] + dv[0] - 0.5f, xp[1] + du[1] + dv[1] - 0.5f, xp[2] + du[2] + dv[2] - 0.5f),
                                new Vector3(xp[0] + dv[0] - 0.5f, xp[1] + dv[1] - 0.5f, xp[2] + dv[2] - 0.5f),
                                UColor.ByteToColor(colorValue), d, maskValue < 0,
                                vertices, colors32, triangles, uv);

                            for (l = 0; l < h; ++l)
                            {
                                for (k = 0; k < w; ++k)
                                {
                                    mask[(n + k) + l * dimensions[u]]      = 0;
                                    colorMask[(n + k) + l * dimensions[u]] = 0;
                                }
                            }

                            i += w;
                            n += w;
                        }
                        else
                        {
                            ++i;
                            ++n;
                        }
                    }
                }
            }
        }

        return(new Mesh
        {
            vertices = vertices.ToArray(),
            triangles = triangles.ToArray(),
            colors32 = colors32.ToArray()
        });
    }