using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; public class Terrain2 : MonoBehaviour { // int mapWidth; // int mapHeight; // float [] heightData; // Color[] tileColors; // byte[] tileOverlays; // bool[] collisions; // float[] walkingHeights; // // GameObject[] chunks; // // MeshRenderer meshRenderer; // MeshFilter meshFilter; // // float lastTerrainRegen; // const float TERRAIN_REGEN_TIME = 0.5f; // // // Start is called before the first frame update // void Start() // { // Texture2D heightMap = (Texture2D)OnDemandLoader.getSprite(15); // heightData = new float[heightMap.width * heightMap.height]; // for (int i = 0; i < heightMap.height; i++) // { // for (int j = 0; j < heightMap.width; j++) // { // heightData[i * heightMap.width + j] = (heightMap.GetPixel(j, i).b * 256.0f); // } // } // Texture2D colorMap = (Texture2D)OnDemandLoader.getSprite(14); // mapWidth = colorMap.width; // mapHeight = colorMap.height; // tileColors = new Color[colorMap.width * colorMap.height]; // for (int i = 0; i < colorMap.height; i++) // { // for (int j = 0; j < colorMap.width; j++) // { // tileColors[i * colorMap.width + j] = colorMap.GetPixel(j, i); // } // } // // Texture2D collisionMap = (Texture2D)OnDemandLoader.getSprite(19); // collisions = new bool[collisionMap.width * collisionMap.height]; // for (int i = 0; i < collisionMap.height; i++) // { // for (int j = 0; j < collisionMap.width; j++) // { // collisions[i * collisionMap.width + j] = collisionMap.GetPixel(j, i).r > 0.8f; // } // } // // Texture2D walkingHeightMap = (Texture2D)OnDemandLoader.getSprite(20); // walkingHeights = new float[walkingHeightMap.width * walkingHeightMap.height]; // for (int i = 0; i < walkingHeightMap.height; i++) // { // for (int j = 0; j < walkingHeightMap.width; j++) // { // walkingHeights[i * walkingHeightMap.width + j] = walkingHeightMap.GetPixel(j, i).b; // // if (walkingHeights[i * walkingHeightMap.width + j] > heightData[i * walkingHeightMap.width + j]) // { // Debug.Log("height higher at " + j + " " + i + " diff " + (walkingHeights[i * walkingHeightMap.width + j] - heightData[i * walkingHeightMap.width + j])); // } // } // } // // Packet overlayMap = OnDemandLoader.getDataFile(0); // tileOverlays = new byte[colorMap.width * colorMap.height]; // for (int i = 0; i < tileOverlays.Length; i++) // { // tileOverlays[i] = (byte)overlayMap.ReadByte(); // } // // //createChunk(0, 0, 64, 64); // lastTerrainRegen = Time.time; // // // chunks = new GameObject[256]; // // for (int i = 0; i < 16; i++) // { // for (int j = 0; j < 16; j++) // { // GameObject g = new GameObject("Chunk_" + j + "_" + i); // g.transform.parent = gameObject.transform; // meshRenderer = g.AddComponent(); // g.AddComponent(); // meshFilter = g.AddComponent(); // meshRenderer.sharedMaterial = new Material(Shader.Find("Unlit/Terrain")); // meshRenderer.material.mainTexture = OnDemandLoader.getSprite(17); // //gameObject.AddComponent().sharedMesh = meshFilter.mesh; // createChunk(g, j * 64, i * 64, 64, 64); // } // } // } // // // Update is called once per frame // void Update() // { // if (Time.time - lastTerrainRegen > TERRAIN_REGEN_TIME) // { // //createChunk((int)Camera.main.transform.position.x - 50, (int)Camera.main.transform.position.z - 50, 100, 100); // //lastTerrainRegen = Time.time; // } // // } // // private Color getTileColor(int x, int y) // { // if ((y * mapWidth + x) > tileColors.Length || x < 0 || y < 0) // { // return Color.blue; // } // return tileColors[y * mapWidth + x]; // } // // private float getTileHeight(int x, int y) // { // if (heightData == null) // { // return -1.0f; // } // if (y * (mapWidth+1) + x > heightData.Length || y * (mapWidth+1) + x < 0) // { // return -1.0f; // } // return heightData[y * (mapWidth + 1) + x]; // } // // private float getWalkingTileHeight(int x, int y) // { // if (walkingHeights == null) // { // return -1.0f; // } // if (y * (mapWidth + 1) + x > walkingHeights.Length || y * (mapWidth + 1) + x < 0) // { // return -1.0f; // } // return walkingHeights[y * (mapWidth + 1) + x]; // } // // public int getTileOverlay(int x, int y) // { // if (y * mapWidth + x > tileOverlays.Length || y * mapWidth + x < 0) // { // return 26; // } // return tileOverlays[y * mapWidth + x]; // } // // public void setTileOverlay(int id, int x, int y) // { // if (y * mapWidth + x > tileOverlays.Length || y * mapWidth + x < 0) // return; // else // tileOverlays[y * mapWidth + x] = (byte)id; // } // // public bool getCollisionTile(int x, int y) // { // return collisions[y * (mapWidth+1) + x]; // } // // public void createChunk(GameObject g, int x, int y, int w, int h) // { // Mesh m = new Mesh(); // List vertices = new List(); // List indices = new List(); // List colors = new List(); // List normals = new List(); // List uvs = new List(); // int indexPosition = 0; // // for (int i = y; i < y+h; i++) // { // for (int j = x; j < x+w; j++) // { // int tileOverlay = getTileOverlay(j, i); // // vertices.Add(new Vector3(j, getTileHeight(j, i), i)); //bottom left // vertices.Add(new Vector3(j, getTileHeight(j, i + 1), i + 1.0f)); //top left // vertices.Add(new Vector3(j + 1.0f, getTileHeight(j + 1, i + 1), i + 1.0f)); //top right // // vertices.Add(new Vector3(j, getTileHeight(j, i), i)); //bottom left // vertices.Add(new Vector3(j + 1.0f, getTileHeight(j + 1, i + 1), i + 1.0f)); //top right // vertices.Add(new Vector3(j + 1.0f, getTileHeight(j + 1, i), i)); //bottom right // // float tileWidth = 1.0f / 8; // int overlayIdX = tileOverlay % 8; // int overlayIdY = tileOverlay / 8; // // uvs.Add(new Vector2(overlayIdX * tileWidth, 1.0f - ((overlayIdY * tileWidth) + tileWidth))); // uvs.Add(new Vector2(overlayIdX * tileWidth, 1.0f - (overlayIdY * tileWidth))); // uvs.Add(new Vector2((overlayIdX * tileWidth) + tileWidth, 1.0f - (overlayIdY * tileWidth))); // // uvs.Add(new Vector2(overlayIdX * tileWidth, 1.0f - ((overlayIdY * tileWidth) + tileWidth))); // uvs.Add(new Vector2((overlayIdX * tileWidth) + tileWidth, 1.0f - (overlayIdY * tileWidth))); // uvs.Add(new Vector2((overlayIdX * tileWidth) + tileWidth, 1.0f - ((overlayIdY * tileWidth) + tileWidth))); // // // //all vertices same color // for (int k = 0; k < 6; k++) // { // colors.Add(getTileColor(j, i)); // indices.Add(indexPosition++); // normals.Add(new Vector3(0, 1, 0)); // } // } // } // // m.SetVertices(vertices); // m.SetColors(colors); // m.SetNormals(normals); // m.SetUVs(0, uvs.ToArray()); // m.SetIndices(indices.ToArray(), MeshTopology.Triangles, 0); // meshFilter.mesh = m; // g.GetComponent().sharedMesh = m; // } // // private void generateEmptyOverlayMap(int w, int h) // { // byte[] data = new byte[w * h]; // for (int i = 0; i < w*h; i++) // { // if (i < 12) // data[i] = (byte)i; // else // data[i] = 26; // } // FileStream file = new FileStream("0.bytes", FileMode.Create); // file.Write(data, 0, w * h); // file.Close(); // } // // public void writeTileOverlays() // { // FileStream file = new FileStream("0.bytes", FileMode.Create); // file.Write(tileOverlays, 0, tileOverlays.Length); // file.Close(); // Debug.Log("Overlay file successfully saved!"); // } // // public float getLowestVertexHeightOfTile(int x, int y) // { // if (x > mapWidth || y > mapWidth) // { // return 0; // } // if (x < 0 || y < 0) // { // return 0; // } // float lowest = int.MaxValue; // lowest = Mathf.Min(getTileHeight(x,y), lowest); // lowest = Mathf.Min(getTileHeight(x+1, y), lowest); // lowest = Mathf.Min(getTileHeight(x, y+1), lowest); // lowest = Mathf.Min(getTileHeight(x+1, y+1), lowest); // // return lowest; // } // // public float getLowestWalkingVertexHeightOfTile(int x, int y) // { // if (x > mapWidth || y > mapWidth) // { // return 0; // } // if (x < 0 || y < 0) // { // return 0; // } // float lowest = int.MaxValue; // lowest = Mathf.Min(getWalkingTileHeight(x, y), lowest); // lowest = Mathf.Min(getWalkingTileHeight(x + 1, y), lowest); // lowest = Mathf.Min(getWalkingTileHeight(x, y + 1), lowest); // lowest = Mathf.Min(getWalkingTileHeight(x + 1, y + 1), lowest); // // return lowest; // } }