private void UpdateTerrain() { if (sim == null || sim.Terrain == null) { return; } WorkPool.QueueUserWorkItem(sync => { int step = 1; for (int x = 0; x < 256; x += step) { for (int y = 0; y < 256; y += step) { float z = 0; int patchNr = ((int)x / 16) * 16 + (int)y / 16; if (sim.Terrain[patchNr] != null && sim.Terrain[patchNr].Data != null) { float[] data = sim.Terrain[patchNr].Data; z = data[(int)x % 16 * 16 + (int)y % 16]; } heightTable[x, y] = z; } } terrainFace = renderer.TerrainMesh(heightTable, 0f, 255f, 0f, 255f); terrainVertices = new ColorVertex[terrainFace.Vertices.Count]; for (int i = 0; i < terrainFace.Vertices.Count; i++) { byte[] part = Utils.IntToBytes(i); terrainVertices[i] = new ColorVertex() { Vertex = terrainFace.Vertices[i], Color = new Color4b() { R = part[0], G = part[1], B = part[2], A = 253 // terrain picking } }; } terrainIndices = new uint[terrainFace.Indices.Count]; for (int i = 0; i < terrainIndices.Length; i++) { terrainIndices[i] = terrainFace.Indices[i]; } terrainInProgress = false; Modified = false; terrainTextureNeedsUpdate = true; terrainTimeSinceUpdate = 0f; }); }
void UpdateTerrain() { terrainModified = false; if (Client.Network.CurrentSim == null || Client.Network.CurrentSim.Terrain == null) { return; } Debug.Log("UpdateTerrain"); int step = 1; for (int x = 0; x < 256; x += step) { for (int y = 0; y < 256; y += step) { float z = 0; int patchNr = ((int)x / 16) * 16 + (int)y / 16; if (Client.Network.CurrentSim.Terrain[patchNr] != null && Client.Network.CurrentSim.Terrain[patchNr].Data != null) { float[] data = Client.Network.CurrentSim.Terrain[patchNr].Data; z = data[(int)x % 16 * 16 + (int)y % 16]; } heightTable[x, y] = z; } } Face face = R.TerrainMesh(heightTable, 0f, 255f, 0f, 255f); //Unity doesn't allow a mesh with over 65000 vertices while face have 65536 vertices. //We need to split face mesh into 2 peices. //mesh1: vertices 0~32768+255=33023, normals 0~33023, uv 0~33023, indices 0~255*2*128*3-1=195839 //mesh2: vertices 32768~65535, normals 32768~65535, uv 32768~65535, indices 195840~390149 MakeMesh(mesh1, face, 0, 33023, 0, 195839, 0); MakeMesh(mesh2, face, 32768, 65535, 195840, 390149, 32768); terrainTimeSinceUpdate = 0f; if (terrainModified) { Debug.Log("terrainModified set to true by other thread!"); } terrainTextureNeedsUpdate = true; }