Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simplex Noise (3D)  (Read 4375 times)
0 Members and 1 Guest are viewing this topic.
Online Roquen
« Posted 2011-03-15 15:07:47 »

Wow.  I totally forget that I threw together a first pass of simplex noise in 3D for your preview pleasure.  This is "totally" incomplete, but any interested party can muck around with it as is.  It is intended to be a reference version that isn't insanely slow.

I never got around to posting it because I'm not really happy with it as is.  If this generates any interest maybe it'll motivate me to get it to a more complete state.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
public class SimplexNoise3D
{
  /**
   * If <i>true</i> the valid input set to noise is restricted
   * to positive numbers.  This will produce serious defects
   * if any of the input coordinates are negative.
   */

  private static final boolean restrictToPositive = false;
 

  /**
   * Compile time selection between Perlin's simplex vectors and
   * the older vector set from Improved (gradient) noise.
   */

  private static final boolean perlinVectors = false;
 
  /**
   * Compile time selection: If <i>true</i> vectors are expanded
   * to a switch table instead of conditional moves.
   */

  private static final boolean expandVectors = true;
 
 
  /**
   * Final scaling factor.  Depends on vector set.
   */

  private static final float finalScale;
 
  static {
    if (perlinVectors)
      finalScale = 8;
    else
      finalScale = 32;
  }
 
  /**
   *
   */

  private static int pseudoFloor(float x)
  {
    return x >= 0 ? (int)x : (int)x-1;
  }
 

  /**
   * Completes coordinate hashing and performs the dot product with the selected vector.
   */

  private static float dotRandVector(int h, float x, float y, float z)
  {  
    // complete hash generation (see eval)
   h = posthash(h) >>> 24;

    if (perlinVectors) {
      float S;

      if (false) {
        int b5 = h >> 5 & 1;
        int b4 = h >> 4 & 1;
        int b3 = h >> 3 & 1;
        int b2 = h >> 2 & 1;
        int b  = h & 3;

        float P,Q,R;

        switch (b) {
          case 1:  P=x; Q=y; R=z; break;
          case 2:  P=y; Q=z; R=x; break;
          default: P=z; Q=x; R=y; break;
        }
        if (true) {
          if (b5 == b3) P = -P;
          if (b5 == b4) Q = -Q;
          if (b5 != (b4^b3)) R = -R;
        } else {
          P = b5 == b3 ? -P : P;
          Q = b5 == b4 ? -Q: Q;
          R = b5 != (b4^b3) ? -R : R;
        }

        S = (b == 0 ? Q + R : b2 == 0 ? Q : R);


        return (P + S);
      }
      else {
        S = 0;//tempHack(h,x,y,z);

        return S;
      }
    }
    else {
      // Use the "older" vector set from Improved Gradient Noise.
     
      if (expandVectors) {
        switch(h & 0xF) {
          case 0x0: return  x+y;
          case 0x1: return -x+y;
          case 0x2: return  x-y;
          case 0x3: return -x-y;
          case 0x4: return  x+z;
          case 0x5: return -x+z;
          case 0x6: return  x-z;
          case 0x7: return -x-z;
          case 0x8: return  y+z;
          case 0x9: return -y+z;
          case 0xA: return  y-z;
          case 0xB: return -y-z;
          case 0xC: return  y+x;
          case 0xD: return -y+z;
          case 0xE: return  y-x;
          case 0xF: return -y-z;
          default: return 0; // never happens
       }
      }
      else {
        h &= 0xF;
   
        float u = (h < 8) ? x : y;
        float v = (h < 4) ? y : ((h == 12 || h == 14) ? x : z);
   
        return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
      }
    }
  }
 
  /** Properly rounded: 1/3 */
  private static final float K0 = 0x1.555556p-2f;
 
  /** Properly rounded: 1/6 */
  private static final float K1 = 0x1.555556p-3f;

  private static final boolean simpleHash = true;
  private static final int M = 0x5bd1e995;
 
  private static final int prehash(int i, int j, int k)
  {
    if (simpleHash)
      return (i<<16)^(j<<8)^(k);
    else {
      int h;
     
      i *= M; i ^= i >>> 24; i *= M;
      j *= M; j ^= j >>> 24; j *= M;
      k *= M; k ^= k >>> 24; k *= M;
     
      h  = i; h *= M;
      h ^= j; h *= M;
      h ^= k;
     
      return h;
    }
  }

  private static final int posthash(int h)
  {
    if (simpleHash)
      h *= 0xB5262C85;
    else {
      h ^= h >>> 13; h *= M; h ^= h >>> 15;
    }
    return h;
  }

 
 
  /**
   *
   */

  public static float eval(float x, float y, float z)
  {
    // project the input coordinate (via skew)
   float s = (x + y + z) * K0;
    int   i, j, k;

    if (restrictToPositive) {
      // This is possible if input is restricted to positive values.
     // The center of the image in the example is (0,0,0), so will
     // show the (serious) defects if used and the inputs are negative.
     //
     // The major upside is that the code will run significantly
     // faster if this input limitation is required.
     i = (int)(x+s);
      j = (int)(y+s);
      k = (int)(z+s);
    }
    else {
      i = pseudoFloor(x+s);
      j = pseudoFloor(y+s);
      k = pseudoFloor(z+s);
    }
   
    // unproject the coordinate back to Euclidean space.
   float t  = (i + j + k) * K1;
    float x0 = x - (i-t);
    float y0 = y - (j-t);
    float z0 = z - (k-t);

    // locate the simplex of the input coordinate.
   int i1, j1, k1;
    int i2, j2, k2;

    if (x0 >= y0) {
      i2 = 1;
      j1 = 0;
     
      if (y0 >= z0) {
        i1 = j2 = 1;
        k1 = k2 = 0;
      }
      else {
        j2 = 0;
        k2 = 1;
       
        if (x0 >= z0)
          i1 = 1;
        else
          i1 = 0;
       
        k1 = i1 ^ 1;
      }
    }
    else {
      i1 = 0;
      j2 = 1;
     
      if (y0 < z0) {
        j1 = i2 = 0;
        k1 = k2 = 1;
      }
      else {
        j1 = 1;
        k1 = 0;
       
        if (x0 < z0) {
          i2 = 0;
          k2 = 1;
        }
        else {
          i2 = 1;
          k2 = 0;
        }
      }
    }

    // calculate the four coordinates
   float x1 = x0 - i1 + K1;
    float y1 = y0 - j1 + K1;
    float z1 = z0 - k1 + K1;

    float x2 = x0 - i2 + 2*K1;
    float y2 = y0 - j2 + 2*K1;
    float z2 = z0 - k2 + 2*K1;

    float x3 = x0 - 1 + 3*K1;
    float y3 = y0 - 1 + 3*K1;
    float z3 = z0 - 1 + 3*K1;

    // Step 1 of hash generation, if needed the hashing
   // is completed in 'doRandVector'.
   // This could be improved
   int h0 = prehash(i,    j,    k   );
    int h1 = prehash(i+i1, j+j1, k+k1);
    int h2 = prehash(i+i2, j+j2, k+k2);
    int h3 = prehash(i+ 1, j+ 1, k+ 1);
   
    float n = 0;
   
    // Calculate and sum the results of the four coordinates.
   
    t = 0.6f - x0*x0 - y0*y0 - z0*z0;

    if (t > 0){
      t *= t;
      n += t*t * dotRandVector(h0, x0, y0, z0);
    }

    t = 0.6f - x1*x1 - y1*y1 - z1*z1;

    if (t > 0) {
      t *= t;
      n += t*t * dotRandVector(h1, x1, y1, z1);
    }

    t = 0.6f - x2*x2 - y2*y2 - z2*z2;

    if (t > 0) {
      t *= t;
      n += t*t * dotRandVector(h2, x2, y2, z2);
    }

    t = 0.6f - x3*x3 - y3*y3 - z3*z3;

    if (t > 0) {
      t *= t;
      n += t*t * dotRandVector(h3, x3, y3, z3);
    }
   
    return finalScale * n;
  }
}


(EDIT 20120830 - added configurable hash)
Offline kaffiene
« Reply #1 - Posted 2011-03-16 00:34:49 »

Are you aware of any GLSL 3d simplex noise implementations that produce a gradient vector as well as a noise value?
Online Roquen
« Reply #2 - Posted 2011-03-16 12:21:02 »

I'm not even aware of any reasonable shader code that simply produces the noise value (that's on my vague to-do list).  By gradient vector, do you mean with respect to the noise values 'around' the point?  So for flow-like information?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline krasse
« Reply #3 - Posted 2011-03-16 14:32:35 »

Are you aware of any GLSL 3d simplex noise implementations that produce a gradient vector as well as a noise value?

It shouldn't be that hard to compute with numeric differentiation over the complete 3D-function but that might be to expensive.

1  
2  
3  
4  
5  
double h = // small and appropriate according to the book Numerical Recipes ;)
double v = noise(x, y, z);
double gradX = (noise(x + h, y, z) - v) / h;
double gradY = (noise(x, y + h, z) - v) / h;
double gradZ = (noise(x, y, z + h) - v) / h;


To get the gradient from simplex noise you could gather all the contributions from the simplex vectors projected on the "normal" coordinate system base vectors and perhaps get away with approx 2 extra evaluations instead of 3 for the brute force method.
I have only tested the brute force for other types of noises, not simplex noise, so I might be completely off here Smiley


Online Spasi
« Reply #4 - Posted 2011-03-17 23:37:25 »

I just found this thread on Opengl.org, it has links to source + demo for a GLSL simplex noise implementation. Apparently it's very fast and hardware friendly.
Online Roquen
« Reply #5 - Posted 2011-03-18 15:32:09 »

Excellent!  Good find.  Quickly looked through the code and it looks reasonable.  Far different from the "sketch" of an implementation that I had in mind, so if I ever get motivated I'll still build a shader version from scratch and compare.

WRT: Gradient field.  It seems like it should be easy, but my brain no worky.
Online Roquen
« Reply #6 - Posted 2012-08-30 15:35:45 »

Modified source with new compile-time configurable.  The original hashing was pretty sucky (and is currently the default).  Defects appear at high frequency sampling and new hash does a better job (and is more expensive).
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (16 views)
2014-07-30 21:08:39

Riven (22 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!