SHARE
TWEET

Untitled

a guest Aug 13th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. float sdf(vec3 p){
  2.     // Yksikköpallo origossa
  3.     return length(p)-1.0;
  4. }
  5.  
  6. vec3 get_normal(vec3 pos){
  7.     // Numeerinen pinnan approksimaatio normaalille
  8.     vec3 eps = vec3(0.01,0,0);
  9.     return normalize(vec3(
  10.         sdf(pos + eps.xyy) - sdf(pos - eps.xyy),
  11.         sdf(pos + eps.yxy) - sdf(pos - eps.yxy),
  12.         sdf(pos + eps.yyx) - sdf(pos - eps.yyx)
  13.     ));
  14. }
  15.  
  16. vec4 get_color(vec3 surface, vec3 camera){
  17.     // Lasketaan pikselin väri Phongin valaistusmallilla
  18.     vec3 normal = get_normal(surface);
  19.     vec3 l = vec3(-1,-1,-0.2); // Valon tulosuunta
  20.     vec3 l_prime = l + 2.0*dot(l,normal)*normal;
  21.     vec3 to_camera = camera - surface;
  22.     float cos_angle = max(dot(-l_prime, to_camera),0.0)/(length(l_prime) * length(to_camera));
  23.     vec3 specular = vec3(1.0,1.0,1.0) * 3.0 * pow(cos_angle,2.5);
  24.     vec3 diffuse = vec3(1.0,0.0,0.0) * 0.2 * max(dot(normal, -l),0.0);
  25.     vec3 ambient = vec3(1.0,0.0,0.0) * 0.3;
  26.     return vec4(clamp(diffuse + specular + ambient,0.0,1.0), 1.0);
  27. }
  28.  
  29. void mainImage(out vec4 outputColor, in vec2 fragCoord){
  30.    
  31.     // Skaalaa pikseleiden koordinaatit välille [-1,-1]
  32.     float aspect_ratio = 16.0/9.0;
  33.     vec2 pixel_pos = (fragCoord.xy / iResolution.xy - vec2(0.5,0.5))
  34.                       * vec2(2.0*aspect_ratio,2.0);    
  35.     vec3 v_0 = vec3(0.0, 0.0, 2.0); // Kameran sijainti
  36.  
  37.     // Luodaan säde kamerasta yhden yksikön päässä leijuvan
  38.     // kuvitteellisen ikkunan läpi annetun pikselin kohdalta
  39.     vec3 ray = normalize(vec3(pixel_pos.x, pixel_pos.y,-1));
  40.    
  41.     // Tehdään 50 iteraatiota sädeaskellusta etäisyyskentän avulla
  42.     vec3 pos = v_0;
  43.     for(int step = 0; step < 50; step++){
  44.           pos += ray * sdf(pos);
  45.     }
  46.    
  47.     if(sdf(pos) > 0.01) // Ei osumaa
  48.         outputColor = vec4(0,0,0,1);
  49.     else{ // Osuma
  50.         outputColor = get_color(pos, v_0);
  51.     }
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top