Beispiel #1
0
    /// <summary>
    /// Uses linear interpolation to find the point between two <c>CubeCorner</c>
    /// where the value change is equal to <c>isoValue</c>
    /// </summary>
    /// <param name="a">The first <c>CubeCorner</c></param>
    /// <param name="b">The second <c>CubeCorner</c></param>
    /// <param name="isoValue">The value which represents a surface.</param>
    /// <returns>The linearly interpolated <c>Vector3</c></returns>
    public static Vector3 InterpolateVerts(CubeCorner a, CubeCorner b, float isoValue)
    {
        float t = (isoValue - a.value) / (b.value - a.value);

        t = Mathf.Clamp01(t);
        return(a.position + t * (b.position - a.position));
    }
Beispiel #2
0
    public List <CubeCorner> GetAdjacent()
    {
        var result = new List <CubeCorner>();

        for (int i = 0; i < 3; i++)
        {
            CubeCorner offset = _Adjacent[i + 3 * (int)_direction];
            result.Add(new CubeCorner(_hex + offset._hex, offset._direction));
        }
        return(result);
    }
Beispiel #3
0
    public List <CubeCorner> GetCorners()
    {
        var result = new List <CubeCorner>(6);

        for (int i = 0; i < 6; i++)
        {
            CubeCorner offset = _Corners[i];
            result.Add(new CubeCorner(this + offset.hex, offset.direction));
        }
        return(result);
    }
Beispiel #4
0
    public CubeTile(TileType pTileType, Coord pCPos, float pSize, Vector3 pWPos) :
        base(pTileType, pCPos, pSize, pWPos)
    {
        height = size;
        width  = size;

        corners[0] = new CubeCorner(this, 0, new Vector3(wPos.x - size / 2, wPos.y, wPos.z + size / 2));
        corners[1] = new CubeCorner(this, 1, new Vector3(wPos.x + size / 2, wPos.y, wPos.z + size / 2));
        corners[2] = new CubeCorner(this, 2, new Vector3(wPos.x + size / 2, wPos.y, wPos.z - size / 2));
        corners[3] = new CubeCorner(this, 3, new Vector3(wPos.x - size / 2, wPos.y, wPos.z - size / 2));
    }
Beispiel #5
0
 public override bool Equals(object obj)
 {
     if (obj == null || GetType() != obj.GetType())
     {
         return(false);
     }
     else
     {
         CubeCorner b = (CubeCorner)obj;
         return(this == b);
     }
 }
    void Start()
    {
        /*
         - We set the empty texture to the inputTex (this might create a copy by reference problem)
         - We set the renderer of the gameobject this script is attached to, to the texture (input texture)
         - We set the pix array of Colors to the texture of the texture (input texture)
        */

        texture = new Texture2D (inputTex.width, inputTex.height);

        Color[,] image = PreProcessing.Instance.GetPixels2D (inputTex);

        image = PreProcessing.Instance.Rgb2greyScale (image);

        image = PreProcessing.Instance.simpleBrightness (image, -30);

        image = PreProcessing.Instance.simpleContrast (image, 1.3f);

        image = PreProcessing.Instance.Rank (image, 3, 1);

        image = PreProcessing.Instance.Threshold (image, 0.8f);

        //PreProcessing.Instance.printSample (image, 300,50,10);

        //image = PreProcessing.Instance.Dilate (image);
        //image = PreProcessing.Instance.Dilate (image);
        //image = PreProcessing.Instance.Dilate (image);

        PreProcessing.Instance.SetPixels2D (image, texture);

        this.GetComponent<Renderer> ().material.mainTexture = texture;
        pix = texture.GetPixels ();

        /*
        - Both of our 2D arrays of integers take the height and width of the image pixels
        - The array of tags gets 10 pixels worth of padding, otherwise it will go out of bounds later
        */

        positionValue = new int[texture.width, texture.height];
        tags = new int[texture.width + 1, texture.height + 1];

        /*
        - We loop through the width and height of the image
        - We set the position values to the greyscale of the image, 0-255
        - We set all tags to 0
        */

        for (int i = 1; i < texture.width; i++) {
            for (int j = 1; j < texture.height; j++) {
                positionValue [i, j] = (int)(pix [i + (pix.Length / texture.height * j)].grayscale * 255);
                tags [i, j] = 0;

                /*
        - If a pixel is not white (some thresholding has to be done before this)
        */
                if (positionValue [i, j] < 255) {
                    /*
        - We loop through a 3*3 kernel, which is applied to the pixel which was not white
        */
                    for (int ky=-1; ky <= 1; ky++) {
                        for (int kx=-1; kx <= 1; kx++) {
                            /*
        - If any of the 9 pixels from the kernel has a tag
        */
                            if (tags [i + ky, j + kx] != 0) {
                                /*
        - We then tag our current non-white pixel with the same tag
        */
                                tags [i, j] = tags [i + ky, j + kx];
                            }
                        }
                    }

                    /*
        - If none of the neighbours has a tag, we create a new one by incrementing tagCheck
        - And we then tag our non-white pixel with the new tag
        */
                    if (tags [i, j] == 0) {

                        tags [i, j] = tagCheck;
                        tagCheck++;
                    }
                }
            }
        }

        /*
        - We set an array of CubeCorners to the size of TagCheck, which is the total amount of objects
        - We set the values of all corners to their theoretical maximum value, either 0 or the size of the input image
        */

        cubecorners = new CubeCorner[tagCheck];

        for (int k = 0; k < tagCheck; k++) {
            cubecorners[k] = new CubeCorner();
        }

        for (int k = 0; k < tagCheck; k++) {

            cubecorners [k].xmin = texture.width;
            cubecorners [k].xmax = 0;
            cubecorners [k].ymin = texture.height;
            cubecorners [k].ymax = 0;

        }

        /*
        - Loop through all the pixels
        - Loop through the cube-objects
        */

        for (int i = 1; i < texture.width; i++) {
            for (int j = 1; j < texture.height; j++) {
                for (int k = 1; k < cubecorners.Length; k++) {

                    /*
        - I ask - If there is something tagged as k (0 -> amount of objects),
        - then set the corresponding cubecorner (k) values:
        - if the i or j is lower or higher than the min or max of the object

        - This will create values for all four corner of a a rectangle of any detected object
        - Ready to be created in to a cube later on
        */
                    if (tags [i, j] == k && i < cubecorners [k].xmin) {
                        cubecorners [k].xmin = i;
                    }
                    if (tags [i, j] == k && i > cubecorners [k].xmax) {
                        cubecorners [k].xmax = i;
                    }
                    if (tags [i, j] == k && j < cubecorners [k].ymin) {
                        cubecorners [k].ymin = j;
                    }
                    if (tags [i, j] == k && j > cubecorners [k].ymax) {
                        cubecorners [k].ymax = j;
                    }
                }
            }
        }

        /*for (int i = 1; i < texture.width; i++) {
            for (int j = 1; j < texture.height; j++) {
                for (int k = 1; k < cubecorners.Length; k++) {

                    }
                    if (tags [i, j] == k && j < cubecorners [k].ymin) {
                        cubecorners [k].ymin = j;
                    }
                    if (tags [i, j] == k && j > cubecorners [k].ymax) {
                        cubecorners [k].ymax = j;
                    }
                }
            }
        }*/

        /*
         *This is a test
         *to check the numbers of all the cubes
         */
        for (int k = 1; k < cubecorners.Length; k++) {
            print ("Cube " + k + " xmin value is: " + cubecorners [k].xmin);
            print ("Cube " + k + " xmax value is: " + cubecorners [k].xmax);
            print ("Cube " + k + " ymin value is: " + cubecorners [k].ymin);
            print ("Cube " + k + " ymax value is: " + cubecorners [k].ymax);

            //starting to create cubes

            GameObject cube = GameObject.CreatePrimitive (PrimitiveType.Cube);

            cube.transform.position = new Vector3 ((cubecorners [k].xmax + cubecorners [k].xmin) / 2, 0, (cubecorners [k].ymax + cubecorners [k].ymin) / 2);
            cube.transform.localScale = new Vector3 (cubecorners [k].xmax - cubecorners [k].xmin, 1, cubecorners [k].ymax - cubecorners [k].ymin);
            cube.AddComponent<Rigidbody> ().useGravity = false;
            cube.GetComponent<Rigidbody> ().isKinematic = true;

            Color newColor = new Color ();
            newColor = new Color (Random.value, Random.value, Random.value, 1.0f);
            cube.GetComponent<Renderer> ().material.color = newColor;  //Will give cubes color after merging
            cube.tag = "Ground";

        }
    }