Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1. #include "C:\Program Files\Autodesk\3ds Max 2021\OSL\vector2.h"
  2. #define vec2 vector2
  3. #define vec3 vector
  4. #include "C:\Program Files\Autodesk\3ds Max 2021\OSL\vector4.h"
  5. #define vec4 vector4
  6.  
  7. vec2 cmul
  8. (
  9.     vec2 a,
  10.     vec2 b
  11. )  
  12. {
  13.     return vec2( a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x );
  14. }
  15.  
  16. vec3 ACESFilm( vec3 x ) {
  17.     float a = 2.51;
  18.     float b = 0.03;
  19.     float c = 2.43;
  20.     float d = 0.59;
  21.     float e = 0.14;
  22.     return clamp((x*(a*x+b))/(x*(c*x+d)+e), 0.0, 1.0);
  23. }
  24.  
  25.  
  26.  
  27. vec2 csqr
  28. (
  29.     vec2 a
  30. )
  31. {
  32.     return vec2( a.x*a.x - a.y*a.y, 2.*a.x*a.y  );
  33. }
  34.  
  35.  
  36. matrix rot
  37. (
  38.     float a
  39. )
  40. {
  41.     return matrix(cos(a),sin(a),0,0,-sin(a),cos(a),0,0,0,0,0,0,0,0,0,0);   
  42. }
  43.  
  44.  
  45. vec2 iSphere
  46. (
  47.     vec3 ro,
  48.     vec3 rd,
  49.     vec4 sph
  50. )
  51. {
  52.     vec3 oc = ro - vec3(sph.x,sph.y,sph.z);
  53.     float b = dot( oc, rd );
  54.     float c = dot( oc, oc ) - sph.w*sph.w;
  55.     float h = b*b - c;
  56.     if( h<0.0 ) return vec2(-1.0,-1.0);
  57.     h = sqrt(h);
  58.    
  59.     return vec2(-b-h, -b+h );
  60. }
  61.  
  62. float map( output vec3 p, float Time, int Animate, int Iterations) {
  63.    
  64.     float res = 0.;
  65.     vec3 c = p;
  66.     for (int i = 0; i < Iterations; ++i) {
  67.        
  68.         if(Animate)
  69.         {
  70.         p =.7*abs(p+cos(Time*0.15+1.6)*0.15)/dot(p,p) -.7+cos(Time*0.15)*0.15;
  71.        
  72.         }
  73.         else
  74.         {
  75.        p =.7*abs(p)/dot(p,p) -.7;
  76.      }  
  77.         vec2 pyz= csqr(vec2(p[1],p[2]));
  78.         p= vec3(p[0],pyz.x,pyz.y);
  79.         p = vec3(p[2],p[0],p[1]);
  80.         res += exp(-19. * abs(dot(p,c)));    
  81.     }
  82.     return res/2.;
  83. }
  84.  
  85.  
  86. vec3 raymarch
  87. (
  88.     vec3 ro,
  89.     vec3 rd,
  90.     vec2 tminmax,
  91.     float Time,
  92.     int Animate,
  93.     int Iterations
  94.    
  95. )
  96. {
  97.     float t = tminmax.x;
  98.     float dt = .02;
  99.     //float dt = .2 - .195*cos(iTime*.05);//animated
  100.     vec3 col= vec3(0.);
  101.     float c = 0.;
  102.     for( int i=0; i<64; i++ )
  103.     {
  104.         t+=dt*exp(-2.*c);
  105.         if(t>tminmax.y)break;
  106.         vec3 pos = ro+t*rd;
  107.        
  108.         c = map(ro+t*rd,Time,Animate,Iterations);              
  109.        
  110.         col = .99*col+ .08*vec3(c*c, c, c*c*c);//green 
  111.         //col = .99*col+ .08*vec3(c*c*c, c*c, c);//blue
  112.     }    
  113.     return col;
  114. }
  115.  
  116. shader FractalMarble
  117. (
  118.     point Po = P,
  119.     float Time = 0,
  120.     float zoom = 1,
  121.     float Offsetx = 0,
  122.     float Offsety = 0,
  123.     int Iterations = 10,
  124.    
  125.                   int    Animate   = 1  
  126.     [[ string widget="checkBox" ]],
  127.    
  128.    
  129.    
  130.           int    ACESFilmMode   = 1  
  131.     [[ string widget="checkBox" ]],
  132.  
  133.     output color Out = 0,
  134. )
  135. {
  136.     vec2 q = vec2(Po[0],Po[1]);
  137.     vec2 p = -1.0 + 2.0 * q;
  138.     vec2 m = vec2(Offsetx,Offsety);
  139.     m-=.5;
  140.  
  141.     // camera
  142.     vec3 ro = zoom*vec3(4.);
  143.     vec3 royz = vec3(ro[1],ro[2],0);
  144.     royz =transform( rot(m.y), royz );
  145.     ro = vec3(ro[0],royz[0],royz[1]);
  146.     vec3 roxz = vec3(ro[0],ro[2],0);
  147.     roxz =transform( rot(m.x+ 0.1*Time), roxz );
  148.     ro = vec3(ro[0],roxz[0],roxz[1]);
  149.     vec3 ta = vec3( 0.0 , 0.0, 0.0 );
  150.     vec3 ww = normalize( ta - ro );
  151.     vec3 uu = normalize( cross(ww,vec3(0.0,1.0,0.0) ) );
  152.     vec3 vv = normalize( cross(uu,ww));
  153.     vec3 rd = normalize( p.x*uu + p.y*vv + 4.0*ww );
  154.     vec2 tmm = iSphere( ro, rd, vec4(0.,0.,0.,2.) );
  155.  
  156.     // raymarch
  157.     vec3 col = raymarch(ro,rd,tmm,Time,Animate,Iterations);
  158.   //  if (tmm.x<0.)col = texture(iChannel0, rd).rgb;
  159.    
  160.     vec3 nor=(ro+tmm.x*rd)/2.;
  161.     nor = reflect(rd, nor);        
  162.     float fre = pow(.5+ clamp(dot(nor,rd),0.0,1.0), 3. )*1.3;
  163.      
  164.     // shade
  165.    
  166.     col =  .5 *(log(1.+col));
  167.     col = clamp(col,0.,1.);
  168.     if(ACESFilmMode)
  169.     {col = ACESFilm(col);
  170.    }
  171.    
  172.     Out = vec3( col);
  173.     Out = pow(Out,2.2);
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement