Exemplo n.º 1
0
		public void Update () 
		{ 
			//checking if instance already exists and disabling if it is another mm
			if (instance != null && instance != this) { Debug.LogError("MapMagic object already present in scene. Disabling duplicate"); this.enabled = false; return; }
		
			//loading old non-asset data
			if (gens == null)
			{
				if (serializer != null && serializer.entities != null && serializer.entities.Count != 0) 
				{	
					Debug.Log("MapMagic: Loading outdated scene format. Please check node consistency and re-save the scene.");
					LoadOldNonAssetData();
					serializer = null;
				}
				else Debug.Log("MapMagic: Could not find the proper graph data. Please assign it manually.");
			}
			
			//checking gens asset
			if (gens == null) gens = ScriptableObject.CreateInstance<GeneratorsAsset>();

			//finding camera positions
			Vector3[] camPoses = GetCamPoses();
			if (camPoses.Length==0) return;

			//displaying debug range
			if (guiDebug && !isEditor) 
				for (int c=0; c<camPoses.Length; c++) 
					transform.TransformPoint(camPoses[c]).DrawDebug(generateRange, Color.green);

			//deploying terrain matrix
			if (!isEditor && generateInfinite) terrains.Deploy(camPoses, allowMove:true);

			//enabling/disabling, switching lods, starting threads
			terrains.SwitchState(camPoses);

			//transforming cam poses to coords
			Coord[] camCoords = GetCamCoords();
			if (camCoords.Length==0) return;

			//calculating number of running threads and checking a need to prepare generators
			runningThreadsCount = 0;
			bool prepareGenerators = false;
			foreach (Chunk tw in terrains.Objects())
			{
				if (tw.running) runningThreadsCount++;
				if (tw.start) prepareGenerators = true; //if any of chunks started this frame
			}

			//preparing generators
			if (prepareGenerators)
			{
				foreach (TextureInput tin in gens.GeneratorsOfType<TextureInput>()) tin.CheckLoadTexture();
			}

			//updating chunks
			foreach (Chunk tw in terrains.ObjectsFromCoords(camCoords)) tw.Update();
		}
Exemplo n.º 2
0
		public void Update () 
		{ 
			//checking if instance already exists and disabling if it is another mm
			if (instance != null && instance != this) { Debug.LogError("MapMagic object already present in scene. Disabling duplicate"); this.enabled = false; return; }
		
			//loading old non-asset data
			if (gens == null)
			{
				if (serializer != null && serializer.entities != null && serializer.entities.Count != 0) 
				{	
					Debug.Log("MapMagic: Loading outdated scene format. Please check node consistency and re-save the scene.");
					LoadOldNonAssetData();
					serializer = null;
				}
				else { Debug.Log("MapMagic: Could not find the proper graph data. It the data file was changed externally reload the scene, otherwise create the new one in the General Settings tab."); return; }
			}

			//checking gens asset
			//if (gens == null) gens = ScriptableObject.CreateInstance<GeneratorsAsset>();

			//finding camera positions
			camPoses = GetCamPoses(camPoses); //TODO: reuse these arrays
			if (camPoses.Length==0) return;

			//displaying debug range
			if (guiDebug && !isEditor) 
				for (int c=0; c<camPoses.Length; c++) 
				{
					transform.TransformPoint(camPoses[c]).DrawDebug(generateRange, Color.yellow);
					transform.TransformPoint(camPoses[c]).DrawDebug(enableRange, Color.green);
					transform.TransformPoint(camPoses[c]).DrawDebug(removeRange, Color.red);
				}

			//do nothing if chink size is zero
			if (terrainSize < 0.1f) return;

			//deploying terrain matrix
			if (!isEditor && generateInfinite) terrains.Deploy(camPoses, allowMove:true);

			//calculating number of running threads
			runningThreadsCount = 0;
			foreach (Chunk tw in terrains.Objects())
				if (tw.running) runningThreadsCount++; 

			//finding distance to each of the chunks
			int chunksNum = terrains.Count;
			if (chunksArray==null || chunksArray.Length!=chunksNum) chunksArray = new Chunk[chunksNum];
			if (distsArray==null || distsArray.Length!=chunksNum) distsArray = new float[chunksNum];

			int counter = 0;
			foreach (Chunk chunk in terrains.Objects()) 
			{
				Vector3 chunkPos = new Vector3(chunk.coord.x*MapMagic.instance.terrainSize, 0, chunk.coord.z*MapMagic.instance.terrainSize); // + transform.position; cam pos is relative to transform pos
				float minDist = 200000000;
				for (int c=0; c<camPoses.Length; c++)
				{
					float dist = camPoses[c].RectangularDistToRect(chunkPos, MapMagic.instance.terrainSize);
					if (dist < minDist) minDist = dist;
				}

				//for (int i=0; i<chunksArray.Length; i++) if (chunksArray[i]==chunk) Debug.Log("Chunk is in the list");
				chunksArray[counter] = chunk;
				distsArray[counter] = minDist;
				counter++;
			}

			//sorting chunks according to distance from cameras
			Array.Sort(distsArray, chunksArray);

			//updating chunks - from cameras to the edge
			for (int i=0; i<chunksArray.Length; i++)
				chunksArray[i].Update(distsArray[i]);

			//resetting prepare flag
			generatorsPrepared = false;
		}