Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // Shader to render floors/ceilings.
  3. //Currently always uses the same texture, will allow lookups in version 2
  4. varying vec2 v_vTexcoord;
  5. varying vec4 v_vColour;
  6.  
  7. uniform float u_floorProps[6*512]; //Big ole array
  8. uniform float u_height;
  9. uniform float u_width;
  10. uniform float u_playerX;
  11. uniform float u_playerY;
  12. uniform float u_distPlayer;
  13. uniform sampler2D s_floorTex;
  14.  
  15. void main()
  16. {      
  17.     //Okay, so we currently have two texture coords that are between 0 and 1.
  18.     //from that we need to know what the screen coords were, so we can look shit up
  19.    
  20.     float X = floor(v_vTexcoord.x * u_width);
  21.     float Y = floor(v_vTexcoord.y * u_height);
  22.    
  23.     int arrayStart = int(X * 6.0);
  24.     float floorScreenY = u_floorProps[arrayStart];
  25.     float distWall = u_floorProps[arrayStart + 3];
  26.     float floorXWall = u_floorProps[arrayStart + 1];
  27.     float floorYWall = u_floorProps[arrayStart + 2];
  28.     //Set alpha to 0 if this pixel is out of range, otherwise set to 1
  29.     if (Y < floorScreenY)
  30.     {
  31.        discard;
  32.     }
  33.  
  34.     //Determine the texture co-ords for this fragment
  35.     float currentDist = u_height / (2.0 * floorScreenY - u_height); //you could make a small lookup table for this instead
  36.     float weight = (currentDist - u_distPlayer) / (distWall - u_distPlayer);
  37.  
  38.     float currentFloorX = weight * floorXWall + (1.0 - weight) * u_playerX;
  39.     float currentFloorY = weight * floorYWall + (1.0 - weight) * u_playerY;
  40.     float floorTexWidth = u_floorProps[arrayStart + 4];
  41.     float floorTexHeight = u_floorProps[arrayStart + 5];  
  42.  
  43.     //okay, this bit is different since we just want UVs, not the exact texture coords
  44.     //that looks dumb as shit, but I think it's correct      
  45.     float floorTexX = mod(floor(currentFloorX * floorTexWidth),floorTexWidth);
  46.     float floorTexY = mod(floor(currentFloorY * floorTexHeight),floorTexHeight);
  47.     vec2 floorUV = vec2(floorTexX,floorTexY);
  48.     gl_FragColor = v_vColour * texture2D( s_floorTex, floorUV);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement