Exemplo n.º 1
0
		public float[,] GenerateFastNoiseMap(MapParameters par, int mapSize, Vector2 noisePosition)
		{
			var fastNoise = new FastNoise(par.noiseSeed);

			if (Mathf.Approximately(par.noiseScale, 0))
				par.noiseScale = 0.001f;

			var frequency = 1 / par.noiseScale;
			fastNoise.SetFrequency(frequency);
			fastNoise.SetFractalOctaves(par.octaves);
			fastNoise.SetNoiseType(par.noiseType);

			float textureResolutionFactor = par.textureResolutionFactor;

			mapSize = mapSize * (int) textureResolutionFactor;
			var retNoiseMap = new float[mapSize, mapSize];

			for (var x = 0; x < mapSize; x++)
			{
				for (var y = 0; y < mapSize; y++)
				{
					var sampleX = x / textureResolutionFactor + noisePosition.x;
					var sampleY = y / textureResolutionFactor + noisePosition.y;
					var noiseHeight = fastNoise.GetNoise(sampleX, sampleY);
					retNoiseMap[x, y] = (noiseHeight + 1) / 2;
				}
			}
			

			return retNoiseMap;
		}
Exemplo n.º 2
0
		public static MeshData GenerateMeshData(float[,] heightMap, MapParameters mapParameters)
		{
			var size = heightMap.GetLength(0);

			var meshSimplificationIncrement = mapParameters.levelOfDetail == 0 ? 1 : mapParameters.levelOfDetail * 2;
			var verticesPerLine = (size - 1) / meshSimplificationIncrement + 1;

			var meshData = new MeshData(verticesPerLine);

			var vertexIndex = 0;

			for (var x = 0; x < size; x += meshSimplificationIncrement)
			for (var y = 0; y < size; y += meshSimplificationIncrement)
			{
				var vertHeight = mapParameters.heightCurve.Evaluate(heightMap[x, y]) * mapParameters.baseHeight *
				                 mapParameters.mapScale;
				var vert = new Vector3(x * mapParameters.mapScale, vertHeight, y * mapParameters.mapScale);
				meshData.vertices[vertexIndex] = vert;

				meshData.uvs[vertexIndex] =
					new Vector2(x / (float) size, y / (float) size);

				if (x < size - 1 && y < size - 1)
				{
					meshData.AddTriangle(vertexIndex, vertexIndex + verticesPerLine + 1, vertexIndex + verticesPerLine);
					meshData.AddTriangle(vertexIndex + verticesPerLine + 1, vertexIndex, vertexIndex + 1);
				}

				vertexIndex++;
			}

			meshData.CalculateNormals();

			return meshData;
		}
Exemplo n.º 3
0
		private IEnumerator ApplyFullMapHeightModifier(float[,] fullHeightMap, MapParameters _parameters,
			GameObject fullMapGameObject)
		{
			var fullHeightMapSize = _parameters.size * fullMapParameters.fullMapSize * _parameters.textureResolutionFactor;
			for (int y = 0; y < fullHeightMapSize; y++)
			{
				for (int x = 0; x < fullHeightMapSize; x++)
				{
					var curveX = fullMapParameters.heightCurveX.Evaluate((float) x / fullHeightMapSize);
					var curveY = fullMapParameters.heightCurveY.Evaluate((float) y / fullHeightMapSize);

					fullHeightMap[x, y] = Mathf.Clamp01(fullHeightMap[x, y] * curveX * curveY);
				}
				if (y % 10 == 0)
					yield return null;

				if (y % (fullHeightMapSize / 3) == 0)
				{
					ProgressBarAddAction(10);
				}
			}

#if UNITY_EDITOR
			if (Application.isEditor)
				EditorCoroutine.Start(GenerateMapChunks(fullHeightMap, _parameters, fullMapGameObject));
			else
				StartCoroutine(GenerateMapChunks(fullHeightMap, _parameters, fullMapGameObject));
#else
			StartCoroutine(GenerateMapChunks(fullHeightMap, _parameters, fullMap));
#endif
		}
Exemplo n.º 4
0
		public void GenerateFullMap(MapParameters mapParameters, FullMapParameters fullMapParameters)
		{
			if (startedGeneration)
			{
				Debug.LogError("Geracao ja iniciada, aguarde");
				return;
			}
			
			if (mapParameters.useMeshColor)
			{
				mapParameters.textureResolutionFactor = 1;
			}
			
			GameObject mapGameObject;
			if (OverrideMesh)
			{
				mapGameObject = GameObject.Find(MapParentName);
				if (mapGameObject)
					DestroyImmediate(mapGameObject);
				mapGameObject = new GameObject(MapParentName);
			}
			else
			{
				mapGameObject = new GameObject();
				mapGameObject.name = MapParentName + " " + mapGameObject.GetInstanceID();
			}

			var fullMapGenerator = mapGameObject.AddComponent<FullMapGenerator>();
			fullMapGenerator.ProgressBarAddAction = ProgressBarAddAction;
			fullMapGenerator.SetGenerator(mapParameters, fullMapParameters);
			fullMapGenerator.StartGeneration();
			
			startedGeneration = true;
		}
Exemplo n.º 5
0
		public GameObject GenerateMapChunk(Vector3 position, MapParameters mapParameters, float[,] noiseMap)
		{
			var heightMap = Noise.SimplifyNoiseMap(noiseMap, mapParameters.textureResolutionFactor);

			var meshData = MeshGenerator.GenerateMeshData(heightMap, mapParameters);
			var mesh = meshData.CreateMesh();
			mesh.name = "Map Mesh";

			GameObject meshGameObject;
			if (mapParameters.useMeshColor)
			{
				var simplification = mapParameters.levelOfDetail == 0 ? 1 : mapParameters.levelOfDetail * 2;
				var simpleHeightMap = Noise.SimplifyNoiseMap(heightMap, simplification);
				mesh.colors = GenerateMeshColorMap(simpleHeightMap);
				meshGameObject = MeshGenerator.CreateMeshGameObject(mesh, null, null);
			}
			else
			{
				var texture2D = GenerateTexture(noiseMap);
				texture2D.filterMode = FilterMode.Point;
				if (mapParameters.useNormalMap)
				{
					Texture2D normalMap = TextureGenerator.NormalMap(texture2D, mapParameters.normalMapStrength);
					meshGameObject = MeshGenerator.CreateMeshGameObject(mesh, texture2D, normalMap);
				}
				else
					meshGameObject = MeshGenerator.CreateMeshGameObject(mesh, texture2D, null);
			}

			meshGameObject.transform.position = position * mapParameters.mapScale;
			return meshGameObject;
		}
Exemplo n.º 6
0
		private void GenerateFullMapGameObject(float[,] fullHeightMap, MapParameters _parameters)
		{
			GameObject mapGameObject;
			if (OverrideMesh)
			{
				mapGameObject = GameObject.Find(MapParentName);
				if (mapGameObject)
					DestroyImmediate(mapGameObject);
				mapGameObject = new GameObject(MapParentName);
			}
			else
			{
				mapGameObject = new GameObject();
				mapGameObject.name = MapParentName + " " + mapGameObject.GetInstanceID();
			}

#if UNITY_EDITOR
			if (Application.isEditor)
				EditorCoroutine.Start(ApplyFullMapHeightModifier(fullHeightMap, _parameters, mapGameObject));
			else
				StartCoroutine(ApplyFullMapHeightModifier(fullHeightMap, _parameters, mapGameObject));
#else
			StartCoroutine(ApplyFullMapHeightModifier(fullHeightMap, _parameters, mapGameObject));
#endif
		}
Exemplo n.º 7
0
		public void GenerateFullMap(MapParameters _parameters)
		{
			if (startedGeneration)
			{
				Debug.LogError("Geracao ja iniciada, aguarde");
				return;
			}

			startedGeneration = true;

			if (_parameters.useMeshColor)
			{
				_parameters.textureResolutionFactor = 1;
			}

			noiseGen = new Noise();
			var fullHeightMapSize = _parameters.size * fullMapParameters.fullMapSize * _parameters.textureResolutionFactor;
			//noiseGen.OnNoiseIsReady.AddListener(GenerateFullMapGameObject);
			noiseGen.ProgressBarAddAction = ProgressBarAddAction;


#if UNITY_EDITOR
			if (Application.isEditor)
				EditorCoroutine.Start(noiseGen.IEGenerateFastNoiseMap(_parameters, fullHeightMapSize, _parameters.noisePosition));
			else
				StartCoroutine(noiseGen.IEGenerateFastNoiseMap(_parameters, fullHeightMapSize, _parameters.noisePosition));
#else
			StartCoroutine(noiseGen.IEGenerateFastNoiseMap(_parameters, fullHeightMapSize, _parameters.noisePosition));
#endif
		}
Exemplo n.º 8
0
		public void UpdateProfile()
		{
			if (mapProfile && !OverrideProfile)
			{
				var mapProperties = mapProfile as MapProperties;
				parameters = mapProperties.MapParameters;
				fullMapParameters = mapProperties.FullMapParameters;
			}
		}
Exemplo n.º 9
0
		public void SetGenerator(MapParameters mapParameters, FullMapParameters fullMapParameters)
		{
			_mapParameters = mapParameters;
			_fullMapParameters = fullMapParameters;
			
			_mapParent = GameObject.Find(MapParentName);
			_mapGenerator = FindObjectOfType<MapGenerator>();
			_fullMapSize = _fullMapParameters.fullMapSize * (_mapParameters.size - 1) + 1;
			
			GeneratorIsSet = true;
		}
Exemplo n.º 10
0
		public static TerrainData CreateTerrain(MapParameters par, int mapSize)
		{
			mapSize = Mathf.ClosestPowerOfTwo(mapSize);
			var noiseGen = new Noise();
			var heightMap = noiseGen.GenerateFastNoiseMap(par, mapSize, par.noisePosition);

			var terrainData = new TerrainData
			{
				size = new Vector3(mapSize / 4, par.baseHeight, mapSize / 4),
				heightmapResolution = mapSize
			};

			for (var x = 0; x < mapSize; x++)
			for (var y = 0; y < mapSize; y++)
				heightMap[x, y] = par.heightCurve.Evaluate(heightMap[x, y]);
			terrainData.SetHeights(0, 0, heightMap);

			return terrainData;
		}
Exemplo n.º 11
0
		public IEnumerator IEGenerateFastNoiseMap(MapParameters par, int mapSize, Vector2 noisePosition)
		{		
			var fastNoise = new FastNoise(par.noiseSeed);

			if (Mathf.Approximately(par.noiseScale, 0))
				par.noiseScale = 0.001f;

			var frequency = 1 / par.noiseScale;
			fastNoise.SetFrequency(frequency);
			fastNoise.SetFractalOctaves(par.octaves);
			fastNoise.SetFractalLacunarity(par.lacunarity);
			fastNoise.SetNoiseType(par.noiseType);

			float textureResolutionFactor = par.textureResolutionFactor;

			mapSize = mapSize * (int) textureResolutionFactor;
			var result = new float[mapSize, mapSize];

			for (var x = 0; x < mapSize; x++)
			{
				for (var y = 0; y < mapSize; y++)
				{
					var sampleX = x / textureResolutionFactor + noisePosition.x;
					var sampleY = y / textureResolutionFactor + noisePosition.y;
					var noiseHeight = fastNoise.GetNoise(sampleX, sampleY);
					result[x, y] = (noiseHeight + 1)/2;
				}

				if (x % 100 == 0)
					yield return null;
				
				if (x % (mapSize / 2) == 0)
				{
					ProgressBarAddAction.Invoke(15);
				}
			}
			
			ProgressBarAddAction.Invoke(10);
			
			OnNoiseIsReady.Invoke(result);
		}
Exemplo n.º 12
0
		private IEnumerator GenerateMapChunks(float[,] fullHeightMap, MapParameters _parameters, GameObject fullMapGameObject)
		{
			for (int y = 0; y < fullMapParameters.fullMapSize; y++)
			{
				for (int x = 0; x < fullMapParameters.fullMapSize; x++)
				{
					var position = new Vector2Int(x * (_parameters.size - 1), y * (_parameters.size - 1));
					var heightMap = Noise.SliceNoiseMap(fullHeightMap, position * _parameters.textureResolutionFactor,
						_parameters.size * _parameters.textureResolutionFactor);
					var mapChunk = GenerateMapChunk(new Vector3(position.x, 0, position.y), _parameters, heightMap);
					mapChunk.transform.parent = fullMapGameObject.transform;
					mapChunk.tag = MapTag;
					if (!AddCollider) continue;
					var collider = mapChunk.AddComponent<MeshCollider>();
					collider.material = defaultPhysicMaterial;
				}

				if (fullMapParameters.fullMapSize > 1)
				{
					if (y % (fullMapParameters.fullMapSize / 2) == 0)
					{
						ProgressBarAddAction(15);
						yield return null;
					}
				}
				else
				{
					ProgressBarAddAction(30);
					yield return null;
				}
			}

			var fullMapSize = (_parameters.size - 1) * fullMapParameters.fullMapSize * _parameters.mapScale;
			fullMapGameObject.transform.position = new Vector3(-fullMapSize / 2, 0, -fullMapSize / 2);
			startedGeneration = false;
			OnMapReady.Invoke();
			Debug.Log("Geracao Finalizada");
		}
Exemplo n.º 13
0
		public TextureParameters(float[,] noiseMap, MapParameters mapParameters)
		{
			if (noiseMap == null) throw new ArgumentNullException(nameof(noiseMap));
			this.noiseMap = noiseMap;
			this.mapParameters = mapParameters;
		}