Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. #version 330
  2.  
  3. uniform mat4 projectionMatrix;
  4. uniform mat4 viewMatrix;
  5. uniform mat4 modelMatrix;
  6. uniform vec3 eyePos;
  7.  
  8. in vec3 in_Position;
  9. in vec2 in_TextureCoord;
  10. in vec3 in_Normal;
  11. in vec4 in_Tangent;
  12. in float in_ModelOffset;
  13. in float in_TexOffset;
  14.  
  15. out vec2 pass_TextureCoord;
  16. out float pass_TexOffset;
  17. out vec3 pass_toLightInTangentSpace;
  18. out vec3 pass_toCameraInTangentSpace;
  19.  
  20. void main(void) {
  21. mat3 normalMatrix = transpose(inverse(mat3(modelMatrix)));
  22.  
  23. pass_TexOffset = in_TexOffset;
  24. pass_TextureCoord = in_TextureCoord;
  25.  
  26. // transform to world space
  27. vec4 worldPosition = modelMatrix * vec4(in_Position, 1);
  28. vec3 worldNormal = normalize(normalMatrix * in_Normal);
  29. vec3 worldTangent = normalize(normalMatrix * in_Tangent.xyz);
  30.  
  31. // calculate vectors to the camera and to the light, hardcoded for now
  32. vec3 worldDirectionToLight = normalize(vec3(0,10,0) - worldPosition.xyz);
  33. vec3 worldDirectionToCamera = normalize(eyePos - worldPosition.xyz);
  34.  
  35. // calculate bitangent from normal and tangent
  36. vec3 worldBitangnent = cross(worldNormal, worldTangent) * in_Tangent.w;
  37.  
  38. // transform direction to the light to tangent space
  39. pass_toLightInTangentSpace = vec3(
  40. dot(worldDirectionToLight, worldTangent),
  41. dot(worldDirectionToLight, worldBitangnent),
  42. dot(worldDirectionToLight, worldNormal)
  43. );
  44.  
  45. // transform direction to the camera to tangent space
  46. pass_toCameraInTangentSpace= vec3(
  47. dot(worldDirectionToCamera, worldTangent),
  48. dot(worldDirectionToCamera, worldBitangnent),
  49. dot(worldDirectionToCamera, worldNormal)
  50. );
  51.  
  52.  
  53. // calculate screen space position of the vertex
  54. gl_Position = projectionMatrix * viewMatrix * worldPosition;
  55.  
  56. }
  57.  
  58. #version 330
  59.  
  60. const int NUM_TEXTURES = 2;
  61.  
  62. uniform sampler2DArray diffuseTexture;
  63.  
  64. uniform vec3 ambientColor;
  65.  
  66. uniform float specularIntensity;
  67. uniform float specularPower;
  68. uniform float renderNormal;
  69. uniform float height_scale;
  70.  
  71. in vec2 pass_TextureCoord;
  72. in float pass_TexOffset;
  73. in vec3 pass_toLightInTangentSpace;
  74. in vec3 pass_toCameraInTangentSpace;
  75.  
  76. out vec4 out_Color;
  77.  
  78. const float parallaxScale = 0.1;
  79.  
  80. //////////////////////////////////////////////////////
  81. // Implements Parallax Mapping technique
  82. // Returns modified texture coordinates, and last used depth
  83. vec2 parallaxMapping(in vec3 V, in vec2 T, out float parallaxHeight)
  84. {
  85. // determine optimal number of layers
  86. const float minLayers = 10;
  87. const float maxLayers = 15;
  88. float numLayers = mix(maxLayers, minLayers, abs(dot(vec3(0, 0, 1), V)));
  89.  
  90. // height of each layer
  91. float layerHeight = 1.0 / numLayers;
  92. // current depth of the layer
  93. float curLayerHeight = 0;
  94. // shift of texture coordinates for each layer
  95. vec2 dtex = parallaxScale * V.xy / V.z / numLayers;
  96.  
  97. // current texture coordinates
  98. vec2 currentTextureCoords = T;
  99.  
  100. // depth from heightmap
  101. float heightFromTexture = texture(diffuseTexture, vec3(currentTextureCoords, pass_TexOffset+1)).a;
  102.  
  103. // while point is above the surface
  104. while(heightFromTexture > curLayerHeight)
  105. {
  106. // to the next layer
  107. curLayerHeight += layerHeight;
  108. // shift of texture coordinates
  109. currentTextureCoords -= dtex;
  110. // new depth from heightmap
  111. heightFromTexture = texture(diffuseTexture, vec3(currentTextureCoords, pass_TexOffset+1)).a;
  112. }
  113.  
  114. ///////////////////////////////////////////////////////////
  115.  
  116. // previous texture coordinates
  117. vec2 prevTCoords = currentTextureCoords + dtex;
  118.  
  119. // heights for linear interpolation
  120. float nextH = heightFromTexture - curLayerHeight;
  121.  
  122. float prevH = texture(diffuseTexture, vec3(prevTCoords, pass_TexOffset+1)).a
  123. - curLayerHeight + layerHeight;
  124.  
  125. // proportions for linear interpolation
  126. float weight = nextH / (nextH - prevH);
  127.  
  128. // interpolation of texture coordinates
  129. vec2 finalTexCoords = prevTCoords * weight + currentTextureCoords * (1.0-weight);
  130.  
  131. // interpolation of depth values
  132. parallaxHeight = curLayerHeight + prevH * weight + nextH * (1.0 - weight);
  133.  
  134. // return result
  135. return finalTexCoords;
  136. }
  137.  
  138. //////////////////////////////////////////////////////
  139. // Implements self-shadowing technique - hard or soft shadows
  140. // Returns shadow factor
  141. float parallaxSoftShadowMultiplier(in vec3 L, in vec2 initialTexCoord,
  142. in float initialHeight)
  143. {
  144. float shadowMultiplier = 1;
  145.  
  146. const float minLayers = 15;
  147. const float maxLayers = 30;
  148.  
  149. // calculate lighting only for surface oriented to the light source
  150. if(dot(vec3(0, 0, 1), L) > 0)
  151. {
  152. // calculate initial parameters
  153. float numSamplesUnderSurface = 0;
  154. shadowMultiplier = 0;
  155. float numLayers = mix(maxLayers, minLayers, abs(dot(vec3(0, 0, 1), L)));
  156. float layerHeight = initialHeight / numLayers;
  157. vec2 texStep = parallaxScale * L.xy / L.z / numLayers;
  158.  
  159. // current parameters
  160. float currentLayerHeight = initialHeight - layerHeight;
  161. vec2 currentTextureCoords = initialTexCoord + texStep;
  162. float heightFromTexture = texture(diffuseTexture, vec3(currentTextureCoords, pass_TexOffset+1)).a;
  163. int stepIndex = 1;
  164.  
  165. // while point is below depth 0.0 )
  166. while(currentLayerHeight > 0)
  167. {
  168. // if point is under the surface
  169. if(heightFromTexture < currentLayerHeight)
  170. {
  171. // calculate partial shadowing factor
  172. numSamplesUnderSurface += 1;
  173. float newShadowMultiplier = (currentLayerHeight - heightFromTexture) *
  174. (1.0 - stepIndex / numLayers);
  175. shadowMultiplier = max(shadowMultiplier, newShadowMultiplier);
  176. }
  177.  
  178. // offset to the next layer
  179. stepIndex += 1;
  180. currentLayerHeight -= layerHeight;
  181. currentTextureCoords += texStep;
  182. heightFromTexture = texture(diffuseTexture, vec3(currentTextureCoords, pass_TexOffset+1)).a;
  183. }
  184.  
  185. // Shadowing factor should be 1 if there were no points under the surface
  186. if(numSamplesUnderSurface < 1)
  187. {
  188. shadowMultiplier = 1;
  189. }
  190. else
  191. {
  192. shadowMultiplier = 1.0 - shadowMultiplier;
  193. }
  194. }
  195. return shadowMultiplier;
  196. }
  197.  
  198. //////////////////////////////////////////////////////
  199. // Calculates lighting by Blinn-Phong model and Normal Mapping
  200. // Returns color of the fragment
  201. vec4 normalMappingLighting(in vec2 T, in vec3 L, in vec3 V, float shadowMultiplier)
  202. {
  203. // restore normal from normal map
  204. vec3 N = normalize(texture(diffuseTexture, vec3(T, pass_TexOffset+1)).xyz * 2 - 1);
  205. vec3 D = texture(diffuseTexture, vec3(T, pass_TexOffset)).rgb;
  206.  
  207. // ambient lighting
  208. float iamb = 0.2;
  209. // diffuse lighting
  210. float idiff = clamp(dot(N, L), 0, 1);
  211. // specular lighting
  212. float ispec = 0;
  213. if(dot(N, L) > 0.2)
  214. {
  215. vec3 R = reflect(-L, N);
  216. ispec = pow(dot(R, V), 32) / 1.5;
  217. }
  218.  
  219. vec4 resColor;
  220. resColor.rgb = D * (vec3(0.1, 0.1, 0.1) + (idiff + ispec) * pow(shadowMultiplier, 4));
  221. resColor.a = 1;
  222.  
  223. return resColor;
  224. }
  225.  
  226. /////////////////////////////////////////////
  227. // Entry point for Parallax Mapping shader
  228. void main(void)
  229. {
  230. // normalize vectors after vertex shader
  231. vec3 V = normalize(pass_toCameraInTangentSpace);
  232. vec3 L = normalize(pass_toLightInTangentSpace);
  233.  
  234. // get new texture coordinates from Parallax Mapping
  235. float parallaxHeight;
  236. vec2 T = parallaxMapping(V, pass_TextureCoord, parallaxHeight);
  237.  
  238. // get self-shadowing factor for elements of parallax
  239. float shadowMultiplier = parallaxSoftShadowMultiplier(L, T, parallaxHeight - 0.05);
  240.  
  241. // calculate lighting
  242. out_Color = normalMappingLighting(T, L, V, shadowMultiplier);
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement