Beispiel #1
0
			// 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);
			}
Beispiel #2
0
			public void Purge ()
			{
				HeightOutput ho = MapMagic.instance.gens.GetGenerator<HeightOutput>(); if (ho==null || !ho.enabled) HeightOutput.Purge(this);
				
				SplatOutput so = MapMagic.instance.gens.GetGenerator<SplatOutput>();
				PreviewOutput po = MapMagic.instance.gens.GetGenerator<PreviewOutput>();
				if ( (po == null || !po.enabled) && (so==null || !so.enabled) ) SplatOutput.Purge(this);
				if (po == null || !po.enabled) previewObjs = null;
				
				GrassOutput go = MapMagic.instance.gens.GetGenerator<GrassOutput>(); if (go==null || !go.enabled) GrassOutput.Purge(this);
				ObjectOutput oo = MapMagic.instance.gens.GetGenerator<ObjectOutput>(); if (oo==null || !oo.enabled) ObjectOutput.Purge(this);
				TreesOutput to = MapMagic.instance.gens.GetGenerator<TreesOutput>(); if (to==null || !to.enabled) TreesOutput.Purge(this);
				purge = false;
			}