What does this error mean? I’m a bit baffled to be honest because I’ve followed this tutorial. So far everything’s gone well until now, and I’ve done absolutely everything I can think of. I’ll post the scripts but be warned, there’s a few and they’re long!
P.S. I’m following Catlike Coding’s Noise Derivatives tutorial and I’m currently on the Analytical Derivatives part. I’ve completed the Noise tutorial as well.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class SurfaceCreator : MonoBehaviour {
public float frequency = 1f;
[Range(0f, 1f)]
public float strength = 1f;
[Range(1, 8)]
public int octaves = 1;
[Range(1f, 4f)]
public float lacunarity = 2f;
[Range(0f, 1f)]
public float persistance = 0.5f;
[Range(1, 3)]
public int dimensions = 3;
[Range(1, 200)]
public int resolution = 10;
public bool coloringForStrength;
public Gradient coloring;
private int currentResolution;
private Mesh mesh;
private void OnEnable(){
if (mesh == null) {
mesh = new Mesh ();
mesh.name = "Surface Mesh";
GetComponent<MeshFilter>().mesh = mesh;
}
Refresh ();
}
public NoiseMethodType type;
public Vector3 offset;
public bool damping;
public Vector3 rotation;
public bool analyticalDerivatives;
public void Refresh(){
if (resolution != currentResolution) {
CreateGrid ();
}
Quaternion q = Quaternion.Euler (rotation);
Vector3 point00 = q * new Vector3(-0.5f,-0.5f) + offset;
Vector3 point10 = q * new Vector3( 0.5f,-0.5f) + offset;
Vector3 point01 = q * new Vector3(-0.5f, 0.5f) + offset;
Vector3 point11 = q * new Vector3( 0.5f, 0.5f) + offset;
NoiseMethod method = Noise.noiseMethods[(int)type][dimensions - 1];
float stepSize = 1f / resolution;
float amplitude = damping ? strength / frequency : strength;
for (int v = 0, y = 0; y <= resolution; y++) {
Vector3 point0 = Vector3.Lerp(point00, point01, y * stepSize);
Vector3 point1 = Vector3.Lerp(point10, point11, y * stepSize);
for (int x = 0; x <= resolution; x++, v++) {
Vector3 point = Vector3.Lerp(point0, point1, x * stepSize);
NoiseSample sample = Noise.Sum(method, point, frequency, octaves, lacunarity, persistance);
sample = type == NoiseMethodType.Value ? (sample - 0.5f) : (sample * 0.5f);
if (coloringForStrength){
colors[v] = coloring.Evaluate(sample.value + 0.5f);
sample *= amplitude;
}
else{
sample *= amplitude;
colors[v] = coloring.Evaluate(sample.value + 0.5f);
}
vertices[v].y = sample.value;
if (analyticalDerivatives) {
normals[v] =
new Vector3(-sample.derivative.x, 1f, -sample.derivative.y).normalized;
}
}
}
mesh.vertices = vertices;
mesh.colors = colors;
if (analyticalDerivatives) {
CalculateNormals ();
}
mesh.normals = normals;
}
private void CalculateNormals () {
for (int v = 0, z = 0; z <= resolution; z++) {
for (int x = 0; x <= resolution; x++, v++) {
normals[v] = new Vector3(-GetXDerivative(x, z), 1f, -GetZDerivative(x, z)).normalized;
}
}
}
private float GetXDerivative (int x, int z) {
int rowOffset = z * (resolution + 1);
float left, right, scale;
if (x > 0) {
left = vertices[rowOffset + x - 1].y;
if (x < resolution) {
right = vertices[rowOffset + x + 1].y;
scale = 0.5f * resolution;
}
else {
right = vertices[rowOffset + x].y;
scale = resolution;
}
}
else {
left = vertices[rowOffset + x].y;
right = vertices[rowOffset + x + 1].y;
scale = resolution;
}
return (right - left) * scale;
}
private float GetZDerivative (int x, int z) {
int rowLength = resolution + 1;
float back, forward, scale;
if (z > 0) {
back = vertices[(z - 1) * rowLength + x].y;
if (z < resolution) {
forward = vertices[(z + 1) * rowLength + x].y;
scale = 0.5f * resolution;
}
else {
forward = vertices[z * rowLength + x].y;
scale = resolution;
}
}
else {
back = vertices[z * rowLength + x].y;
forward = vertices[(z + 1) * rowLength + x].y;
scale = resolution;
}
return (forward - back) * scale;
}
private Vector3[] vertices;
private Vector3[] normals;
private Color[] colors;
private void CreateGrid(){
currentResolution = resolution;
mesh.Clear ();
vertices = new Vector3[(resolution + 1) * (resolution + 1)];
colors = new Color[vertices.Length];
normals = new Vector3[vertices.Length];
Vector2[] uv = new Vector2[vertices.Length];
float stepSize = 1f / resolution;
for (int v = 0, z = 0; z <= resolution; z++){
for (int x = 0; x <= resolution; x++, v++){
vertices[v] = new Vector3(x * stepSize - 0.5f, 0f, z * stepSize - 0.5f);
colors[v] = Color.black;
normals[v] = Vector3.up;
uv[v] = new Vector2(x * stepSize, z * stepSize);
}
}
mesh.vertices = vertices;
mesh.colors = colors;
mesh.normals = normals;
mesh.uv = uv;
int[] triangles = new int[resolution * resolution * 6];
for (int t = 0, v = 0, y = 0; y < resolution; y++, v++){
for (int x = 0; x < resolution; x++, v++, t += 6){
triangles[t] = v;
triangles[t + 1] = v + resolution + 1;
triangles[t + 2] = v + 1;
triangles[t + 3] = v + 1;
triangles[t + 4] = v + resolution + 1;
triangles[t + 5] = v + resolution + 2;
}
}
mesh.triangles = triangles;
}
public bool showNormals;
private void OnDrawGizmosSelected () {
float scale = 1f / resolution;
if (showNormals && vertices != null) {
Gizmos.color = Color.yellow;
for (int v = 0; v < vertices.Length; v++) {
Gizmos.DrawRay(vertices[v], normals[v] * scale);
}
}
}
}
public class TextureCreator : MonoBehaviour {
[Range(1, 8)]
public int octaves = 1;
[Range(1f, 4f)]
public float lacunarity = 2f;
[Range(0f, 1f)]
public float persistance = 0.5f;
[Range(2, 512)]
public int resolution = 256;
[Range(1, 3)]
public int dimensions = 3;
private Texture2D texture;
// Use this for initialization
private void OnEnable () {
if (texture == null) {
texture = new Texture2D (resolution, resolution, TextureFormat.RGB24, true);
texture.name = "Procedural Texture";
texture.wrapMode = TextureWrapMode.Clamp;
texture.filterMode = FilterMode.Trilinear;
texture.anisoLevel = 9;
GetComponent<MeshRenderer> ().material.mainTexture = texture;
FillTexture ();
}
}
public float frequency = 1f;
public NoiseMethodType type;
public void FillTexture(){
if (texture.width != resolution) {
texture.Resize (resolution, resolution);
}
Vector3 point00 = transform.TransformPoint (new Vector3 (-0.5f, -0.5f));
Vector3 point10 = transform.TransformPoint (new Vector3 (0.5f, -0.5f));
Vector3 point01 = transform.TransformPoint (new Vector3 (-0.5f, 0.5f));
Vector3 point11 = transform.TransformPoint (new Vector3 (0.5f, 0.5f));
NoiseMethod method = Noise.noiseMethods[(int)type] [dimensions - 1];
float stepSize = 1f / resolution;
for (int y = 0; y < resolution; y++) {
Vector3 point0 = Vector3.Lerp(point00, point01, (y + 0.5f) * stepSize);
Vector3 point1 = Vector3.Lerp(point10, point11, (y + 0.5f) * stepSize);
for (int x = 0; x < resolution; x++){
Vector3 point = Vector3.Lerp (point0, point1, (x + 0.5f) * stepSize);
float sample = Noise.Sum(method, point, frequency, octaves, lacunarity, persistance).value;
if(type != NoiseMethodType.Value){
sample = sample * 0.5f + 0.5f;
}
texture.SetPixel (x, y, coloring.Evaluate(sample));
}
}
texture.Apply ();
}
// Update is called once per frame
private void Update () {
if (transform.hasChanged) {
transform.hasChanged = false;
FillTexture ();
}
}
public Gradient coloring;
}
using UnityEngine;
public delegate NoiseSample NoiseMethod (Vector3 point, float frequency);
public enum NoiseMethodType {
Value,
Perlin
}
public static class Noise {
public static NoiseMethod[] valueMethods = {
Value1D,
Value2D,
Value3D
};
private static float Smooth(float t){
return t * t * t * (t * (t * 6f - 15f) + 10f);
}
public static NoiseMethod[] perlinMethods = {
Perlin1D,
Perlin2D,
Perlin3D
};
public static NoiseMethod[][]noiseMethods = {
valueMethods,
perlinMethods
};
private static int[] hash = {
151,160,137, 91, 90, 15,131, 13,201, 95, 96, 53,194,233, 7,225,
140, 36,103, 30, 69,142, 8, 99, 37,240, 21, 10, 23,190, 6,148,
247,120,234, 75, 0, 26,197, 62, 94,252,219,203,117, 35, 11, 32,
57,177, 33, 88,237,149, 56, 87,174, 20,125,136,171,168, 68,175,
74,165, 71,134,139, 48, 27,166, 77,146,158,231, 83,111,229,122,
60,211,133,230,220,105, 92, 41, 55, 46,245, 40,244,102,143, 54,
65, 25, 63,161, 1,216, 80, 73,209, 76,132,187,208, 89, 18,169,
200,196,135,130,116,188,159, 86,164,100,109,198,173,186, 3, 64,
52,217,226,250,124,123, 5,202, 38,147,118,126,255, 82, 85,212,
207,206, 59,227, 47, 16, 58, 17,182,189, 28, 42,223,183,170,213,
119,248,152, 2, 44,154,163, 70,221,153,101,155,167, 43,172, 9,
129, 22, 39,253, 19, 98,108,110, 79,113,224,232,178,185,112,104,
218,246, 97,228,251, 34,242,193,238,210,144, 12,191,179,162,241,
81, 51,145,235,249, 14,239,107, 49,192,214, 31,181,199,106,157,
184, 84,204,176,115,121, 50, 45,127, 4,150,254,138,236,205, 93,
222,114, 67, 29, 24, 72,243,141,128,195, 78, 66,215, 61,156,180,
151,160,137, 91, 90, 15,131, 13,201, 95, 96, 53,194,233, 7,225,
140, 36,103, 30, 69,142, 8, 99, 37,240, 21, 10, 23,190, 6,148,
247,120,234, 75, 0, 26,197, 62, 94,252,219,203,117, 35, 11, 32,
57,177, 33, 88,237,149, 56, 87,174, 20,125,136,171,168, 68,175,
74,165, 71,134,139, 48, 27,166, 77,146,158,231, 83,111,229,122,
60,211,133,230,220,105, 92, 41, 55, 46,245, 40,244,102,143, 54,
65, 25, 63,161, 1,216, 80, 73,209, 76,132,187,208, 89, 18,169,
200,196,135,130,116,188,159, 86,164,100,109,198,173,186, 3, 64,
52,217,226,250,124,123, 5,202, 38,147,118,126,255, 82, 85,212,
207,206, 59,227, 47, 16, 58, 17,182,189, 28, 42,223,183,170,213,
119,248,152, 2, 44,154,163, 70,221,153,101,155,167, 43,172, 9,
129, 22, 39,253, 19, 98,108,110, 79,113,224,232,178,185,112,104,
218,246, 97,228,251, 34,242,193,238,210,144, 12,191,179,162,241,
81, 51,145,235,249, 14,239,107, 49,192,214, 31,181,199,106,157,
184, 84,204,176,115,121, 50, 45,127, 4,150,254,138,236,205, 93,
222,114, 67, 29, 24, 72,243,141,128,195, 78, 66,215, 61,156,180
};
private const int hashMask = 255;
public static NoiseSample Value1D (Vector3 point, float frequency) {
point *= frequency;
int i0 = Mathf.FloorToInt(point.x);
float t = point.x - i0;
i0 &= hashMask;
int i1 = i0 + 1;
int h0 = hash[i0];
int h1 = hash[i1];
return dt = SmoothDerivative (t);
t = Smooth(t);
float a = h0;
float b = h1 - h0;
NoiseSample sample;
sample.value = a + b * t;
sample.derivative.x = b * dt;
sample.derivative.y = 0f;
sample.derivative.z = 0f;
sample.derivative *= frequency;
return sample * (1f / hashMask);
}
public static float Value2D (Vector3 point, float frequency) {
point *= frequency;
int ix0 = Mathf.FloorToInt(point.x);
int iy0 = Mathf.FloorToInt(point.y);
float tx = point.x - ix0;
float ty = point.y - iy0;
ix0 &= hashMask;
iy0 &= hashMask;
int ix1 = ix0 + 1;
int iy1 = iy0 + 1;
int h0 = hash[ix0];
int h1 = hash[ix1];
int h00 = hash[h0 + iy0];
int h10 = hash[h1 + iy0];
int h01 = hash[h0 + iy1];
int h11 = hash[h1 + iy1];
tx = Smooth(tx);
ty = Smooth(ty);
return Mathf.Lerp(
Mathf.Lerp(h00, h10, tx),
Mathf.Lerp(h01, h11, tx),
ty) * (1f / hashMask);
}
public static float Value3D (Vector3 point, float frequency) {
point *= frequency;
int ix0 = Mathf.FloorToInt(point.x);
int iy0 = Mathf.FloorToInt(point.y);
int iz0 = Mathf.FloorToInt(point.z);
float tx = point.x - ix0;
float ty = point.y - iy0;
float tz = point.z - iz0;
ix0 &= hashMask;
iy0 &= hashMask;
iz0 &= hashMask;
int ix1 = ix0 + 1;
int iy1 = iy0 + 1;
int iz1 = iz0 + 1;
int h0 = hash[ix0];
int h1 = hash[ix1];
int h00 = hash[h0 + iy0];
int h10 = hash[h1 + iy0];
int h01 = hash[h0 + iy1];
int h11 = hash[h1 + iy1];
int h000 = hash[h00 + iz0];
int h100 = hash[h10 + iz0];
int h010 = hash[h01 + iz0];
int h110 = hash[h11 + iz0];
int h001 = hash[h00 + iz1];
int h101 = hash[h10 + iz1];
int h011 = hash[h01 + iz1];
int h111 = hash[h11 + iz1];
tx = Smooth(tx);
ty = Smooth(ty);
tz = Smooth(tz);
return Mathf.Lerp(
Mathf.Lerp(Mathf.Lerp(h000, h100, tx), Mathf.Lerp(h010, h110, tx), ty),
Mathf.Lerp(Mathf.Lerp(h001, h101, tx), Mathf.Lerp(h011, h111, tx), ty),
tz) * (1f / hashMask);
}
private static float[] gradients1D = {
1f, -1f
};
private const int gradientsMask1D = 1;
public static float Perlin1D (Vector3 point, float frequency){
point *= frequency;
int i0 = Mathf.FloorToInt(point.x);
float t0 = point.x - i0;
float t1 = t0 - 1f;
i0 &= hashMask;
int i1 = i0 + 1;
float g0 = gradients1D[hash[i0] & gradientsMask1D];
float g1 = gradients1D[hash[i1] & gradientsMask1D];
float v0 = g0 * t0;
float v1 = g1 * t1;
float t = Smooth (t0);
return Mathf.Lerp(v0, v1, t) * 2f;
}
public static float Perlin2D (Vector3 point, float frequency){
point *= frequency;
int ix0 = Mathf.FloorToInt(point.x);
int iy0 = Mathf.FloorToInt(point.y);
float tx0 = point.x - ix0;
float ty0 = point.y - iy0;
float tx1 = tx0 - 1f;
float ty1 = tx1 - 1f;
ix0 &= hashMask;
iy0 &= hashMask;
int ix1 = ix0 + 1;
int iy1 = iy0 + 1;
int h0 = hash [ix0];
int h1 = hash [ix1];
Vector2 g00 = gradients2D [hash[h0 + iy0] & gradientsMask2D];
Vector2 g10 = gradients2D [hash[h1 + iy0] & gradientsMask2D];
Vector2 g01 = gradients2D [hash[h0 + iy1] & gradientsMask2D];
Vector2 g11 = gradients2D [hash[h1 + iy1] & gradientsMask2D];
float v00 = Dot (g00, tx0, ty0);
float v10 = Dot (g10, tx1, ty0);
float v01 = Dot (g01, tx0, ty1);
float v11 = Dot (g11, tx1, ty1);
float tx = Smooth (tx0);
float ty = Smooth (ty0);
return Mathf.Lerp (
Mathf.Lerp(v00, v10, tx),
Mathf.Lerp(v01, v11, tx),
ty) * sqr2;
}
private static float sqr2 = Mathf.Sqrt (2f);
private static Vector2[] gradients2D = {
new Vector2(1f, 0f),
new Vector2(-1f, 0f),
new Vector2(0f, 1f),
new Vector2(0f, -1f),
new Vector2(1f, 1f).normalized,
new Vector2(-1f, 1f).normalized,
new Vector2(1f, 1f).normalized,
new Vector2(1f, -1f).normalized
};
private const int gradientsMask2D = 7;
private static float Dot (Vector2 g, float x, float y) {
return g.x * x + g.y * y;
}
private static float Dot (Vector3 g, float x, float y, float z) {
return g.x * x + g.y * y + g.z * z;
}
public static float Perlin3D (Vector3 point, float frequency){
point *= frequency;
int ix0 = Mathf.FloorToInt(point.x);
int iy0 = Mathf.FloorToInt(point.y);
int iz0 = Mathf.FloorToInt(point.z);
float tx0 = point.x - ix0;
float ty0 = point.y - iy0;
float tz0 = point.z - iz0;
float tx1 = tx0 - 1f;
float ty1 = ty0 - 1f;
float tz1 = tz0 - 1f;
ix0 &= hashMask;
iy0 &= hashMask;
iz0 &= hashMask;
int ix1 = ix0 + 1;
int iy1 = iy0 + 1;
int iz1 = iz0 + 1;
int h0 = hash [ix0];
int h1 = hash [ix1];
int h00 = hash [h0 + iy0];
int h10 = hash [h1 + iy0];
int h01 = hash [h0 + iy1];
int h11 = hash [h1 + iy1];
Vector3 g000 = gradients3D [hash[h00 + iz0] & gradientsMask3D];
Vector3 g100 = gradients3D [hash[h10 + iz0] & gradientsMask3D];
Vector3 g010 = gradients3D [hash[h01 + iz0] & gradientsMask3D];
Vector3 g110 = gradients3D [hash[h11 + iz0] & gradientsMask3D];
Vector3 g001 = gradients3D [hash[h00 + iz1] & gradientsMask3D];
Vector3 g101 = gradients3D [hash[h10 + iz1] & gradientsMask3D];
Vector3 g011 = gradients3D [hash[h01 + iz1] & gradientsMask3D];
Vector3 g111 = gradients3D [hash[h11 + iz1] & gradientsMask3D];
float v000 = Dot (g000, tx0, ty0, tz0);
float v100 = Dot (g100, tx1, ty0, tz0);
float v010 = Dot (g010, tx0, ty1, tz0);
float v110 = Dot (g110, tx1, ty1, tz0);
float v001 = Dot (g001, tx0, ty0, tz1);
float v101 = Dot (g101, tx1, ty0, tz1);
float v011 = Dot (g011, tx0, ty1, tz1);
float v111 = Dot (g111, tx1, ty1, tz1);
float tx = Smooth (tx0);
float ty = Smooth (ty0);
float tz = Smooth (tz0);
return Mathf.Lerp (
Mathf.Lerp (Mathf.Lerp (v000, v100, tx), Mathf.Lerp (v010, v110, tx), ty),
Mathf.Lerp (Mathf.Lerp (v001, v101, tx), Mathf.Lerp (v011, v111, tx), ty),
tz);
}
private static Vector3[] gradients3D = {
new Vector3 (1f, 1f, 0f),
new Vector3 (-1f, 1f, 0f),
new Vector3 (1f, -1f, 0f),
new Vector3 (-1f, -1f, 0f),
new Vector3 (1f, 0f, 1f),
new Vector3 (-1f, 0f, 1f),
new Vector3 (1f, 0f, -1f),
new Vector3 (-1f, 0f, -1f),
new Vector3 (0f, 1f, 1f),
new Vector3 (0f, -1f, 1f),
new Vector3 (0f, 1f, -1f),
new Vector3 (0f, -1f, -1f),
new Vector3 (1f, 1f, 0f),
new Vector3 (-1f, 1f, 0f),
new Vector3 (0f, -1f, 1f),
new Vector3 (0f, -1f, -1f)
};
private const int gradientsMask3D = 15;
public static NoiseSample Sum (
NoiseMethod method, Vector3 point, float frequency, int octaves, float lacunarity, float persistence
) {
NoiseSample sum = method(point, frequency);
float amplitude = 1f;
float range = 1f;
for (int o = 1; o < octaves; o++) {
frequency *= lacunarity;
amplitude *= persistence;
range += amplitude;
sum += method(point, frequency) * amplitude;
}
return sum * (1f / range);
}
private static float SmoothDerivative (float t){
return 30f * t * t * (t * (t - 2f) + 1f);
}
}
using UnityEngine;
using System.Collections;
public struct NoiseSample {
public float value;
public Vector3 derivative;
public static NoiseSample operator + (NoiseSample a, float b) {
a.value += b;
return a;
}
public static NoiseSample operator + (float a, NoiseSample b) {
b.value += a;
return b;
}
public static NoiseSample operator - (NoiseSample a, float b) {
a.value -= b;
return a;
}
public static NoiseSample operator - (float a, NoiseSample b) {
b.value = a - b.value;
b.derivative = -b.derivative;
return b;
}
public static NoiseSample operator - (NoiseSample a, NoiseSample b) {
a.value -= b.value;
a.derivative -= b.derivative;
return a;
}
public static NoiseSample operator * (NoiseSample a, float b) {
a.value *= b;
a.derivative *= b;
return a;
}
public static NoiseSample operator * (float a, NoiseSample b) {
b.value *= a;
b.derivative *= a;
return b;
}
public static NoiseSample operator * (NoiseSample a, NoiseSample b) {
a.derivative = a.derivative * b.value + b.derivative * a.value;
a.value *= b.value;
return a;
}
}
I also have a picture of the errors.