public void SetSettings () { MapMagic magic = MapMagic.instance; terrain.heightmapPixelError = magic.pixelError; if (magic.showBaseMap) terrain.basemapDistance = magic.baseMapDist; else terrain.basemapDistance = 999999; terrain.castShadows = magic.castShadows; #if UNITY_2017_4_OR_NEWER terrain.reflectionProbeUsage = magic.reflectionProbeUsage; #endif if (terrainCollider==null) terrainCollider = terrain.GetComponent<TerrainCollider>(); if (terrainCollider!=null) terrainCollider.enabled = MapMagic.instance.applyColliders; #if UNITY_2018_3_OR_NEWER terrain.drawInstanced = magic.drawInstanced; terrain.allowAutoConnect = magic.autoConnect; #endif //material if (!Preview.enabled) { terrain.materialType = MapMagic.instance.terrainMaterialType; if (MapMagic.instance.terrainMaterialType == Terrain.MaterialType.Custom && MapMagic.instance.assignCustomTerrainMaterial) terrain.materialTemplate = MapMagic.instance.customTerrainMaterial; /*if (MapMagic.instance.terrainMaterialType == Terrain.MaterialType.Custom) { if (MapMagic.instance.materialTemplateMode) { //assigning new mat only it has different name or shader in template mode if (terrain.materialTemplate == null || terrain.materialTemplate.shader != MapMagic.instance.customTerrainMaterial.shader || terrain.materialTemplate.name != MapMagic.instance.customTerrainMaterial.name) terrain.materialTemplate = MapMagic.instance.customTerrainMaterial; } else terrain.materialTemplate = MapMagic.instance.customTerrainMaterial; // terrain.materialTemplate = MapMagic.instance.customTerrainMaterial; }*/ } terrain.drawTreesAndFoliage = magic.detailDraw; terrain.detailObjectDistance = magic.detailDistance; terrain.detailObjectDensity = magic.detailDensity; terrain.treeDistance = magic.treeDistance; terrain.treeBillboardDistance = magic.treeBillboardStart; terrain.treeCrossFadeLength = magic.treeFadeLength; terrain.treeMaximumFullLODCount = magic.treeFullLod; #if UNITY_EDITOR terrain.bakeLightProbesForTrees = magic.bakeLightProbesForTrees; #endif terrain.terrainData.wavingGrassSpeed = magic.windSpeed; terrain.terrainData.wavingGrassAmount = magic.windSize; terrain.terrainData.wavingGrassStrength = magic.windBending; terrain.terrainData.wavingGrassTint = magic.grassTint; //copy layer, tag, scripts from mm to terrains if (MapMagic.instance.copyLayersTags) { GameObject go = terrain.gameObject; go.layer = MapMagic.instance.gameObject.layer; go.isStatic = MapMagic.instance.gameObject.isStatic; try { go.tag = MapMagic.instance.gameObject.tag; } catch { Debug.LogError("MapMagic: could not copy object tag"); } } if (MapMagic.instance.copyComponents) { GameObject go = terrain.gameObject; MonoBehaviour[] components = MapMagic.instance.GetComponents<MonoBehaviour>(); for (int i=0; i<components.Length; i++) { if (components[i] is MapMagic || components[i] == null) continue; //if MapMagic itself or script not assigned if (terrain.gameObject.GetComponent(components[i].GetType()) == null) Extensions.CopyComponent(components[i], go); } } }
public void SetSettings () { MapMagic magic = MapMagic.instance; terrain.heightmapPixelError = magic.pixelError; if (magic.showBaseMap) terrain.basemapDistance = magic.baseMapDist; else terrain.basemapDistance = 999999; terrain.castShadows = magic.castShadows; #if UNITY_2017_4_OR_NEWER terrain.reflectionProbeUsage = magic.reflectionProbeUsage; #endif if (terrainCollider==null) terrainCollider = terrain.GetComponent<TerrainCollider>(); if (terrainCollider!=null) terrainCollider.enabled = MapMagic.instance.applyColliders; #if UNITY_2018_3_OR_NEWER terrain.drawInstanced = magic.drawInstanced; terrain.allowAutoConnect = magic.autoConnect; #endif //material if (!Preview.enabled) { #if UNITY_2019_2_OR_NEWER if (MapMagic.instance.customTerrainMaterial != null) { //checking if customTerrainMaterial assigned as a terrain mat if (terrain.materialTemplate == MapMagic.instance.customTerrainMaterial) { Debug.LogError("Terrain material template == MM.customTerrainMaterial (" + terrain.materialTemplate + "," + terrain.materialTemplate.shader + ")"); terrain.materialTemplate = null; } //remove previous material if the shader doesn't match if (terrain.materialTemplate != null && terrain.materialTemplate.shader != MapMagic.instance.customTerrainMaterial.shader) { GameObject.DestroyImmediate(terrain.materialTemplate); //removes custom shader textures as well. Unity does not remove them! Resources.UnloadUnusedAssets(); terrain.materialTemplate = null; //need to reset material template to prevent unity crash } //duplicating material to terrain if (terrain.materialTemplate == null) { terrain.materialTemplate = new Material(MapMagic.instance.customTerrainMaterial); terrain.materialTemplate.name += " (Copy)"; } } #else terrain.materialType = MapMagic.instance.terrainMaterialType; if (MapMagic.instance.terrainMaterialType == Terrain.MaterialType.Custom && MapMagic.instance.assignCustomTerrainMaterial) terrain.materialTemplate = MapMagic.instance.customTerrainMaterial; #endif } terrain.drawTreesAndFoliage = magic.detailDraw; terrain.detailObjectDistance = magic.detailDistance; terrain.detailObjectDensity = magic.detailDensity; terrain.treeDistance = magic.treeDistance; terrain.treeBillboardDistance = magic.treeBillboardStart; terrain.treeCrossFadeLength = magic.treeFadeLength; terrain.treeMaximumFullLODCount = magic.treeFullLod; #if UNITY_EDITOR terrain.bakeLightProbesForTrees = magic.bakeLightProbesForTrees; #endif terrain.terrainData.wavingGrassSpeed = magic.windSpeed; terrain.terrainData.wavingGrassAmount = magic.windSize; terrain.terrainData.wavingGrassStrength = magic.windBending; terrain.terrainData.wavingGrassTint = magic.grassTint; //copy layer, tag, scripts from mm to terrains if (MapMagic.instance.copyLayersTags) { GameObject go = terrain.gameObject; go.layer = MapMagic.instance.gameObject.layer; go.isStatic = MapMagic.instance.gameObject.isStatic; try { go.tag = MapMagic.instance.gameObject.tag; } catch { Debug.LogError("MapMagic: could not copy object tag"); } } if (MapMagic.instance.copyComponents) { GameObject go = terrain.gameObject; MonoBehaviour[] components = MapMagic.instance.GetComponents<MonoBehaviour>(); for (int i=0; i<components.Length; i++) { if (components[i] is MapMagic || components[i] == null) continue; //if MapMagic itself or script not assigned if (terrain.gameObject.GetComponent(components[i].GetType()) == null) Extensions.CopyComponent(components[i], go); } } }
// applying public IEnumerator ApplyRoutine () { //calling before-apply event MapMagic.CallOnGenerateCompleted(terrain); //if (MapMagic.OnGenerateCompleted != null) MapMagic.OnGenerateCompleted(terrain); MapMagic.instance.applyRunning = true; //apply foreach (KeyValuePair<Type,object> kvp in apply) { //Type output = kvp.Key; //if (!(output as Generator).enabled) continue; //if (output is SplatOutput && MapMagic.instance.gens.GetGenerator<PreviewOutput>()!=null) continue; //skip splat out if preview exists //callback MapMagic.CallOnApply(terrain, kvp.Value); //if (OnApply!=null) OnApply(terrain, kvp.Value); //selecting apply enumerator (with switch, not reflection) IEnumerator e = null; System.Type type = kvp.Key; if (type == typeof(HeightOutput)) e = HeightOutput.Apply(this); else if (type == typeof(SplatOutput)) e = SplatOutput.Apply(this); else if (type == typeof(ObjectOutput)) e = ObjectOutput.Apply(this); else if (type == typeof(TreesOutput)) e = TreesOutput.Apply(this); else if (type == typeof(GrassOutput)) e = GrassOutput.Apply(this); else if (type == typeof(RTPOutput)) e = RTPOutput.Apply(this); //apply enumerator while (e.MoveNext()) { if (terrain==null) yield break; //guard in case max terrains count < actual terrains: terrain destroyed or still processing yield return null; } } //purging unused outputs HashSet<Type> existingOutputs = MapMagic.instance.gens.GetExistingOutputTypes(onlyEnabled:true, checkBiomes:true); if (!existingOutputs.Contains(typeof(HeightOutput)) && terrain.terrainData.heightmapResolution != 33) HeightOutput.Purge(this); if (!existingOutputs.Contains(typeof(SplatOutput)) && !existingOutputs.Contains(typeof(RTPOutput)) && terrain.terrainData.alphamapResolution != 16) SplatOutput.Purge(this); if (!existingOutputs.Contains(typeof(ObjectOutput)) && terrain.transform.childCount != 0) ObjectOutput.Purge(this); if (!existingOutputs.Contains(typeof(TreesOutput)) && terrain.terrainData.treeInstanceCount != 0) TreesOutput.Purge(this); if (!existingOutputs.Contains(typeof(GrassOutput)) && terrain.terrainData.detailResolution != 16) GrassOutput.Purge(this); //previewing /* if (instance.previewOutput != null && previewObject != null) { SplatOutput.Preview((Matrix)previewObject, this); }*/ //creating initial texture if splatmap count is 0 - just to look good if (terrain.terrainData.splatPrototypes.Length == 0) ClearSplats(); //clearing intermediate results apply.Clear(); if (!MapMagic.instance.isEditor || !MapMagic.instance.saveIntermediate) { results.Clear(); ready.Clear(); } //this should be done in thread, but thread has no access to isPlaying //if (!terrain.gameObject.activeSelf) terrain.gameObject.SetActive(true); //terrain.enabled = true; MapMagic.instance.applyRunning = false; //copy layer, tag, scripts from mm to terrains if (MapMagic.instance.copyLayersTags) { GameObject go = terrain.gameObject; go.layer = MapMagic.instance.gameObject.layer; go.isStatic = MapMagic.instance.gameObject.isStatic; try { go.tag = MapMagic.instance.gameObject.tag; } catch { Debug.LogError("MapMagic: could not copy object tag"); } //#if UNITY_EDITOR //UnityEditor.GameObjectUtility.SetStaticEditorFlags(go, UnityEditor.GameObjectUtility.GetStaticEditorFlags(MapMagic.instance.gameObject)); //#endif } if (MapMagic.instance.copyComponents) { GameObject go = terrain.gameObject; MonoBehaviour[] components = MapMagic.instance.GetComponents<MonoBehaviour>(); for (int i=0; i<components.Length; i++) { if (components[i] is MapMagic || components[i] == null) continue; //if MapMagic itself or script not assigned if (terrain.gameObject.GetComponent(components[i].GetType()) == null) Extensions.CopyComponent(components[i], go); } } //calling after-apply event MapMagic.CallOnApplyCompleted(terrain); //if (MapMagic.OnApplyCompleted != null) MapMagic.OnApplyCompleted(terrain); queuedApply = false; //returning preview if it is enabled //if (MapMagic.instance.previewOutput != null) Preview(forceRefresh:true); }
// applying public IEnumerator ApplyRoutine () { //calling before-apply event if (MapMagic.OnGenerateCompleted != null) MapMagic.OnGenerateCompleted(terrain); MapMagic.instance.applyRunning = true; //apply foreach (KeyValuePair<IOutput,object> kvp in apply) { IOutput output = kvp.Key; if (!(output as Generator).enabled) continue; if (output is SplatOutput && MapMagic.instance.gens.GetGenerator<PreviewOutput>()!=null) continue; //skip splat out if preview exists //callback if (OnApply!=null) OnApply(terrain, kvp.Value); //apply enumerator IEnumerator e = kvp.Key.Apply(this, terrain.terrainData); while (e.MoveNext()) { if (terrain==null) yield break; //guard in case max terrains count < actual terrains: terrain destroyed or still processing yield return null; } } //creating initial texture if splatmap count is 0 - just to look good if (terrain.terrainData.splatPrototypes.Length == 0) ClearSplats(); //clearing intermediate results apply.Clear(); if (!MapMagic.instance.isEditor || !MapMagic.instance.saveIntermediate) { results.Clear(); ready.Clear(); } //this should be done in thread, but thread has no access to isPlaying //if (!terrain.gameObject.activeSelf) terrain.gameObject.SetActive(true); //terrain.enabled = true; MapMagic.instance.applyRunning = false; //copy layer, tag, scripts from mm to terrains if (MapMagic.instance.copyLayersTags) { GameObject go = terrain.gameObject; go.layer = MapMagic.instance.gameObject.layer; go.isStatic = MapMagic.instance.gameObject.isStatic; go.tag = MapMagic.instance.gameObject.tag; //#if UNITY_EDITOR //UnityEditor.GameObjectUtility.SetStaticEditorFlags(go, UnityEditor.GameObjectUtility.GetStaticEditorFlags(MapMagic.instance.gameObject)); //#endif } if (MapMagic.instance.copyComponents) { GameObject go = terrain.gameObject; MonoBehaviour[] components = MapMagic.instance.GetComponents<MonoBehaviour>(); for (int i=0; i<components.Length; i++) { if (components[i] is MapMagic || components[i] == null) continue; //if MapMagic itself or script not assigned if (terrain.gameObject.GetComponent(components[i].GetType()) == null) Extensions.CopyComponent(components[i], go); } } //calling after-apply event if (MapMagic.OnApplyCompleted != null) MapMagic.OnApplyCompleted(terrain); }