StealthGlobal

Curved World vert shader

Oct 30th, 2016
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 120
  2. #extension GL_EXT_texture_array : require
  3. #extension GL_EXT_texture_array : enable
  4. //curved world feature/effect; https://forum.blockland.us/index.php?topic=77967.msg7787623#msg7787623
  5. bool curveTheWorld = true;
  6. bool curveDirectionDown = true;
  7. //adjust the effect
  8. float curvePower = 2.0f; //exponential curve
  9. float curveScale = 0.02f; //'fisheye factor', note that high values make lights higher than their real position
  10.  
  11. // Varying.
  12. varying vec4 vPos;
  13. varying vec3 worldNormal;
  14. varying vec3 worldPos;
  15.  
  16. // Misc uniforms.
  17. uniform vec3 camPos;
  18. uniform mat4 obj2World;
  19. uniform mat4 world2Cam;
  20.  
  21. // Surface calculations, including specular power.
  22. varying vec2 texCoord;
  23. vec4 viewDelta;
  24.  
  25. // Fogging.
  26. uniform vec4 fogBaseColor;
  27. uniform vec4 fogConsts;
  28. uniform sampler2D fogTex;
  29. varying vec2 fogCoords;
  30.  
  31. void main()
  32. {
  33.     // Bricks always in world space.
  34.     worldPos = (obj2World * vec4(gl_Vertex.xyz, 1.0f)).xyz;
  35.     worldNormal = ((obj2World * vec4(gl_Normal.xyz, 0.0f)).xyz);
  36.  
  37.     if(length(gl_Normal.xyz) < 1.1f)
  38.         worldNormal.xyz = normalize(worldNormal.xyz);
  39.  
  40.     // Do some camera delta math.
  41.     viewDelta.xyz = worldPos - camPos;
  42.     viewDelta.w   = length(viewDelta.xyz);
  43.     viewDelta.xyz = -normalize(viewDelta.xyz);
  44.  
  45.     // Fog can be done per-vertex.
  46.     fogCoords.x = clamp((fogConsts.y - viewDelta.w) * fogConsts.x, 0.0f, 1.0f);
  47.     fogCoords.y = clamp((worldPos.z - fogConsts.z) * fogConsts.w, 0.0f, 1.0f);
  48.  
  49.     // Project the brick pos and normal.
  50.     vPos = world2Cam * vec4(worldPos, 1.0f);
  51.    
  52.     if (curveTheWorld)
  53.     {
  54.         if (curveDirectionDown)
  55.             worldPos.z -= pow(viewDelta.w * curveScale, curvePower);
  56.         else
  57.             worldPos.z += pow(viewDelta.w * curveScale, curvePower);
  58.         gl_Position = gl_ProjectionMatrix * world2Cam * vec4(worldPos, 1.0f);
  59.     }
  60.     else
  61.     {
  62.         gl_Position = gl_ProjectionMatrix * vPos;
  63.     }
  64.  
  65.     // Pass color through.
  66.     gl_FrontColor = gl_Color;
  67.    
  68.     // Pass texcoords through.
  69.     texCoord = gl_MultiTexCoord0.st;   
  70. }
Add Comment
Please, Sign In to add comment