Hello everyone.
I was wondering if there is way to check what is the texture at a given point on terrain
Thanks.
Hello everyone.
I was wondering if there is way to check what is the texture at a given point on terrain
Thanks.
Using the information provided in the below links, I have made a uJS version of reading the terrain textures at the player position, and returning an integer for the most dominant terrain texture at the players coordinates. The GUI is only there for a visual indicator, that can all be removed.
Reference Links :
the script ( in Unity JavaScript ) :
// based on the answer here : http://answers.unity3d.com/questions/34328/terrain-with-multiple-splat-textures-how-can-i-det.html
#pragma strict
var surfaceIndex : int = 0;
private var terrain : Terrain;
private var terrainData : TerrainData;
private var terrainPos : Vector3;
function Start()
{
terrain = Terrain.activeTerrain;
terrainData = terrain.terrainData;
terrainPos = terrain.transform.position;
}
function Update()
{
surfaceIndex = GetMainTexture( transform.position );
}
// - just for GUI demonstration -
function OnGUI()
{
GUI.Box( Rect( 10, 10, 25, 25 ), surfaceIndex.ToString() );
}
// ----
function GetTextureMix( worldPos : Vector3 ) : float[]
{
// returns an array containing the relative mix of textures
// on the main terrain at this world position.
// The number of values in the array will equal the number
// of textures added to the terrain.
// calculate which splat map cell the worldPos falls within (ignoring y)
var mapX : int = parseInt( ((worldPos.x - terrainPos.x) / terrainData.size.x) * terrainData.alphamapWidth );
var mapZ : int = parseInt( ((worldPos.z - terrainPos.z) / terrainData.size.z) * terrainData.alphamapHeight );
// get the splat data for this cell as a 1x1xN 3d array (where N = number of textures)
var splatmapData : float[,,] = terrainData.GetAlphamaps( mapX, mapZ, 1, 1 );
// extract the 3D array data to a 1D array:
var cellMix : float[] = new float[ splatmapData.GetUpperBound(2) + 1 ];
for ( var n : int = 0; n < cellMix.Length; n ++ )
{
cellMix[n] = splatmapData[ 0, 0, n ];
}
return cellMix;
}
function GetMainTexture( worldPos : Vector3 ) : int
{
// returns the zero-based index of the most dominant texture
// on the main terrain at this world position.
var mix : float[] = GetTextureMix( worldPos );
var maxMix : float = 0;
var maxIndex : int = 0;
// loop through each mix value and find the maximum
for ( var n : int = 0; n < mix.Length; n ++ )
{
if ( mix[n] > maxMix )
{
maxIndex = n;
maxMix = mix[n];
}
}
return maxIndex;
}
// ----
Thanks For Amazing Work…!