@@ -25,13 +25,8 @@ which takes these values and constructs the mesh.
2525
2626 You can use the sliders to control the landscape's form in real - time, in Unity Editor's edit-mode
2727 There is also another option, to enable the "animate" choice, which produces*/
28- //main variables
29- float myTime ; // time
30- float myTimeScale ; // timescale: scales the flow of time
31- int landscapeWidthSegments ; // number of segments along the X axis
32- int landscapeLengthSegments ; // number of segments along the Z axis
33- float landscapeWidth ; // landscape width: actual size along the X axis
34- float landscapeLength ; // landscape length: actual size along the Z axis
28+
29+
3530 //landscape formation variables
3631 //float seed;
3732
@@ -93,39 +88,48 @@ will be lost once you spress the Stop button. */
9388 public float layer3Height = 1.0f ; // the current height of Layer 3
9489 float layer3HeightPreviousValue = 0 ; // stores the layer3Height's previous value, for change - detection
9590
96- [ Range ( 1000.0f , 1100.0f ) ]
97- public float layer3OffsetX = 1000.0f ;
98- float layer3OffsetXPreviousValue = 0 ;
91+ [ Range ( 1000.0f , 1100.0f ) ] // layer3OffsetX - value slider ranges from 1000 to 1100
92+ public float layer3OffsetX = 1000.0f ; // The current offset of Layer 3 along the X-axis
93+ float layer3OffsetXPreviousValue = 0 ; // stores the layer3OffsetX's previous value, for change - detection
9994
100- [ Range ( 1000.0f , 1100.0f ) ]
101- public float layer3OffsetZ = 1000.0f ;
102- float layer3OffsetZPreviousValue = 0 ;
95+ [ Range ( 1000.0f , 1100.0f ) ] // layer3OffsetZ - value slider ranges from 1000 to 1100
96+ public float layer3OffsetZ = 1000.0f ; // The current offset of Layer 3 along the Z-axis
97+ float layer3OffsetZPreviousValue = 0 ; // stores the layer3OffsetZ's previous value, for change - detection
10398
10499 [ Space ( 8 ) ] // leave a gap of 8 pixels between the parameters in editor
105100
106101 // material - specific parameters
107- public Color peakColor ; // the color that will be applied at the peak of the hills
108- Color peakColorPreviousValue ; //the previous value of peakColor, for change - detection
102+ public Color peakColor ; // the color that will be applied at the peak of the hills
103+ Color peakColorPreviousValue ; //the previous value of peakColor, for change - detection
109104
110- public Color bottomColor ; // the color that will be used at the bottom of the hills
111- Color bottomColorPreviousValue ; // the previous value of bottomColor, for change - detection
105+ public Color bottomColor ; // the color that will be used at the bottom of the hills
106+ Color bottomColorPreviousValue ; // the previous value of bottomColor, for change - detection
112107
113- [ Range ( 0.0f , 1.0f ) ]
114- public float materialSmoothness ;
115- float materialSmoothnessPreviousValue ;
108+ [ Range ( 0.0f , 1.0f ) ] // Material Smoothness - value slider ranges from 1000 to 1100
109+ public float materialSmoothness ; // The current Material Smoothness
110+ float materialSmoothnessPreviousValue ; // materialSmoothness's previous value, for change detection
116111
117- [ Range ( 0.0f , 1.0f ) ]
118- public float materialMetallicness ;
119- float materialMetallicnessPreviousValue ;
112+ [ Range ( 0.0f , 1.0f ) ] // Material Metallicness - value slider ranges from 1000 to 1100
113+ public float materialMetallicness ; // The current Material Metallicness
114+ float materialMetallicnessPreviousValue ; // materialMetallicness's previous value, for change detection
120115
121116
117+ //main variables
118+ float localTime ; // time
119+ float localTimeScale ; // timescale: scales the flow of time
120+
122121 Mesh myMesh ; // the mesh (is created in this script)
123122 Material landScapeMaterial ; // the material that will be applied on the mesh (is created in this script)
124123 Texture2D landscapeMaterialTexture ; // the material's texture (is created in this script)
124+
125+ int landscapeWidthSegments ; // number of segments along the X axis
126+ int landscapeLengthSegments ; // number of segments along the Z axis
127+ float landscapeWidth ; // landscape width: actual size along the X axis
128+ float landscapeLength ; // landscape length: actual size along the Z axis
125129 //mesh creation lists
126- List < Vector3 > verticeList = new List < Vector3 > ( ) ; //list of vertices
127- List < Vector2 > uvList = new List < Vector2 > ( ) ; //list of uvs
128- List < int > triList = new List < int > ( ) ; //list of triangles
130+ List < Vector3 > verticeList = new List < Vector3 > ( ) ; // list of vertices
131+ List < Vector2 > uvList = new List < Vector2 > ( ) ; // list of uvs
132+ List < int > triList = new List < int > ( ) ; // list of triangles
129133 // Use this for initialization
130134 void Awake ( ) {
131135 //animate = true;
@@ -136,8 +140,8 @@ void Awake () {
136140 landscapeLength = 4000 ;
137141
138142 //initialize landscape formation variables
139- myTime = 0.0f ;
140- myTimeScale = 0.3f ;
143+ localTime = Random . Range ( 0.0f , 1000.0f ) ;
144+ localTimeScale = 0.3f ;
141145 CreateLandscapeGeometry ( ) ; // 1. make the landscape object (geometry)
142146 CreateLandscapeTexture ( ) ; // 2. make the 2d texture
143147 CreateLandscapeMaterial ( ) ; // 3. create the landscape material
@@ -147,27 +151,41 @@ void Awake () {
147151
148152 // Update is called once per frame
149153 void Update ( ) {
150- if ( animate == true ) {
151- /*if the "animate" variable is set to true, then the variables that produce the landscape are animated.
154+ if ( ( animate == true ) && ( Application . isPlaying ) ) {
155+ /*if the "animate" variable is set to true and the Application is Playing,
156+ then the variables that produce the landscape are "animated".
152157 otherwise they are steady and can be changed by the user, using the sliders.*/
153- myTime += Time . deltaTime * myTimeScale ;
154- layer1Scale = map ( Mathf . Sin ( myTime ) , - 1.0f , 1.0f , 700.0f , 2500.0f ) ;
155- layer1Height = map ( Mathf . Sin ( myTime * 1.1f ) , - 1.0f , 1.0f , 1.0f , 700.0f ) ;
156- layer1OffsetX = map ( Mathf . Sin ( myTime * 0.3f ) , - 1.0f , 1.0f , 1000.0f , 1010.0f ) ;
157- layer1OffsetZ = map ( Mathf . Cos ( myTime * 0.4f ) , - 1.0f , 1.0f , 1000.0f , 1010.0f ) ;
158- layer2Scale = map ( Mathf . Sin ( myTime * 1.2f ) , - 1.0f , 1.0f , 100.0f , 150.0f ) ;
159- layer2Height = map ( Mathf . Sin ( myTime * 1.3f ) , - 1.0f , 1.0f , 50.0f , 150.0f ) ;
160- layer3Scale = map ( Mathf . Sin ( myTime * 1.4f ) , - 1.0f , 1.0f , 10.0f , 40.0f ) ;
161- layer3Height = map ( Mathf . Sin ( myTime * 1.5f ) , - 1.0f , 1.0f , 10.0f , 40.0f ) ;
158+ localTime += Time . deltaTime * localTimeScale ;
159+ layer1Scale = map ( Mathf . Sin ( localTime ) , - 1.0f , 1.0f , 700.0f , 2500.0f ) ;
160+ layer1Height = map ( Mathf . Sin ( localTime * 1.1f ) , - 1.0f , 1.0f , 1.0f , 700.0f ) ;
161+ /*layer1OffsetX = map (Mathf.Sin (localTime * 0.3f), -1.0f, 1.0f, 1000.0f, 1010.0f);
162+ layer1OffsetZ = map (Mathf.Cos (localTime * 0.4f), -1.0f, 1.0f, 1000.0f, 1010.0f);*/
163+ layer2Scale = map ( Mathf . Sin ( localTime * 1.2f ) , - 1.0f , 1.0f , 200.0f , 500.0f ) ;
164+ layer2Height = map ( Mathf . Sin ( localTime * 1.3f ) , - 1.0f , 1.0f , 10.0f , 500.0f ) ;
165+ layer3Scale = map ( Mathf . Sin ( localTime * 1.4f ) , - 1.0f , 1.0f , 20.0f , 100.0f ) ;
166+ layer3Height = map ( Mathf . Sin ( localTime * 1.5f ) , - 1.0f , 1.0f , 1.0f , 100.0f ) ;
167+ materialMetallicness = map ( Mathf . Sin ( localTime * 2.0f ) , - 1.0f , 1.0f , 0.0f , 1.0f ) ;
168+ materialSmoothness = map ( Mathf . Sin ( localTime * 1.6f ) , - 1.0f , 1.0f , 0.0f , 1.0f ) ;
169+ bottomColor . r = map ( Mathf . Sin ( localTime * 0.5f ) , - 1.0f , 1.0f , 0.0f , 1.0f ) ;
170+ bottomColor . g = map ( Mathf . Sin ( localTime * 0.6f ) , - 1.0f , 1.0f , 0.0f , 1.0f ) ;
171+ bottomColor . b = map ( Mathf . Sin ( localTime * 0.7f ) , - 1.0f , 1.0f , 0.0f , 1.0f ) ;
172+ peakColor . r = map ( Mathf . Sin ( localTime * 0.3f ) , - 1.0f , 1.0f , 0.0f , 1.0f ) ;
173+ peakColor . g = map ( Mathf . Sin ( localTime * 0.4f ) , - 1.0f , 1.0f , 0.0f , 1.0f ) ;
174+ peakColor . b = map ( Mathf . Sin ( localTime * 0.5f ) , - 1.0f , 1.0f , 0.0f , 1.0f ) ;
162175 }
163176 if ( UpdateVariableValueChange ( ) ) {
177+ /* If ANY of the Updateable Variables has changed, then the Landscape Geometry will be updated.
178+ This works in Edit Mode, as well as in Play Mode. */
164179 UpdateLandscapeGeometry ( ) ;
180+ UpdateLandscapeTexture ( ) ;
181+ UpdateLandscapeMaterial ( ) ;
182+ GetComponent < Renderer > ( ) . material = landScapeMaterial ;
165183 }
166184 }
167185
168186 private void UpdateLandscapeGeometry ( ) {
169- /*This function updates the mesh geometry by repositioning the mesh's existing vertices
170- and then recalculating the normals. The mesh's inner structure does not change.*/
187+ /* This function updates the mesh geometry by repositioning the mesh's existing vertices
188+ and then recalculating the normals. The mesh's inner structure does not change. */
171189 myMesh = GetComponent < MeshFilter > ( ) . sharedMesh ;
172190 int cnt = 0 ;
173191 for ( int i = 0 ; i < landscapeLengthSegments ; i ++ ) {
@@ -179,10 +197,6 @@ and then recalculating the normals. The mesh's inner structure does not change.*
179197 myMesh . vertices = verticeList . ToArray ( ) ;
180198 myMesh . RecalculateNormals ( ) ;
181199 GetComponent < MeshFilter > ( ) . mesh = myMesh ;
182-
183- CreateLandscapeTexture ( ) ;
184- CreateLandscapeMaterial ( ) ;
185- GetComponent < Renderer > ( ) . material = landScapeMaterial ;
186200 }
187201
188202 private void CreateLandscapeGeometry ( ) {
@@ -199,12 +213,12 @@ private void CreateLandscapeGeometry(){
199213 if ( i == 0 || j == 0 )
200214 continue ;
201215 //Adds the index of the three vertices in order to make up each of the two tris
202- triList . Add ( landscapeWidthSegments * i + j ) ; //Top right
203- triList . Add ( landscapeWidthSegments * i + j - 1 ) ; //Bottom right
204- triList . Add ( landscapeWidthSegments * ( i - 1 ) + j - 1 ) ; //Bottom left - First triangle
205- triList . Add ( landscapeWidthSegments * ( i - 1 ) + j - 1 ) ; //Bottom left
206- triList . Add ( landscapeWidthSegments * ( i - 1 ) + j ) ; //Top left
207- triList . Add ( landscapeWidthSegments * i + j ) ; //Top right - Second triangle
216+ triList . Add ( landscapeWidthSegments * i + j ) ; //Top right
217+ triList . Add ( landscapeWidthSegments * i + j - 1 ) ; //Bottom right
218+ triList . Add ( landscapeWidthSegments * ( i - 1 ) + j - 1 ) ; //Bottom left - First triangle
219+ triList . Add ( landscapeWidthSegments * ( i - 1 ) + j - 1 ) ; //Bottom left
220+ triList . Add ( landscapeWidthSegments * ( i - 1 ) + j ) ; //Top left
221+ triList . Add ( landscapeWidthSegments * i + j ) ; //Top right - Second triangle
208222 }
209223 }
210224 myMesh . vertices = verticeList . ToArray ( ) ;
@@ -215,13 +229,34 @@ private void CreateLandscapeGeometry(){
215229 GetComponent < MeshFilter > ( ) . mesh = myMesh ;
216230 }
217231
232+ private void UpdateLandscapeTexture ( ) {
233+ landscapeMaterialTexture = GetComponent < MeshRenderer > ( ) . sharedMaterial . mainTexture as Texture2D ;
234+ float landscapeMinHeight = GetMinimumLandscapeHeight ( ) ;
235+ float landscapeMaxHeight = GetMaximumLandscapeHeight ( ) ;
236+ for ( int i = 0 ; i < landscapeWidthSegments ; i ++ ) {
237+ for ( int j = 0 ; j < landscapeLengthSegments ; j ++ ) {
238+ //cycle through all of the landscape's vertices
239+ Vector3 p = LandscapePoint ( i , j ) ; // get the landscape point
240+ //calculate the landscape point's relative height: where it lies between the current minimum ang maximum height of the landscape
241+ float landscapeRelativeHeight = map ( p . y , landscapeMinHeight , landscapeMaxHeight , 0.0f , 1.0f ) ; // will receive a value 0.0f to 1.0f (relative to minimum to maximum)
242+ //use the
243+ float cr = map ( landscapeRelativeHeight , 0.0f , 1.0f , bottomColor . r , peakColor . r ) ; // set the color's red value accordingly
244+ float cg = map ( landscapeRelativeHeight , 0.0f , 1.0f , bottomColor . g , peakColor . g ) ; // set the color's green value accordingly
245+ float cb = map ( landscapeRelativeHeight , 0.0f , 1.0f , bottomColor . b , peakColor . b ) ; // set the color's blue value accordingly
246+ Color cc = new Color ( cr , cg , cb , 1.0f ) ;
247+ landscapeMaterialTexture . SetPixel ( i , j , cc ) ;
248+ }
249+ }
250+ landscapeMaterialTexture . Apply ( ) ;
251+ }
252+
218253 private void CreateLandscapeTexture ( ) {
219254 //creates the texture that will then be applied to the landscape's material.
220255 landscapeMaterialTexture = new Texture2D ( landscapeWidthSegments , landscapeLengthSegments , TextureFormat . RGB24 , true ) ;
221256 landscapeMaterialTexture . name = "LandscapeMaterialTexture" ;
222257 landscapeMaterialTexture . wrapMode = TextureWrapMode . Clamp ;
223- float landscapeMinHeight = minimumLandscapeHeight ( ) ;
224- float landscapeMaxHeight = maximumLandscapeHeight ( ) ;
258+ float landscapeMinHeight = GetMinimumLandscapeHeight ( ) ;
259+ float landscapeMaxHeight = GetMaximumLandscapeHeight ( ) ;
225260 for ( int i = 0 ; i < landscapeWidthSegments ; i ++ ) {
226261 for ( int j = 0 ; j < landscapeLengthSegments ; j ++ ) {
227262 //cycle through all of the landscape's vertices
@@ -239,6 +274,17 @@ private void CreateLandscapeTexture(){
239274 landscapeMaterialTexture . Apply ( ) ;
240275 }
241276
277+ private void UpdateLandscapeMaterial ( ) {
278+ landScapeMaterial = GetComponent < MeshRenderer > ( ) . sharedMaterial ;
279+ landScapeMaterial . mainTexture = landscapeMaterialTexture ;
280+ Vector2 matScale = new Vector2 ( 1.0f / landscapeWidth , 1.0f / landscapeLength ) ;
281+ landScapeMaterial . mainTextureScale = matScale ;
282+ Vector2 matOffset = new Vector2 ( 0.5f , 0.5f ) ;
283+ landScapeMaterial . mainTextureOffset = matOffset ;
284+ landScapeMaterial . SetFloat ( "_Metallic" , materialMetallicness ) ;
285+ landScapeMaterial . SetFloat ( "_Glossiness" , materialSmoothness ) ;
286+ }
287+
242288 private void CreateLandscapeMaterial ( ) {
243289 /*
244290 This function creates the
@@ -283,7 +329,7 @@ float map(float fromValue, float fromMin, float fromMax, float toMin, float toMa
283329 return toMin + ( fromValue - fromMin ) * ( toMax - toMin ) / ( fromMax - fromMin ) ;
284330 }
285331
286- float minimumLandscapeHeight ( ) {
332+ float GetMinimumLandscapeHeight ( ) {
287333 float h = verticeList [ 0 ] . y ;
288334 for ( int i = 0 ; i < verticeList . Count ; i ++ ) {
289335 if ( verticeList [ i ] . y < h )
@@ -292,7 +338,7 @@ float minimumLandscapeHeight(){
292338 return h ;
293339 }
294340
295- float maximumLandscapeHeight ( ) {
341+ float GetMaximumLandscapeHeight ( ) {
296342 float h = verticeList [ 0 ] . y ;
297343 for ( int i = 0 ; i < verticeList . Count ; i ++ ) {
298344 if ( verticeList [ i ] . y > h )
0 commit comments