Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Segmenting an Image into Tiles

    • 0
    • 3
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 629
    Comment on it

    Segmenting of an Image can be done by applying different parts of a texture to Quads.

    This can be done by mapping different UV vertex points of an image with a Quad or any other 3d object.

    Following script demonstrate the Segmenting of an Image with UV vertex mapping :

    // Script to demonstrate Segmenting of a Image.
    
    using UnityEngine;
    using System.Collections;
    
    public class TextureSegmenting : MonoBehaviour
    {
    
        public Material mat ;
        public Texture2D tex ;
        public int rows ;
        public int cols ;
        void Start ()
        {
            mat.mainTexture = tex; // Appling Texture to material ;
    
            BuildPieces ();
        }    
    
        void BuildPieces()
        {
            Vector3 offset = Vector3.zero;         // A Vecter3 viariable for postioning
    
            // Offset for Placement of tiles
            offset.x = -Mathf.RoundToInt ((float)cols / 2.0f - 0.5f);
            offset.y = -Mathf.RoundToInt ((float)rows / 2.0f - 0.5f);
            float startX = offset.x;
            float uvWidth = 1.0f / cols;  
            float uvHeight = 1.0f / rows;
            
            
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    // Creating a primitive Quad
                    GameObject quad = GameObject.CreatePrimitive (PrimitiveType.Quad);
                    quad.tag ="Player";
                    Transform t = quad.transform;
                    t.position = offset;
                    t.localScale = new Vector3(0.95f, 0.95f, 0.95f);
                    quad.renderer.material = mat;
                    
    
                    // Getting mesh component of the "quad"
                    Mesh mesh = quad.GetComponent<MeshFilter>().mesh;
                    Vector2[] uvs = mesh.uv;  // Getting UV vertices from mesh
    
                    // Mapping each UV vertex of quad with texture coordinates
                    uvs[0] = new Vector2(j * uvWidth, i * uvHeight);
                    uvs[1] = new Vector2((j + 1) * uvWidth, (i + 1) * uvHeight);
                    uvs[2] = new Vector2((j + 1) * uvWidth, i * uvHeight);
                    uvs[3] = new Vector2(j * uvWidth, (i + 1) * uvHeight);
    
                    // Setting UV vertices back to mesh
                    mesh.uv = uvs;
                    offset.x += 1.0f;
                }
                offset.y += 1.0f;
                offset.x = startX;
            }
        }
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: