Th3NiKo

Terrain

Jun 18th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class terrainTest : MonoBehaviour {
  6.  
  7.     public int width = 256;
  8.     public int height = 256;
  9.  
  10.     public int depth = 20;
  11.     public float scale = 20f;
  12.  
  13.  
  14.     void Update () {
  15.         Terrain terrain = GetComponent<Terrain>();
  16.         terrain.terrainData = Generate(terrain.terrainData);       
  17.     }
  18.  
  19.  
  20.     TerrainData Generate(TerrainData terrainData){
  21.         terrainData.heightmapResolution = width + 1;
  22.         terrainData.size = new Vector3(width, depth, height);
  23.         terrainData.SetHeights(0,0,GenerateHeights());
  24.         return terrainData;
  25.     }
  26.  
  27.     float[,] GenerateHeights(){
  28.         float[,] heights = new float [width, height];
  29.         for(int x = 0; x < width; x++){
  30.             for(int y = 0; y < height; y++){
  31.                 heights[x,y] = CalculateHeight(x,y);
  32.             }
  33.         }
  34.         return heights;
  35.     }
  36.  
  37.     float CalculateHeight(float x, float y){
  38.         float xCoord = (float)x / width * scale;
  39.         float yCoord = (float)y / height * scale;
  40.         float perlin = Mathf.PerlinNoise(xCoord, yCoord);
  41.         return perlin;
  42.     }
  43.    
  44. }
Advertisement
Add Comment
Please, Sign In to add comment