I have been trying to apply a texture to a terrain from C for over a week now. New to Unity3D however applying a texture to a primitive took me 30 seconds to figure out what am I not doing correctly here?
using UnityEngine;
using System.Collections;
using Utility;
public class test : MonoBehaviour
{
//use this for initialization
void Start ()
{
//create a new terrain data
TerrainData _terrainData = new TerrainData();
//set terrain width, height, length
_terrainData.size = new Vector3(20, 1, 20);
SplatPrototype[] terrainTexture = new SplatPrototype[1];
terrainTexture[0] = new SplatPrototype();
terrainTexture[0].texture = (Texture2D)Resources.Load("Terrain Assets/Terrain Textures/Grass (Hill)");
_terrainData.splatPrototypes = terrainTexture;
//Create a terrain with the set terrain data
GameObject _terrain = Terrain.CreateTerrainGameObject(_terrainData);
}
// Update is called once per frame
void Update () {
}
}
I create the splatmap, load the texture, how is it then applied?
I understand that I will need to ultimately have more than one texture and itterate through the heightmap (something like below) to apply the textures but a little clue as to just how to see a single texture on my terrain is what I need right now. How is this done?
float[,,] map = new float[_terrainData.alphamapWidth, _terrainData.alphamapHeight, 2];
// For each point on the alphamap...
for (var y = 0; y < _terrainData.alphamapHeight; y++) {
//print("Y = " + y.ToString());
for (var x = 0; x < _terrainData.alphamapWidth; x++) {
// Get the normalized terrain coordinate that
// corresponds to the the point.
var normX = x * 1.0 / (_terrainData.alphamapWidth - 1);
var normY = y * 1.0 / (_terrainData.alphamapHeight - 1);
//print("X = " + normX.ToString());
// Get the steepness value at the normalized coordinate.
var angle = _terrainData.GetSteepness(normX, normY);
// Steepness is given as an angle, 0..90 degrees. Divide
// by 90 to get an alpha blending value in the range 0..1.
//var frac = angle / 90.0;
//map[x, y, 0] = frac;
//map[x, y, 1] = 1 - frac;
}
}
_terrainData.SetAlphamaps(0, 0, map);
You are only using 1 splatmap so the third array size of map needs to be 1
There value used should be 1 as well because you want your grass texture to show every were.
Try this, havnt tested it so sorry if it doesnt work.
float[,,] map = new float[_terrainData.alphamapWidth, _terrainData.alphamapHeight, 1]; // needs to be 1
// For each point on the alphamap...
for (var y = 0; y < _terrainData.alphamapHeight; y++) {
for (var x = 0; x < _terrainData.alphamapWidth; x++) {
map[x, y, 0] = 1.0; //needs to be 1 as well
}
}
_terrainData.SetAlphamaps(0, 0, map);
I have tried your suggest and still I see nothing.
Isn’t there a way to just apply the loaded texture without iterating through the alphamap width and height?
Perhaps my texture load is incorrect - how do I test that the texture is indeed loading?
What should the map alphamap array look like - can somebody just post an array to show me how it’s structured?
Can’t believe how poor the documentation is on this.
using UnityEngine;
using System.Collections;
using Utility;
public class test : MonoBehaviour
{
//use this for initialization
void Start ()
{
//create a new terrain data
TerrainData _terrainData = new TerrainData();
//set terrain width, height, length
_terrainData.size = new Vector3(20, 1, 20);
SplatPrototype[] terrainTexture = new SplatPrototype[1];
terrainTexture[0] = new SplatPrototype();
terrainTexture[0].texture = (Texture2D)Resources.Load("Terrain Assets/Terrain Textures/Grass (Hill)");
_terrainData.splatPrototypes = terrainTexture;
//PLEASE IGNORE THIS - AT THE MOMENT THIS IS JUST ADDING COMPLEXITY I DO NOT NEED
//HOW DO I APPLY THE TEXTURE LOADED ABOVE EVERYWHERE?
float[,,] map = new float[_terrainData.alphamapWidth, _terrainData.alphamapHeight, 1]; // needs to be 1
// For each point on the alphamap...
for (var y = 0; y < _terrainData.alphamapHeight; y++)
{
for (var x = 0; x < _terrainData.alphamapWidth; x++)
{
map[x, y, 0] = (float)1.0; //needs to be 1 as well
}
}
_terrainData.SetAlphamaps(0, 0, map);
//PLEASE IGNORE THIS - AT THE MOMENT THIS IS JUST ADDING COMPLEXITY I DO NOT NEED
//HOW DO I APPLY THE TEXTURE LOADED ABOVE EVERYWHERE?
//Create a terrain with the set terrain data
GameObject _terrain = Terrain.CreateTerrainGameObject(_terrainData);
}
// Update is called once per frame
void Update () {
}
}
I know this was some time ago, but Resources.Load requires that your texture be in a ‘Resources’ directory (it can have any path up until the ‘Resources’ directory). And it looks like you are trying to load it from a different path.