public void SetCurrentArea(TCUnityTerrain tcTerrain) { currentTerrain = tcTerrain.terrain; currentTCUnityTerrain = tcTerrain; currentTCTerrain = tcTerrain; currentTerrainArea = terrainAreas[0]; }
float SampleTerrainHeight(Vector3 pos) { TC_TerrainArea terrainArea = TC_Generate.instance.area2D.terrainAreas[0]; for (int i = 0; i < terrainArea.terrains.Count; i++) { TCUnityTerrain tcTerrain = terrainArea.terrains[i]; if (tcTerrain.terrain == null) { continue; } if (tcTerrain.terrain.terrainData == null) { continue; } Vector3 posTerrain = tcTerrain.terrain.transform.position; Vector3 sizeTerrain = tcTerrain.terrain.terrainData.size; Rect rect = new Rect(posTerrain.x, posTerrain.z, sizeTerrain.x, sizeTerrain.z); if (rect.Contains(pos)) { return(tcTerrain.terrain.SampleHeight(pos)); } } return(-1); }
public void GetSplatTextures(TC_TerrainArea terrainArea) { if (!CheckValidUnityTerrain()) { return; } splatPrototypes.Clear(); #if UNITY_5 || UNITY_2017 || UNITY_2018_1 || UNITY_2018_2 SplatPrototype[] terrainSplatPrototypes = terrain.terrainData.splatPrototypes; for (int i = 0; i < terrainSplatPrototypes.Length; i++) { SplatPrototype s = terrainSplatPrototypes[i]; TC_SplatPrototype d = new TC_SplatPrototype(); d.texture = s.texture; d.normalMap = s.normalMap; d.metallic = s.metallic; d.smoothness = s.smoothness; d.tileOffset = s.tileOffset; d.tileSize = s.tileSize; splatPrototypes.Add(d); } #else TerrainLayer[] terrainLayers = terrain.terrainData.terrainLayers; if (terrainLayers == null) { return; } for (int i = 0; i < terrainLayers.Length; i++) { TerrainLayer s = terrainLayers[i]; if (s == null) { continue; } TC_SplatPrototype d = new TC_SplatPrototype(); d.texture = s.diffuseTexture; d.normalMap = s.normalMapTexture; d.metallic = s.metallic; d.smoothness = s.smoothness; d.tileOffset = s.tileOffset; d.tileSize = s.tileSize; splatPrototypes.Add(d); } #endif // if (splatColors == null) splatColors = new Color[splatPrototypes.Count]; // if (splatColors.Length != splatPrototypes.Count) splatColors = new Color[splatPrototypes.Count]; // Debug.Log("Getsplat texture colors"); // for (int i = 0; i < splatColors.Length; i++) // { // if (splatPrototypes[i].texture != null) splatColors[i] = GetTextureColor(splatPrototypes[i].texture, 1); // } }
public bool SetCurrentArea(TCUnityTerrain tcTerrain, int outputId) { // Debug.Log(tcTerrain.terrain.name); Terrain terrain = currentTerrain = tcTerrain.terrain; currentTCUnityTerrain = tcTerrain; currentTCTerrain = tcTerrain; currentTerrainArea = terrainAreas[0]; if (!currentTCUnityTerrain.active) { return(false); } intResolution = new Int2(); Int2 resolution2 = new Int2(); if (terrain.terrainData.heightmapResolution > 2049) { resExpandBorder = 0; resExpandBorderSize = 0; } else { resExpandBorder = Mathf.RoundToInt((terrain.terrainData.heightmapResolution - 1) * resExpandBorderPercentage); resExpandBorderSize = terrain.terrainData.size.x * resExpandBorderPercentage; } // Debug.Log(resExpandBorder); // Debug.Log(resExpandBorderSize); if (outputId == TC.heightOutput) { intResolution.x = intResolution.y = (terrain.terrainData.heightmapResolution) + (resExpandBorder * 2); resolution2 = new Int2(terrain.terrainData.heightmapResolution, terrain.terrainData.heightmapResolution); } else if (outputId == TC.splatOutput) { intResolution.x = intResolution.y = terrain.terrainData.alphamapResolution; resolution2 = intResolution; splatLength = currentTerrain.terrainData.splatPrototypes.Length; splatmapLength = currentTerrain.terrainData.alphamapTextures.Length; } else if (outputId == TC.treeOutput) { intResolution.x = intResolution.y = (int)(terrain.terrainData.size.x / terrainLayer.treeResolutionPM); resolution2 = intResolution; } else if (outputId == TC.grassOutput) { intResolution.x = intResolution.y = terrain.terrainData.detailResolution; resolution2 = intResolution; } else if (outputId == TC.objectOutput) { intResolution.x = intResolution.y = (int)(terrain.terrainData.size.x / terrainLayer.objectResolutionPM); resolution2 = intResolution; //if (false) //{ // area.center = new Vector2((int)terrainLayer.objectTransform.position.x, (int)terrainLayer.objectTransform.position.z); // area.size = new Vector2(terrainLayer.objectResolutionPM, terrainLayer.objectResolutionPM); // totalArea.position = area.position; // totalArea.size = terrainLayer.objectAreaSize; // resolutionPM = new Vector2(terrainLayer.objectAreaSize.x / (resolution2.x), terrainLayer.objectAreaSize.y / (resolution2.y)); //} } else if (outputId == TC.colorOutput) { intResolution.x = intResolution.y = terrainLayer.colormapResolution; resolution2 = intResolution; } outputOffsetV2 = new Vector2(terrainLayer.layerGroups[outputId].t.position.x, terrainLayer.layerGroups[outputId].t.position.z); outputOffsetV3 = new Vector3(outputOffsetV2.x, 0, outputOffsetV2.y); resolution = intResolution.ToVector2(); if (intResolution.x < TC_Settings.instance.previewResolution) { previewResolution = intResolution.x; TC_Reporter.Log("From " + TC_Settings.instance.previewResolution + " To " + previewResolution); } else { previewResolution = TC_Settings.instance.previewResolution; } resToPreview = new Vector2((previewResolution - 0) / (totalArea.width + 0), (previewResolution - 0) / (totalArea.height + 0)); if (outputId == TC.heightOutput || outputId == TC.splatOutput) { resolutionPM = new Vector2(terrain.terrainData.size.x / (resolution2.x - 1), terrain.terrainData.size.z / (resolution2.y - 1)); } else { resolutionPM = new Vector2(terrain.terrainData.size.x / (resolution2.x - 0), terrain.terrainData.size.z / (resolution2.y - 0)); } // if (outputId == TC.objectOutput) Debug.Log(resolutionPM); // resolutionPM = new Vector2(terrain.terrainData.size.x / (resolution2.x - 1), terrain.terrainData.size.z / (resolution2.y - 1)); if (outputId == TC.heightOutput) { // area = new Rect(terrain.transform.position.x - resolutionPM.x, terrain.transform.position.z - resolutionPM.y, intResolution.x - 0, intResolution.y - 0); area = new Rect(terrain.transform.position.x - (resolutionPM.x * resExpandBorder), terrain.transform.position.z - (resolutionPM.y * resExpandBorder), intResolution.x - 0, intResolution.y - 0); } else { // resolutionPM = new Vector2(terrain.terrainData.size.x / (resolution2.x), terrain.terrainData.size.z / (resolution2.y)); Vector2 posSnap; posSnap.x = Mathw.Snap(terrain.transform.position.x, resolutionPM.x); posSnap.y = Mathw.Snap(terrain.transform.position.z, resolutionPM.y); if (outputId == TC.treeOutput || outputId == TC.objectOutput) { // posSnap += resolutionPM / 2; } area = new Rect(posSnap.x, posSnap.y, intResolution.x, intResolution.y); snapOffsetUV = (new Vector2(terrain.transform.position.x, terrain.transform.position.z) - posSnap); snapOffsetUV.x /= terrain.terrainData.size.x; snapOffsetUV.y /= terrain.terrainData.size.z; // Debug.Log(area); } bounds = new Bounds(terrain.transform.position + terrain.terrainData.size / 2, terrain.terrainData.size); startPos = new Vector3(area.xMin, terrain.transform.position.y, area.yMin); terrainSize = terrain.terrainData.size; return(true); }
void OnEnable() { current = this; currentTerrainArea = terrainAreas[0]; }
public void RemoveTerrainArea(TC_TerrainArea terrainArea) { // int index = terrainAreas.IndexOf(terrainArea); // if (index != -1) terrainAreas.RemoveAt(index); }
public void AddTerrainArea(TC_TerrainArea terrainArea) { // if (!terrainAreas.Contains(terrainArea)) terrainAreas.Add(terrainArea); }
public void ApplySplatTextures(TC_TerrainArea terrainArea, TCUnityTerrain sTerrain = null) { if (!CheckValidUnityTerrain()) { return; } if (sTerrain == null) { sTerrain = this; } // CleanSplatTextures(sTerrain); #if UNITY_5 || UNITY_2017 || UNITY_2018_1 || UNITY_2018_2 List <SplatPrototype> splatPrototypesCleaned = new List <SplatPrototype>(); bool tooManySplatsMessage = false; for (int i = 0; i < sTerrain.splatPrototypes.Count; i++) { if (splatPrototypesCleaned.Count >= TC.splatLimit) { tooManySplatsMessage = true; break; } TC_SplatPrototype s = sTerrain.splatPrototypes[i]; if (s.texture != null) { SplatPrototype d = new SplatPrototype(); d.texture = s.texture; d.normalMap = s.normalMap; d.metallic = s.metallic; d.smoothness = s.smoothness; d.tileOffset = s.tileOffset; float tileSize = sTerrain.terrain.terrainData.size.x / Mathf.Round(sTerrain.terrain.terrainData.size.x / s.tileSize.x); d.tileSize = new Vector2(tileSize, tileSize); splatPrototypesCleaned.Add(d); TC.SetTextureReadWrite(s.texture); } } if (tooManySplatsMessage) { TC.AddMessage("TC2 supports generating maximum " + TC.splatLimit + " splat textures."); Debug.Log("TC2 supports generating maximum " + TC.splatLimit + " splat textures."); } terrain.terrainData.splatPrototypes = splatPrototypesCleaned.ToArray(); #else List <TerrainLayer> splatPrototypesCleaned = new List <TerrainLayer>(); bool tooManySplatsMessage = false; for (int i = 0; i < sTerrain.splatPrototypes.Count; i++) { if (splatPrototypesCleaned.Count >= TC.splatLimit) { tooManySplatsMessage = true; break; } TC_SplatPrototype s = sTerrain.splatPrototypes[i]; if (s.texture != null) { TerrainLayer d = new TerrainLayer(); d.diffuseTexture = s.texture; d.normalMapTexture = s.normalMap; d.normalScale = s.normalScale; d.metallic = s.metallic; d.smoothness = s.smoothness; d.tileOffset = s.tileOffset; float tileSize = sTerrain.terrain.terrainData.size.x / Mathf.Round(sTerrain.terrain.terrainData.size.x / s.tileSize.x); d.tileSize = new Vector2(tileSize, tileSize); splatPrototypesCleaned.Add(d); TC.SetTextureReadWrite(s.texture); } } if (tooManySplatsMessage) { TC.AddMessage("TC2 supports generating maximum " + TC.splatLimit + " splat textures."); Debug.Log("TC2 supports generating maximum " + TC.splatLimit + " splat textures."); } TerrainLayer[] newTerrainLayers = splatPrototypesCleaned.ToArray(); #if UNITY_EDITOR string terrainPath = "Assets" + terrainArea.terrainDataPath.Replace(Application.dataPath, String.Empty); for (int i = 0; i < newTerrainLayers.Length; i++) { string path; if (terrainArea.useSameTerrainLayersForAllTerrains) { path = terrainPath + "/" + terrainArea.terrainName + "_TerrainLayer_" + i + ".asset"; } else { path = terrainPath + "/" + terrain.name + "_TerrainLayer_" + i + ".asset"; } TerrainLayer terrainLayer = UnityEditor.AssetDatabase.LoadAssetAtPath(path, typeof(TerrainLayer)) as TerrainLayer; if (terrainLayer == null) { UnityEditor.AssetDatabase.DeleteAsset(path); UnityEditor.AssetDatabase.CreateAsset(newTerrainLayers[i], path); } else { CopyTerrainLayer(newTerrainLayers[i], terrainLayer); newTerrainLayers[i] = terrainLayer; } } #endif terrain.terrainData.terrainLayers = newTerrainLayers; #endif }
void OnEnable() { terrainArea = TC_Area2D.current.terrainAreas[0]; tcTerrains = terrainArea.terrains; Start(); }