So I’ve been following a course for procedural terrain generation and I’m completely new to all of these. I created a table for my textures for splatmaps and a class for them in my other script. But when I save my project the terrain loses it’s texture completely.
what am I doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEditor;
using System.Linq;
using Unity.VisualScripting;
using static CustomTerrain;
using System.Globalization;
[ExecuteInEditMode]
public class CustomTerrain : MonoBehaviour
//there are other codes but im just showing the ones that are related to my question
//SplatMaps-----------------------------------
[System.Serializable]
public class SplatHeights
{
public Texture2D texture = null;
public float minHeight = 0.1f;
public float maxHeight = 0.2f;
public float minSlope = 0;
public float maxSlope = 1.5f;
public Vector2 tileOffset = new Vector2(0, 0);
public Vector2 tileSize = new Vector2(50, 50);
public float splatOffset = 0.1f;
public float splatNoiseXScale = 0.01f;
public float splatNoiseYScale = 0.01f;
public float splatNoiseScaler = 0.1f;
public bool remove = false;
}
public List<SplatHeights> splatHeights = new List<SplatHeights>()
{
new SplatHeights()
};
//SplatMaps
public void AddNewSplatHeight()
{
splatHeights.Add(new SplatHeights());
}
public void RemoveSplatHeight()
{
List<SplatHeights> keptSplatHeights = new List<SplatHeights>();
for (int i = 0; i < splatHeights.Count; i++)
{
if (!splatHeights[i].remove)
{
keptSplatHeights.Add(splatHeights[i]);
}
}
if (keptSplatHeights.Count == 0) //don't want to keep any
{
keptSplatHeights.Add(splatHeights[0]); //add at least 1
}
splatHeights = keptSplatHeights;
}
float GetSteepness(float[,] heightmap, int x, int y, int width, int height)
{
float h = heightmap[x, y];
int nx = x + 1;
int ny = y + 1;
if (nx > width - 1) nx = x - 1;
if (ny > height - 1) ny = y - 1;
float dx = heightmap[nx, y] - h;
float dy = heightmap[x,ny] - h;
Vector2 gradient = new Vector2(dx, dy);
float steep=gradient.magnitude;
return steep;
}
public void SplatMaps()
{
TerrainLayer[] newSplatPrototype;
newSplatPrototype= new TerrainLayer[splatHeights.Count];
int spindex = 0;
foreach (SplatHeights sh in splatHeights)
{
newSplatPrototype[spindex] = new TerrainLayer();
newSplatPrototype[spindex].diffuseTexture = sh.texture;
newSplatPrototype[spindex].tileOffset = sh.tileOffset;
newSplatPrototype[spindex].tileSize = sh.tileSize;
newSplatPrototype[spindex].diffuseTexture.Apply(true);
string path = "Assets/New Terrain Layer" + spindex + ".terrainlayer";
spindex++;
Selection.activeGameObject = this.gameObject;
}
terrainData.terrainLayers = newSplatPrototype;
float[,] heightMap = terrainData.GetHeights(0, 0, terrainData.heightmapResolution,
terrainData.heightmapResolution);
float[,,] splatmapData = new float[terrainData.alphamapWidth,
terrainData.alphamapHeight,
terrainData.alphamapLayers];
for (int y = 0; y < terrainData.alphamapHeight; y++)
{
for (int x = 0; x < terrainData.alphamapWidth; x++)
{
float[] splat = new float[terrainData.alphamapLayers];
for (int i = 0; i < splatHeights.Count; i++)
{
float noise = Mathf.PerlinNoise(x * splatHeights[i].splatNoiseXScale,
y * splatHeights[i].splatNoiseYScale) *
splatHeights[i].splatNoiseScaler;
float offset = splatHeights[i].splatOffset + noise;
float thisHeightStart = splatHeights[i].minHeight - offset;
float thisHeightStop = splatHeights[i].maxHeight + offset;
/* float steepness = GetSteepness(heightMap, x, y,
terrainData.heightmapResolution,
terrainData.heightmapResolution);*/
float steepness = terrainData.GetSteepness(y / (float)terrainData.alphamapHeight, x / (float)terrainData.alphamapWidth);
if ((heightMap[x, y] >= thisHeightStart && heightMap[x, y] <= thisHeightStop) &&
(steepness >= splatHeights[i].minSlope && steepness <= splatHeights[i].maxSlope))
{
splat[i] = 1;
}
}
NormalizeVector(splat);
for (int j = 0; j < splatHeights.Count; j++)
{
splatmapData[x, y, j] = splat[j];
}
}
}
terrainData.SetAlphamaps(0, 0, splatmapData);
}
void NormalizeVector(float[] v)
{
float total = 0;
for (int i = 0; i < v.Length; i++)
{
total += v[i];
}
for (int i = 0; i < v.Length; i++)
{
v[i] /= total;
}
}