Advertisement
Guest User

Untitled

a guest
Apr 1st, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.83 KB | None | 0 0
  1. #ifdef GL_ES
  2. precision highp float;
  3. #endif
  4.  
  5. uniform float time;
  6. uniform vec2 resolution;
  7. uniform float screenDistance;
  8. uniform vec3 cameraLocation;
  9. uniform vec3 cameraForward;
  10. uniform vec3 cameraUp;
  11.  
  12. vec3 mod289(vec3 x) {
  13.   return x - floor(x * (1.0 / 289.0)) * 289.0;
  14. }
  15.  
  16. vec2 mod289(vec2 x) {
  17.   return x - floor(x * (1.0 / 289.0)) * 289.0;
  18. }
  19.  
  20. vec3 permute(vec3 x) {
  21.   return mod289(((x*34.0)+1.0)*x);
  22. }
  23.  
  24. float snoise(vec2 v)
  25.   {
  26.   const vec4 C = vec4(0.211324865405187,  // (3.0-sqrt(3.0))/6.0
  27.                       0.366025403784439,  // 0.5*(sqrt(3.0)-1.0)
  28.                      -0.577350269189626,  // -1.0 + 2.0 * C.x
  29.                       0.024390243902439); // 1.0 / 41.0
  30. // First corner
  31.   vec2 i  = floor(v + dot(v, C.yy) );
  32.   vec2 x0 = v -   i + dot(i, C.xx);
  33.  
  34. // Other corners
  35.   vec2 i1;
  36.   //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0
  37.   //i1.y = 1.0 - i1.x;
  38.   i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
  39.   // x0 = x0 - 0.0 + 0.0 * C.xx ;
  40.   // x1 = x0 - i1 + 1.0 * C.xx ;
  41.   // x2 = x0 - 1.0 + 2.0 * C.xx ;
  42.   vec4 x12 = x0.xyxy + C.xxzz;
  43.   x12.xy -= i1;
  44.  
  45. // Permutations
  46.   i = mod289(i); // Avoid truncation effects in permutation
  47.   vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0 ))
  48.         + i.x + vec3(0.0, i1.x, 1.0 ));
  49.  
  50.   vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0);
  51.   m = m*m ;
  52.   m = m*m ;
  53.  
  54. // Gradients: 41 points uniformly over a line, mapped onto a diamond.
  55. // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287)
  56.  
  57.   vec3 x = 2.0 * fract(p * C.www) - 1.0;
  58.   vec3 h = abs(x) - 0.5;
  59.   vec3 ox = floor(x + 0.5);
  60.   vec3 a0 = x - ox;
  61.  
  62. // Normalise gradients implicitly by scaling m
  63. // Approximation of: m *= inversesqrt( a0*a0 + h*h );
  64.   m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h );
  65.  
  66. // Compute final noise value at P
  67.   vec3 g;
  68.   g.x  = a0.x  * x0.x  + h.x  * x0.y;
  69.   g.yz = a0.yz * x12.xz + h.yz * x12.yw;
  70.   return 130.0 * dot(m, g);
  71. }
  72.  
  73. /////////////////////////////////////////////////////////////////////////////////////////
  74. // Basic random function used in interpolation functions for purlin noise.
  75. // Inputs:
  76. //     co: The coordinate to generate a random value for.
  77. // Result:
  78. //     A random value for that coordinate, the same coordinate used again
  79. //     returns the same value.
  80. /////////////////////////////////////////////////////////////////////////////////////////
  81. float rand(in vec2 co){
  82.     return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
  83. }
  84.  
  85.  
  86. /////////////////////////////////////////////////////////////////////////////////////////
  87. // Applies cosine interpolation on the interval 'a' to 'b', and returns the
  88. // result at slice 'x' where 'x' ranges from 0.0 to 1.0.
  89. /////////////////////////////////////////////////////////////////////////////////////////
  90. float cosineInterpolation(in float a,  in float b,  in float x) {
  91.     float ft = x * 3.1415927;
  92.     float f = (1.0 - cos(ft)) * 0.5;
  93.     return a * (1.0 - f) + b * f;
  94. }
  95.  
  96. float linearInterpolation(in float a, in float b, in float x) {
  97.     return a * (1.0 - x) + b * x;
  98. }
  99.  
  100. // a switch for which interpolation to use
  101. float interpolate(in float a, in float b, in float x) {
  102.     //return linearInterpolation(a, b, x);
  103.     return cosineInterpolation(a, b, x);
  104. }
  105.  
  106. /////////////////////////////////////////////////////////////////////////////////////////
  107. // A wrapper for rand(in vec2 co) that smoothes out its values.
  108. /////////////////////////////////////////////////////////////////////////////////////////
  109. float smoothedNoise2(in vec2 co) {
  110.     float corners = (rand(vec2(co.x-1.0,co.y-1.0)) +
  111.                      rand(vec2(co.x+1.0,co.y-1.0)) +
  112.                      rand(vec2(co.x-1.0,co.y+1.0)) +
  113.                      rand(vec2(co.x+1.0,co.y+1.0))) / 16.0;
  114.     float sides = (rand(vec2(co.x-1.0, co.y)) +
  115.                    rand(vec2(co.x, co.y+1.0)) +
  116.                    rand(vec2(co.x+1.0, co.y)) +
  117.                    rand(vec2(co.x-1.0, co.y-1.0))) / 8.0;
  118.     float centre = rand(vec2(co)) / 4.0;
  119.     return corners + sides + centre;
  120. }
  121.  
  122. /////////////////////////////////////////////////////////////////////////////////////////
  123. // Cosine interpolation wrapper for the smoothNoise2(in vec2 co) function.
  124. // This function gives ya the smooth curvy surface.
  125. /////////////////////////////////////////////////////////////////////////////////////////
  126. float interpolate2(in vec2 co) {
  127.     float intX = floor(co.x);
  128.     float fractX = fract(co.x);
  129.     float intY = floor(co.y);
  130.     float fractY = fract(co.y);
  131.    
  132.     float v1 = smoothedNoise2(vec2(intX, intY));
  133.     float v2 = smoothedNoise2(vec2(intX + 1.0, intY));
  134.     float v3 = smoothedNoise2(vec2(intX, intY + 1.0));
  135.     float v4 = smoothedNoise2(vec2(intX + 1.0, intY + 1.0));
  136.    
  137.     float i1 = interpolate(v1, v2, fractX);
  138.     float i2 = interpolate(v3, v4, fractX);
  139.    
  140.     return cosineInterpolation(i1, i2, fractY);
  141. }
  142.  
  143. /////////////////////////////////////////////////////////////////////////////////////////
  144. // Purlin Noise, the star of the show, Purlin Noise can be used for generating
  145. // many different types of textures, as well as making other effects, such
  146. // as clouds.
  147. /////////////////////////////////////////////////////////////////////////////////////////
  148. float purlinNoise(in vec2 co, in float persistance, in float numOctaves) {
  149.     float total = 0.0;
  150.     float frequency = 1.0;
  151.     float amplitude = 1.0;
  152.     float i;
  153.     for (i = 0.0; i < numOctaves; i = i + 1.0) {
  154.         total += interpolate2(co * frequency) * amplitude;
  155.         frequency *= 2.0;
  156.         amplitude *= persistance;
  157.     }
  158.     return total;
  159. }
  160.  
  161. /////////////////////////////////////////////////////////////////////////////////////////
  162. // Defines our current terrain as a height map function
  163. /////////////////////////////////////////////////////////////////////////////////////////
  164.  
  165. float noise2f(in float x, in float z) {
  166.     //return rand(vec2(x*0.0001,z*0.0001)) * 2.0 - 1.0;
  167.     //return interpolate2(vec2(x,z)) * 2.0 - 1.0;
  168.     return cos(x) * sin(z);
  169.     //return cos(x + 0.001 * sin(z)) * sin(z);
  170.     //return 0.0;
  171.     //return noise1(vec2(x, z));
  172.     //return fract(snoise(vec2(x, z)));
  173.     //return sin(length(vec2(x,z)));
  174.     //return sin(cos(length(vec2(x,z))) + sin(z));
  175.     //return sin(x * z * 0.001);
  176. }
  177.  
  178. /////////////////////////////////////////////////////////////////////////////////////////
  179. // Calculates the origin and direction of the ray to use in ray marching for
  180. // the current fragment coordinate.
  181. /////////////////////////////////////////////////////////////////////////////////////////
  182. void calculateRay(out vec3 ro, out vec3 rd) {
  183.     vec3 up = normalize(cameraUp);
  184.     vec3 forward = normalize(cameraForward);
  185.     vec3 right = cross(cameraForward, cameraUp);
  186.     rd.x = (gl_FragCoord.x - resolution.x * 0.5) * right.x;
  187.     rd.x += (gl_FragCoord.y - resolution.y * 0.5) * up.x;
  188.     rd.x += screenDistance * forward.x;
  189.     rd.y = (gl_FragCoord.x - resolution.x * 0.5) * right.y;
  190.     rd.y += (gl_FragCoord.y - resolution.y * 0.5) * up.y;
  191.     rd.y += screenDistance * forward.y;
  192.     rd.z = (gl_FragCoord.x - resolution.x * 0.5) * right.z;
  193.     rd.z += (gl_FragCoord.y - resolution.y * 0.5) * up.z;
  194.     rd.z += screenDistance * forward.z;
  195.     rd = normalize(rd);
  196.     ro = cameraLocation;
  197. }
  198.  
  199. float dSpace(in vec3 p, out float id) {
  200.     // distance to the sphere
  201.     float d1 = length(mod(p,100.0) - vec3(50.0)) - 20.0;
  202.     // distance to cylinder
  203.     float d2 = length(mod(p.xy,100.0) - vec2(50.0)) - 5.0;
  204.     // distance to cylinder
  205.     float d3 = length(mod(p.yz,100.0) - vec2(50.0)) - 5.0;
  206.     // distance to cylinder
  207.     float d4 = length(mod(p.zx,100.0) - vec2(50.0)) - 5.0;
  208.     // find the nearest
  209.     float nearestD = min(min(d1, d2), min(d3, d4));
  210.     id = 1.0;
  211.     if (nearestD == d2) { id = 2.0; }
  212.     if (nearestD == d3) { id = 3.0; }
  213.     if (nearestD == d4) { id = 4.0; }
  214.     return nearestD;
  215. }
  216.  
  217. vec3 nSpace(in vec3 p, in float id) {
  218.     if (id > 0.5 && id < 1.5) {
  219.         return normalize(mod(p,100.0) - vec3(50.0));
  220.     } else if (id > 1.5 && id < 2.5) {
  221.         return normalize(vec3((mod(p,100.0) - vec3(50.0)).xy, 0.0));
  222.     } else if (id > 2.5 && id < 3.5) {
  223.         return normalize(vec3((mod(p,100.0) - vec3(50.0)).zy, 0.0));
  224.     } else {
  225.         return normalize(vec3((mod(p,100.0) - vec3(50.0)).zx, 0.0));
  226.     }
  227.     return normalize(vec3(1.0));
  228. }
  229.  
  230. float castRay(in vec3 ro, in vec3 rd, out float id) {
  231.     float minT = 10.0;
  232.     float maxT = 5000.0;
  233.     float minStep = 5.0;
  234.     float t;
  235.     float dist;
  236.     float dt;
  237.     vec3 p;
  238.     float lastDist = dSpace(ro, id);
  239.     for (t = minT; t < maxT; t += dt) {
  240.         p = ro + t * rd;
  241.         dist = dSpace(p, id);
  242.         if (dist < 0.001) {
  243.             float a = (dist - lastDist) / dt;
  244.             float b = dist - a * t;
  245.             float resT = abs(-b / a);
  246.             return resT;
  247.         }
  248.         dt = max(dist, minStep);
  249.         lastDist = dist;
  250.     }
  251.     return -1.0;
  252. }
  253.  
  254. float intersect(in vec3 ro, in vec3 rd, out float resT) {
  255.     float id = -1.0;
  256.     float t = castRay(ro, rd, id);
  257.     resT = t;
  258.     return id;
  259. }
  260.  
  261. //// FOG /////////////////////////////////
  262.  
  263. vec3 applyFog( in vec3  rgb,       // original color of the pixel
  264.                in float dist )     // camera to point distance
  265. {
  266.     float fogAmount = exp( -dist*0.0005 );
  267.     vec3  fogColor  = vec3(0.5,0.6,0.7);
  268.     return mix( rgb, fogColor, 1.0 - fogAmount );
  269. }
  270.  
  271. //////////////////////////////////////////
  272.  
  273. void main() {
  274.     vec3 light = normalize(vec3(0.57703, 0.57703, -0.57703));
  275.    
  276.     // uv are the pixel coordinates, from 0 to 1
  277.     vec2 uv = gl_FragCoord.xy / resolution.xy;
  278.    
  279.     gl_FragColor = vec4(1.0,uv.x,uv.y,1.0);
  280.    
  281.     // generate a ray with origin ro and direction rd
  282.     vec3 ro;
  283.     vec3 rd;
  284.     calculateRay(ro, rd);
  285.    
  286.     // intersect the ray with the 3d scene
  287.     float t;
  288.     float id = intersect(ro, rd, t);
  289.    
  290.     // draw black by default
  291.     vec3 col = vec3(0.0);
  292.     if (id > 0.5) {
  293.         vec3 pos = ro + t * rd;
  294.         vec3 nor = nSpace(pos, id);
  295.         float dif = clamp(dot(nor, light), 0.0, 1.0);
  296.         float amb = 0.5 + 0.5 * nor.y;
  297.         col = vec3(1.0, 0.8, 0.6) * dif + amb * vec3(0.5, 0.6, 0.7);
  298.     }
  299.    
  300.     if (id > 0.0) {
  301.         col = applyFog(col, t);
  302.     } else {
  303.         col = applyFog(col, 10000.0);
  304.     }
  305.    
  306.     gl_FragColor = vec4(col, 1.0);
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement