Altair200333

glsl code

Jan 15th, 2021
1,027
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 460
  2. out vec4 FragColor;
  3. in vec2 texcoord;
  4.  
  5. #define PI 3.1415926f
  6. uniform sampler2D background;
  7. uniform int textureWidth;
  8. uniform int textureHeight;
  9. uniform int backType;
  10.  
  11. uniform samplerBuffer VertexSampler0;
  12. uniform vec3 origin;
  13. uniform vec3 front;
  14. uniform vec3 right;
  15. uniform vec3 up;
  16. uniform float fov;
  17.  
  18. uniform float scale;
  19.  
  20. uniform float width;
  21. uniform float height;
  22.  
  23.  
  24. struct DirectionalLight
  25. {
  26.     float strength;
  27.     vec3 color;
  28.     float lightDist;
  29.     vec3 dir;
  30. };
  31. struct World
  32. {
  33.     vec3 skyColor;
  34.     vec3 floorColor;
  35.     vec3 horizonColor;
  36.     float horizonAngle;
  37. };
  38.  
  39. uniform DirectionalLight dirLight;
  40. uniform World world;
  41.  
  42. struct Octree
  43. {
  44.     vec3 color;
  45.     vec3 pos;
  46.     float size;
  47.     int children[8];
  48.     vec3 bounds[2];
  49.     bool isLeaf;
  50.     bool isEmpty;
  51.  
  52. };
  53. struct Ray
  54. {
  55.     vec3 orig, dir;
  56.     vec3 invdir;
  57.     int sign[3];
  58. };
  59.  
  60. Octree load(int pos)
  61. {
  62.     int id = pos/4;
  63.     vec4 v1 = texelFetch(VertexSampler0, id + 0);
  64.     vec4 v2 = texelFetch(VertexSampler0, id + 1);
  65.     vec4 v3 = texelFetch(VertexSampler0, id + 2);
  66.     vec4 v4 = texelFetch(VertexSampler0, id + 3);
  67.     vec4 v5 = texelFetch(VertexSampler0, id + 4);
  68.     vec4 v6 = texelFetch(VertexSampler0, id + 5);
  69.  
  70.     Octree oc;
  71.     oc.pos = v1.xyz;
  72.     oc.size = v1.w;
  73.     oc.color = vec3(v2.x, v2.y, v2.z);
  74.  
  75.     oc.bounds[0] = vec3(v2.w, v3.xy);
  76.     oc.bounds[1] = vec3(v3.zw, v4.x);
  77.  
  78.     oc.children[0] = int(v4.y);
  79.     oc.children[1] = int(v4.z);
  80.     oc.children[2] = int(v4.w);
  81.     oc.children[3] = int(v5.x);
  82.     oc.children[4] = int(v5.y);
  83.     oc.children[5] = int(v5.z);
  84.     oc.children[6] = int(v5.w);
  85.     oc.children[7] = int(v6.x);
  86.  
  87.     oc.isLeaf = v6.y > 0;
  88.     oc.isEmpty = v6.z > 0;
  89.  
  90.     return oc;
  91. }
  92.  
  93. struct Contact
  94. {
  95.     vec3 position;
  96.     vec3 normal;
  97. };
  98.  
  99. int iterativeHitNode(Ray r)
  100. {
  101.     int buf[1000];
  102.     int front = 0;
  103.     int rear = 0;
  104.     buf[front++] = 0;
  105.    
  106.     int closest = -1;
  107.  
  108.     while (front != rear)
  109.     {
  110.         if(front >= 1000)
  111.             return -1;
  112.         Octree node = load(buf[rear++]);
  113.         Octree closestNode = load(closest);
  114.         if(node.isLeaf && (closest == -1 || (sqDist(node.pos, r.orig) <= sqDist(closestNode.pos, r.orig))))
  115.         {
  116.             closest = buf[rear - 1];
  117.         }
  118.         if(node.isEmpty)
  119.             continue;
  120.         for (int i = 0; i < 8; ++i)
  121.         {
  122.             if(node.children[i] == -1)
  123.                 continue;
  124.             Octree child = load(node.children[i]);
  125.             if(node.children[i] != -1 && AABBtest(child, r))
  126.             {
  127.                 buf[front++] = node.children[i];
  128.             }
  129.            
  130.         }
  131.     }
  132.     return closest;
  133. }
  134.  
  135. vec3 getImageBackground(vec3 from, vec3 direction)
  136. {
  137.     float theta = atan(direction.y, direction.x) * 180 / PI + 180;
  138.     float alpha = atan(direction.z, sqrt(direction.x * direction.x + direction.y * direction.y)) * 180 / PI + 90;
  139.  
  140.     float x = textureWidth * theta / 360;
  141.     float y = textureHeight * alpha / 180;
  142.     vec2 pos = vec2( 1 - x/textureWidth, 1 - y/textureHeight);
  143.  
  144.     return texture(background, pos).xyz;
  145. }
  146. vec3 getBackground(vec3 from, vec3 direction)
  147. {
  148.     if(backType == 0)
  149.         return getSkyBackground(from, direction);
  150.     else if(backType == 1)
  151.     {
  152.         return getImageBackground(from, direction);
  153.     }
  154.  
  155.     return vec3(0);
  156. }
  157. vec3 multiplyColors(vec3 color1, vec3 otherColor)
  158. {
  159.     return vec3(otherColor.x * color1.x, otherColor.y * color1.y, otherColor.z * color1.z);
  160. }
  161.  
  162. void main()
  163. {    
  164.     float px = texcoord.x*width - width * 0.5f;
  165.     float py = texcoord.y*height - height * 0.5f;
  166.     Ray r = initRay(front + right * px * scale + up * py * scale, origin);
  167.  
  168.     int res = iterativeHitNode(r);
  169.  
  170.     if(res != -1)
  171.     {
  172.         Octree oc = load(res);
  173.        
  174.         Contact c = cubeHit(r.dir, r.orig, oc.pos, oc.size*0.5f);
  175.         float slope = abs(dot(dirLight.dir, c.normal));
  176.         vec3 color = multiplyColors(oc.color, dirLight.color) * slope * dirLight.strength;
  177.  
  178.         vec3 shadowPos = c.position + c.normal*0.001f;
  179.  
  180.         Ray shadowRay = initRay(-dirLight.dir, shadowPos);
  181.        
  182.         int shadowHit = iterativeHitNode(shadowRay);
  183.        
  184.         if(shadowHit != -1)
  185.             color = color*0.1f;
  186.        
  187.         FragColor = vec4(color, 1);
  188.     }
  189.     else
  190.     {
  191.         FragColor = vec4(getBackground(r.orig, r.dir), 1);
  192.     }
  193. }
RAW Paste Data