Advertisement
Guest User

Error

a guest
Aug 30th, 2015
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.64 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Linq; // used for Sum of array
  4.  
  5. public class AssignSplatMap : MonoBehaviour {
  6.  
  7.     void Start () {
  8.         // Get the attached terrain component
  9.                 Terrain terrain = GetComponent<Terrain>();
  10.          
  11.         // Get a reference to the terrain data
  12.             TerrainData terrainData = terrain.terrainData;
  13.  
  14.         // Splatmap data is stored internally as a 3d array of floats, so declare a new empty array ready for your custom splatmap data:
  15.         float[, ,] splatmapData = new float[terrainData.alphamapWidth, terrainData.alphamapHeight, terrainData.alphamapLayers];
  16.          
  17.         for (int y = 0; y < terrainData.alphamapHeight; y++)
  18.             {
  19.             for (int x = 0; x < terrainData.alphamapWidth; x++)
  20.              {
  21.                 // Normalise x/y coordinates to range 0-1
  22.                 float y_01 = (float)y/(float)terrainData.alphamapHeight;
  23.                 float x_01 = (float)x/(float)terrainData.alphamapWidth;
  24.                  
  25.                 // Sample the height at this location (note GetHeight expects int coordinates corresponding to locations in the heightmap array)
  26.                         float height = terrainData.GetHeight(Mathf.RoundToInt(y_01 * terrainData.heightmapHeight),Mathf.RoundToInt(x_01 * terrainData.heightmapWidth) );
  27.                  
  28.                 // Calculate the normal of the terrain (note this is in normalised coordinates relative to the overall terrain dimensions)
  29.                 Vector3 normal = terrainData.GetInterpolatedNormal(y_01,x_01);
  30.      
  31.                 // Calculate the steepness of the terrain
  32.                 float steepness = terrainData.GetSteepness(y_01,x_01);
  33.                  
  34.                 // Setup an array to record the mix of texture weights at this point
  35.                 float[] splatWeights = new float[terrainData.alphamapLayers];
  36.                  
  37.                 // CHANGE THE RULES BELOW TO SET THE WEIGHTS OF EACH TEXTURE ON WHATEVER RULES YOU WANT
  38.      
  39.                 // Texture[0] has constant influence
  40.                 splatWeights[0] = 0.5f;
  41.                  
  42.                 // Texture[1] is stronger at lower altitudes
  43.                 splatWeights[1] = Mathf.Clamp01((terrainData.heightmapHeight - height));
  44.                  
  45.                 // Texture[2] stronger on flatter terrain
  46.                 // Note "steepness" is unbounded, so we "normalise" it by dividing by the extent of heightmap height and scale factor
  47.                 // Subtract result from 1.0 to give greater weighting to flat surfaces
  48.                 splatWeights[2] = 1.0f - Mathf.Clamp01(steepness*steepness/(terrainData.heightmapHeight/5.0f));
  49.                  
  50.                 // Texture[3] increases with height but only on surfaces facing positive Z axis
  51.                 splatWeights[3] = height * Mathf.Clamp01(normal.z);
  52.                  
  53.                 // Sum of all textures weights must add to 1, so calculate normalization factor from sum of weights
  54.                 float z = splatWeights.Sum();
  55.                  
  56.                 // Loop through each terrain texture
  57.                 for(int i = 0; i<terrainData.alphamapLayers; i++){
  58.                      
  59.                     // Normalize so that sum of all texture weights = 1
  60.                     splatWeights[i] /= z;
  61.                      
  62.                     // Assign this point to the splatmap array
  63.                     splatmapData[x, y, i] = splatWeights[i];
  64.                 }
  65.             }
  66.         }
  67.      
  68.         // Finally assign the new splatmap to the terrainData:
  69.         terrainData.SetAlphamaps(0, 0, splatmapData);
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement