Advertisement
Guest User

fbm.jxs

a guest
Jun 26th, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.48 KB | None | 0 0
  1. <jittershader name="fbm">
  2.     <description>
  3.     Fractional Brownian Noise
  4.     </description>
  5.     <param name="time" type="float" default="0." />
  6.     <param name="bSmooth" type="int" default="1." />
  7.     <param name="octaves" type="int" default="4" />
  8.     <param name="map" type="vec2" default="0. 1." />
  9.     <param name="scale" type="vec2" default="5. 5." />
  10.     <language name="glsl" version="2.1">
  11.         <bind param="time" program="fp" />
  12.         <bind param="bSmooth" program="fp" />
  13.         <bind param="octaves" program="fp" />
  14.         <bind param="map" program="fp" />
  15.         <bind param="scale" program="fp" />
  16.         <program name="vp" type="vertex" source="sh.passthrudim.vp.glsl" />
  17.         <program name="fp" type="fragment">
  18.  
  19. <![CDATA[
  20.  
  21.  
  22. //
  23. // Description : Array and textureless GLSL 2D/3D/4D simplex
  24. //               noise functions.
  25. //      Author : Ian McEwan, Ashima Arts.
  26. //  Maintainer : stegu
  27. //     Lastmod : 20110822 (ijm)
  28. //     License : Copyright (C) 2011 Ashima Arts. All rights reserved.
  29. //               Distributed under the MIT License. See LICENSE file.
  30. //               https://github.com/ashima/webgl-noise
  31. //               https://github.com/stegu/webgl-noise
  32. //
  33.  
  34. vec3 mod289(vec3 x) {
  35.     return x - floor(x * (1.0 / 289.0)) * 289.0;
  36. }
  37.  
  38. vec4 mod289(vec4 x) {
  39.     return x - floor(x * (1.0 / 289.0)) * 289.0;
  40. }
  41.  
  42. vec4 permute(vec4 x) {
  43.     return mod289(((x*34.0)+1.0)*x);
  44. }
  45.  
  46. vec4 taylorInvSqrt(vec4 r)
  47. {
  48.     return 1.79284291400159 - 0.85373472095314 * r;
  49. }
  50.  
  51. float snoise(vec3 v)
  52. {
  53.     const vec2  C = vec2(1.0/6.0, 1.0/3.0) ;
  54.     const vec4  D = vec4(0.0, 0.5, 1.0, 2.0);
  55.  
  56.     // First corner
  57.     vec3 i  = floor(v + dot(v, C.yyy) );
  58.     vec3 x0 =   v - i + dot(i, C.xxx) ;
  59.  
  60.     // Other corners
  61.     vec3 g = step(x0.yzx, x0.xyz);
  62.     vec3 l = 1.0 - g;
  63.     vec3 i1 = min( g.xyz, l.zxy );
  64.     vec3 i2 = max( g.xyz, l.zxy );
  65.  
  66.     //   x0 = x0 - 0.0 + 0.0 * C.xxx;
  67.     //   x1 = x0 - i1  + 1.0 * C.xxx;
  68.     //   x2 = x0 - i2  + 2.0 * C.xxx;
  69.     //   x3 = x0 - 1.0 + 3.0 * C.xxx;
  70.     vec3 x1 = x0 - i1 + C.xxx;
  71.     vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
  72.     vec3 x3 = x0 - D.yyy;      // -1.0+3.0*C.x = -0.5 = -D.y
  73.  
  74.     // Permutations
  75.     i = mod289(i);
  76.     vec4 p = permute( permute( permute(
  77.                                        i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
  78.                               + i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
  79.                      + i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
  80.  
  81.     // Gradients: 7x7 points over a square, mapped onto an octahedron.
  82.     // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
  83.     float n_ = 0.142857142857; // 1.0/7.0
  84.     vec3  ns = n_ * D.wyz - D.xzx;
  85.  
  86.     vec4 j = p - 49.0 * floor(p * ns.z * ns.z);  //  mod(p,7*7)
  87.  
  88.     vec4 x_ = floor(j * ns.z);
  89.     vec4 y_ = floor(j - 7.0 * x_ );    // mod(j,N)
  90.  
  91.     vec4 x = x_ *ns.x + ns.yyyy;
  92.     vec4 y = y_ *ns.x + ns.yyyy;
  93.     vec4 h = 1.0 - abs(x) - abs(y);
  94.  
  95.     vec4 b0 = vec4( x.xy, y.xy );
  96.     vec4 b1 = vec4( x.zw, y.zw );
  97.  
  98.     //vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0;
  99.     //vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0;
  100.     vec4 s0 = floor(b0)*2.0 + 1.0;
  101.     vec4 s1 = floor(b1)*2.0 + 1.0;
  102.     vec4 sh = -step(h, vec4(0.0));
  103.  
  104.     vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
  105.     vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
  106.  
  107.     vec3 p0 = vec3(a0.xy,h.x);
  108.     vec3 p1 = vec3(a0.zw,h.y);
  109.     vec3 p2 = vec3(a1.xy,h.z);
  110.     vec3 p3 = vec3(a1.zw,h.w);
  111.  
  112.     //Normalise gradients
  113.     vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
  114.     p0 *= norm.x;
  115.     p1 *= norm.y;
  116.     p2 *= norm.z;
  117.     p3 *= norm.w;
  118.  
  119.     // Mix final noise value
  120.     vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
  121.     m = m * m;
  122.     return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
  123.                                  dot(p2,x2), dot(p3,x3) ) );
  124. }
  125.  
  126. varying vec2 texcoord0;
  127. varying vec2 texdim0;
  128.  
  129. uniform float time;
  130. uniform int bSmooth;
  131. uniform vec2 map;
  132. uniform vec2 scale;
  133. uniform int octaves;
  134.  
  135. void main()
  136. {
  137.     float noise;
  138.     float octave = 1.0;
  139.     vec2  offset = vec2(1.0,0.0);
  140.  
  141.     vec2 uv = (texcoord0 / texdim0) + (offset + time);
  142.  
  143.     for(int i = 0; i < octaves; i++){
  144.         noise += (1./octave) * snoise(octave * vec3(uv.x * scale.x, uv.y * scale.y, time));
  145.         octave *= 2.0;
  146.     }
  147.  
  148.     if(noise < map.x){
  149.         noise = 0.0;
  150.     }else if(noise > map.y){
  151.         noise = 1.0;
  152.     }
  153.  
  154.     if(bSmooth == 1){
  155.         noise = smoothstep(map.x, map.y, noise);
  156.     }
  157.  
  158.     gl_FragColor = vec4(vec3(noise),1.);
  159. }
  160.  
  161. ]]>
  162.         </program>
  163.     </language>
  164. </jittershader>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement