TerrainData.GetAlphamaps finding only 0 and 1 textures in splatdata array

Hello!

So I’m making a script where I’m spawning objects based on what texture is on the terrain at that point. I find a random point, then I find the texture of the terrain using GetAlphamaps. However, my terrain has 3 textures on it, yet GetAlphaMaps only finds 2 (it returns only 0 and 1).

Does GetAlphaMaps work with more than 2 terrain textures? If not, how can I get by without GetAlphaMaps?

Thank you so much!

FULL SCRIPT:

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class CustomPlacerScript1 : MonoBehaviour {
    public Terrain terrain;
    float[,,] SplatData;
    float CurrentSplatIndex;
    public int TotalObjectsToSpawn;
    public int terrainWidth;
    public int terrainLength;
    public int terrainPosX;
    public int terrainPosZ;
    public Biome[] biomes;
    public Vector3 CurrentTerrainPoint;
    float RandomX;
    float RandomY;
    float RandomZ;
    // Use this for initialization
    void Start ()
    {
        terrainWidth = (int)terrain.terrainData.size.x;
        terrainLength = (int)terrain.terrainData.size.z;
        terrainPosX = (int)terrain.transform.position.x;
        terrainPosZ = (int)terrain.transform.position.z;
        SplatData = terrain.terrainData.GetAlphamaps(0, 0, terrain.terrainData.alphamapWidth, terrain.terrainData.alphamapHeight);
        //basically, pick random points on the terrain. For each point, spawn the right object for its texture
        for (int i = 0; i<TotalObjectsToSpawn; i++)
        {
            GetRandomPoint();
            try
            {
                CurrentSplatIndex = SplatData[(int)CurrentTerrainPoint.z, (int)CurrentTerrainPoint.x, 0];
                Debug.Log("FOUND THIS SPLAT:" + CurrentSplatIndex);
                //Debug.Log("CurrentSplatIndex " + CurrentSplatIndex);
                SpawnObject();
            }
            catch { i--; }
        }
    }

    void GetRandomPoint()
    {
        RandomX = Random.Range(terrainPosX, terrainPosX + terrainWidth);
        RandomZ = Random.Range(terrainPosZ, terrainPosZ + terrainLength);
        CurrentTerrainPoint = new Vector3(RandomX, 0, RandomZ);
        CurrentTerrainPoint.y = terrain.SampleHeight(CurrentTerrainPoint);
        //Debug.Log("Terrain point: " + CurrentTerrainPoint);
    }

    void SpawnObject()
    {
        for (int i = 0; i < biomes.Length; i++)
        {
            if (CurrentSplatIndex == biomes[i].textureIndex)
            {
                GameObject obj = Instantiate(biomes[i].prefab, CurrentTerrainPoint, Quaternion.identity) as GameObject;
                float min = biomes[i].minSize;
                float max = biomes[i].maxSize;
                float random = Random.Range(min, max);
                Vector3 scale = new Vector3(random, random, random);
                obj.transform.localScale = scale;
                obj.transform.position = new Vector3(obj.transform.position.x, obj.transform.position.y + scale.x / 2, obj.transform.position.z);
            }
        }
    }
}

Is alphamap really returning your textures? Or just the alphamaps of those? I’m just not clear about what GetAlphamaps really returns.

Okay, just read about it. Is it possible that your terrain has like one texture over the whole terrain? In this case, it could be possible, that this texture needs no alpha map because of being at the lowest layer. only the next two textures need alpha information then.

I’m not so sure. I just painted 3 textures in random places, but when adding the 1st texture Unity automatically fills the entire terrain with it. Is that what you mean?

Yeah, Thats what I mean. Could you try something? Delete the textures, add one that fills the whole terrain, then add 3 more and check your code again. Debug.Log the names of your textures and see if the last three textures you put at the terrain get logged in the console.

The array GetAlphamaps returns has no texture names, but returns a number. It’s returning only 0 and 1, regardless of how many textures I put on the terrain :frowning:

@DroidfyDevs Alpha maps are not splat textures. Splat textures referenced by a terrain are saved in TerrainData.splatPrototypes and then SplatPrototype.texture.