Example #1
0
    void GetHeightFromTexture(Texture2D texture)
    {
        Color[] pixels = texture.GetPixels();
        _heights = new float[pixels.Length / 4];
        //_xSize = texture.width-1;
        //_zSize = texture.height-1;
        int y   = 0;
        int row = 0;

        for (int i = 0; i < _heights.Length; i++)
        {
            if (row == _heights.Length)
            {
                row = 0;
                y  += 4;
            }
            int   x     = row * 4;
            Color pixel = texture.GetPixel(x, y);
            _heights[i] = pixels[i].grayscale * _precision;
            row++;
        }


        GetComponent <MeshFilter>().mesh = MeshGeneratorTypeTwo.Plane(new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1), 128, 128);
        var oldHeights = GetComponent <MeshFilter>().mesh.vertices;
        //var newVerticals = new Vector3[oldHeights.Length];
        List <Vector3> newVerticals = new List <Vector3>();

        for (int i = 0; i < oldHeights.Length; i++)
        {
            //newVerticals[i] = new Vector3(oldHeights[i].x, _heights[i], oldHeights[i].z);
            newVerticals.Add(new Vector3(oldHeights[i].x, _heights[i], oldHeights[i].z));
        }
        GetComponent <MeshFilter>().mesh.SetVertices(newVerticals);
    }
Example #2
0
    void CreateFromGenerator()
    {
        var uv = new[] { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) };

        // get generated triangle
        GetComponent <MeshFilter>().mesh = MeshGeneratorTypeTwo.Triangle(new Vector3(0, 0, 0), new Vector3(0, 0, 1), new Vector3(1, 0, 1));

        // get generated quad
        //GetComponent<MeshFilter>().mesh = MeshGeneratorTypeTwo.Quad(new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1),uv);

        // get generated cube
        //GetComponent<MeshFilter>().mesh = MeshGeneratorTypeTwo.Cube(new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0, 1, 0));

        // get generated plane with size
        //GetComponent<MeshFilter>().mesh = MeshGeneratorTypeTwo.Plane(new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 1), _xSize, _zSize);
    }