almost 5 years ago
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; } } }
Starting with Chrome version 45, NPAPI is no longer supported for Google Chrome. For more information, see Chrome and NPAPI (blog.chromium.org).
Firefox and Microsoft Internet Explorer are recommended browsers for websites using java applets.
Chrome Version Support
Are you sure, you want to delete this comment?
Sign up using
0 Comment(s)