public void ApplyTmp(Terrain terrain) { //checking microsplat component MicroSplatTerrain mso = terrain.GetComponent <MicroSplatTerrain>(); if (mso == null) { mso = terrain.gameObject.AddComponent <MicroSplatTerrain>(); } mso.terrain = terrain; //otherwise nullref on newly created tiles mso.templateMaterial = terrain.materialTemplate; int numTextures = colors.Length; if (numTextures == 0) { return; } int resolution = (int)Mathf.Sqrt(colors[0].Length); for (int t = 0; t < numTextures; t++) { if (colors[t] == null) { continue; } Texture2D tex = GetTex(mso, t); if (tex == null || tex.width != resolution || tex.height != resolution || tex.format != TextureFormat.RGBA32) { if (tex != null) { #if UNITY_EDITOR if (!UnityEditor.AssetDatabase.Contains(tex)) #endif GameObject.DestroyImmediate(tex); } tex = new Texture2D(resolution, resolution, TextureFormat.RGBA32, false, true); tex.wrapMode = TextureWrapMode.Mirror; //to avoid border seams tex.name = "CustomControl " + t; SetTex(mso, t, tex); //tex.hideFlags = HideFlags.DontSave; } tex.SetPixels(0, 0, tex.width, tex.height, colors[t]); tex.Apply(); } mso.Sync(); //terrain.basemapDistance = 1000000; }
//private Loading _loader; // Start is called before the first frame update private void Start() { DontDestroyOnLoad(this.gameObject); Terrain[] terrains = GameObject.FindObjectsOfType <Terrain>(); foreach (Terrain terrain in terrains) { MicroSplatTerrain msTerrain = terrain.GetComponent <MicroSplatTerrain>(); msTerrain.Sync(); } terrainData = new TerrainMap(terrains, scene); pathFinderData = new PathfinderData(terrainData); }
public Material materialTemplate; //source material assigned. Can't use terrain.materialTemplate since it will be changed with copy public override void Apply(Terrain terrain) { //checking microsplat component //this should be done before applying control since //microsplat removes template from terrain on disable (lod switch), so ensuring we have a material before base.Apply MicroSplatTerrain mso = null; if (assignComponent) { mso = terrain.GetComponent <MicroSplatTerrain>(); if (mso == null) { mso = terrain.gameObject.AddComponent <MicroSplatTerrain>(); } mso.terrain = terrain; //otherwise nullref on newly created tiles MapMagic.Core.MapMagicObject mapMagic = terrain.transform.parent.parent.GetComponent <MapMagic.Core.MapMagicObject>(); mso.templateMaterial = mapMagic.terrainSettings.material; if (terrain.materialTemplate == mso.templateMaterial || terrain.materialTemplate == null) //if material instance assigned (first run) { mso.matInstance = new Material(mapMagic.terrainSettings.material); terrain.materialTemplate = mso.matInstance; } else { mso.matInstance = terrain.materialTemplate; } mso.propData = propData; } else if (terrain.materialTemplate == null) //prevents an error (materialTemplate is null) on disabling "Set Component" { MapMagic.Core.MapMagicObject mapMagic = terrain.transform.parent.parent.GetComponent <MapMagic.Core.MapMagicObject>(); terrain.materialTemplate = mapMagic.terrainSettings.material; } base.Apply(terrain); if (assignComponent) { mso.Sync(); } }
/// <summary> /// Based on camera distance, change terrain settings to improve appearance. /// /// TODO: we need to write our own shader instead of employing this hack /// </summary> private void MaybeChangeTerrainMaterial() { float camAltitude = transform.position.y - _terrain.transform.position.y; foreach (TerrainMaterial mat in _terrainMaterials) { if (camAltitude < mat.MaxAltitude) { if (_microSplatTerrain.templateMaterial != mat.Material) { _microSplatTerrain.templateMaterial = mat.Material; // In the inspector this is the "Debug/Keywords" field. _microSplatTerrain.keywordSO = mat.Keywords; // In the inspector this is the "Debug/Per Texture Data" field. _microSplatTerrain.propData = mat.PerTextureData; _microSplatTerrain.Sync(); } break; } } }