/// <summary> /// Find the terrain texture index at the given position. /// This considers all terrains in the group of the active terrain /// </summary> /// <param name="worldPosition"></param> /// <returns>The layer index or -1 if none found at the world position</returns> public int GetTerrainLayerIndex(Vector3 worldPosition) { Vector3 terrainCord = new Vector3(); Terrain terrain = Terrain.activeTerrain; bool ok = ConvertToSplatMapCoordinate(worldPosition, ref terrainCord, ref terrain); if (!ok) { return(-1); } int activeTerrainIndex = 0; float largestOpacity = 0f; TerrainRecord record = map[terrain]; for (int i = 0; i < record.numTextures; i++) { if (largestOpacity < record.splatmapData[(int)terrainCord.z, (int)terrainCord.x, i]) { activeTerrainIndex = i; largestOpacity = record.splatmapData[(int)terrainCord.z, (int)terrainCord.x, i]; } } return(activeTerrainIndex); }
public TerrainManager() { // get all terrains in group terrainGroup = TerrainUtilities.GetTerrainsInGroup(); // create record of all terrains foreach (Terrain terrain in terrainGroup) { TerrainRecord record = new TerrainRecord(terrain); map.Add(terrain, record); } }
IEnumerator AddTerrainCoroutine(int col, int row) { mTerrainNum++; GetTerrainName(col, row); #if LOAD_ADDITIVE_ASYNC TerrainIndex tr = new TerrainIndex(); tr.col = col; tr.row = row; sTerrainRecords.Add(tr); TerrainAttachment ta = new TerrainAttachment(); ta.attachments = new List <GameObject>(4); mTerrainAttachments.Add(ta); lock ( sTerrainTasks ) { TerrainTask tt = new TerrainTask(); tt.col = col; tt.row = row; tt.terrainName = terrainName.ToString(); sTerrainTasks.Add(tt); } AsyncOperation async = Application.LoadLevelAdditiveAsync(sceneName.ToString()); yield return(async); #else float z = (col > 0 ? col - 1 : col) * TILE_SIZE; float x = -(row < 0 ? row + 1 : row) * TILE_SIZE; // for memory limitation, don't cache anything. Object t = Resources.Load(terrainName.ToString()); // the prefab has been deleted from project by me, so it won't work here. TerrainRecord tr = new TerrainRecord(); tr.col = col; tr.row = row; sTerrainRecords.Add(tr); TerrainAttachment ta = new TerrainAttachment(); yield return(ta.terrain = Instantiate(t, new Vector3(x, 0, z), Quaternion.identity)); ta.attachments = new List <GameObject>(4); t = null; mTerrainAttachments.Add(ta); #endif }