Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(MeshFilter))]
- [RequireComponent(typeof(MeshCollider))]
- [RequireComponent(typeof(MeshRenderer))]
- public class TileMap : MonoBehaviour {
- public int size_x = 100;
- public int size_z = 50;
- public float tileSize = 1.0f;
- public Texture2D terrainTiles;
- Texture2D texture;
- public int tileResolution = 16;
- Vector3 currentTileCoord;
- public Transform selectionCube;
- Color[][] tiles;
- Color[] p;
- Color[] q;
- int numTilesPerRow;
- int numRows;
- public float scale;
- void Start()
- {
- BuildMesh();
- }
- //Chops up the sprite sheet into tiles, to be used by BuildTexture()
- Color[][] ChopUpTiles()
- {
- numTilesPerRow = terrainTiles.width / tileResolution;
- numRows = terrainTiles.height / tileResolution;
- Color[][] tiles = new Color[numTilesPerRow * numRows][];
- for(int y=0; y<numRows; y++)
- {
- for (int x = 0; x<numTilesPerRow; x ++)
- {
- tiles[y*numTilesPerRow + x] = terrainTiles.GetPixels(x*tileResolution, y*tileResolution, tileResolution, tileResolution);
- }
- }
- return tiles;
- }
- //Builds the texture for the map, tile by tile
- void BuildTexture()
- {
- int texWidth = size_x * tileResolution;
- int texHeight = size_z * tileResolution;
- texture = new Texture2D(texWidth, texHeight);
- tiles = ChopUpTiles();
- for (int y=0; y <size_z; y++)
- {
- for(int x=0; x<size_x; x++)
- {
- //Perlin Noise Code
- //This sets the color of the tile to the number corresponding to the Spritesheet array. It is multiplied by 6 because I
- //use 7 different sprites for the terrain
- Debug.Log(Mathf.RoundToInt(Mathf.PerlinNoise(x / scale, y / scale) * 6));
- p = tiles[Mathf.RoundToInt(Mathf.PerlinNoise(x / scale, y / scale) * 6)];
- texture.SetPixels(x*tileResolution, y*tileResolution, tileResolution, tileResolution, p);
- }
- }
- texture.filterMode = FilterMode.Point;
- texture.Apply();
- MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
- mesh_renderer.sharedMaterial.mainTexture = texture;
- }
- public void BuildMesh()
- {
- int numTiles = size_x * size_z;
- int numTris = numTiles * 2;
- int vsize_x = size_x + 1;
- int vsize_z = size_z + 1;
- int numVerts = vsize_x * vsize_z;
- //Generate Mesh Data
- Vector3[] vertices = new Vector3[numVerts];
- Vector3[] normals = new Vector3[numVerts];
- Vector2[] uv = new Vector2[numVerts];
- int[] triangles = new int[numTris * 3];
- int x, z;
- for (z = 0; z < vsize_z; z++)
- {
- for (x = 0; x < vsize_x; x++)
- {
- vertices[z * vsize_x + x] = new Vector3(x*tileSize, 0, z*tileSize);
- normals[z * vsize_x + x] = Vector3.up;
- uv[z * vsize_x + x] = new Vector2((float)x / size_x, (float)z / size_x);
- }
- }
- for (z = 0; z < size_z; z++)
- {
- for (x = 0; x < size_x; x++)
- {
- int squareIndex = z * size_x + x;
- int triIndex = squareIndex * 6;
- triangles[triIndex + 0] = z * vsize_x + x + 0;
- triangles[triIndex + 1] = z * vsize_x + x + vsize_x + 0;
- triangles[triIndex + 2] = z * vsize_x + x + vsize_x + 1;
- triangles[triIndex + 3] = z * vsize_x + x + 0;
- triangles[triIndex + 4] = z * vsize_x + x + vsize_x + 1;
- triangles[triIndex + 5] = z * vsize_x + x + 1;
- }
- }
- //Create new mesh and populate it
- Mesh mesh = new Mesh();
- mesh.vertices = vertices;
- mesh.triangles = triangles;
- mesh.normals = normals;
- mesh.uv = uv;
- //Assign our mesh to filter/collider
- MeshFilter mf = GetComponent<MeshFilter>();
- MeshRenderer mr = GetComponent<MeshRenderer>();
- MeshCollider mc = GetComponent<MeshCollider>();
- mf.mesh = mesh;
- mc.sharedMesh = mesh;
- BuildTexture();
- }
- void Update()
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hitInfo;
- texture.Apply();
- if (GetComponent<Collider>().Raycast(ray, out hitInfo, Mathf.Infinity))
- {
- int coordX = Mathf.FloorToInt(hitInfo.point.x / tileSize);
- int coordZ = Mathf.FloorToInt(hitInfo.point.z / tileSize);
- currentTileCoord.x = coordX;
- currentTileCoord.z = coordZ;
- selectionCube.transform.position = currentTileCoord * tileSize;
- q = tiles[1];
- if (Input.GetMouseButton(0))
- {
- texture.SetPixels(coordX * tileResolution, coordZ * tileResolution, tileResolution, tileResolution, q);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement