Lighting.vert 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #define ATTENUATION
  2. //#define HQ_ATTENUATION
  3. #import "Common/ShaderLib/Skinning.glsllib"
  4. uniform mat4 g_WorldViewProjectionMatrix;
  5. uniform mat4 g_WorldViewMatrix;
  6. uniform mat3 g_NormalMatrix;
  7. uniform mat4 g_ViewMatrix;
  8. uniform vec4 m_Ambient;
  9. uniform vec4 m_Diffuse;
  10. uniform vec4 m_Specular;
  11. uniform float m_Shininess;
  12. uniform vec4 g_LightColor;
  13. uniform vec4 g_LightPosition;
  14. uniform vec4 g_AmbientLightColor;
  15. varying vec2 texCoord;
  16. #ifdef SEPARATE_TEXCOORD
  17. varying vec2 texCoord2;
  18. attribute vec2 inTexCoord2;
  19. #endif
  20. varying vec3 AmbientSum;
  21. varying vec4 DiffuseSum;
  22. varying vec3 SpecularSum;
  23. attribute vec3 inPosition;
  24. attribute vec2 inTexCoord;
  25. attribute vec3 inNormal;
  26. varying vec3 lightVec;
  27. //varying vec4 spotVec;
  28. #ifdef VERTEX_COLOR
  29. attribute vec4 inColor;
  30. #endif
  31. #ifndef VERTEX_LIGHTING
  32. attribute vec4 inTangent;
  33. #ifndef NORMALMAP
  34. varying vec3 vNormal;
  35. #endif
  36. //varying vec3 vPosition;
  37. varying vec3 vViewDir;
  38. varying vec4 vLightDir;
  39. #else
  40. varying vec2 vertexLightValues;
  41. uniform vec4 g_LightDirection;
  42. #endif
  43. #ifdef USE_REFLECTION
  44. uniform vec3 g_CameraPosition;
  45. uniform mat4 g_WorldMatrix;
  46. uniform vec3 m_FresnelParams;
  47. varying vec4 refVec;
  48. /**
  49. * Input:
  50. * attribute inPosition
  51. * attribute inNormal
  52. * uniform g_WorldMatrix
  53. * uniform g_CameraPosition
  54. *
  55. * Output:
  56. * varying refVec
  57. */
  58. void computeRef(in vec4 modelSpacePos){
  59. vec3 worldPos = (g_WorldMatrix * modelSpacePos).xyz;
  60. vec3 I = normalize( g_CameraPosition - worldPos ).xyz;
  61. vec3 N = normalize( (g_WorldMatrix * vec4(inNormal, 0.0)).xyz );
  62. refVec.xyz = reflect(I, N);
  63. refVec.w = m_FresnelParams.x + m_FresnelParams.y * pow(1.0 + dot(I, N), m_FresnelParams.z);
  64. }
  65. #endif
  66. // JME3 lights in world space
  67. void lightComputeDir(in vec3 worldPos, in vec4 color, in vec4 position, out vec4 lightDir){
  68. float posLight = step(0.5, color.w);
  69. vec3 tempVec = position.xyz * sign(posLight - 0.5) - (worldPos * posLight);
  70. lightVec = tempVec;
  71. #ifdef ATTENUATION
  72. float dist = length(tempVec);
  73. lightDir.w = clamp(1.0 - position.w * dist * posLight, 0.0, 1.0);
  74. lightDir.xyz = tempVec / vec3(dist);
  75. #else
  76. lightDir = vec4(normalize(tempVec), 1.0);
  77. #endif
  78. }
  79. #ifdef VERTEX_LIGHTING
  80. float lightComputeDiffuse(in vec3 norm, in vec3 lightdir){
  81. return max(0.0, dot(norm, lightdir));
  82. }
  83. float lightComputeSpecular(in vec3 norm, in vec3 viewdir, in vec3 lightdir, in float shiny){
  84. if (shiny <= 1.0){
  85. return 0.0;
  86. }
  87. #ifndef LOW_QUALITY
  88. vec3 H = (viewdir + lightdir) * vec3(0.5);
  89. return pow(max(dot(H, norm), 0.0), shiny);
  90. #else
  91. return 0.0;
  92. #endif
  93. }
  94. vec2 computeLighting(in vec3 wvPos, in vec3 wvNorm, in vec3 wvViewDir, in vec4 wvLightPos){
  95. vec4 lightDir;
  96. lightComputeDir(wvPos, g_LightColor, wvLightPos, lightDir);
  97. float spotFallOff = 1.0;
  98. if(g_LightDirection.w != 0.0){
  99. vec3 L=normalize(lightVec.xyz);
  100. vec3 spotdir = normalize(g_LightDirection.xyz);
  101. float curAngleCos = dot(-L, spotdir);
  102. float innerAngleCos = floor(g_LightDirection.w) * 0.001;
  103. float outerAngleCos = fract(g_LightDirection.w);
  104. float innerMinusOuter = innerAngleCos - outerAngleCos;
  105. spotFallOff = clamp((curAngleCos - outerAngleCos) / innerMinusOuter, 0.0, 1.0);
  106. }
  107. float diffuseFactor = lightComputeDiffuse(wvNorm, lightDir.xyz);
  108. float specularFactor = lightComputeSpecular(wvNorm, wvViewDir, lightDir.xyz, m_Shininess);
  109. //specularFactor *= step(0.01, diffuseFactor);
  110. return vec2(diffuseFactor, specularFactor) * vec2(lightDir.w)*spotFallOff;
  111. }
  112. #endif
  113. void main(){
  114. vec4 modelSpacePos = vec4(inPosition, 1.0);
  115. vec3 modelSpaceNorm = inNormal;
  116. vec3 modelSpaceTan = inTangent.xyz;
  117. #ifdef NUM_BONES
  118. Skinning_Compute(modelSpacePos, modelSpaceNorm, modelSpaceTan);
  119. #endif
  120. gl_Position = g_WorldViewProjectionMatrix * modelSpacePos;
  121. texCoord = inTexCoord;
  122. #ifdef SEPARATE_TEXCOORD
  123. texCoord2 = inTexCoord2;
  124. #endif
  125. vec3 wvPosition = (g_WorldViewMatrix * modelSpacePos).xyz;
  126. vec3 wvNormal = normalize(g_NormalMatrix * modelSpaceNorm);
  127. vec3 viewDir = normalize(-wvPosition);
  128. //vec4 lightColor = g_LightColor[gl_InstanceID];
  129. //vec4 lightPos = g_LightPosition[gl_InstanceID];
  130. //vec4 wvLightPos = (g_ViewMatrix * vec4(lightPos.xyz, lightColor.w));
  131. //wvLightPos.w = lightPos.w;
  132. vec4 wvLightPos = (g_ViewMatrix * vec4(g_LightPosition.xyz,clamp(g_LightColor.w,0.0,1.0)));
  133. wvLightPos.w = g_LightPosition.w;
  134. vec4 lightColor = g_LightColor;
  135. #if defined(NORMALMAP) && !defined(VERTEX_LIGHTING)
  136. vec3 wvTangent = normalize(g_NormalMatrix * modelSpaceTan);
  137. vec3 wvBinormal = cross(wvNormal, wvTangent);
  138. mat3 tbnMat = mat3(wvTangent, wvBinormal * -inTangent.w,wvNormal);
  139. //vPosition = wvPosition * tbnMat;
  140. //vViewDir = viewDir * tbnMat;
  141. vViewDir = -wvPosition * tbnMat;
  142. lightComputeDir(wvPosition, lightColor, wvLightPos, vLightDir);
  143. vLightDir.xyz = (vLightDir.xyz * tbnMat).xyz;
  144. #elif !defined(VERTEX_LIGHTING)
  145. vNormal = wvNormal;
  146. //vPosition = wvPosition;
  147. vViewDir = viewDir;
  148. lightComputeDir(wvPosition, lightColor, wvLightPos, vLightDir);
  149. #ifdef V_TANGENT
  150. vNormal = normalize(g_NormalMatrix * inTangent.xyz);
  151. vNormal = -cross(cross(vLightDir.xyz, vNormal), vNormal);
  152. #endif
  153. #endif
  154. //computing spot direction in view space and unpacking spotlight cos
  155. // spotVec = (g_ViewMatrix * vec4(g_LightDirection.xyz, 0.0) );
  156. // spotVec.w = floor(g_LightDirection.w) * 0.001;
  157. // lightVec.w = fract(g_LightDirection.w);
  158. lightColor.w = 1.0;
  159. #ifdef MATERIAL_COLORS
  160. AmbientSum = (m_Ambient * g_AmbientLightColor).rgb;
  161. DiffuseSum = m_Diffuse * lightColor;
  162. SpecularSum = (m_Specular * lightColor).rgb;
  163. #else
  164. AmbientSum = vec3(0.2, 0.2, 0.2) * g_AmbientLightColor.rgb; // Default: ambient color is dark gray
  165. DiffuseSum = lightColor;
  166. SpecularSum = vec3(0.0);
  167. #endif
  168. #ifdef VERTEX_COLOR
  169. AmbientSum *= inColor.rgb;
  170. DiffuseSum *= inColor;
  171. #endif
  172. #ifdef VERTEX_LIGHTING
  173. vertexLightValues = computeLighting(wvPosition, wvNormal, viewDir, wvLightPos);
  174. #endif
  175. #ifdef USE_REFLECTION
  176. computeRef(modelSpacePos);
  177. #endif
  178. }