Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Modified version of the script found here
- http://wiki.unity3d.com/index.php/MeshCreationGrid
- https://creativecommons.org/licenses/by-sa/3.0/
- */
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(MeshFilter))]
- [RequireComponent(typeof(MeshRenderer))]
- public class MeshMaker : MonoBehaviour
- {
- public int GridWidth, GridHeight; // Size of the mesh grid
- public int TilesX, TilesY; // Number of tiles in your tile sheet
- private float tileSizeX, tileSizeY; // Value between 0 and 1 used for getting tile uvs
- //public Texture2D Texture; // Sprite/tile sheet texture
- public Texture2D sheet; // Sprite sheet texture (sprite mode: multiple)
- public Sprite[] spritesInSheet; // Array to store all sprites from the sheet
- //public int tileIndex;
- void Start()
- {
- spritesInSheet = Resources.LoadAll<Sprite>(sheet.name);
- CreateMesh();
- }
- Vector2 SpriteToUV(Sprite s)
- {
- // Get sprite Rect on the texture
- var rect = s.rect;
- // Calculate x and y position scaled to range 0..1 by dividing by texture size
- float x = rect.x / sheet.width;
- float y = rect.y / sheet.height;
- // Return as vector 2
- return new Vector2(x, y);
- }
- void CreateMesh()
- {
- // Tile data. random here but could be replaced if you had an array of tiles in the level for example (x,y = leveltile[x, y].tiletype)
- int[,] tileData = new int[GridWidth, GridHeight];
- for (int y = 0; y < GridHeight; y++)
- {
- for (int x = 0; x < GridWidth; x++)
- {
- //tileData[x, y] = tileIndex; // used for testing, will set the tile at x, y to this value
- tileData[x, y] = Random.Range(0, TilesX * TilesY);
- }
- }
- // Create mesh
- var mesh = new Mesh();
- // Get MeshFilter and give it the mesh
- var mf = GetComponent<MeshFilter>();
- mf.mesh = mesh;
- // Get MeshRenderer and set the texture to our tile sheet
- var mr = GetComponent<MeshRenderer>();
- //mr.material.SetTexture("_MainTex", Texture);
- mr.material.SetTexture("_MainTex", sheet);
- // Lists to hold all mesh data
- var vertices = new List<Vector3>();
- var triangles = new List<int>();
- var normals = new List<Vector3>();
- var uvs = new List<Vector2>();
- // TileSize for uvs 1/width 1/height for simple grid, can be edited if you want padding etc on the texture sheet
- tileSizeX = 1f / TilesX;
- tileSizeY = 1f / TilesY;
- // Looping through our grid
- var index = 0;
- for (var x = 0; x < GridWidth; x++)
- {
- for (var y = 0; y < GridHeight; y++)
- {
- // For each 'tile' create verts, triangles, normals
- AddVertices(1, 1, y, x, vertices);
- index = AddTriangles(index, triangles);
- AddNormals(normals);
- // Get tile data (this will be your type of tile, i just used random ints for easiness)
- int tile = tileData[x, y];
- /* old stuff
- // Calculate grid position of the tile (position on the tile sheet)
- int ty = TilesX - 1 - tile / TilesX;
- int tx = tile % TilesX;
- AddUVsGrid(tx, ty, uvs);
- */
- // Add tile uvs given the coordinates for our tile sheet
- var uv = SpriteToUV(spritesInSheet[tileData[x, y]]);
- AddUVs(uv.x, uv.y, uvs);
- }
- }
- // Add all the data back to the mesh
- mesh.vertices = vertices.ToArray();
- mesh.normals = normals.ToArray();
- mesh.triangles = triangles.ToArray();
- mesh.uv = uvs.ToArray();
- mesh.RecalculateNormals();
- }
- private void AddVertices(int tileHeight, int tileWidth, int y, int x, ICollection<Vector3> vertices)
- {
- vertices.Add(new Vector3((x * tileWidth), (y * tileHeight), 0));
- vertices.Add(new Vector3((x * tileWidth) + tileWidth, (y * tileHeight), 0));
- vertices.Add(new Vector3((x * tileWidth) + tileWidth, (y * tileHeight) + tileHeight, 0));
- vertices.Add(new Vector3((x * tileWidth), (y * tileHeight) + tileHeight, 0));
- }
- private int AddTriangles(int index, ICollection<int> triangles)
- {
- triangles.Add(index + 2);
- triangles.Add(index + 1);
- triangles.Add(index);
- triangles.Add(index);
- triangles.Add(index + 3);
- triangles.Add(index + 2);
- index += 4;
- return index;
- }
- private void AddNormals(ICollection<Vector3> normals)
- {
- normals.Add(Vector3.forward);
- normals.Add(Vector3.forward);
- normals.Add(Vector3.forward);
- normals.Add(Vector3.forward);
- }
- /// <summary>
- /// Adds uvs given an x and y coordinate for the texture
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- /// <param name="uvs"></param>
- private void AddUVsGrid(int x, int y, ICollection<Vector2> uvs)
- {
- uvs.Add(new Vector2(x * tileSizeX, y * tileSizeY));
- uvs.Add(new Vector2((x + 1) * tileSizeX, y * tileSizeY));
- uvs.Add(new Vector2((x + 1) * tileSizeX, (y + 1) * tileSizeY));
- uvs.Add(new Vector2(x * tileSizeX, (y + 1) * tileSizeY));
- }
- /// <summary>
- /// Adds uvs given exact uv start position
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- /// <param name="uvs"></param>
- private void AddUVs(float x, float y, ICollection<Vector2> uvs)
- {
- uvs.Add(new Vector2(x, y));
- uvs.Add(new Vector2(x + tileSizeX, y));
- uvs.Add(new Vector2(x + tileSizeX, y + tileSizeY));
- uvs.Add(new Vector2(x, y + tileSizeY));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement