为什么我的GLSL简单噪声质量比在Java上运行的要差很多呢?

huangapple 未分类评论49阅读模式
英文:

Why is the quality of my GLSL simplex noise so much worse then the one run on Java?

问题

  1. public class SimplexNoise {
  2. private static Grad grad3[] = {new Grad(1,1,0),new Grad(-1,1,0),new Grad(1,-1,0),new Grad(-1,-1,0),
  3. new Grad(1,0,1),new Grad(-1,0,1),new Grad(1,0,-1),new Grad(-1,0,-1),
  4. new Grad(0,1,1),new Grad(0,-1,1),new Grad(0,1,-1),new Grad(0,-1,-1)};
  5. private static Grad grad4[]= {new Grad(0,1,1,1),new Grad(0,1,1,-1),new Grad(0,1,-1,1),new Grad(0,1,-1,-1),
  6. new Grad(0,-1,1,1),new Grad(0,-1,1,-1),new Grad(0,-1,-1,1),new Grad(0,-1,-1,-1),
  7. new Grad(1,0,1,1),new Grad(1,0,1,-1),new Grad(1,0,-1,1),new Grad(1,0,-1,-1),
  8. new Grad(-1,0,1,1),new Grad(-1,0,1,-1),new Grad(-1,0,-1,1),new Grad(-1,0,-1,-1),
  9. new Grad(1,1,0,1),new Grad(1,1,0,-1),new Grad(1,-1,0,1),new Grad(1,-1,0,-1),
  10. new Grad(-1,1,0,1),new Grad(-1,1,0,-1),new Grad(-1,-1,0,1),new Grad(-1,-1,0,-1),
  11. new Grad(1,1,1,0),new Grad(1,1,-1,0),new Grad(1,-1,1,0),new Grad(1,-1,-1,0),
  12. new Grad(-1,1,1,0),new Grad(-1,1,-1,0),new Grad(-1,-1,1,0),new Grad(-1,-1,-1,0)};
  13. // ... Other constants and helper functions ...
  14. // 2D simplex noise
  15. public static double noise(double xin, double yin) {
  16. // Implementation details for 2D simplex noise
  17. }
  18. // 3D simplex noise
  19. public static double noise(double xin, double yin, double zin) {
  20. // Implementation details for 3D simplex noise
  21. }
  22. // 4D simplex noise
  23. public static double noise(double x, double y, double z, double w) {
  24. // Implementation details for 4D simplex noise
  25. }
  26. // ... Other methods ...
  27. // Grad class and other details ...
  28. }

Please note that I have omitted some parts of the original code, such as the fastfloor function and the Grad class, to focus on the structure and the core noise generation functions. If you need the full implementation, you can combine the provided parts with the missing parts from the original code.

英文:

I've been working on an infinetly procedurally generated terrain using Simplex Noise. I've decided to improve its performance by transferring the Code for simplex noise to my compute shader. The problem was that the code I found for GLSL simplex noise performs worse then the code I found for Java simplex noise. Both of them work properly but the Java simplex noise produces terrain that looks way better and more interesting then the one in GLSL. Is there a way for me to improve the Simplex noise algorithm on my Compute shader or is it better to risk performance speed by using the Java Noise and get better results?

Compute Shader Simplex noise Code

  1. #version 430 core
  2. layout (local_size_x = 10, local_size_y = 10, local_size_z = 10) in;
  3. layout(std430, binding=0) buffer Pos3D{
  4. float Position3D[];
  5. };
  6. layout(std430, binding=1) buffer Pos2D{
  7. float Position2D[];
  8. };
  9. uniform float size;
  10. vec4 permute(vec4 x){return mod(((x*34.0)+1.0)*x, 289.0);}
  11. vec4 taylorInvSqrt(vec4 r){return 1.79284291400159 - 0.85373472095314 * r;}
  12. float snoise(vec3 v){
  13. const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
  14. const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
  15. // First corner
  16. vec3 i = floor(v + dot(v, C.yyy) );
  17. vec3 x0 = v - i + dot(i, C.xxx) ;
  18. // Other corners
  19. vec3 g = step(x0.yzx, x0.xyz);
  20. vec3 l = 1.0 - g;
  21. vec3 i1 = min( g.xyz, l.zxy );
  22. vec3 i2 = max( g.xyz, l.zxy );
  23. // x0 = x0 - 0. + 0.0 * C
  24. vec3 x1 = x0 - i1 + 1.0 * C.xxx;
  25. vec3 x2 = x0 - i2 + 2.0 * C.xxx;
  26. vec3 x3 = x0 - 1. + 3.0 * C.xxx;
  27. // Permutations
  28. i = mod(i, 289.0 );
  29. vec4 p = permute( permute( permute(
  30. i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
  31. + i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
  32. + i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
  33. // Gradients
  34. // ( N*N points uniformly over a square, mapped onto an octahedron.)
  35. float n_ = 1.0/7.0; // N=7
  36. vec3 ns = n_ * D.wyz - D.xzx;
  37. vec4 j = p - 49.0 * floor(p * ns.z *ns.z); // mod(p,N*N)
  38. vec4 x_ = floor(j * ns.z);
  39. vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
  40. vec4 x = x_ *ns.x + ns.yyyy;
  41. vec4 y = y_ *ns.x + ns.yyyy;
  42. vec4 h = 1.0 - abs(x) - abs(y);
  43. vec4 b0 = vec4( x.xy, y.xy );
  44. vec4 b1 = vec4( x.zw, y.zw );
  45. vec4 s0 = floor(b0)*2.0 + 1.0;
  46. vec4 s1 = floor(b1)*2.0 + 1.0;
  47. vec4 sh = -step(h, vec4(0.0));
  48. vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
  49. vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
  50. vec3 p0 = vec3(a0.xy,h.x);
  51. vec3 p1 = vec3(a0.zw,h.y);
  52. vec3 p2 = vec3(a1.xy,h.z);
  53. vec3 p3 = vec3(a1.zw,h.w);
  54. //Normalise gradients
  55. vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
  56. p0 *= norm.x;
  57. p1 *= norm.y;
  58. p2 *= norm.z;
  59. p3 *= norm.w;
  60. // Mix final noise value
  61. vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
  62. m = m * m;
  63. return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
  64. dot(p2,x2), dot(p3,x3) ) );
  65. }
  66. vec3 permute(vec3 x) { return mod(((x*34.0)+1.0)*x, 289.0); }
  67. float snoise(vec2 v){
  68. const vec4 C = vec4(0.211324865405187, 0.366025403784439,
  69. -0.577350269189626, 0.024390243902439);
  70. vec2 i = floor(v + dot(v, C.yy) );
  71. vec2 x0 = v - i + dot(i, C.xx);
  72. vec2 i1;
  73. i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
  74. vec4 x12 = x0.xyxy + C.xxzz;
  75. x12.xy -= i1;
  76. i = mod(i, 289.0);
  77. vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
  78. + i.x + vec3(0.0, i1.x, 1.0 ));
  79. vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy),
  80. dot(x12.zw,x12.zw)), 0.0);
  81. m = m*m ;
  82. m = m*m ;
  83. vec3 x = 2.0 * fract(p * C.www) - 1.0;
  84. vec3 h = abs(x) - 0.5;
  85. vec3 ox = floor(x + 0.5);
  86. vec3 a0 = x - ox;
  87. m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
  88. vec3 g;
  89. g.x = a0.x * x0.x + h.x * x0.y;
  90. g.yz = a0.yz * x12.xz + h.yz * x12.yw;
  91. return 130.0 * dot(m, g);
  92. }
  93. float sumOctaves(int iterations, vec3 pos, double persistance, double scale, double low, double high){
  94. double maxamp = 0;
  95. double amp = 1;
  96. double frequency = scale;
  97. double noise = 0;
  98. for(int i = 0; i<iterations; i++){
  99. noise += snoise(vec3(pos.x*frequency, pos.y*frequency, pos.z*frequency))*amp;
  100. maxamp += amp;
  101. amp *= persistance;
  102. frequency *= 2;
  103. }
  104. noise /= maxamp;
  105. noise = noise * (high - low) / 2 + (high + low) / 2;
  106. return float(noise);
  107. }
  108. float sumOctaves(int iterations, vec2 pos, double persistance, double scale, double low, double high){
  109. double maxamp = 0;
  110. double amp = 1;
  111. double frequency = scale;
  112. double noise = 0;
  113. for(int i = 0; i<iterations; i++){
  114. noise += snoise(vec2(pos.x*frequency, pos.y*frequency))*amp;
  115. maxamp += amp;
  116. amp *= persistance;
  117. frequency *= 2;
  118. }
  119. noise /= maxamp;
  120. noise = noise * (high - low) / 2 + (high + low) / 2;
  121. return float(noise);
  122. }
  123. int getPosition(vec3 v){
  124. return int(v.x+v.z*size+v.y*size*size);
  125. }
  126. int getPosition(vec2 v){
  127. return int(v.x+v.y*size);
  128. }
  129. void main(){
  130. if(gl_GlobalInvocationID.x < size && gl_GlobalInvocationID.y < size && gl_GlobalInvocationID.z < size){
  131. Position3D[getPosition(gl_GlobalInvocationID)] = sumOctaves(4,gl_GlobalInvocationID,0.5,0.01,0,1);
  132. Position2D[getPosition(gl_GlobalInvocationID.xz)] = sumOctaves(4,gl_GlobalInvocationID.xz,0.5,0.01,0,1);
  133. }
  134. }

Java Simplex Noise Code

  1. public class SimplexNoise { /[![enter image description here][1]][1]/ Simplex noise in 2D, 3D and 4D
  2. private static Grad grad3[] = {new Grad(1,1,0),new Grad(-1,1,0),new Grad(1,-1,0),new Grad(-1,-1,0),
  3. new Grad(1,0,1),new Grad(-1,0,1),new Grad(1,0,-1),new Grad(-1,0,-1),
  4. new Grad(0,1,1),new Grad(0,-1,1),new Grad(0,1,-1),new Grad(0,-1,-1)};
  5. private static Grad grad4[]= {new Grad(0,1,1,1),new Grad(0,1,1,-1),new Grad(0,1,-1,1),new Grad(0,1,-1,-1),
  6. new Grad(0,-1,1,1),new Grad(0,-1,1,-1),new Grad(0,-1,-1,1),new Grad(0,-1,-1,-1),
  7. new Grad(1,0,1,1),new Grad(1,0,1,-1),new Grad(1,0,-1,1),new Grad(1,0,-1,-1),
  8. new Grad(-1,0,1,1),new Grad(-1,0,1,-1),new Grad(-1,0,-1,1),new Grad(-1,0,-1,-1),
  9. new Grad(1,1,0,1),new Grad(1,1,0,-1),new Grad(1,-1,0,1),new Grad(1,-1,0,-1),
  10. new Grad(-1,1,0,1),new Grad(-1,1,0,-1),new Grad(-1,-1,0,1),new Grad(-1,-1,0,-1),
  11. new Grad(1,1,1,0),new Grad(1,1,-1,0),new Grad(1,-1,1,0),new Grad(1,-1,-1,0),
  12. new Grad(-1,1,1,0),new Grad(-1,1,-1,0),new Grad(-1,-1,1,0),new Grad(-1,-1,-1,0)};
  13. private static short p[] = {151,160,137,91,90,15,
  14. 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  15. 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  16. 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  17. 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  18. 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
  19. 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
  20. 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  21. 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
  22. 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
  23. 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
  24. 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
  25. 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180};
  26. // To remove the need for index wrapping, double the permutation table length
  27. private static short perm[] = new short[512];
  28. private static short permMod12[] = new short[512];
  29. static {
  30. for(int i=0; i<512; i++)
  31. {
  32. perm[i]=p[i & 255];
  33. permMod12[i] = (short)(perm[i] % 12);
  34. }
  35. }
  36. // Skewing and unskewing factors for 2, 3, and 4 dimensions
  37. private static final double F2 = 0.5*(Math.sqrt(3.0)-1.0);
  38. private static final double G2 = (3.0-Math.sqrt(3.0))/6.0;
  39. private static final double F3 = 1.0/3.0;
  40. private static final double G3 = 1.0/6.0;
  41. private static final double F4 = (Math.sqrt(5.0)-1.0)/4.0;
  42. private static final double G4 = (5.0-Math.sqrt(5.0))/20.0;
  43. // This method is a *lot* faster than using (int)Math.floor(x)
  44. private static int fastfloor(double x) {
  45. int xi = (int)x;
  46. return x<xi ? xi-1 : xi;
  47. }
  48. private static double dot(Grad g, double x, double y) {
  49. return g.x*x + g.y*y; }
  50. private static double dot(Grad g, double x, double y, double z) {
  51. return g.x*x + g.y*y + g.z*z; }
  52. private static double dot(Grad g, double x, double y, double z, double w) {
  53. return g.x*x + g.y*y + g.z*z + g.w*w; }
  54. // 2D simplex noise
  55. public static double noise(double xin, double yin) {
  56. double n0, n1, n2; // Noise contributions from the three corners
  57. // Skew the input space to determine which simplex cell we're in
  58. double s = (xin+yin)*F2; // Hairy factor for 2D
  59. int i = fastfloor(xin+s);
  60. int j = fastfloor(yin+s);
  61. double t = (i+j)*G2;
  62. double X0 = i-t; // Unskew the cell origin back to (x,y) space
  63. double Y0 = j-t;
  64. double x0 = xin-X0; // The x,y distances from the cell origin
  65. double y0 = yin-Y0;
  66. // For the 2D case, the simplex shape is an equilateral triangle.
  67. // Determine which simplex we are in.
  68. int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
  69. if(x0>y0) {i1=1; j1=0;} // lower triangle, XY order: (0,0)->(1,0)->(1,1)
  70. else {i1=0; j1=1;} // upper triangle, YX order: (0,0)->(0,1)->(1,1)
  71. // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
  72. // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
  73. // c = (3-sqrt(3))/6
  74. double x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
  75. double y1 = y0 - j1 + G2;
  76. double x2 = x0 - 1.0 + 2.0 * G2; // Offsets for last corner in (x,y) unskewed coords
  77. double y2 = y0 - 1.0 + 2.0 * G2;
  78. // Work out the hashed gradient indices of the three simplex corners
  79. int ii = i & 255;
  80. int jj = j & 255;
  81. int gi0 = permMod12[ii+perm[jj]];
  82. int gi1 = permMod12[ii+i1+perm[jj+j1]];
  83. int gi2 = permMod12[ii+1+perm[jj+1]];
  84. // Calculate the contribution from the three corners
  85. double t0 = 0.5 - x0*x0-y0*y0;
  86. if(t0<0) n0 = 0.0;
  87. else {
  88. t0 *= t0;
  89. n0 = t0 * t0 * dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient
  90. }
  91. double t1 = 0.5 - x1*x1-y1*y1;
  92. if(t1<0) n1 = 0.0;
  93. else {
  94. t1 *= t1;
  95. n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
  96. }
  97. double t2 = 0.5 - x2*x2-y2*y2;
  98. if(t2<0) n2 = 0.0;
  99. else {
  100. t2 *= t2;
  101. n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
  102. }
  103. // Add contributions from each corner to get the final noise value.
  104. // The result is scaled to return values in the interval [-1,1].
  105. return 70.0 * (n0 + n1 + n2);
  106. }
  107. // 3D simplex noise
  108. public static double noise(double xin, double yin, double zin) {
  109. double n0, n1, n2, n3; // Noise contributions from the four corners
  110. // Skew the input space to determine which simplex cell we're in
  111. double s = (xin+yin+zin)*F3; // Very nice and simple skew factor for 3D
  112. int i = fastfloor(xin+s);
  113. int j = fastfloor(yin+s);
  114. int k = fastfloor(zin+s);
  115. double t = (i+j+k)*G3;
  116. double X0 = i-t; // Unskew the cell origin back to (x,y,z) space
  117. double Y0 = j-t;
  118. double Z0 = k-t;
  119. double x0 = xin-X0; // The x,y,z distances from the cell origin
  120. double y0 = yin-Y0;
  121. double z0 = zin-Z0;
  122. // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
  123. // Determine which simplex we are in.
  124. int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
  125. int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
  126. if(x0>=y0) {
  127. if(y0>=z0)
  128. { i1=1; j1=0; k1=0; i2=1; j2=1; k2=0; } // X Y Z order
  129. else if(x0>=z0) { i1=1; j1=0; k1=0; i2=1; j2=0; k2=1; } // X Z Y order
  130. else { i1=0; j1=0; k1=1; i2=1; j2=0; k2=1; } // Z X Y order
  131. }
  132. else { // x0<y0
  133. if(y0<z0) { i1=0; j1=0; k1=1; i2=0; j2=1; k2=1; } // Z Y X order
  134. else if(x0<z0) { i1=0; j1=1; k1=0; i2=0; j2=1; k2=1; } // Y Z X order
  135. else { i1=0; j1=1; k1=0; i2=1; j2=1; k2=0; } // Y X Z order
  136. }
  137. // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
  138. // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
  139. // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
  140. // c = 1/6.
  141. double x1 = x0 - i1 + G3; // Offsets for second corner in (x,y,z) coords
  142. double y1 = y0 - j1 + G3;
  143. double z1 = z0 - k1 + G3;
  144. double x2 = x0 - i2 + 2.0*G3; // Offsets for third corner in (x,y,z) coords
  145. double y2 = y0 - j2 + 2.0*G3;
  146. double z2 = z0 - k2 + 2.0*G3;
  147. double x3 = x0 - 1.0 + 3.0*G3; // Offsets for last corner in (x,y,z) coords
  148. double y3 = y0 - 1.0 + 3.0*G3;
  149. double z3 = z0 - 1.0 + 3.0*G3;
  150. // Work out the hashed gradient indices of the four simplex corners
  151. int ii = i & 255;
  152. int jj = j & 255;
  153. int kk = k & 255;
  154. int gi0 = permMod12[ii+perm[jj+perm[kk]]];
  155. int gi1 = permMod12[ii+i1+perm[jj+j1+perm[kk+k1]]];
  156. int gi2 = permMod12[ii+i2+perm[jj+j2+perm[kk+k2]]];
  157. int gi3 = permMod12[ii+1+perm[jj+1+perm[kk+1]]];
  158. // Calculate the contribution from the four corners
  159. double t0 = 0.6 - x0*x0 - y0*y0 - z0*z0;
  160. if(t0<0) n0 = 0.0;
  161. else {
  162. t0 *= t0;
  163. n0 = t0 * t0 * dot(grad3[gi0], x0, y0, z0);
  164. }
  165. double t1 = 0.6 - x1*x1 - y1*y1 - z1*z1;
  166. if(t1<0) n1 = 0.0;
  167. else {
  168. t1 *= t1;
  169. n1 = t1 * t1 * dot(grad3[gi1], x1, y1, z1);
  170. }
  171. double t2 = 0.6 - x2*x2 - y2*y2 - z2*z2;
  172. if(t2<0) n2 = 0.0;
  173. else {
  174. t2 *= t2;
  175. n2 = t2 * t2 * dot(grad3[gi2], x2, y2, z2);
  176. }
  177. double t3 = 0.6 - x3*x3 - y3*y3 - z3*z3;
  178. if(t3<0) n3 = 0.0;
  179. else {
  180. t3 *= t3;
  181. n3 = t3 * t3 * dot(grad3[gi3], x3, y3, z3);
  182. }
  183. // Add contributions from each corner to get the final noise value.
  184. return 32.0*(n0 + n1 + n2 + n3);
  185. }
  186. // 4D simplex noise, better simplex rank ordering method 2012-03-09
  187. public static double noise(double x, double y, double z, double w) {
  188. double n0, n1, n2, n3, n4; // Noise contributions from the five corners
  189. // Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
  190. double s = (x + y + z + w) * F4; // Factor for 4D skewing
  191. int i = fastfloor(x + s);
  192. int j = fastfloor(y + s);
  193. int k = fastfloor(z + s);
  194. int l = fastfloor(w + s);
  195. double t = (i + j + k + l) * G4; // Factor for 4D unskewing
  196. double X0 = i - t; // Unskew the cell origin back to (x,y,z,w) space
  197. double Y0 = j - t;
  198. double Z0 = k - t;
  199. double W0 = l - t;
  200. double x0 = x - X0; // The x,y,z,w distances from the cell origin
  201. double y0 = y - Y0;
  202. double z0 = z - Z0;
  203. double w0 = w - W0;
  204. // For the 4D case, the simplex is a 4D shape I won't even try to describe.
  205. // To find out which of the 24 possible simplices we're in, we need to
  206. // determine the magnitude ordering of x0, y0, z0 and w0.
  207. // Six pair-wise comparisons are performed between each possible pair
  208. // of the four coordinates, and the results are used to rank the numbers.
  209. int rankx = 0;
  210. int ranky = 0;
  211. int rankz = 0;
  212. int rankw = 0;
  213. if(x0 > y0) rankx++; else ranky++;
  214. if(x0 > z0) rankx++; else rankz++;
  215. if(x0 > w0) rankx++; else rankw++;
  216. if(y0 > z0) ranky++; else rankz++;
  217. if(y0 > w0) ranky++; else rankw++;
  218. if(z0 > w0) rankz++; else rankw++;
  219. int i1, j1, k1, l1; // The integer offsets for the second simplex corner
  220. int i2, j2, k2, l2; // The integer offsets for the third simplex corner
  221. int i3, j3, k3, l3; // The integer offsets for the fourth simplex corner
  222. // [rankx, ranky, rankz, rankw] is a 4-vector with the numbers 0, 1, 2 and 3
  223. // in some order. We use a thresholding to set the coordinates in turn.
  224. // Rank 3 denotes the largest coordinate.
  225. i1 = rankx >= 3 ? 1 : 0;
  226. j1 = ranky >= 3 ? 1 : 0;
  227. k1 = rankz >= 3 ? 1 : 0;
  228. l1 = rankw >= 3 ? 1 : 0;
  229. // Rank 2 denotes the second largest coordinate.
  230. i2 = rankx >= 2 ? 1 : 0;
  231. j2 = ranky >= 2 ? 1 : 0;
  232. k2 = rankz >= 2 ? 1 : 0;
  233. l2 = rankw >= 2 ? 1 : 0;
  234. // Rank 1 denotes the second smallest coordinate.
  235. i3 = rankx >= 1 ? 1 : 0;
  236. j3 = ranky >= 1 ? 1 : 0;
  237. k3 = rankz >= 1 ? 1 : 0;
  238. l3 = rankw >= 1 ? 1 : 0;
  239. // The fifth corner has all coordinate offsets = 1, so no need to compute that.
  240. double x1 = x0 - i1 + G4; // Offsets for second corner in (x,y,z,w) coords
  241. double y1 = y0 - j1 + G4;
  242. double z1 = z0 - k1 + G4;
  243. double w1 = w0 - l1 + G4;
  244. double x2 = x0 - i2 + 2.0*G4; // Offsets for third corner in (x,y,z,w) coords
  245. double y2 = y0 - j2 + 2.0*G4;
  246. double z2 = z0 - k2 + 2.0*G4;
  247. double w2 = w0 - l2 + 2.0*G4;
  248. double x3 = x0 - i3 + 3.0*G4; // Offsets for fourth corner in (x,y,z,w) coords
  249. double y3 = y0 - j3 + 3.0*G4;
  250. double z3 = z0 - k3 + 3.0*G4;
  251. double w3 = w0 - l3 + 3.0*G4;
  252. double x4 = x0 - 1.0 + 4.0*G4; // Offsets for last corner in (x,y,z,w) coords
  253. double y4 = y0 - 1.0 + 4.0*G4;
  254. double z4 = z0 - 1.0 + 4.0*G4;
  255. double w4 = w0 - 1.0 + 4.0*G4;
  256. // Work out the hashed gradient indices of the five simplex corners
  257. int ii = i & 255;
  258. int jj = j & 255;
  259. int kk = k & 255;
  260. int ll = l & 255;
  261. int gi0 = perm[ii+perm[jj+perm[kk+perm[ll]]]] % 32;
  262. int gi1 = perm[ii+i1+perm[jj+j1+perm[kk+k1+perm[ll+l1]]]] % 32;
  263. int gi2 = perm[ii+i2+perm[jj+j2+perm[kk+k2+perm[ll+l2]]]] % 32;
  264. int gi3 = perm[ii+i3+perm[jj+j3+perm[kk+k3+perm[ll+l3]]]] % 32;
  265. int gi4 = perm[ii+1+perm[jj+1+perm[kk+1+perm[ll+1]]]] % 32;
  266. // Calculate the contribution from the five corners
  267. double t0 = 0.6 - x0*x0 - y0*y0 - z0*z0 - w0*w0;
  268. if(t0<0) n0 = 0.0;
  269. else {
  270. t0 *= t0;
  271. n0 = t0 * t0 * dot(grad4[gi0], x0, y0, z0, w0);
  272. }
  273. double t1 = 0.6 - x1*x1 - y1*y1 - z1*z1 - w1*w1;
  274. if(t1<0) n1 = 0.0;
  275. else {
  276. t1 *= t1;
  277. n1 = t1 * t1 * dot(grad4[gi1], x1, y1, z1, w1);
  278. }
  279. double t2 = 0.6 - x2*x2 - y2*y2 - z2*z2 - w2*w2;
  280. if(t2<0) n2 = 0.0;
  281. else {
  282. t2 *= t2;
  283. n2 = t2 * t2 * dot(grad4[gi2], x2, y2, z2, w2);
  284. }
  285. double t3 = 0.6 - x3*x3 - y3*y3 - z3*z3 - w3*w3;
  286. if(t3<0) n3 = 0.0;
  287. else {
  288. t3 *= t3;
  289. n3 = t3 * t3 * dot(grad4[gi3], x3, y3, z3, w3);
  290. }
  291. double t4 = 0.6 - x4*x4 - y4*y4 - z4*z4 - w4*w4;
  292. if(t4<0) n4 = 0.0;
  293. else {
  294. t4 *= t4;
  295. n4 = t4 * t4 * dot(grad4[gi4], x4, y4, z4, w4);
  296. }
  297. // Sum up and scale the result to cover the range [-1,1]
  298. return 27.0 * (n0 + n1 + n2 + n3 + n4);
  299. }
  300. // Inner class to speed upp gradient computations
  301. // (In Java, array access is a lot slower than member access)
  302. private static class Grad
  303. {
  304. double x, y, z, w;
  305. Grad(double x, double y, double z)
  306. {
  307. this.x = x;
  308. this.y = y;
  309. this.z = z;
  310. }
  311. Grad(double x, double y, double z, double w)
  312. {
  313. this.x = x;
  314. this.y = y;
  315. this.z = z;
  316. this.w = w;
  317. }
  318. }
  319. }

Both code is rendered using the same octave, frequency and amplitude.
Java Simplex Noise Render
为什么我的GLSL简单噪声质量比在Java上运行的要差很多呢?

GLSL Simplex Noise Render
为什么我的GLSL简单噪声质量比在Java上运行的要差很多呢?

huangapple
  • 本文由 发表于 2020年7月23日 10:34:39
  • 转载请务必保留本文链接:https://java.coder-hub.com/63046006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定