Search Unity

Get 3D texture from a Perlin noise system made from scratch

Discussion in 'Scripting' started by zenden_1, Jun 16, 2019.

  1. zenden_1

    zenden_1

    Joined:
    May 20, 2017
    Posts:
    53
    Hi, actually for create my 3D texture use the script down. I know the problem from the noise Method because if color is set to Random.ColorHSV it work.

    Code (CSharp):
    1.  
    2.        
    3.  
    4.         Color[] colorArray = new Color[res * res * res];
    5. tex3D = new Texture3D (res, res, res, TextureFormat.ARGB32, true);
    6.         tex3D.name = "Procedural Texture";
    7.         tex3D.filterMode = FilterMode.Bilinear;
    8.         tex3D.wrapMode = TextureWrapMode.Clamp;
    9.  
    10.         float r = 1.0f / (size - 1.0f);
    11.  
    12.  
    13.  
    14.         Vector3 point00 = transform.TransformPoint(new Vector3(-0.5f,-0.5f));
    15.         Vector3 point10 = transform.TransformPoint(new Vector3( 0.5f,-0.5f));
    16.         Vector3 point01 = transform.TransformPoint(new Vector3(-0.5f, 0.5f));
    17.         Vector3 point11 = transform.TransformPoint(new Vector3( 0.5f, 0.5f));
    18.  
    19.         NoiseMethod method = Noise.methods[(int)type][dimensions - 1];
    20.         float stepSize = 1f / res;
    21.         for (int y = 0; y < res; y++) {
    22.             Vector3 point0 = Vector3.Lerp(point00, point01, (y + 0.5f) * stepSize);
    23.             Vector3 point1 = Vector3.Lerp(point10, point11, (y + 0.5f) * stepSize);
    24.             for (int x = 0; x < res; x++) {
    25.                 Vector3 point = Vector3.Lerp(point0, point1, (x + 0.5f) * stepSize);
    26.                 float sample = Noise.Sum(method, point, frequency, octaves, lacunarity, persistence);
    27.  
    28.  
    29.                 if (type != NoiseMethodType.Value) {
    30.                     sample = sample * 0.5f + 0.5f;
    31.                 }
    32.  
    33.                 texture.SetPixel(x, y, Color.white * sample);
    34.  
    35.  
    36.                 for (int z = 0; z < res; z++)
    37.                 {
    38.                     Color c = Color.white * sample ;
    39.                     colorArray[x + y * res + z * res * res] = c;
    40.                    
    41.                 }
    42.  
    43.                
    44.  
    45.  
    46.      
    47.                
    48.             }
    49.         }
    50.  
    51.         tex3D.SetPixels(colorArray);
    52.         tex3D.Apply ();
    53.        
    54.  
    55.         texture.Apply();
    56.  
    57.         mat3D.SetTexture("_3DNoise",tex3D);
    58.  
    59.        
    60.     }
    61.  
    My noise Method :
    Code (CSharp):
    1. public static float Sum (NoiseMethod method, Vector3 point, float frequency, int octaves, float lacunarity, float persistence) {
    2.         float sum = method(point, frequency);
    3.         float amplitude = 1f;
    4.         float range = 1f;
    5.         for (int o = 1; o < octaves; o++) {
    6.             frequency *= lacunarity;
    7.             amplitude *= persistence;
    8.             range += amplitude;
    9.             sum += method(point, frequency) * amplitude;
    10.         }
    11.         return sum / range;
    12.     }
    And my Perlin Noise :
    Code (CSharp):
    1. public static float Perlin3D (Vector3 point, float frequency) {
    2.         point *= frequency;
    3.         int ix0 = Mathf.FloorToInt(point.x);
    4.         int iy0 = Mathf.FloorToInt(point.y);
    5.         int iz0 = Mathf.FloorToInt(point.z);
    6.         float tx0 = point.x - ix0;
    7.         float ty0 = point.y - iy0;
    8.         float tz0 = point.z - iz0;
    9.         float tx1 = tx0 - 1f;
    10.         float ty1 = ty0 - 1f;
    11.         float tz1 = tz0 - 1f;
    12.         ix0 &= hashMask;
    13.         iy0 &= hashMask;
    14.         iz0 &= hashMask;
    15.         int ix1 = ix0 + 1;
    16.         int iy1 = iy0 + 1;
    17.         int iz1 = iz0 + 1;
    18.        
    19.         int h0 = hash[ix0];
    20.         int h1 = hash[ix1];
    21.         int h00 = hash[h0 + iy0];
    22.         int h10 = hash[h1 + iy0];
    23.         int h01 = hash[h0 + iy1];
    24.         int h11 = hash[h1 + iy1];
    25.         Vector3 g000 = gradients3D[hash[h00 + iz0] & gradientsMask3D];
    26.         Vector3 g100 = gradients3D[hash[h10 + iz0] & gradientsMask3D];
    27.         Vector3 g010 = gradients3D[hash[h01 + iz0] & gradientsMask3D];
    28.         Vector3 g110 = gradients3D[hash[h11 + iz0] & gradientsMask3D];
    29.         Vector3 g001 = gradients3D[hash[h00 + iz1] & gradientsMask3D];
    30.         Vector3 g101 = gradients3D[hash[h10 + iz1] & gradientsMask3D];
    31.         Vector3 g011 = gradients3D[hash[h01 + iz1] & gradientsMask3D];
    32.         Vector3 g111 = gradients3D[hash[h11 + iz1] & gradientsMask3D];
    33.  
    34.         float v000 = Dot(g000, tx0, ty0, tz0);
    35.         float v100 = Dot(g100, tx1, ty0, tz0);
    36.         float v010 = Dot(g010, tx0, ty1, tz0);
    37.         float v110 = Dot(g110, tx1, ty1, tz0);
    38.         float v001 = Dot(g001, tx0, ty0, tz1);
    39.         float v101 = Dot(g101, tx1, ty0, tz1);
    40.         float v011 = Dot(g011, tx0, ty1, tz1);
    41.         float v111 = Dot(g111, tx1, ty1, tz1);
    42.  
    43.         float tx = Smooth(tx0);
    44.         float ty = Smooth(ty0);
    45.         float tz = Smooth(tz0);
    46.         return Mathf.Lerp(
    47.             Mathf.Lerp(Mathf.Lerp(v000, v100, tx), Mathf.Lerp(v010, v110, tx), ty),
    48.             Mathf.Lerp(Mathf.Lerp(v001, v101, tx), Mathf.Lerp(v011, v111, tx), ty),
    49.             tz);
    50.     }

    and i get this result :


    But normally i have to get this result :
     
  2. zenden_1

    zenden_1

    Joined:
    May 20, 2017
    Posts:
    53