duck

Unity 3D Procedural Terrain - Step 2 - Overapping Sine Wave

Mar 1st, 2013
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class TerrainStuff : MonoBehaviour {
  5.    
  6.     Terrain terrain;
  7.    
  8.     // Use this for initialization
  9.    
  10.     // frequencies
  11.     float f1,f2;
  12.    
  13.     // heights
  14.     float h1,h2;
  15.    
  16.     void OnGUI()
  17.     {
  18.         GUILayout.Label("Frequency 1: "+f1);
  19.         f1 = GUILayout.HorizontalSlider(f1, .001f, 1f);
  20.        
  21.         GUILayout.Label("Height 1: "+h1);
  22.         h1 = GUILayout.HorizontalSlider(h1, .01f, 1f);
  23.        
  24.         GUILayout.Label("Frequency 2: "+f2);
  25.         f2 = GUILayout.HorizontalSlider(f2, .001f, 1f);
  26.        
  27.         GUILayout.Label("Height 2: "+h2);
  28.         h2 = GUILayout.HorizontalSlider(h2, .01f, 1f);
  29.        
  30.        
  31.        
  32.     }
  33.    
  34.    
  35.     void SetTerrainHeights () {
  36.        
  37.         terrain = GetComponent<Terrain>(); 
  38.        
  39.         TerrainData data = terrain.terrainData;
  40.        
  41.         float[,] heights = data.GetHeights(0, 0, data.heightmapWidth, data.heightmapHeight);
  42.            
  43.         for( int x=0; x<512; ++x)
  44.         {
  45.             for( int z=0; z<512; ++z)
  46.             {
  47.                 // resuts
  48.                 float r1 =  Mathf.Sin ( x*f1 ) * h1 + h1;
  49.                 float r2 =  Mathf.Sin ( z*f2 ) * h2 + h2;
  50.                
  51.                 heights[x,z] = r1 + r2;
  52.                
  53.             }
  54.         }
  55.        
  56.         data.SetHeights(0, 0, heights);
  57.        
  58.     }
  59.    
  60.     void Update()
  61.     {
  62.         SetTerrainHeights();   
  63.     }
  64.    
  65.    
  66. }
Advertisement
Add Comment
Please, Sign In to add comment