daily pastebin goal
88%
SHARE
TWEET

Demo 87 - ClassicNoise3D.cginc

tonynogo Jul 6th, 2017 1,104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // Noise Shader Library for Unity - https://github.com/keijiro/NoiseShader
  3. //
  4. // Original work (webgl-noise) Copyright (C) 2011 Stefan Gustavson
  5. // Translation and modification was made by Keijiro Takahashi.
  6. //
  7. // This shader is based on the webgl-noise GLSL shader. For further details
  8. // of the original shader, please see the following description from the
  9. // original source code.
  10. //
  11.  
  12. //
  13. // GLSL textureless classic 3D noise "cnoise",
  14. // with an RSL-style periodic variant "pnoise".
  15. // Author:  Stefan Gustavson (stefan.gustavson@liu.se)
  16. // Version: 2011-10-11
  17. //
  18. // Many thanks to Ian McEwan of Ashima Arts for the
  19. // ideas for permutation and gradient selection.
  20. //
  21. // Copyright (c) 2011 Stefan Gustavson. All rights reserved.
  22. // Distributed under the MIT license. See LICENSE file.
  23. // https://github.com/ashima/webgl-noise
  24. //
  25.  
  26. float3 mod(float3 x, float3 y)
  27. {
  28.   return x - y * floor(x / y);
  29. }
  30.  
  31. float3 mod289(float3 x)
  32. {
  33.   return x - floor(x / 289.0) * 289.0;
  34. }
  35.  
  36. float4 mod289(float4 x)
  37. {
  38.   return x - floor(x / 289.0) * 289.0;
  39. }
  40.  
  41. float4 permute(float4 x)
  42. {
  43.   return mod289(((x*34.0)+1.0)*x);
  44. }
  45.  
  46. float4 taylorInvSqrt(float4 r)
  47. {
  48.   return (float4)1.79284291400159 - r * 0.85373472095314;
  49. }
  50.  
  51. float3 fade(float3 t) {
  52.   return t*t*t*(t*(t*6.0-15.0)+10.0);
  53. }
  54.  
  55. // Classic Perlin noise
  56. float cnoise(float3 P)
  57. {
  58.   float3 Pi0 = floor(P); // Integer part for indexing
  59.   float3 Pi1 = Pi0 + (float3)1.0; // Integer part + 1
  60.   Pi0 = mod289(Pi0);
  61.   Pi1 = mod289(Pi1);
  62.   float3 Pf0 = frac(P); // Fractional part for interpolation
  63.   float3 Pf1 = Pf0 - (float3)1.0; // Fractional part - 1.0
  64.   float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  65.   float4 iy = float4(Pi0.y, Pi0.y, Pi1.y, Pi1.y);
  66.   float4 iz0 = (float4)Pi0.z;
  67.   float4 iz1 = (float4)Pi1.z;
  68.  
  69.   float4 ixy = permute(permute(ix) + iy);
  70.   float4 ixy0 = permute(ixy + iz0);
  71.   float4 ixy1 = permute(ixy + iz1);
  72.  
  73.   float4 gx0 = ixy0 / 7.0;
  74.   float4 gy0 = frac(floor(gx0) / 7.0) - 0.5;
  75.   gx0 = frac(gx0);
  76.   float4 gz0 = (float4)0.5 - abs(gx0) - abs(gy0);
  77.   float4 sz0 = step(gz0, (float4)0.0);
  78.   gx0 -= sz0 * (step((float4)0.0, gx0) - 0.5);
  79.   gy0 -= sz0 * (step((float4)0.0, gy0) - 0.5);
  80.  
  81.   float4 gx1 = ixy1 / 7.0;
  82.   float4 gy1 = frac(floor(gx1) / 7.0) - 0.5;
  83.   gx1 = frac(gx1);
  84.   float4 gz1 = (float4)0.5 - abs(gx1) - abs(gy1);
  85.   float4 sz1 = step(gz1, (float4)0.0);
  86.   gx1 -= sz1 * (step((float4)0.0, gx1) - 0.5);
  87.   gy1 -= sz1 * (step((float4)0.0, gy1) - 0.5);
  88.  
  89.   float3 g000 = float3(gx0.x,gy0.x,gz0.x);
  90.   float3 g100 = float3(gx0.y,gy0.y,gz0.y);
  91.   float3 g010 = float3(gx0.z,gy0.z,gz0.z);
  92.   float3 g110 = float3(gx0.w,gy0.w,gz0.w);
  93.   float3 g001 = float3(gx1.x,gy1.x,gz1.x);
  94.   float3 g101 = float3(gx1.y,gy1.y,gz1.y);
  95.   float3 g011 = float3(gx1.z,gy1.z,gz1.z);
  96.   float3 g111 = float3(gx1.w,gy1.w,gz1.w);
  97.  
  98.   float4 norm0 = taylorInvSqrt(float4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
  99.   g000 *= norm0.x;
  100.   g010 *= norm0.y;
  101.   g100 *= norm0.z;
  102.   g110 *= norm0.w;
  103.  
  104.   float4 norm1 = taylorInvSqrt(float4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
  105.   g001 *= norm1.x;
  106.   g011 *= norm1.y;
  107.   g101 *= norm1.z;
  108.   g111 *= norm1.w;
  109.  
  110.   float n000 = dot(g000, Pf0);
  111.   float n100 = dot(g100, float3(Pf1.x, Pf0.y, Pf0.z));
  112.   float n010 = dot(g010, float3(Pf0.x, Pf1.y, Pf0.z));
  113.   float n110 = dot(g110, float3(Pf1.x, Pf1.y, Pf0.z));
  114.   float n001 = dot(g001, float3(Pf0.x, Pf0.y, Pf1.z));
  115.   float n101 = dot(g101, float3(Pf1.x, Pf0.y, Pf1.z));
  116.   float n011 = dot(g011, float3(Pf0.x, Pf1.y, Pf1.z));
  117.   float n111 = dot(g111, Pf1);
  118.  
  119.   float3 fade_xyz = fade(Pf0);
  120.   float4 n_z = lerp(float4(n000, n100, n010, n110), float4(n001, n101, n011, n111), fade_xyz.z);
  121.   float2 n_yz = lerp(n_z.xy, n_z.zw, fade_xyz.y);
  122.   float n_xyz = lerp(n_yz.x, n_yz.y, fade_xyz.x);
  123.   return 2.2 * n_xyz;
  124. }
  125.  
  126. // Classic Perlin noise, periodic variant
  127. float pnoise(float3 P, float3 rep)
  128. {
  129.   float3 Pi0 = mod(floor(P), rep); // Integer part, modulo period
  130.   float3 Pi1 = mod(Pi0 + (float3)1.0, rep); // Integer part + 1, mod period
  131.   Pi0 = mod289(Pi0);
  132.   Pi1 = mod289(Pi1);
  133.   float3 Pf0 = frac(P); // Fractional part for interpolation
  134.   float3 Pf1 = Pf0 - (float3)1.0; // Fractional part - 1.0
  135.   float4 ix = float4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
  136.   float4 iy = float4(Pi0.y, Pi0.y, Pi1.y, Pi1.y);
  137.   float4 iz0 = (float4)Pi0.z;
  138.   float4 iz1 = (float4)Pi1.z;
  139.  
  140.   float4 ixy = permute(permute(ix) + iy);
  141.   float4 ixy0 = permute(ixy + iz0);
  142.   float4 ixy1 = permute(ixy + iz1);
  143.  
  144.   float4 gx0 = ixy0 / 7.0;
  145.   float4 gy0 = frac(floor(gx0) / 7.0) - 0.5;
  146.   gx0 = frac(gx0);
  147.   float4 gz0 = (float4)0.5 - abs(gx0) - abs(gy0);
  148.   float4 sz0 = step(gz0, (float4)0.0);
  149.   gx0 -= sz0 * (step((float4)0.0, gx0) - 0.5);
  150.   gy0 -= sz0 * (step((float4)0.0, gy0) - 0.5);
  151.  
  152.   float4 gx1 = ixy1 / 7.0;
  153.   float4 gy1 = frac(floor(gx1) / 7.0) - 0.5;
  154.   gx1 = frac(gx1);
  155.   float4 gz1 = (float4)0.5 - abs(gx1) - abs(gy1);
  156.   float4 sz1 = step(gz1, (float4)0.0);
  157.   gx1 -= sz1 * (step((float4)0.0, gx1) - 0.5);
  158.   gy1 -= sz1 * (step((float4)0.0, gy1) - 0.5);
  159.  
  160.   float3 g000 = float3(gx0.x,gy0.x,gz0.x);
  161.   float3 g100 = float3(gx0.y,gy0.y,gz0.y);
  162.   float3 g010 = float3(gx0.z,gy0.z,gz0.z);
  163.   float3 g110 = float3(gx0.w,gy0.w,gz0.w);
  164.   float3 g001 = float3(gx1.x,gy1.x,gz1.x);
  165.   float3 g101 = float3(gx1.y,gy1.y,gz1.y);
  166.   float3 g011 = float3(gx1.z,gy1.z,gz1.z);
  167.   float3 g111 = float3(gx1.w,gy1.w,gz1.w);
  168.  
  169.   float4 norm0 = taylorInvSqrt(float4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
  170.   g000 *= norm0.x;
  171.   g010 *= norm0.y;
  172.   g100 *= norm0.z;
  173.   g110 *= norm0.w;
  174.   float4 norm1 = taylorInvSqrt(float4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
  175.   g001 *= norm1.x;
  176.   g011 *= norm1.y;
  177.   g101 *= norm1.z;
  178.   g111 *= norm1.w;
  179.  
  180.   float n000 = dot(g000, Pf0);
  181.   float n100 = dot(g100, float3(Pf1.x, Pf0.y, Pf0.z));
  182.   float n010 = dot(g010, float3(Pf0.x, Pf1.y, Pf0.z));
  183.   float n110 = dot(g110, float3(Pf1.x, Pf1.y, Pf0.z));
  184.   float n001 = dot(g001, float3(Pf0.x, Pf0.y, Pf1.z));
  185.   float n101 = dot(g101, float3(Pf1.x, Pf0.y, Pf1.z));
  186.   float n011 = dot(g011, float3(Pf0.x, Pf1.y, Pf1.z));
  187.   float n111 = dot(g111, Pf1);
  188.  
  189.   float3 fade_xyz = fade(Pf0);
  190.   float4 n_z = lerp(float4(n000, n100, n010, n110), float4(n001, n101, n011, n111), fade_xyz.z);
  191.   float2 n_yz = lerp(n_z.xy, n_z.zw, fade_xyz.y);
  192.   float n_xyz = lerp(n_yz.x, n_yz.y, fade_xyz.x);
  193.   return 2.2 * n_xyz;
  194. }
RAW Paste Data
Top