Advertisement
Gary_Keen27

Oren-Nayar Shading

Oct 25th, 2016
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /////////////////////////////////////////////////////
  2. ////////////////////VERTEX SHADER////////////////////
  3. /////////////////////////////////////////////////////
  4. #version 330
  5.  
  6. layout(location = 0) in vec3 inPosition;
  7. layout(location = 1) in vec3 inNormal;
  8. layout(location = 2) in vec2 inTexCoord;
  9. layout(location = 3) in vec3 inTangent;
  10. layout(location = 4) in vec3 inBitangent;
  11.  
  12. out vec2 texCoord;
  13. out vec3 camDir;
  14. out vec3 lightDir;
  15. out float attenuation;
  16.  
  17. uniform mat4 viewMatrix;
  18. uniform mat4 modelMatrix;
  19. uniform mat4 projectionMatrix;
  20. uniform mat3 normalMatrix;
  21. uniform vec3 camPosIn;
  22. uniform vec3 lightPosIn;
  23.  
  24. void main()
  25. {
  26.     vec3 N = normalize(normalMatrix * inNormal);
  27.     vec3 T = normalize(normalMatrix * inTangent);
  28.          T = normalize(T - (dot(N, T) * N));
  29.     vec3 B = normalize(normalMatrix * inBitangent);
  30.  
  31.     if (dot(cross(N, T), B) < 0.0)
  32.         T = T * -1.0;
  33.  
  34.     mat3 TBNMatrix = transpose(mat3(T, B, N));
  35.  
  36.     vec3 position = vec3((viewMatrix * modelMatrix) * vec4(inPosition, 1.0)); //Vector is a position so W = 1.0. If it were a direction w = 0.0.
  37.     camDir = TBNMatrix * (normalize(camPosIn - position));
  38.     lightDir = TBNMatrix * (normalize(lightPosIn - position));
  39.     texCoord = inTexCoord;
  40.  
  41.     float radius = 50.0;
  42.     float dist = length(vec3((viewMatrix * modelMatrix) * vec4(lightPosIn, 1.0)) - position);
  43.     attenuation = 1.0 / (1.0 + ((2.0 / radius) * dist) + ((1.0 / (radius * radius)) * (dist * dist)));
  44.  
  45.     gl_Position = (projectionMatrix * (viewMatrix * modelMatrix)) * vec4(inPosition, 1.0);
  46. }
  47.  
  48. ///////////////////////////////////////////////////////
  49. ////////////////////FRAGMENT SHADER////////////////////
  50. ///////////////////////////////////////////////////////
  51. #version 330
  52. precision mediump float;
  53.  
  54. in vec2 texCoord;
  55. in vec3 camDir;
  56. in vec3 lightDir;
  57. in float attenuation;
  58.  
  59. layout (location = 0) out vec4 FragColour;
  60.  
  61. uniform sampler2D textureMap;
  62. uniform sampler2D normalMap;
  63. uniform sampler2D AOMap;
  64. uniform sampler2D specMap;
  65. uniform sampler2D roughMap;
  66. uniform vec3 lightColour;
  67.  
  68. void main()
  69. {
  70.     const float PI = 3.14159;
  71.  
  72.     vec3 albedo = texture(textureMap, texCoord).rgb;
  73.     vec3 normals = normalize(texture(normalMap, texCoord).rgb * 2.0 - 1.0);
  74.     float roughness = texture(roughMap, texCoord).r;
  75.     float AO = texture(AOMap, texCoord).r; 
  76.    
  77.     float NdotV = clamp(dot(normals, camDir), 0.0, 1.0);
  78.     float angleVN = acos(NdotV);
  79.    
  80.     vec3 OrenNayar = vec3(0.0);
  81.     vec3 ambientColour = vec3(0.0);
  82.  
  83.     for (int i = 0; i < 1; i++)
  84.     {
  85.         float radius = 50.0;
  86.  
  87.         float NdotL = clamp(dot(normals, lightDir), 0.0, 1.0);
  88.         float angleLN = acos(NdotL);
  89.  
  90.         float alpha = max(angleVN, angleLN);
  91.         float beta = min(angleVN, angleLN);
  92.         float gamma = cos(angleVN - angleLN);
  93.  
  94.         float roughness2 = roughness * roughness;
  95.  
  96.         float A = 1.0 - 0.5 * (roughness2 / (roughness2 + 0.57));
  97.         float B = 0.45 * (roughness2 / (roughness2 + 0.09));
  98.         float C = sin(alpha) * tan(beta);
  99.  
  100.         vec3 diffuse = lightColour * (NdotL * (A + ((B * max(0.0, gamma)) * C)));
  101.  
  102.         OrenNayar += diffuse * attenuation;
  103.         ambientColour += lightColour * attenuation;
  104.     }
  105.     vec3 ambience = (0.01 * ambientColour) * AO;
  106.  
  107.     vec3 final = albedo * (OrenNayar + ambience);
  108.     final = pow(final, vec3(1.0 / 2.2));
  109.     FragColour = vec4(final, 1.0);
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement