Gary_Keen27

Parallax Occlusion Mapping - BP

Oct 28th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  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. vec2 POM(vec3 camDir, vec2 texCoord, out float fragDepth)
  69. {
  70. float samples = mix(30, 5, abs(dot(vec3(0.0, 0.0, 1.0), camDir)));
  71. float sampleDist = 1.0 / samples;
  72. float currentDepth = 0.0;
  73.  
  74. vec2 offsetTexCoord = 0.1 * (camDir.xy / (camDir.z * samples));
  75.  
  76. vec2 currentTexCoord = texCoord;
  77. float depthFromMesh = texture(normalMap, currentTexCoord).a;
  78.  
  79. while(depthFromMesh > currentDepth)
  80. {
  81. currentDepth += sampleDist;
  82. currentTexCoord -= offsetTexCoord;
  83. depthFromMesh = texture(normalMap, currentTexCoord).a;
  84. }
  85.  
  86. vec2 prevTexCoord = currentTexCoord + offsetTexCoord;
  87.  
  88. float afterIntersection = depthFromMesh - currentDepth;
  89. float beforeIntersection = texture(normalMap, prevTexCoord).a - currentDepth + sampleDist;
  90.  
  91. float ratio = afterIntersection / (afterIntersection - beforeIntersection);
  92.  
  93. vec2 finalTexCoord = prevTexCoord * ratio + currentTexCoord * (1.0 - ratio);
  94. fragDepth = currentDepth + beforeIntersection * ratio + afterIntersection * (1.0 - ratio);
  95.  
  96. return finalTexCoord;
  97. }
  98.  
  99. float selfOcclusion(vec3 lightDir, vec2 texCoord, float initialDepth)
  100. {
  101. float coefficient = 0.0;
  102. float occludedSamples = 0.0;
  103. float newCoefficient = 0.0;
  104.  
  105. float samples = 30.0;//mix(30, 5, abs(dot(normal, lightDir)));
  106. float sampleDist = initialDepth / samples;
  107. vec2 offsetTexCoord = 0.1 * (lightDir.xy / (lightDir.z * samples));
  108.  
  109. float currentDepth = initialDepth - sampleDist;
  110. vec2 currentTexCoord = texCoord + offsetTexCoord;
  111. float depthFromMesh = texture(normalMap, currentTexCoord).a;
  112. float steps = 1.0;
  113.  
  114. while(currentDepth > 0.0)
  115. {
  116. if(depthFromMesh < currentDepth)
  117. {
  118. occludedSamples += 1.0;
  119. newCoefficient = (currentDepth - depthFromMesh) * (1.0 - steps / samples);
  120. coefficient = max(coefficient, newCoefficient);
  121. }
  122. currentDepth -= sampleDist;
  123. currentTexCoord += offsetTexCoord;
  124. depthFromMesh = texture(normalMap, currentTexCoord).a;
  125. steps += 1.0;
  126. }
  127.  
  128. if(occludedSamples < 1.0)
  129. coefficient = 1.0;
  130. else
  131. coefficient = 1.0 - coefficient;
  132.  
  133. return coefficient;
  134. }
  135.  
  136. void main()
  137. {
  138. float fragDepth = 0.0;
  139.  
  140. vec2 offsetTexCoord = POM(camDir, texCoord, fragDepth);
  141. if (offsetTexCoord.x > 1.0 || offsetTexCoord.y > 1.0 || offsetTexCoord.x < 0.0 || offsetTexCoord.y < 0.0)
  142. discard;
  143.  
  144. vec3 albedo = texture(textureMap, offsetTexCoord.xy).rgb;
  145. vec3 normals = normalize(texture(normalMap, offsetTexCoord.xy).rgb * 2.0 - 1.0);
  146. float roughness = texture(roughMap, offsetTexCoord.xy).r;
  147. float spec = texture(specMap, offsetTexCoord.xy).r;
  148. float AO = texture(AOMap, offsetTexCoord.xy).r;
  149.  
  150. vec3 diffuse = vec3(0.0);
  151. vec3 specular = vec3(0.0);
  152. vec3 ambientColour = vec3(0.0);
  153. float occlusion = 0.0;
  154.  
  155. for (int i = 0; i < 1; i++)
  156. {
  157. float radius = 50.0;
  158.  
  159. vec3 halfAngle = normalize(camDir + lightDir);
  160. float NdotL = clamp(dot(normals, lightDir), 0.0, 1.0);
  161. float NdotH = clamp(dot(normals, halfAngle), 0.0, 1.0);
  162.  
  163. float specularHighlight = pow(NdotH, 1.0 - roughness);
  164. vec3 S = spec * (lightColour * specularHighlight);
  165. vec3 D = (1.0 - roughness) * (lightColour * NdotL);
  166.  
  167. occlusion += selfOcclusion(lightDir, offsetTexCoord, fragDepth);
  168. diffuse += D * attenuation;
  169. specular += S * attenuation;
  170. ambientColour += lightColour * attenuation;
  171. }
  172.  
  173. vec3 ambience = (0.01 * ambientColour) * AO;
  174.  
  175. vec3 BlinnPhong = occlusion * (ambience + diffuse + specular);
  176. vec3 final = albedo * BlinnPhong;
  177. final = pow(final, vec3(1.0 / 2.2));
  178. FragColour = vec4(final, 1.0);
  179. }
Add Comment
Please, Sign In to add comment