public void PerlinNoise() { float[,] heightMap = GetHeightMap(); for (int y = 0; y < terrainData.heightmapResolution; y++) { for (int x = 0; x < terrainData.heightmapResolution; x++) { heightMap[x, y] += TerrainOptions.fBm((x + perlinXoffset) * perlinX, (y + perlinYoffset) * perlinY, perlinOctaves, perlinPersistance) * perlinHeightScale; } } terrainData.SetHeights(0, 0, heightMap); }
public void MultiplePerlinNoise() { float[,] heightMap = GetHeightMap(); for (int y = 0; y < terrainData.heightmapResolution; y++) { for (int x = 0; x < terrainData.heightmapResolution; x++) { //adding all the PerlinNoise Curves foreach (PerlinParameters t in perlinParameters) { heightMap[x, y] += TerrainOptions.fBm((x + t.mPerlinXoffset) * t.mPerlinX, (y + t.mPerlinYoffset) * t.mPerlinY, t.mPerlinOctaves, t.mPerlinPersistance) * t.mPerlinHeightScale; } } } terrainData.SetHeights(0, 0, heightMap); }
public Level GenerateNextLevel(int seed, int i) { //int difficulty; seed = seed + (seed + 1) * i; thisSeed = seed; Random rand = new Random(seed); Level levelNew = levelBase;//Instantiate(levelBase); TerrainOptions options = levelNew.terrainBuilder.terrainOptions; int[] sizes = { 20, 30, 40 }; var randInd = rand.Next(0, sizes.Length); options.width = options.height = sizes[randInd]; options.seed = seed; options.mountainCount = rand.Next(2, 6); options.waterCount = rand.Next(1, 5); options.maxWaterSize = rand.Next(10, 40); levelNew.enemySpawns.Clear(); int nbEnnemy = rand.Next(i, i * 2); //nbEnnemy = 8;//nbEnnemy > 8 ? 8 : nbEnnemy; for (int j = 0; j < nbEnnemy; j++) { EnemySpawn es = new EnemySpawn(); es.position = new Vector2(rand.Next(-options.width / 2 + 1, options.width / 2 - 1), rand.Next(-options.height / 2 + 1, options.height / 2 - 1)); Array values = Enum.GetValues(typeof(EntityType)); es.entityType = (EntityType)values.GetValue(rand.Next(values.Length)); es.entityType = SoftEntityType(rand, es.entityType, (i - 2) / 3f); levelNew.enemySpawns.Add(es); } levelList.addLevel(levelNew); return(levelNew); }
public void Init(TerrainOptions terrainOptions) { _gridSize = terrainOptions.width; // Terrain limits Vector3 halfVec = Vector3.one / 2; halfVec.y = 0f; terrainStart.position = -Vector3.right * terrainOptions.width / 2 - Vector3.forward * terrainOptions.height / 2 + halfVec; terrainEnd.position = Vector3.right * terrainOptions.width / 2 + Vector3.forward * terrainOptions.height / 2 - halfVec; float castHeight = terrainOptions.maxMountainHeight + 2; // INIT GRID int xInd = 0, zInd = 0; _grid = new int[_gridSize][]; for (float z = terrainStart.position.z; z <= terrainEnd.position.z; z++) { _grid[zInd] = new int[_gridSize]; for (float x = terrainStart.position.x; x <= terrainEnd.position.x; x++) { Physics.Raycast(new Vector3(x, castHeight, z), Vector3.down, out var hitInfo, 100f, 1 << 8); _grid[zInd][xInd] = hitInfo.point.y > 0.01f || hitInfo.point.y < -0.01f ? 1 : 0; xInd++; } xInd = 0; zInd++; } terrainStart.position = halfVec; terrainEnd.position = Vector3.right * terrainOptions.width + Vector3.forward * terrainOptions.height - halfVec; _wayPoints = new List <Vector3>(); _wayPoints = new List <Vector3>(); var linesCount = _grid.Length; var colsCount = _grid[0].Length; var costMatrixWidth = linesCount * colsCount; _costMatrix = new NativeArray <float>(costMatrixWidth * costMatrixWidth, Allocator.Persistent); _heuristicMatrix = new NativeArray <float>(costMatrixWidth, Allocator.Persistent); for (var i = 0; i < linesCount; i++) { for (var j = 0; j < colsCount; j++) { var sourceIdx = i * colsCount + j; for (var i1 = 0; i1 < linesCount; i1++) { for (var j1 = 0; j1 < colsCount; j1++) { var targetIdx = i1 * colsCount + j1; if (_grid[i][j] != 1 && _grid[i1][j1] != 1 && (i == i1 && Mathf.Abs(j - j1) == 1 || j == j1 && Mathf.Abs(i - i1) == 1)) { _costMatrix[sourceIdx * costMatrixWidth + targetIdx] = 1; } else { _costMatrix[sourceIdx * costMatrixWidth + targetIdx] = float.MaxValue; } } } } } }