Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.25 KB | None | 0 0
  1. vec3 light1Position = vec3(0.0, sin(TIME)*5.0, -5.0*sin(TIME)*1.0);
  2. vec3 light2Position = vec3(0.0, -1.0, 1.0);
  3. float objIndex = 0.0;
  4.  
  5. // float distanceToSurface(vec3 ourPos){
  6. //  vec3 sphereOrigin1 = vec3(2.0*sin(TIME), 0.0, 5.0);
  7. //  float dist1 = sdSphere(ourPos, sphereOrigin1, 1.5);
  8. //  vec3 sphereOrigin2 = vec3(-2.0, 0.0, 5.0);
  9. //  float dist2 = sdSphere(ourPos, sphereOrigin2, 1.5);
  10. //  // float lightSurf = sdSphere(ourPos, vec3(light1Position.xy, -light1Position.z), 0.1);
  11. //  float mainSurf = min(dist1, dist2);
  12. //  // if (lightSurf < mainSurf){
  13. //  //  objIndex = 1.0;
  14. //  // } else {
  15. //  //  objIndex = 0.0;
  16. //  // }
  17. //  return mainSurf;
  18. // }
  19.  
  20. vec3 rotateX(vec3 p, float angle)
  21. {
  22.     float c = cos(angle);
  23.     float s = sin(angle);
  24.     return vec3(p.x, c*p.y+s*p.z, -s*p.y+c*p.z);
  25. }
  26.  
  27. vec3 rotateY(vec3 p, float angle)
  28. {
  29.     float c = cos(angle);
  30.     float s = sin(angle);
  31.     return vec3(c*p.x-s*p.z, p.y, s*p.x+c*p.z);
  32. }
  33.  
  34. vec3 rotateZ(vec3 p, float angle)
  35. {
  36.     float c = cos(angle);
  37.     float s = sin(angle);
  38.     return vec3(c*p.x+s*p.y, -s*p.x+c*p.y, p.z);
  39. }
  40. float sdSphere(vec3 p, vec3 offset, float s )
  41. {
  42.   return length(p-offset)-s;
  43. }
  44.  
  45. float sdPlane(vec3 p, vec4 n)
  46. {
  47.   // n must be normalized
  48.   return dot(p,n.xyz) + n.w;
  49. }
  50.  
  51. float sdHexPrism(vec3 p, vec2 h)
  52. {
  53.   const vec3 k = vec3(-0.8660254, 0.5, 0.57735);
  54.   p = abs(p);
  55.   p.xy -= 2.0*min(dot(k.xy, p.xy), 0.0)*k.xy;
  56.   vec2 d = vec2(
  57.        length(p.xy-vec2(clamp(p.x,-k.z*h.x,k.z*h.x), h.x))*sign(p.y-h.x),
  58.        p.z-h.y );
  59.   return min(max(d.x,d.y),0.0) + length(max(d,0.0));
  60. }
  61.  
  62. float distanceToSurface(vec3 ourPos){
  63.     vec3 sphereOrigin1 = vec3(0.5, 0.5, 0.5);
  64.     ourPos = vec3(mod(ourPos.x, 1.0), mod(ourPos.y, 5.0), mod(ourPos.z, 1.0));
  65.     float dist1 = sdSphere(ourPos, sphereOrigin1, 0.25);
  66.     return dist1;
  67. }
  68.  
  69. vec3 normal(vec3 testPos){
  70.     const float eps = 0.001;
  71.     vec3 p = testPos;
  72.     return normalize(vec3(
  73.         distanceToSurface(vec3(p.x + eps, p.y, p.z)) - distanceToSurface(vec3(p.x - eps, p.y, p.z)),
  74.         distanceToSurface(vec3(p.x, p.y + eps, p.z)) - distanceToSurface(vec3(p.x, p.y - eps, p.z)),
  75.         distanceToSurface(vec3(p.x, p.y, p.z + eps)) - distanceToSurface(vec3(p.x, p.y, p.z - eps))
  76.     ));
  77. }
  78.  
  79. // Tri-Planar blending function. Based on an old Nvidia tutorial.
  80. vec3 tex3D(sampler2D tex, in vec3 p, in vec3 n, float texZoom){
  81.   n = max((abs(n) - 0.2)*7., 0.001); // max(abs(n), 0.001), etc.
  82.   n /= (n.x + n.y + n.z);
  83.     return (
  84.         texture(tex, p.yz*texZoom)*n.x +
  85.         texture(tex, p.zx*texZoom)*n.y +
  86.         texture(tex, p.xy*texZoom)*n.z).xyz;
  87. }
  88.  
  89. //SET UP THE CAMERA
  90. //DO THE MARCHING (let the scene understand our 3D surface)
  91. //COLOR THE SCENE
  92.  
  93. #define NUMSTEPS 256
  94.  
  95. vec4 renderMain(void) {
  96.     vec3 rayDirection = normalize(vec3(_uvc, FOV));
  97.     vec3 rayOrigin = vec3(0.0);
  98.  
  99.     vec3 currentPosition = rayOrigin;
  100.     float dist = 0.0;
  101.     float hit = 0.0;
  102.     float stepsNeededToReachSurface = 0.0;
  103.     vec3 hitPosition = vec3(0.0);
  104.     for (int i = 0; i<NUMSTEPS; i++){
  105.         dist = distanceToSurface(currentPosition);
  106.         currentPosition += rayDirection*dist;
  107.         if (dist < 0.0001){
  108.             hit = 1.0;
  109.             stepsNeededToReachSurface = i;
  110.             hitPosition = currentPosition;
  111.             break;
  112.         }
  113.     }
  114.  
  115.     vec3 bkgCol = vec3(1.0,0.75,0.0)*0.4*(1.5-_uv.y);
  116.     vec3 surfaceNormal = normal(hitPosition);
  117.  
  118.     vec3 light1Direction = normalize(light1Position - rayDirection);
  119.     float light1Diff = max(dot(surfaceNormal, light1Direction), 0.0);
  120.     vec3 light1Color = vec3(1.0,0.0,0.4);
  121.  
  122.     vec3 light2Direction = normalize(light2Position - rayDirection);
  123.     float light2Diff = max(dot(surfaceNormal, light2Direction), 0.0);
  124.     vec3 light2Color = vec3(0.0,0.4,1.0);
  125.  
  126.     float spec1 = pow(max(dot(reflect(-light1Direction, surfaceNormal), -rayDirection), 0.0), 8.);
  127.     float spec2 = pow(max(dot(reflect(-light2Direction, surfaceNormal), -rayDirection), 0.0), 8.);
  128.  
  129.     vec3 finalCol = vec3(0.0);
  130.     finalCol += bkgCol*(1.0-hit);
  131.     if (hit == 1.0){
  132.             if (objIndex == 0.0){
  133.             //We're dealing with our two large spheres
  134.             finalCol += (light1Diff+spec1)*light1Color*hit*0.75;
  135.             finalCol += (light2Diff+spec2)*light2Color*hit*0.75;
  136.             // finalCol += tex3D(syn_UserImage, hitPosition+1.5, surfaceNormal, 0.25);
  137.         } else if (objIndex == 1.0){
  138.             finalCol += vec3(0.0,0.6,1.0)*0.5;
  139.         }
  140.     }
  141.  
  142.     return vec4(finalCol, 1.0);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement