Advertisement
zaboodable

MeshMaker with tiles

Jan 27th, 2017
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.93 KB | None | 0 0
  1. /*
  2.     Modified version of the script found here
  3.     http://wiki.unity3d.com/index.php/MeshCreationGrid
  4.     https://creativecommons.org/licenses/by-sa/3.0/
  5. */
  6.  
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9.  
  10. [RequireComponent(typeof(MeshFilter))]
  11. [RequireComponent(typeof(MeshRenderer))]
  12. public class MeshMaker : MonoBehaviour
  13. {
  14.     public int GridWidth, GridHeight;       // Size of the mesh grid
  15.     public int TilesX, TilesY;              // Number of tiles in your tile sheet
  16.     private float tileSizeX, tileSizeY;     // Value between 0 and 1 used for getting tile uvs
  17.     //public Texture2D Texture;             // Sprite/tile sheet texture
  18.  
  19.     public Texture2D sheet;                 // Sprite sheet texture (sprite mode: multiple)
  20.     public Sprite[] spritesInSheet;         // Array to store all sprites from the sheet
  21.  
  22.     //public int tileIndex;
  23.  
  24.     void Start()
  25.     {
  26.         spritesInSheet = Resources.LoadAll<Sprite>(sheet.name);
  27.  
  28.         CreateMesh();
  29.  
  30.     }
  31.  
  32.     Vector2 SpriteToUV(Sprite s)
  33.     {  
  34.         // Get sprite Rect on the texture
  35.         var rect = s.rect;
  36.  
  37.         // Calculate x and y position scaled to range 0..1 by dividing by texture size
  38.         float x = rect.x / sheet.width;
  39.         float y = rect.y / sheet.height;
  40.  
  41.         // Return as vector 2
  42.         return new Vector2(x, y);
  43.     }
  44.  
  45.     void CreateMesh()
  46.     {
  47.         // 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)
  48.         int[,] tileData = new int[GridWidth, GridHeight];
  49.         for (int y = 0; y < GridHeight; y++)
  50.         {
  51.             for (int x = 0; x < GridWidth; x++)
  52.             {
  53.                 //tileData[x, y] = tileIndex;   // used for testing, will set the tile at x, y to this value
  54.                 tileData[x, y] = Random.Range(0, TilesX * TilesY);
  55.             }
  56.         }
  57.  
  58.         // Create mesh
  59.         var mesh = new Mesh();
  60.  
  61.         // Get MeshFilter and give it the mesh
  62.         var mf = GetComponent<MeshFilter>();
  63.         mf.mesh = mesh;
  64.  
  65.         // Get MeshRenderer and set the texture to our tile sheet
  66.         var mr = GetComponent<MeshRenderer>();
  67.         //mr.material.SetTexture("_MainTex", Texture);
  68.         mr.material.SetTexture("_MainTex", sheet);
  69.  
  70.         // Lists to hold all mesh data
  71.         var vertices = new List<Vector3>();
  72.         var triangles = new List<int>();
  73.         var normals = new List<Vector3>();
  74.         var uvs = new List<Vector2>();
  75.  
  76.         // TileSize for uvs 1/width 1/height for simple grid, can be edited if you want padding etc on the texture sheet
  77.         tileSizeX = 1f / TilesX;
  78.         tileSizeY = 1f / TilesY;
  79.  
  80.         // Looping through our grid
  81.         var index = 0;
  82.         for (var x = 0; x < GridWidth; x++)
  83.         {
  84.             for (var y = 0; y < GridHeight; y++)
  85.             {
  86.                 // For each 'tile' create verts, triangles, normals
  87.                 AddVertices(1, 1, y, x, vertices);
  88.                 index = AddTriangles(index, triangles);
  89.                 AddNormals(normals);
  90.  
  91.                 // Get tile data (this will be your type of tile, i just used random ints for easiness)
  92.                 int tile = tileData[x, y];
  93.  
  94.  
  95.                 /* old stuff
  96.                 // Calculate grid position of the tile (position on the tile sheet)
  97.                 int ty = TilesX - 1 - tile / TilesX;
  98.                 int tx = tile % TilesX;
  99.                 AddUVsGrid(tx, ty, uvs);
  100.                 */
  101.  
  102.                 // Add tile uvs given the coordinates for our tile sheet
  103.                 var uv = SpriteToUV(spritesInSheet[tileData[x, y]]);
  104.                 AddUVs(uv.x, uv.y, uvs);
  105.             }
  106.         }
  107.  
  108.         // Add all the data back to the mesh
  109.         mesh.vertices = vertices.ToArray();
  110.         mesh.normals = normals.ToArray();
  111.         mesh.triangles = triangles.ToArray();
  112.         mesh.uv = uvs.ToArray();
  113.         mesh.RecalculateNormals();
  114.     }
  115.  
  116.     private void AddVertices(int tileHeight, int tileWidth, int y, int x, ICollection<Vector3> vertices)
  117.     {
  118.         vertices.Add(new Vector3((x * tileWidth), (y * tileHeight), 0));
  119.         vertices.Add(new Vector3((x * tileWidth) + tileWidth, (y * tileHeight), 0));
  120.         vertices.Add(new Vector3((x * tileWidth) + tileWidth, (y * tileHeight) + tileHeight, 0));
  121.         vertices.Add(new Vector3((x * tileWidth), (y * tileHeight) + tileHeight, 0));
  122.     }
  123.     private int AddTriangles(int index, ICollection<int> triangles)
  124.     {
  125.         triangles.Add(index + 2);
  126.         triangles.Add(index + 1);
  127.         triangles.Add(index);
  128.         triangles.Add(index);
  129.         triangles.Add(index + 3);
  130.         triangles.Add(index + 2);
  131.         index += 4;
  132.         return index;
  133.     }
  134.     private void AddNormals(ICollection<Vector3> normals)
  135.     {
  136.         normals.Add(Vector3.forward);
  137.         normals.Add(Vector3.forward);
  138.         normals.Add(Vector3.forward);
  139.         normals.Add(Vector3.forward);
  140.     }
  141.  
  142.     /// <summary>
  143.     /// Adds uvs given an x and y coordinate for the texture
  144.     /// </summary>
  145.     /// <param name="x"></param>
  146.     /// <param name="y"></param>
  147.     /// <param name="uvs"></param>
  148.     private void AddUVsGrid(int x, int y, ICollection<Vector2> uvs)
  149.     {
  150.         uvs.Add(new Vector2(x * tileSizeX, y * tileSizeY));
  151.         uvs.Add(new Vector2((x + 1) * tileSizeX, y * tileSizeY));
  152.         uvs.Add(new Vector2((x + 1) * tileSizeX, (y + 1) * tileSizeY));
  153.         uvs.Add(new Vector2(x * tileSizeX, (y + 1) * tileSizeY));
  154.     }
  155.  
  156.     /// <summary>
  157.     /// Adds uvs given exact uv start position
  158.     /// </summary>
  159.     /// <param name="x"></param>
  160.     /// <param name="y"></param>
  161.     /// <param name="uvs"></param>
  162.     private void AddUVs(float x, float y, ICollection<Vector2> uvs)
  163.     {
  164.         uvs.Add(new Vector2(x, y));
  165.         uvs.Add(new Vector2(x + tileSizeX, y));
  166.         uvs.Add(new Vector2(x + tileSizeX, y + tileSizeY));
  167.         uvs.Add(new Vector2(x, y + tileSizeY));
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement