Created
July 28, 2013 20:54
-
-
Save jonbro/6100194 to your computer and use it in GitHub Desktop.
unity sprite grid rendering
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| public class renderTiles : MonoBehaviour { | |
| public Vector2 gridSize; | |
| public Vector2 worldSize; | |
| public float perlinScale, timeScale; | |
| public int spriteRow; | |
| public Color hiColor; | |
| public Color lowColor; | |
| private Mesh mesh; | |
| private Vector2[] uv; | |
| private Color32[] cs; | |
| // Use this for initialization | |
| void Start () { | |
| CreateMesh(); | |
| for (int x = 0; x < gridSize.x; x++) { | |
| for (int y = 0; y < gridSize.y; y++) { | |
| AssignSprite(x,y,10, 10); | |
| } | |
| } | |
| } | |
| void Update(){ | |
| /* | |
| for (int x = 0; x < gridSize.x; x++) { | |
| for (int y = 0; y < gridSize.y; y++) { | |
| //AssignSprite(x,y,10, 10); | |
| AssignSprite(x,y,spriteRow, (int)(Mathf.PerlinNoise(x/perlinScale+Time.time*timeScale, y/perlinScale)*12.0f)); | |
| AssignColor(x,y,Color.Lerp(lowColor, hiColor, Mathf.PerlinNoise(x/perlinScale*2+Time.time*timeScale, y/perlinScale))); | |
| } | |
| } | |
| */ | |
| } | |
| // Update is called once per frame | |
| void LateUpdate () { | |
| GetComponent<MeshFilter>().mesh.uv = uv; | |
| GetComponent<MeshFilter>().mesh.colors32 = cs; | |
| } | |
| public void AssignSprite(int mX, int mY, int sX, int sY){ | |
| // should extract the proper uv, then reassign, and assign back to mesh | |
| // might want to calculate this from the tile size eventually, but for now, hardcoding | |
| float tileSizeX = 8.0f/128.0f; | |
| float tileSizeY = 8.0f/256.0f; | |
| int index = (mY*(int)gridSize.x+mX)*4; | |
| uv[index] = new Vector2(sX*tileSizeX,sY*tileSizeY); | |
| uv[index+1] = new Vector2((sX+1)*tileSizeX,sY*tileSizeY); | |
| uv[index+2] = new Vector2((sX+0)*tileSizeX,(sY+1)*tileSizeY); | |
| uv[index+3] = new Vector2((sX+1)*tileSizeX,(sY+1)*tileSizeY); | |
| } | |
| public Color GetColor(int x, int y){ | |
| int index = (y*(int)gridSize.x+x)*4; | |
| return (Color)cs[index]; | |
| } | |
| public void AssignColor(int x, int y, Color c){ | |
| int index = (y*(int)gridSize.x+x)*4; | |
| cs[index+3] = cs[index+2] = cs[index+1] = cs[index] = (Color32)c; | |
| } | |
| public void clearAll(){ | |
| for(int x=0;x<gridSize.x;x++){ | |
| for(int y=0;y<gridSize.y;y++){ | |
| AssignColor(x, y, Color.clear); | |
| } | |
| } | |
| } | |
| public void CreateMesh(){ | |
| if(mesh == null){ | |
| GetComponent<MeshFilter>().mesh = mesh = new Mesh(); | |
| mesh.name = "Star Mesh"; | |
| mesh.hideFlags = HideFlags.HideAndDontSave; | |
| } | |
| mesh.Clear(); | |
| // feel free to waste triangles, I have plenty! | |
| Vector3[] vertices = new Vector3[(int)gridSize.x*(int)gridSize.y*4]; | |
| int[] tri = new int[(int)gridSize.x*(int)gridSize.y*6]; | |
| uv = new Vector2[(int)gridSize.x*(int)gridSize.y*4]; | |
| cs = new Color32[(int)gridSize.x*(int)gridSize.y*4]; | |
| float xSize = worldSize.x; | |
| float ySize = worldSize.y; | |
| Vector3 worldOffset = new Vector3(0, 0, 0); | |
| for(int x=0;x<gridSize.x;x++){ | |
| for(int y=0;y<gridSize.y;y++){ | |
| int index = (y*(int)gridSize.x+x)*4; | |
| int tindex = (y*(int)gridSize.x+x)*6; | |
| //Debug.Log(xSize*x); | |
| vertices[0+index] = new Vector3(xSize*x,0, ySize*y)+worldOffset; | |
| vertices[1+index] = new Vector3(xSize*(x+1), 0, ySize*y)+worldOffset; | |
| vertices[2+index] = new Vector3(xSize*x, 0, ySize*(y+1))+worldOffset; | |
| vertices[3+index] = new Vector3(xSize*(x+1), 0, ySize*(y+1))+worldOffset; | |
| tri[0+tindex] = 0+index; | |
| tri[1+tindex] = 2+index; | |
| tri[2+tindex] = 1+index; | |
| tri[3+tindex] = 2+index; | |
| tri[4+tindex] = 3+index; | |
| tri[5+tindex] = 1+index; | |
| uv[0+index] = new Vector2((float)x/(float)gridSize.x, y/(float)gridSize.y); | |
| uv[1+index] = new Vector2((x+1)/(float)gridSize.x, y/(float)gridSize.y); | |
| uv[2+index] = new Vector2(x/(float)gridSize.x, (y+1)/(float)gridSize.y); | |
| uv[3+index] = new Vector2((x+1)/(float)gridSize.x, (y+1)/(float)gridSize.y); | |
| } | |
| } | |
| mesh.vertices = vertices; | |
| mesh.triangles = tri; | |
| mesh.uv = uv; | |
| mesh.RecalculateNormals(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment