Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.20 KB | None | 0 0
  1. #define ATTENUATION
  2. //#define HQ_ATTENUATION
  3.  
  4. #import "Common/ShaderLib/Skinning.glsllib"
  5.  
  6. uniform mat4 g_WorldViewProjectionMatrix;
  7. uniform mat4 g_WorldViewMatrix;
  8. uniform mat3 g_NormalMatrix;
  9. uniform mat4 g_ViewMatrix;
  10.  
  11. //------------< SHADOW >------------
  12. uniform mat4 g_WorldMatrix;
  13.  
  14. uniform mat4 m_LightViewProjectionMatrix0;
  15. uniform mat4 m_LightViewProjectionMatrix1;
  16. uniform mat4 m_LightViewProjectionMatrix2;
  17. uniform mat4 m_LightViewProjectionMatrix3;
  18.  
  19.  
  20. out vec4 projCoord0;
  21. out vec4 projCoord1;
  22. out vec4 projCoord2;
  23. out vec4 projCoord3;
  24.  
  25. #ifdef POINTLIGHT
  26. uniform mat4 m_LightViewProjectionMatrix4;
  27. uniform mat4 m_LightViewProjectionMatrix5;
  28. out vec4 projCoord4;
  29. out vec4 projCoord5;
  30. out vec4 worldPos;
  31. #else
  32. #ifndef PSSM
  33. uniform vec3 m_LightDir;
  34. out float lightDot;
  35. #endif
  36. #endif
  37.  
  38. #if defined(PSSM) || defined(FADE)
  39. out float shadowPosition;
  40. #endif
  41.  
  42. const mat4 biasMat = mat4(0.5, 0.0, 0.0, 0.0,
  43. 0.0, 0.5, 0.0, 0.0,
  44. 0.0, 0.0, 0.5, 0.0,
  45. 0.5, 0.5, 0.5, 1.0);
  46.  
  47.  
  48. //------------< SHADOW >------------
  49.  
  50.  
  51. uniform vec4 m_Ambient;
  52. uniform vec4 m_Diffuse;
  53. uniform vec4 m_Specular;
  54. uniform float m_Shininess;
  55.  
  56. //varying float initMaxss;
  57. uniform vec4 m_LightColor;
  58. uniform vec3 m_LightPos;
  59. uniform float m_LightRadius;
  60. //uniform vec4 g_AmbientLightColor;
  61.  
  62. varying vec2 texCoord;
  63. #ifdef SEPARATE_TEXCOORD
  64. varying vec2 texCoord2;
  65. attribute vec2 inTexCoord2;
  66. #endif
  67.  
  68. //varying vec3 AmbientSum;
  69. varying vec4 DiffuseSum;
  70. varying vec3 SpecularSum;
  71.  
  72. attribute vec3 inPosition;
  73. attribute vec2 inTexCoord;
  74. attribute vec3 inNormal;
  75.  
  76. varying vec3 lightVec;
  77. //varying vec4 spotVec;
  78.  
  79. #ifdef VERTEX_COLOR
  80. attribute vec4 inColor;
  81. #endif
  82.  
  83. #ifndef VERTEX_LIGHTING
  84. attribute vec4 inTangent;
  85.  
  86. #ifndef NORMALMAP
  87. varying vec3 vNormal;
  88. #endif
  89. //varying vec3 vPosition;
  90. varying vec3 vViewDir;
  91. varying vec4 vLightDir;
  92. #else
  93. varying vec2 vertexLightValues;
  94. // uniform vec4 g_LightDirection;
  95. #endif
  96.  
  97. #ifdef USE_REFLECTION
  98. uniform vec3 g_CameraPosition;
  99.  
  100. uniform vec3 m_FresnelParams;
  101. varying vec4 refVec;
  102.  
  103. /**
  104. * Input:
  105. * attribute inPosition
  106. * attribute inNormal
  107. * uniform g_WorldMatrix
  108. * uniform g_CameraPosition
  109. *
  110. * Output:
  111. * varying refVec
  112. */
  113. void computeRef(in vec4 modelSpacePos){
  114. // vec3 worldPos = (g_WorldMatrix * modelSpacePos).xyz;
  115. vec3 worldPos = TransformWorld(modelSpacePos).xyz;
  116.  
  117. vec3 I = normalize( g_CameraPosition - worldPos ).xyz;
  118. // vec3 N = normalize( (g_WorldMatrix * vec4(inNormal, 0.0)).xyz );
  119. vec3 N = normalize( TransformWorld(vec4(inNormal, 0.0)).xyz );
  120.  
  121. refVec.xyz = reflect(I, N);
  122. refVec.w = m_FresnelParams.x + m_FresnelParams.y * pow(1.0 + dot(I, N), m_FresnelParams.z);
  123. }
  124. #endif
  125.  
  126. // JME3 lights in world space
  127. void lightComputeDir(in vec3 worldPos, in vec4 color, in vec4 position, out vec4 lightDir){
  128. float posLight = step(0.5, color.w);
  129. vec3 tempVec = position.xyz * sign(posLight - 0.5) - (worldPos * posLight);
  130. lightVec = tempVec;
  131. #ifdef ATTENUATION
  132. float dist = length(tempVec);
  133. lightDir.w = clamp(1.0 - position.w * dist * posLight, 0.0, 1.0);
  134. lightDir.xyz = tempVec / vec3(dist);
  135. #else
  136. lightDir = vec4(normalize(tempVec), 1.0);
  137. #endif
  138. }
  139.  
  140. #ifdef VERTEX_LIGHTING
  141. float lightComputeDiffuse(in vec3 norm, in vec3 lightdir){
  142. return max(0.0, dot(norm, lightdir));
  143. }
  144.  
  145. float lightComputeSpecular(in vec3 norm, in vec3 viewdir, in vec3 lightdir, in float shiny){
  146. if (shiny <= 1.0){
  147. return 0.0;
  148. }
  149. #ifndef LOW_QUALITY
  150. vec3 H = (viewdir + lightdir) * vec3(0.5);
  151. return pow(max(dot(H, norm), 0.0), shiny);
  152. #else
  153. return 0.0;
  154. #endif
  155. }
  156.  
  157. vec2 computeLighting(in vec3 wvPos, in vec3 wvNorm, in vec3 wvViewDir, in vec4 wvLightPos){
  158. vec4 lightDir;
  159. lightComputeDir(wvPos, m_LightColor, wvLightPos, lightDir);
  160. float spotFallOff = 1.0;
  161. // if(g_LightDirection.w != 0.0){
  162. // vec3 L=normalize(lightVec.xyz);
  163. // vec3 spotdir = normalize(g_LightDirection.xyz);
  164. // float curAngleCos = dot(-L, spotdir);
  165. // float innerAngleCos = floor(g_LightDirection.w) * 0.001;
  166. // float outerAngleCos = fract(g_LightDirection.w);
  167. // float innerMinusOuter = innerAngleCos - outerAngleCos;
  168. // spotFallOff = clamp((curAngleCos - outerAngleCos) / innerMinusOuter, 0.0, 1.0);
  169. // }
  170. float diffuseFactor = lightComputeDiffuse(wvNorm, lightDir.xyz);
  171. float specularFactor = lightComputeSpecular(wvNorm, wvViewDir, lightDir.xyz, m_Shininess);
  172. //specularFactor *= step(0.01, diffuseFactor);
  173. return vec2(diffuseFactor, specularFactor) * vec2(lightDir.w)*spotFallOff;
  174. }
  175. #endif
  176.  
  177. void main(){
  178.  
  179. vec4 modelSpacePos = vec4(inPosition, 1.0);
  180. vec3 modelSpaceNorm = inNormal;
  181.  
  182. #ifndef VERTEX_LIGHTING
  183. vec3 modelSpaceTan = inTangent.xyz;
  184. #endif
  185.  
  186. #ifdef NUM_BONES
  187. #ifndef VERTEX_LIGHTING
  188. Skinning_Compute(modelSpacePos, modelSpaceNorm, modelSpaceTan);
  189. #else
  190. Skinning_Compute(modelSpacePos, modelSpaceNorm);
  191. #endif
  192. #endif
  193.  
  194. gl_Position = g_WorldViewProjectionMatrix * modelSpacePos;
  195. texCoord = inTexCoord;
  196. #ifdef SEPARATE_TEXCOORD
  197. texCoord2 = inTexCoord2;
  198. #endif
  199.  
  200. vec3 wvPosition = (g_WorldViewMatrix * modelSpacePos).xyz;
  201. vec3 wvNormal = normalize(g_NormalMatrix * modelSpaceNorm);
  202. vec3 viewDir = normalize(-wvPosition);
  203.  
  204. //vec4 lightColor = m_LightColor[gl_InstanceID];
  205. //vec4 lightPos = m_LightPos[gl_InstanceID];
  206. //vec4 wvLightPos = (g_ViewMatrix * vec4(lightPos.xyz, lightColor.w));
  207. //wvLightPos.w = lightPos.w;
  208.  
  209. //vec4 wvLightPos = (g_ViewMatrix * vec4(m_LightPos.xyz,clamp(m_LightColor.w,0.0,1.0)));
  210. //wvLightPos.w = m_LightPos.w;
  211. //vec4 lightColor = m_LightColor;
  212.  
  213. // Using vec3 m_LightPos and m_LightRadius instead of vec4 m_LightPos
  214. vec4 wvLightPos = (g_ViewMatrix * vec4(m_LightPos,clamp(m_LightColor.w,0.0,1.0))); //m_LightPos.xyz
  215. wvLightPos.w = m_LightRadius; //m_LightPos.w;
  216. vec4 lightColor = m_LightColor;
  217.  
  218. #if defined(NORMALMAP) && !defined(VERTEX_LIGHTING)
  219. vec3 wvTangent = normalize(g_NormalMatrix * modelSpaceTan);
  220. vec3 wvBinormal = cross(wvNormal, wvTangent);
  221.  
  222. mat3 tbnMat = mat3(wvTangent, wvBinormal * inTangent.w,wvNormal);
  223.  
  224. //vPosition = wvPosition * tbnMat;
  225. //vViewDir = viewDir * tbnMat;
  226. vViewDir = -wvPosition * tbnMat;
  227. lightComputeDir(wvPosition, lightColor, wvLightPos, vLightDir);
  228. vLightDir.xyz = (vLightDir.xyz * tbnMat).xyz;
  229. #elif !defined(VERTEX_LIGHTING)
  230. vNormal = wvNormal;
  231.  
  232. //vPosition = wvPosition;
  233. vViewDir = viewDir;
  234.  
  235. lightComputeDir(wvPosition, lightColor, wvLightPos, vLightDir);
  236.  
  237. #ifdef V_TANGENT
  238. vNormal = normalize(g_NormalMatrix * inTangent.xyz);
  239. vNormal = -cross(cross(vLightDir.xyz, vNormal), vNormal);
  240. #endif
  241. #endif
  242.  
  243. //computing spot direction in view space and unpacking spotlight cos
  244. // spotVec = (g_ViewMatrix * vec4(g_LightDirection.xyz, 0.0) );
  245. // spotVec.w = floor(g_LightDirection.w) * 0.001;
  246. // lightVec.w = fract(g_LightDirection.w);
  247.  
  248. lightColor.w = 1.0;
  249. #ifdef MATERIAL_COLORS
  250. DiffuseSum = m_Diffuse * lightColor;
  251. SpecularSum = (m_Specular * lightColor).rgb;
  252. #else
  253. DiffuseSum = lightColor;
  254. SpecularSum = vec3(0.0);
  255. #endif
  256.  
  257. #ifdef VERTEX_COLOR
  258. DiffuseSum *= inColor;
  259. #endif
  260.  
  261. #ifdef VERTEX_LIGHTING
  262. vertexLightValues = computeLighting(wvPosition, wvNormal, viewDir, wvLightPos);
  263. #endif
  264.  
  265. #ifdef USE_REFLECTION
  266. computeRef(modelSpacePos);
  267. #endif
  268.  
  269. //------------< SHADOW >------------
  270.  
  271. #if defined(PSSM) || defined(FADE)
  272. shadowPosition = gl_Position.z;
  273. #endif
  274.  
  275. #ifndef POINTLIGHT
  276. vec4 worldPos=vec4(0.0);
  277. #endif
  278. // get the vertex in world space
  279. worldPos = g_WorldMatrix * modelSpacePos;
  280.  
  281.  
  282. //vec3 lightVector = m_LightPos - worldPos.xyz; //vLightPos.xyz - worldPos.xyz;
  283. //float dist = length(lightVector);
  284. //float lrh = (1.0 / m_LightRadius) / 2;
  285. //initMaxss = 0.0;
  286. //if (dist > lrh && lrh > 0)
  287. //{
  288. // dist -= lrh;
  289. // initMaxss = 1.0 - (dist / lrh);
  290. //}
  291.  
  292. // populate the light view matrices array and convert vertex to light viewProj space
  293. projCoord0 = biasMat * m_LightViewProjectionMatrix0 * worldPos;
  294. projCoord1 = biasMat * m_LightViewProjectionMatrix1 * worldPos;
  295. projCoord2 = biasMat * m_LightViewProjectionMatrix2 * worldPos;
  296. projCoord3 = biasMat * m_LightViewProjectionMatrix3 * worldPos;
  297. #ifdef POINTLIGHT
  298. projCoord4 = biasMat * m_LightViewProjectionMatrix4 * worldPos;
  299. projCoord5 = biasMat * m_LightViewProjectionMatrix5 * worldPos;
  300. #else
  301. #ifndef PSSM
  302. vec3 lightDir = worldPos.xyz - m_LightPos;
  303. lightDot = dot(m_LightDir,lightDir);
  304. #endif
  305. #endif
  306. //------------< SHADOW >------------
  307.  
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement