ZMWSLI0-SL2021-GR11/cw6/Terrain/Assets/Scripts/ProceduralTextureScript.cs

82 lines
2.0 KiB
C#
Raw Normal View History

2021-06-28 21:23:25 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq; // used for Sum of array
2021-06-28 21:42:09 +02:00
public class ProceduralTextureScript : MonoBehaviour
{
2021-06-28 21:23:25 +02:00
public float snowHeight;
public float rockSlope;
// Start is called before the first frame update
void Start()
{
2021-06-28 21:42:09 +02:00
2021-06-28 21:23:25 +02:00
}
2021-06-28 21:42:09 +02:00
float transition(float start, float end, float value)
{
if (start > end)
{
2021-06-28 21:23:25 +02:00
return 1 - transition(end, start, value);
}
2021-06-28 21:42:09 +02:00
if (value < start)
{
2021-06-28 21:23:25 +02:00
return 0;
}
2021-06-28 21:42:09 +02:00
if (value >= end)
{
2021-06-28 21:23:25 +02:00
return 1;
}
var scaledValue = (value - start) / (end - start);
return scaledValue;
}
2021-06-28 21:42:09 +02:00
public void runProcedrualTexturing()
{
2021-06-28 21:23:25 +02:00
var terrain = gameObject.GetComponent<Terrain>();
var layers = terrain.terrainData.alphamapLayers;
var height = terrain.terrainData.alphamapWidth;
var width = terrain.terrainData.alphamapHeight;
Debug.Log(layers);
Debug.Log(height);
Debug.Log(width);
2021-06-28 21:42:09 +02:00
Debug.Log(terrain.terrainData.GetInterpolatedHeight(0.5f, 0.5f));
2021-06-28 21:23:25 +02:00
var newSplatMap = new float[width, height, layers];
//for (int i=0; i<)
2021-06-28 21:42:09 +02:00
for (int i = 0; i < width; i++)
{
for (int j = 0; j < width; j++)
{
2021-06-28 21:23:25 +02:00
float x = j / (float)height;
float y = i / (float)width;
var splatWeights = new float[layers];
2021-06-28 21:42:09 +02:00
var terrainSteepness = terrain.terrainData.GetSteepness(x, y);
for (int k = 0; k < layers; k++)
{
splatWeights[k] = terrainSteepness;
2021-06-28 21:23:25 +02:00
}
float sum = splatWeights.Sum();
2021-06-28 21:42:09 +02:00
for (int k = 0; k < layers; k++)
{
newSplatMap[i, j, k] = splatWeights[k] / sum;
2021-06-28 21:23:25 +02:00
}
}
}
terrain.terrainData.SetAlphamaps(0, 0, newSplatMap);
}
// Update is called once per frame
void Update()
{
2021-06-28 21:42:09 +02:00
2021-06-28 21:23:25 +02:00
}
}