I need help with this tutorial I’m following online. I’m creating noise using C# but I’ve run into a problem. It’s hard to explain. I was following the tutorial and everything was fine, until it got to a point where the rendered quad wasn’t changing as the tutorial shows. I just have a bunch of random lines. Am I missing something here or have I gone wrong somewhere? It’s annoying because it’s a long but good tutorial and I’ve done so much to go wrong now. I’ll post all the scripts here for you to see, and I’ll try and get my scene as well. I’m following the Noise tutorial from Catlike Coding.
using UnityEngine;
using System.Collections;
public class TextureCreator : MonoBehaviour {
[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, (y + 0.5f) * stepSize);
float sample = method(point, frequency);
if(type != NoiseMethodType.Value){
sample = sample * 0.5f + 0.5f;
}
texture.SetPixel (x, y, Color.white * sample);
}
}
texture.Apply ();
}
// Update is called once per frame
private void Update () {
if (transform.hasChanged) {
transform.hasChanged = false;
FillTexture ();
}
}
}
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class SurfaceCreator : MonoBehaviour {
private Mesh mesh;
private void OnEnable(){
if (mesh == null) {
mesh = new Mesh ();
mesh.name = "Surface Mesh";
GetComponent<MeshFilter>().mesh = mesh;
}
Refresh ();
}
public void Refresh(){
}
}
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(TextureCreator))]
public class TextureCreatorInspector : Editor {
private TextureCreator creator;
private void OnEnable(){
creator = target as TextureCreator;
Undo.undoRedoPerformed += RefreshCreator;
}
private void OnDisable(){
Undo.undoRedoPerformed -= RefreshCreator;
}
private void RefreshCreator(){
if (Application.isPlaying) {
creator.FillTexture ();
}
}
public override void OnInspectorGUI(){
EditorGUI.BeginChangeCheck ();
DrawDefaultInspector ();
if (EditorGUI.EndChangeCheck()) {
RefreshCreator();
}
}
}
using UnityEngine;
using System.Collections;
using System;
public delegate float NoiseMethod (Vector3 point, float frequency);
public enum NoiseMethodType {
Value,
Perlin
}
public static class Noise {
public static NoiseMethod[] valueMethods = {
Value1D,
Value2D,
Value3D
};
public static float 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];
t = Smooth (t);
return Mathf.Lerp(h0, h1, t) * (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 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
};
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;
}
private static float[] gradients1D = {
1f, -1f
};
private const int gradientsMask1D = 1;
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) * 2f;
}
private static Vector2[] gradients2D = {
new Vector2(1f, 0f),
new Vector2(-1f, 0f),
new Vector2(0f, 1f),
new Vector2(0f, -1f)
};
private const int gradientsMask2D = 3;
private static float Dot (Vector2 g, float x, float y) {
return g.x * x + g.y * y;
}
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 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 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;
}
Sorry it’s so long. Has anyone else followed this tutorial and been successful? I’ve written the code manually instead of copy and paste which is why I have this problem.
I think the problem might be in the TextureCreator or Noise script because these have the most coding out of the four and the ones I’ve worked in the most, if you know what I mean.[/Code]