Advertisement
DEKTEN

SDF_004

Nov 11th, 2020 (edited)
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** ******************************************************** ***
  2.  
  3.  
  4.  
  5.     SOURCE_____CODE : tinyurl.com/SDF-004
  6.  
  7.  
  8.  
  9.     FILE_______NAME : SDF_004._
  10.     PASTEBIN__TITLE : SDF_004  
  11.     PASTEBIN____URL : https://pastebin.com/ybuKVRMG
  12.     TWITCH______URL : www.twitch.com/kanjicoder
  13.     TWITTER_____URL : twitter.com/MakeGamesHappen
  14.     DESMOS____GRAPH : www.desmos.com/calculator/h7rkp6mhpx
  15.  
  16. *** ******************************************************** **/
  17.  
  18. #define MAX_STP  100  /** MAX_STEPS **/
  19. #define MIN_LEN  0.02 /** SURF_DIST **/
  20. #define MAX_LEN  100.0
  21.  
  22. float sdf_SmoothMinAttempt( float a , float b ){
  23.  
  24.     float u = min(a,b)                              ;
  25.     float m = (max(0.0,min(2.0,((a-b)+1.0)))) / 2.0 ;
  26.     float n = m * (1.0-m)                           ;
  27.     float w = (1.0-(n*4.0))                         ;
  28.     float q = (a*(1.0-m))+(b*(0.0+m))               ;
  29.     float z = q - ((q-u)*w )                        ;
  30.  
  31.     return( z );
  32.  
  33. }
  34.  
  35. float sdf_Box( vec3 pop, vec3 pos, vec3 dim ){
  36.  
  37.     float x_dist = length( pop.x - pos.x ) - (dim.x/2.0) ;
  38.     float y_dist = length( pop.y - pos.y ) - (dim.y/2.0) ;
  39.     float z_dist = length( pop.z - pos.z ) - (dim.z/2.0) ;
  40.  
  41.     return( max(max( x_dist , y_dist ), z_dist ) );
  42. }
  43.  
  44. /** AKA: sdf_Scene **/
  45. float sdf_DistanceToNearestObject( vec3 pop ){
  46.  
  47.     vec4  obj_001 = vec4( 0+1,0,0,   1   ); //:w == radius
  48.     vec4  obj_002 = vec4( 0-1,0,0,   1   ); //:w == radius
  49.    
  50.  
  51.     float t = ( iTime / 10.0 ); //:iTime:Slow_Motion
  52.     obj_001.x = sin( t      )*2.0;
  53.     obj_002.x = sin( t * 2.0)*2.0;
  54.  
  55.     float dis_001 = length( obj_001.xyz - pop ) - obj_001.w;
  56.     float dis_002 = length( obj_002.xyz - pop ) - obj_002.w;
  57.     float dis_003 = sdf_Box( pop, vec3(0-3,0+1,0), vec3(1) );
  58.  
  59.     return(
  60.         sdf_SmoothMinAttempt(
  61.         sdf_SmoothMinAttempt( dis_001 , dis_002 )
  62.        ,                                dis_003
  63.         )
  64.     );;
  65.    
  66. }
  67.  
  68. vec3 sdf_GetLight( vec3 pop /** AKA: surface XYZ **/ ){
  69.  
  70.     //:  l_p : light_position
  71.     //:  l_v : light_vector
  72.     //:  l_n : light_normal
  73.  
  74.     //:LIGHT_NORMAL:-----------------------------------------://
  75.     vec3 l_p = vec3( 0,0,9 ) +vec3(
  76.         cos( iTime ) * 6.0
  77.     ,   sin( iTime ) * 6.0
  78.     ,    0
  79.     );;
  80.     vec3 l_v = ( l_p - pop );
  81.     vec3 l_n = normalize( l_v );
  82.     //:-----------------------------------------:LIGHT_NORMAL://
  83.     //:SURFACE_NORMAL:---------------------------------------://
  84.    
  85.         #define S sdf_DistanceToNearestObject
  86.         #define E 0.0001
  87.         #define X pop.x
  88.         #define Y pop.y
  89.         #define Z pop.z
  90.         vec3 s_n =normalize(vec3(
  91.            S(vec3(X+E,Y  ,Z  )) -  S(vec3(X-E,Y  ,Z  ))  //:X
  92.         ,  S(vec3(X  ,Y+E,Z  )) -  S(vec3(X  ,Y-E,Z  ))  //:Y
  93.         ,  S(vec3(X  ,Y  ,Z+E)) -  S(vec3(X  ,Y  ,Z-E))  //:Z
  94.         ));;
  95.         #undef  S
  96.         #undef  E
  97.         #undef  X
  98.         #undef  Y
  99.         #undef  Z
  100.     //:---------------------------------------:SURFACE_NORMAL://
  101.     //:LIGHT_INTENSITY:--------------------------------------://
  102.  
  103.         float   l_i ; //:l_i:light_intensity
  104.         vec3    l_t ; //:l_t:light_tuple
  105.         float   g_i ; //:g_i:global_illumination
  106.  
  107.                 g_i = 0.01;
  108.                 l_i = dot( l_n , s_n );
  109.  
  110.                 //:IF statement currently un-necessary.
  111.                 if( l_i <= 0.0 ){
  112.                     l_t = vec3( 0.0+g_i , 0.0+g_i ,0.0+g_i );
  113.                 }else{                                  
  114.                     l_t = vec3( l_i+g_i , l_i+g_i ,l_i+g_i );
  115.                 };;
  116.  
  117.     //:--------------------------------------:LIGHT_INTENSITY://
  118.  
  119.     return( l_t ); //:Light_Triplet
  120.  
  121. }
  122.  
  123. vec3 sdf_Render( vec3 cam_org , vec3 ray_dir ){
  124.  
  125.     //:RAY_MARCH:--------------------------------------------://
  126.  
  127.         float     d_p = 0.0;     //:d_p:Distance_From:pop
  128.         float     dfc = 0.0;     //:dfc:Distance_From_Camera
  129.         vec3      pop = cam_org; //:pop:Point_On_Path
  130.  
  131.         for( int i = 0; i <= MAX_STP; i++ ){
  132.             pop = cam_org + (ray_dir*dfc);
  133.             d_p = sdf_DistanceToNearestObject( pop );
  134.             if( d_p <= MIN_LEN || dfc >= MAX_LEN ){ break; }
  135.             dfc += d_p;
  136.         };;
  137.  
  138.     //:--------------------------------------------:RAY_MARCH://
  139.  
  140.     //: hit == 1.0 for intersecting scene geom
  141.     //: hit == 0.0 for missing      scene geom
  142.     float d_p_bounded = max(0.0,min(1.0,d_p));
  143.     float hit = ( 1.0  -  d_p_bounded  );;
  144.     vec3  l_t = sdf_GetLight( pop ); //:l_t:light_triplet
  145.  
  146.     return( float(hit) * l_t.xyz );
  147.     //:return( vec3(1.0) );
  148. }
  149.  
  150. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  151. {
  152.  
  153.     vec2 uv = (fragCoord - .5*iResolution.xy)/iResolution.yy;
  154.  
  155.     //: float len = length( uv );
  156.     //: fragColor = vec4(  len , 0.0 , 0.0 , 1.0 );
  157.  
  158.     //:   cam_org : Origin of camera looking down on world.
  159.     //:   glass_d : Glass's distance in front of camera
  160.     //:   glass_c : Glass's phosphore cell world position.  
  161.     //:   ray_dir : Camera To The Phosphoric Glass
  162.     vec3  cam_org = vec3( 0,0,9 );
  163.     float glass_d = 3.0;
  164.     vec3  glass_c =vec3( uv.x, uv.y, cam_org.z - glass_d );
  165.     vec3  ray_dir = normalize( glass_c - cam_org );
  166.  
  167.     fragColor = vec4( sdf_Render( cam_org , ray_dir ) , 1.0 );
  168.  
  169.  
  170. }
  171.  
  172.  
  173. //: HOW IS THIS VALID SYNTAX?
  174. //: vec3  glass_c =( uv.x, uv.y, cam_org - glass_d );
  175.  
  176.  
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement