Skip to content
Snippets Groups Projects
Commit 7ac112c4 authored by Julian Thijssen's avatar Julian Thijssen
Browse files

Make procedural shader fast

parent c0253c96
No related branches found
No related tags found
No related merge requests found
......@@ -54,74 +54,67 @@ public class ProceduralShader : MonoBehaviour
public void SetColors(Mesh mesh, Texture2D heightMap, int subdivisions, int numVerts)
{
//Color32[] colors = new Color32[numVerts];
System.DateTime before = System.DateTime.Now;
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
Color32[] colors = new Color32[numVerts];
float[] ys = new float[numVerts];
for (int i = 0; i < numVerts; i++)
{
ys[i] = mesh.vertices[i].y;
}
Vector3[] vertices = mesh.vertices;
Vector3[] normals = mesh.normals;
mesh.RecalculateNormals();
Debug.Log("mesh vertices: " + numVerts);
Debug.Log("mesh normals: " + mesh.normals.Length);
float subdivRecip = 1.0f / (float)(subdivisions + 1);
int index = 0;
int index = -1;
for (int z = 0; z < subdivisions + 1; z++)
{
for (int x = 0; x < subdivisions + 1; x++)
{
if (ys[index] <= maxWaterHeight)
index++;
colors[index] = cliffColor;
float height = vertices[index].y;
// If vertex height less than water, give water color
if (height <= maxWaterHeight)
{
float xDiv = (float)x * subdivRecip * 2 + 200;
float zDiv = (float)z * subdivRecip * 2 + 300;
float perl = Mathf.PerlinNoise(xDiv, zDiv);
colors[index] = waterColor.Evaluate(perl);
continue;
}
if (Vector3.Dot(normals[index], Vector3.up) < 0.5f)
{
colors[index] = cliffColor;
continue;
}
else
{
if (Vector3.Dot(mesh.normals[index], Vector3.up) < 0.5f)
{
colors[index] = cliffColor;
index++;
continue;
}
else
float xDiv = (float)x * subdivRecip * 8;
float zDiv = (float)z * subdivRecip * 8;
float perl = Mathf.PerlinNoise(xDiv, zDiv);
colors[index] = terrainColorPerlin.Evaluate(perl);
if(height > snowHeightMin)
{
float xDiv = (float)x * subdivRecip * 8;
float zDiv = (float)z * subdivRecip * 8;
float perl = Mathf.PerlinNoise(xDiv, zDiv);
colors[index] = terrainColorPerlin.Evaluate(perl);
if(ys[index] > snowHeightMin)
{
// TODO: altitude based exponent / frequency
xDiv = (float)x * subdivRecip * 16 + 1000;
zDiv = (float)z * subdivRecip * 16 + 1000;
float heightMult = (ys[index] - snowHeightMin) / (snowHeightMax - snowHeightMin);
float heightMultClamp = Mathf.Min(heightMult, 1.0f);
float perlVal = Mathf.PerlinNoise(xDiv, zDiv);
perl = Mathf.Pow(perlVal, snowExponent) * heightMult;
colors[index] = Color.Lerp(colors[index], Color.white, perl);
}
}
}
// TODO: altitude based exponent / frequency
xDiv = (float)x * subdivRecip * 16 + 1000;
zDiv = (float)z * subdivRecip * 16 + 1000;
float heightMult = (height - snowHeightMin) / (snowHeightMax - snowHeightMin);
float heightMultClamp = Mathf.Min(heightMult, 1.0f);
index++;
float perlVal = Mathf.PerlinNoise(xDiv, zDiv);
perl = Mathf.Pow(perlVal, snowExponent) * heightMult;
colors[index] = Color.Lerp(colors[index], Color.white, perl);
}
}
}
}
mesh.SetColors(colors);
Debug.Log("DONE");
System.DateTime after = System.DateTime.Now;
System.TimeSpan diff = after.Subtract(before);
Debug.Log("duration of coloring (ms): " + diff.Milliseconds);
stopwatch.Stop();
UnityEngine.Debug.Log ("Coloring time taken: "+(stopwatch.Elapsed));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment