Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. lightView =
  2. Matrix4.LookAt(light.lightPosition, light.lightPosition - Vector3.UnitZ, Vector3.UnitY);
  3.  
  4. #version 120
  5.  
  6. uniform mat4 worldView;
  7. uniform float direction;
  8.  
  9. attribute vec3 position; //usage:position
  10. attribute vec3 normal; //usage:normal
  11.  
  12. varying highp vec2 depth;
  13.  
  14. varying float clip;
  15.  
  16. const float near = 0.1f;
  17. const float far = 1000.0f;
  18.  
  19. void main()
  20. {
  21.  
  22. highp vec4 pos4 = worldView * vec4(position, 1);
  23.  
  24.  
  25. pos4 = pos4 / pos4.w;
  26. pos4.z = pos4.z * direction;
  27.  
  28. highp float L = length(pos4.xyz);
  29. pos4 = pos4 / L;
  30.  
  31. clip = pos4.z;
  32. pos4.z = pos4.z + 1;
  33.  
  34. pos4.x = pos4.x / pos4.z;
  35. pos4.y = pos4.y / pos4.z;
  36.  
  37. pos4.z = (L - near) / (far - near);
  38. pos4.w = 1;
  39.  
  40. depth = pos4.zw;
  41.  
  42. gl_Position = pos4;
  43.  
  44. }
  45.  
  46. #version 120
  47.  
  48. varying highp vec2 depth;
  49. varying float clip;
  50.  
  51. vec4 mapDepthToARGB32(float value)
  52. {
  53. const vec4 bitSh = vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0);
  54. const vec4 mask = vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);
  55. highp vec4 res = fract(value * bitSh);
  56. res -= res.xxyz * mask;
  57. return res;
  58. }
  59.  
  60.  
  61. void main()
  62. {
  63.  
  64. if(clip < 0)
  65. discard;
  66.  
  67. float finalDepth = depth.x / depth.y;
  68. gl_FragColor = mapDepthToARGB32(finalDepth); //vec4(depth.y, depth.y, depth.y, 1.0f);
  69.  
  70. }
  71.  
  72. #version 120
  73.  
  74. uniform highp vec3 lightPosition;
  75.  
  76. uniform mat4 worldViewProjection;
  77. uniform mat4 world;
  78.  
  79. attribute vec3 position; //usage:position
  80. attribute vec2 uv; //usage:uv
  81. attribute vec3 normal; //usage:normal
  82.  
  83. varying highp vec3 faceNormal;
  84. varying highp vec3 lightNormal;
  85. varying highp vec2 finalUv;
  86. varying highp vec3 pos;
  87.  
  88. void main()
  89. {
  90.  
  91. vec4 pos4 = vec4(position, 1);
  92. pos = (world * pos4).xyz;
  93.  
  94. gl_Position = worldViewProjection * pos4;
  95.  
  96. lightNormal = lightPosition - pos;
  97. faceNormal = (world * vec4(normal , 0)).xyz;
  98.  
  99. finalUv = uv;
  100.  
  101. }
  102.  
  103. #version 120
  104.  
  105. uniform highp vec3 lightColor;
  106. uniform highp vec3 ambientColor;
  107.  
  108. uniform mat4 lightView;
  109.  
  110. uniform highp float uvMultiplier;
  111.  
  112. uniform sampler2D colorTexture1;
  113. uniform sampler2D colorTexture2;
  114. uniform sampler2D colorTexture3;
  115.  
  116. uniform sampler2D weightTexture;
  117.  
  118. uniform sampler2D frontShadowMap;
  119. uniform sampler2D backShadowMap;
  120.  
  121. varying highp vec3 faceNormal;
  122. varying highp vec3 lightNormal;
  123. varying highp vec2 finalUv;
  124. varying highp vec3 pos;
  125.  
  126. const float SHADOW_EPSILON = 0.00005f;
  127.  
  128. float getDepthFromARGB32(vec4 value)
  129. {
  130. const vec4 bitSh = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);
  131. return(dot(value, bitSh));
  132. }
  133.  
  134. const float near = 0.1f;
  135. const float far = 1000;
  136.  
  137. void main()
  138. {
  139.  
  140. highp float factor = clamp(dot(normalize(lightNormal), normalize(faceNormal)), 0.0, 1.0);
  141.  
  142. highp vec3 finalColor = vec3(0,0,0);
  143.  
  144. lowp vec4 weights = texture2D(weightTexture, finalUv);
  145.  
  146. finalColor += texture2D(colorTexture1, finalUv * uvMultiplier).xyz * weights.x;
  147. finalColor += texture2D(colorTexture2, finalUv * uvMultiplier).xyz * weights.y;
  148. finalColor += texture2D(colorTexture3, finalUv * uvMultiplier).xyz * weights.z;
  149.  
  150. finalColor = (finalColor * lightColor * factor) + (finalColor * ambientColor);
  151.  
  152. highp vec3 vPosDP = (vec4(pos, 1) * lightView).xyz;
  153.  
  154. float fLength = length(vPosDP);
  155.  
  156. vPosDP /= fLength;
  157.  
  158. float fDPDepth;
  159. float fSceneDepth;
  160.  
  161. if(vPosDP.z >= 0.0f)
  162. {
  163. vec2 vTexFront;
  164. vTexFront.x = (vPosDP.x / (1.0f + vPosDP.z)) * 0.5f + 0.5f;
  165. vTexFront.y = 1.0f - ((vPosDP.y / (1.0f + vPosDP.z)) * 0.5f + 0.5f);
  166.  
  167. fSceneDepth = (fLength - near) / (far - near);
  168.  
  169. fDPDepth = getDepthFromARGB32(texture2D(frontShadowMap, vTexFront));
  170. }
  171. else
  172. {
  173. // for the back the z has to be inverted
  174. vec2 vTexBack;
  175. vTexBack.x = (vPosDP.x / (1.0f - vPosDP.z)) * 0.5f + 0.5f;
  176. vTexBack.y = 1.0f - ((vPosDP.y / (1.0f - vPosDP.z)) * 0.5f + 0.5f);
  177.  
  178. fSceneDepth = (fLength - near) / (far - near);
  179.  
  180. fDPDepth = getDepthFromARGB32(texture2D(backShadowMap, vTexBack));
  181. }
  182.  
  183. if((fDPDepth + SHADOW_EPSILON) < fSceneDepth)
  184. finalColor = finalColor * 0.3f;
  185.  
  186. gl_FragColor = vec4(finalColor, 1);
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement