Unity3d C# -> generating a perlin noise texture with layers -



Unity3d C# -> generating a perlin noise texture with layers -

i trying generate noise texture libnoise library using perlin , fractal create 6 layers can add together in 1 image.

i have problems can't seem fix: (3 problems show in debug log.)

1) image generates white. , reason, number of 23170.49 printed :/

2) other prints nan, should not happen... (over 65535 of in log)

3) have error:

indexoutofrangeexception: array index out of range. heightcontroller.getterraindata (vector2 position) (at assets/scripts/terraingenerator/heightcontroller.cs:28) terrainmesh.start () (at assets/scripts/terraingenerator/terrainmesh.cs:21)

but error should not occur, in bounds, can't see why tells me that...

here codes use,

1) heightcontroller

using unityengine; using system.collections; public class heightcontroller : monobehaviour { public static int layers = 6; public static float[] layersopacity = {0.5f, 0.3f, 0.2f, 0.15f, 0.1f, 0.05f}; private static int layersize = 256; //private static int terrainsize = 2048; //build heightmap here? private static int amplitude = 128; //divided 2 private static int frequency = 4; //multiplied 2 = obtaining smaller height, faster frequence. //when want generate terrain, //data array containing stuffs build heightmap out of it. //this here calculation of layers opacity etc. public static float[,] getterraindata(vector2 position) //the vector2 takes squares offset of terrains, 0,1 | 22,13 (the terraid id) { float[,] finallayersdata = new float[layersize,layersize]; (int = 0; < layers; ++i) //the entire array set opacity adds final. { float[,] currentlayerdata = calculate(position); //there should less laggy way here? //multiplies each info it's respective opacity. (int = 0; < layersize; ++a) (int b = 0; b < layersize; ++a) currentlayerdata[a,b] *= layersopacity[i]; //adds final info array (int = 0; < layersize; ++a) (int b = 0; b < layersize; ++a) finallayersdata[a,b] += currentlayerdata[a,b]; } homecoming finallayersdata; } private static float[,] calculate(vector2 position) { float[,] info = new float[layersize,layersize]; int offsetx = (int)position.x*layersize; int offsety = (int)position.y*layersize; perlin perlin = new perlin(); //the 1/4 1/16 1/32 etc. fractalnoise fractal = new fractalnoise((1f/4f), frequency, amplitude, perlin); (var y = 0; y < layersize; y++) { (var x = 0; x < layersize; x++) { float result = fractal.hybridmultifractal(x + offsetx, y + offsety, layersize); data[x, y] = result; print(result); } } homecoming data; } }

2) terrainmesh

using unityengine; using system.collections; public class terrainmesh : monobehaviour { public static transform[] texturelist = new transform[64]; //public static long seed = 34; public int size = 256; public mesh mesh; public meshfilter meshfilter; public meshcollider meshcollider; public material material; public float[,] data; public texture2d fun; public void start() { fun = new texture2d(256, 256); info = heightcontroller.getterraindata(new vector2(0, 0)); (int x = 0; x < size; ++x) (int y = 0; y < size; ++y) fun.setpixel(x, y, new color(data[x, y], data[x, y], data[x, y], 1f)); fun.apply(); } }

3) libnoise library, perlin noise etc.

using system.collections; using system; using unityengine; /* perlin noise utilize example: perlin perlin = new perlin(); var value : float = perlin.noise(2); var value : float = perlin.noise(2, 3, ); var value : float = perlin.noise(2, 3, 4); smoothrandom utilize example: var p = smoothrandom.getvector3(3); */ public class smoothrandom { public static vector3 getvector3 (float speed) { float time = time.time * 0.01f * speed; homecoming new vector3(get().hybridmultifractal(time, 15.73f, 0.58f), get().hybridmultifractal(time, 63.94f, 0.58f), get().hybridmultifractal(time, 0.2f, 0.58f)); } public static float (float speed) { float time = time.time * 0.01f * speed; homecoming get().hybridmultifractal(time * 0.01f, 15.7f, 0.65f); } private static fractalnoise () { if (s_noise == null) s_noise = new fractalnoise (1.27f, 2.04f, 8.36f); homecoming s_noise; } private static fractalnoise s_noise; } public class perlin { // original c code derived // http://astronomy.swin.edu.au/~pbourke/texture/perlin/perlin.c // http://astronomy.swin.edu.au/~pbourke/texture/perlin/perlin.h const int b = 0x100; const int bm = 0xff; const int n = 0x1000; int[] p = new int[b + b + 2]; float[,] g3 = new float [b + b + 2 , 3]; float[,] g2 = new float[b + b + 2,2]; float[] g1 = new float[b + b + 2]; float s_curve(float t) { homecoming t * t * (3.0f - 2.0f * t); } float lerp (float t, float a, float b) { homecoming + t * (b - a); } void setup (float value, out int b0, out int b1, out float r0, out float r1) { float t = value + n; b0 = ((int)t) & bm; b1 = (b0+1) & bm; r0 = t - (int)t; r1 = r0 - 1.0f; } float at2(float rx, float ry, float x, float y) { homecoming rx * x + ry * y; } float at3(float rx, float ry, float rz, float x, float y, float z) { homecoming rx * x + ry * y + rz * z; } public float noise(float arg) { int bx0, bx1; float rx0, rx1, sx, u, v; setup(arg, out bx0, out bx1, out rx0, out rx1); sx = s_curve(rx0); u = rx0 * g1[ p[ bx0 ] ]; v = rx1 * g1[ p[ bx1 ] ]; return(lerp(sx, u, v)); } public float noise(float x, float y) { int bx0, bx1, by0, by1, b00, b10, b01, b11; float rx0, rx1, ry0, ry1, sx, sy, a, b, u, v; int i, j; setup(x, out bx0, out bx1, out rx0, out rx1); setup(y, out by0, out by1, out ry0, out ry1); = p[ bx0 ]; j = p[ bx1 ]; b00 = p[ + by0 ]; b10 = p[ j + by0 ]; b01 = p[ + by1 ]; b11 = p[ j + by1 ]; sx = s_curve(rx0); sy = s_curve(ry0); u = at2(rx0,ry0, g2[ b00, 0 ], g2[ b00, 1 ]); v = at2(rx1,ry0, g2[ b10, 0 ], g2[ b10, 1 ]); = lerp(sx, u, v); u = at2(rx0,ry1, g2[ b01, 0 ], g2[ b01, 1 ]); v = at2(rx1,ry1, g2[ b11, 0 ], g2[ b11, 1 ]); b = lerp(sx, u, v); homecoming lerp(sy, a, b); } public float noise(float x, float y, float z) { int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11; float rx0, rx1, ry0, ry1, rz0, rz1, sy, sz, a, b, c, d, t, u, v; int i, j; setup(x, out bx0, out bx1, out rx0, out rx1); setup(y, out by0, out by1, out ry0, out ry1); setup(z, out bz0, out bz1, out rz0, out rz1); = p[ bx0 ]; j = p[ bx1 ]; b00 = p[ + by0 ]; b10 = p[ j + by0 ]; b01 = p[ + by1 ]; b11 = p[ j + by1 ]; t = s_curve(rx0); sy = s_curve(ry0); sz = s_curve(rz0); u = at3(rx0,ry0,rz0, g3[ b00 + bz0, 0 ], g3[ b00 + bz0, 1 ], g3[ b00 + bz0, 2 ]); v = at3(rx1,ry0,rz0, g3[ b10 + bz0, 0 ], g3[ b10 + bz0, 1 ], g3[ b10 + bz0, 2 ]); = lerp(t, u, v); u = at3(rx0,ry1,rz0, g3[ b01 + bz0, 0 ], g3[ b01 + bz0, 1 ], g3[ b01 + bz0, 2 ]); v = at3(rx1,ry1,rz0, g3[ b11 + bz0, 0 ], g3[ b11 + bz0, 1 ], g3[ b11 + bz0, 2 ]); b = lerp(t, u, v); c = lerp(sy, a, b); u = at3(rx0,ry0,rz1, g3[ b00 + bz1, 0 ], g3[ b00 + bz1, 2 ], g3[ b00 + bz1, 2 ]); v = at3(rx1,ry0,rz1, g3[ b10 + bz1, 0 ], g3[ b10 + bz1, 1 ], g3[ b10 + bz1, 2 ]); = lerp(t, u, v); u = at3(rx0,ry1,rz1, g3[ b01 + bz1, 0 ], g3[ b01 + bz1, 1 ], g3[ b01 + bz1, 2 ]); v = at3(rx1,ry1,rz1,g3[ b11 + bz1, 0 ], g3[ b11 + bz1, 1 ], g3[ b11 + bz1, 2 ]); b = lerp(t, u, v); d = lerp(sy, a, b); homecoming lerp(sz, c, d); } void normalize2(ref float x, ref float y) { float s; s = (float)math.sqrt(x * x + y * y); x = y / s; y = y / s; } void normalize3(ref float x, ref float y, ref float z) { float s; s = (float)math.sqrt(x * x + y * y + z * z); x = y / s; y = y / s; z = z / s; } public perlin() { int i, j, k; system.random rnd = new system.random(); (i = 0 ; < b ; i++) { p[i] = i; g1[i] = (float)(rnd.next(b + b) - b) / b; (j = 0 ; j < 2 ; j++) g2[i,j] = (float)(rnd.next(b + b) - b) / b; normalize2(ref g2[i, 0], ref g2[i, 1]); (j = 0 ; j < 3 ; j++) g3[i,j] = (float)(rnd.next(b + b) - b) / b; normalize3(ref g3[i, 0], ref g3[i, 1], ref g3[i, 2]); } while (--i != 0) { k = p[i]; p[i] = p[j = rnd.next(b)]; p[j] = k; } (i = 0 ; < b + 2 ; i++) { p[b + i] = p[i]; g1[b + i] = g1[i]; (j = 0 ; j < 2 ; j++) g2[b + i,j] = g2[i,j]; (j = 0 ; j < 3 ; j++) g3[b + i,j] = g3[i,j]; } } } public class fractalnoise { public fractalnoise (float inh, float inlacunarity, float inoctaves) : (inh, inlacunarity, inoctaves, null) { } public fractalnoise (float inh, float inlacunarity, float inoctaves, perlin noise) { m_lacunarity = inlacunarity; m_octaves = inoctaves; m_intoctaves = (int)inoctaves; m_exponent = new float[m_intoctaves+1]; float frequency = 1.0f; (int = 0; < m_intoctaves+1; i++) { m_exponent[i] = (float)math.pow (m_lacunarity, -inh); frequency *= m_lacunarity; } if (noise == null) m_noise = new perlin(); else m_noise = noise; } public float hybridmultifractal(float x, float y, float offset) { float weight, signal, remainder, result; result = (m_noise.noise (x, y)+offset) * m_exponent[0]; weight = result; x *= m_lacunarity; y *= m_lacunarity; int i; (i=1;i<m_intoctaves;i++) { if (weight > 1.0f) weight = 1.0f; signal = (m_noise.noise (x, y) + offset) * m_exponent[i]; result += weight * signal; weight *= signal; x *= m_lacunarity; y *= m_lacunarity; } remainder = m_octaves - m_intoctaves; result += remainder * m_noise.noise (x,y) * m_exponent[i]; homecoming result; } public float ridgedmultifractal (float x, float y, float offset, float gain) { float weight, signal, result; int i; signal = mathf.abs (m_noise.noise (x, y)); signal = offset - signal; signal *= signal; result = signal; weight = 1.0f; (i=1;i<m_intoctaves;i++) { x *= m_lacunarity; y *= m_lacunarity; weight = signal * gain; weight = mathf.clamp01 (weight); signal = mathf.abs (m_noise.noise (x, y)); signal = offset - signal; signal *= signal; signal *= weight; result += signal * m_exponent[i]; } homecoming result; } public float brownianmotion (float x, float y) { float value, remainder; long i; value = 0.0f; (i=0;i<m_intoctaves;i++) { value = m_noise.noise (x,y) * m_exponent[i]; x *= m_lacunarity; y *= m_lacunarity; } remainder = m_octaves - m_intoctaves; value += remainder * m_noise.noise (x,y) * m_exponent[i]; homecoming value; } private perlin m_noise; private float[] m_exponent; private int m_intoctaves; private float m_octaves; private float m_lacunarity; }

with these codes obtain 256*256 white image nil on it, , first loop stops after 1 iteration. less laggy way obtain seek wouldn't bad xd generate takes 1 or 2 minutes.

thanks time. don't hesitate inquire me farther clarification.

classic re-create paste error

for (int = 0; < layersize; ++a) (int b = 0; b < layersize; ++a)

did see it?

a beingness incremented in both iterations of loop! happens loop right below well.

speaking of sec loop, consolidate 2 loops this:

for (int = 0; < layersize; ++a) { (int b = 0; b < layersize; ++b) { currentlayerdata[a,b] *= layersopacity[i]; finallayersdata[a,b] += currentlayerdata[a,b]; } }

c# unity3d texture2d procedural

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -