shadow.glslv 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. #version GLSL_VERSION
  2. /*==============================================================================
  3. VARS
  4. ==============================================================================*/
  5. #var PRECISION highp
  6. #var CAUSTICS 0
  7. #var CALC_TBN_SPACE 0
  8. #var MAIN_BEND_COL 0
  9. #var DETAIL_BEND 0
  10. #var CALC_TBN 0
  11. #var USE_INSTANCED_PARTCLS 0
  12. #var NODES 0
  13. #var USE_TBN_SHADING 0
  14. #var REFLECTION_TYPE REFL_NONE
  15. #var SMAA_JITTER 0
  16. #var MAC_OS_SHADOW_HACK 0
  17. #var USE_POSITION_CLIP 0
  18. #var REFRACTIVE 0
  19. #var AU_QUALIFIER GLSL_IN
  20. #var BEND_CENTER_ONLY 0
  21. #var CSM_SECTION1 0
  22. #var CSM_SECTION2 0
  23. #var CSM_SECTION3 0
  24. #var HAIR_BILLBOARD 0
  25. #var FRAMES_BLENDING 0
  26. #var SHADOW_TEX_RES 2048.0
  27. #var VERTEX_ANIM 0
  28. #var NUM_CAST_LAMPS 0
  29. #var TEXTURE_COLOR 0
  30. #var ALPHA 0
  31. #var VERTEX_ANIM_MIX_NORMALS_FACTOR u_va_frame_factor
  32. #var MAX_BONES 0
  33. #var WIND_BEND 0
  34. #var SHADOW_USAGE NO_SHADOWS
  35. #var SKINNED 0
  36. #var DYNAMIC_GRASS 0
  37. #var BILLBOARD 0
  38. #var STATIC_BATCH 0
  39. #var BILLBOARD_JITTERED 0
  40. #var BILLBOARD_PRES_GLOB_ORIENTATION 0
  41. /*==============================================================================
  42. INCLUDES
  43. ==============================================================================*/
  44. #include <std.glsl>
  45. #include <math.glslv>
  46. #include <to_world.glslv>
  47. #include <scale_texcoord.glslv>
  48. /*==============================================================================
  49. SHADER INTERFACE
  50. ==============================================================================*/
  51. GLSL_IN vec3 a_position;
  52. GLSL_IN vec4 a_tbn_quat;
  53. #if USE_INSTANCED_PARTCLS
  54. GLSL_IN vec4 a_part_ts;
  55. GLSL_IN vec4 a_part_r;
  56. #endif
  57. #if USE_TBN_SHADING
  58. GLSL_IN vec3 a_shade_tangs;
  59. #endif
  60. #if SKINNED
  61. GLSL_IN vec4 a_influence;
  62. #endif
  63. #if (WIND_BEND || DYNAMIC_GRASS || BILLBOARD) && !USE_INSTANCED_PARTCLS
  64. AU_QUALIFIER vec3 au_center_pos;
  65. #endif
  66. #if WIND_BEND
  67. # if MAIN_BEND_COL
  68. GLSL_IN float a_bending_col_main;
  69. # if DETAIL_BEND
  70. GLSL_IN vec3 a_bending_col_detail;
  71. AU_QUALIFIER float au_detail_bending_amp;
  72. AU_QUALIFIER float au_branch_bending_amp;
  73. AU_QUALIFIER float au_detail_bending_freq;
  74. # endif // DETAIL_BEND
  75. # endif // MAIN_BEND_COL
  76. AU_QUALIFIER float au_wind_bending_amp;
  77. AU_QUALIFIER float au_wind_bending_freq;
  78. # if BEND_CENTER_ONLY
  79. GLSL_IN vec3 a_emitter_center;
  80. # endif
  81. #endif // WIND_BEND
  82. #if VERTEX_ANIM
  83. GLSL_IN vec3 a_position_next;
  84. # if NODES && ALPHA
  85. # if USE_NODE_MATERIAL_BEGIN || USE_NODE_GEOMETRY_NO || USE_NODE_NORMAL_MAP \
  86. || CAUSTICS || CALC_TBN_SPACE
  87. GLSL_IN vec4 a_tbn_quat_next;
  88. # endif
  89. # endif
  90. #endif // VERTEX_ANIM
  91. #if !(NODES && ALPHA) && TEXTURE_COLOR
  92. GLSL_IN vec2 a_texcoord;
  93. #endif
  94. //------------------------------------------------------------------------------
  95. #if NODES && ALPHA
  96. GLSL_OUT vec3 v_pos_world;
  97. GLSL_OUT vec3 v_normal;
  98. # if CALC_TBN_SPACE
  99. GLSL_OUT vec4 v_tangent;
  100. # endif
  101. #else
  102. # if TEXTURE_COLOR
  103. GLSL_OUT vec2 v_texcoord;
  104. # endif
  105. #endif
  106. #if SHADOW_USAGE == SHADOW_MASK_GENERATION || NODES && ALPHA
  107. GLSL_OUT vec4 v_pos_view;
  108. #endif
  109. #if SHADOW_USAGE == SHADOW_MASK_GENERATION
  110. GLSL_OUT vec4 v_shadow_coord0;
  111. # if CSM_SECTION1 || NUM_CAST_LAMPS > 1
  112. GLSL_OUT vec4 v_shadow_coord1;
  113. # endif
  114. # if CSM_SECTION2 || NUM_CAST_LAMPS > 2
  115. GLSL_OUT vec4 v_shadow_coord2;
  116. # endif
  117. # if CSM_SECTION3 || NUM_CAST_LAMPS > 3
  118. GLSL_OUT vec4 v_shadow_coord3;
  119. # endif
  120. #endif
  121. #if REFLECTION_TYPE == REFL_PLANE || USE_POSITION_CLIP
  122. GLSL_OUT vec3 v_tex_pos_clip;
  123. #endif
  124. #if NODES && ALPHA
  125. # if USE_NODE_B4W_REFRACTION && REFRACTIVE
  126. GLSL_OUT float v_view_depth;
  127. # endif
  128. #endif
  129. #if USE_TBN_SHADING
  130. GLSL_OUT vec3 v_shade_tang;
  131. #endif
  132. /*==============================================================================
  133. UNIFORMS
  134. ==============================================================================*/
  135. #if STATIC_BATCH
  136. // NOTE: mat3(0.0, 0.0, 0.0, --- trans
  137. // 1.0, --- scale
  138. // 0.0, 0.0, 0.0, 1.0, --- quat
  139. // 0.0);
  140. const mat3 u_model_tsr = mat3(0.0, 0.0, 0.0,
  141. 1.0,
  142. 0.0, 0.0, 0.0, 1.0,
  143. 0.0);
  144. #else
  145. uniform mat3 u_model_tsr;
  146. #endif
  147. #if SMAA_JITTER
  148. uniform vec2 u_subpixel_jitter;
  149. #endif
  150. uniform mat3 u_view_tsr;
  151. uniform mat4 u_proj_matrix;
  152. # if DYNAMIC_GRASS || BILLBOARD
  153. uniform vec3 u_camera_eye;
  154. # endif
  155. #if BILLBOARD && SHADOW_USAGE == SHADOW_CASTING
  156. uniform mat3 u_shadow_cast_billboard_view_tsr;
  157. #endif
  158. #if DYNAMIC_GRASS
  159. uniform PRECISION sampler2D u_grass_map_depth;
  160. uniform sampler2D u_grass_map_color;
  161. uniform vec4 u_camera_quat;
  162. uniform vec3 u_grass_map_dim;
  163. uniform float u_grass_size;
  164. uniform float u_scale_threshold;
  165. #endif
  166. #if SKINNED
  167. uniform vec4 u_quatsb[MAX_BONES];
  168. uniform vec4 u_transb[MAX_BONES];
  169. uniform vec4 u_arm_rel_trans;
  170. uniform vec4 u_arm_rel_quat;
  171. #if FRAMES_BLENDING
  172. uniform vec4 u_quatsa[MAX_BONES];
  173. uniform vec4 u_transa[MAX_BONES];
  174. // near 0 if before, near 1 if after
  175. uniform float u_frame_factor;
  176. #endif
  177. #endif
  178. #if WIND_BEND
  179. #if BILLBOARD_JITTERED
  180. uniform float u_jitter_amp;
  181. uniform float u_jitter_freq;
  182. #endif
  183. uniform vec3 u_wind;
  184. uniform float u_time;
  185. #endif
  186. #if VERTEX_ANIM
  187. uniform float u_va_frame_factor;
  188. #endif
  189. #if !(NODES && ALPHA) && TEXTURE_COLOR
  190. uniform vec3 u_texture_scale;
  191. #endif
  192. #if SHADOW_USAGE == SHADOW_MASK_GENERATION
  193. uniform float u_normal_offset;
  194. # if MAC_OS_SHADOW_HACK
  195. uniform mat3 u_v_light_tsr[NUM_CAST_LAMPS];
  196. # else
  197. uniform vec4 u_v_light_ts[NUM_CAST_LAMPS];
  198. uniform vec4 u_v_light_r[NUM_CAST_LAMPS];
  199. # endif
  200. uniform mat4 u_p_light_matrix0;
  201. # if CSM_SECTION1 || NUM_CAST_LAMPS > 1
  202. uniform mat4 u_p_light_matrix1;
  203. # endif
  204. # if CSM_SECTION2 || NUM_CAST_LAMPS > 2
  205. uniform mat4 u_p_light_matrix2;
  206. # endif
  207. # if CSM_SECTION3 || NUM_CAST_LAMPS > 3
  208. uniform mat4 u_p_light_matrix3;
  209. # endif
  210. #endif
  211. #if USE_NODE_B4W_REFRACTION
  212. uniform PRECISION float u_view_max_depth;
  213. #endif
  214. /*==============================================================================
  215. INCLUDES
  216. ==============================================================================*/
  217. #include <dynamic_grass.glslv>
  218. #include <shadow.glslv>
  219. #include <skin.glslv>
  220. #include <wind_bending.glslv>
  221. #if NODES && ALPHA
  222. #include <nodes.glslv>
  223. #endif
  224. /*==============================================================================
  225. MAIN
  226. ==============================================================================*/
  227. void main(void) {
  228. mat3 view_tsr = u_view_tsr;
  229. vec3 position = a_position;
  230. #if SHADOW_USAGE == SHADOW_MASK_GENERATION || CALC_TBN_SPACE || USE_NODE_MATERIAL_BEGIN \
  231. || USE_NODE_GEOMETRY_NO || USE_NODE_NORMAL_MAP \
  232. || CAUSTICS || WIND_BEND && MAIN_BEND_COL && DETAIL_BEND \
  233. || USE_NODE_BSDF_BEGIN || USE_NODE_FRESNEL || USE_NODE_TEX_COORD_NO \
  234. || USE_NODE_TEX_COORD_RE || USE_NODE_LAYER_WEIGHT || USE_NODE_BUMP
  235. vec3 norm_tbn = qrot(a_tbn_quat, vec3(0.0, 1.0, 0.0));
  236. vec3 normal = norm_tbn;
  237. #else
  238. vec3 normal = vec3(0.0);
  239. #endif
  240. #if NODES && ALPHA && CALC_TBN_SPACE
  241. vec3 tangent = qrot(a_tbn_quat, vec3(1.0, 0.0, 0.0));
  242. vec3 binormal = sign(a_tbn_quat[3]) * cross(normal, tangent);
  243. #else
  244. vec3 tangent = vec3(0.0);
  245. vec3 binormal = vec3(0.0);
  246. #endif
  247. #if VERTEX_ANIM
  248. position = mix(position, a_position_next, u_va_frame_factor);
  249. # if NODES && ALPHA
  250. # if USE_NODE_MATERIAL_BEGIN || USE_NODE_GEOMETRY_NO || USE_NODE_NORMAL_MAP || CAUSTICS || CALC_TBN_SPACE
  251. vec3 normal_next = qrot(a_tbn_quat_next, vec3(0.0, 1.0, 0.0));
  252. normal = mix(normal, normal_next, VERTEX_ANIM_MIX_NORMALS_FACTOR);
  253. # endif
  254. # if CALC_TBN_SPACE
  255. vec3 tangent_next = qrot(a_tbn_quat_next, vec3(1.0, 0.0, 0.0));
  256. vec3 binormal_next = sign(a_tbn_quat_next[3]) * cross(normal_next, tangent_next);
  257. tangent = mix(tangent, tangent_next, u_va_frame_factor);
  258. binormal = mix(binormal, binormal_next, u_va_frame_factor);
  259. # endif
  260. # endif // NODES && ALPHA
  261. #endif // VERTEX_ANIM
  262. #if SKINNED
  263. skin(position, tangent, binormal, normal);
  264. #endif
  265. # if USE_INSTANCED_PARTCLS
  266. mat3 model_tsr = mat3(a_part_ts[0], a_part_ts[1], a_part_ts[2],
  267. a_part_ts[3], a_part_r[0], a_part_r[1],
  268. a_part_r[2], a_part_r[3], 1.0);
  269. # if !STATIC_BATCH
  270. model_tsr = tsr_multiply(u_model_tsr, model_tsr);
  271. # endif
  272. # else
  273. # if !DYNAMIC_GRASS
  274. mat3 model_tsr = u_model_tsr;
  275. # endif
  276. # endif
  277. #if (WIND_BEND || DYNAMIC_GRASS || BILLBOARD) && !USE_INSTANCED_PARTCLS
  278. vec3 center = au_center_pos;
  279. #elif DYNAMIC_GRASS && USE_INSTANCED_PARTCLS
  280. vec3 center = a_part_ts.xyz;
  281. position = tsr9_transform(model_tsr, position);
  282. #else
  283. vec3 center = vec3(0.0);
  284. #endif
  285. #if USE_TBN_SHADING
  286. # if CALC_TBN
  287. vec3 norm_world = normalize(tsr9_transform_dir(model_tsr, norm_tbn));
  288. vec3 shade_binormal = cross(vec3(0.0, 0.0, 1.0), norm_world);
  289. vec3 shade_tangent = cross(norm_world, shade_binormal);
  290. # else
  291. vec3 shade_tangent = a_shade_tangs;
  292. # endif
  293. #else
  294. vec3 shade_tangent = vec3(0.0);
  295. #endif
  296. #if DYNAMIC_GRASS
  297. vertex world = grass_vertex(position, vec3(0.0), vec3(0.0), vec3(0.0), normal,
  298. center, u_grass_map_depth, u_grass_map_color, u_grass_map_dim,
  299. u_grass_size, u_camera_eye, u_camera_quat, view_tsr);
  300. #else
  301. # if BILLBOARD
  302. vec3 wcen = tsr9_transform(model_tsr, center);
  303. // NOTE: only for non-particles geometry on SHADOW_CAST subscene
  304. # if !HAIR_BILLBOARD && SHADOW_USAGE == SHADOW_CASTING
  305. mat3 bill_view_tsr = u_shadow_cast_billboard_view_tsr;
  306. # else
  307. mat3 bill_view_tsr = view_tsr;
  308. # endif
  309. # if BILLBOARD_PRES_GLOB_ORIENTATION && !STATIC_BATCH || USE_INSTANCED_PARTCLS
  310. model_tsr = billboard_tsr_global(u_camera_eye, wcen,
  311. bill_view_tsr, model_tsr);
  312. # else
  313. model_tsr = billboard_tsr(u_camera_eye, wcen, bill_view_tsr);
  314. # endif
  315. # if WIND_BEND && BILLBOARD_JITTERED
  316. model_tsr = bend_jitter_rotate_tsr(u_wind, u_time,
  317. u_jitter_amp, u_jitter_freq, wcen, model_tsr);
  318. # endif
  319. vertex world = to_world(position - center, center, vec3(0.0), shade_tangent,
  320. vec3(0.0), normal, model_tsr);
  321. world.center = wcen;
  322. # else
  323. vertex world = to_world(position, center, vec3(0.0), shade_tangent, vec3(0.0),
  324. normal, model_tsr);
  325. # endif
  326. #endif
  327. #if WIND_BEND
  328. # if MAIN_BEND_COL && DETAIL_BEND
  329. vec3 bend_normal = norm_tbn;
  330. # else
  331. vec3 bend_normal = vec3(0.0);
  332. # endif
  333. bend_vertex(world.position, world.center, bend_normal, mat4(0.0));
  334. #endif
  335. #if NODES && ALPHA
  336. v_pos_world = world.position;
  337. # if USE_NODE_MATERIAL_BEGIN || USE_NODE_GEOMETRY_NO || USE_NODE_NORMAL_MAP \
  338. || CAUSTICS || CALC_TBN_SPACE || WIND_BEND && MAIN_BEND_COL && DETAIL_BEND \
  339. || USE_NODE_TEX_COORD_NO || USE_NODE_BSDF_BEGIN || USE_NODE_FRESNEL \
  340. || USE_NODE_TEX_COORD_RE || USE_NODE_LAYER_WEIGHT || USE_NODE_BUMP
  341. v_normal = world.normal;
  342. # endif
  343. # if CALC_TBN_SPACE
  344. // calculate handedness as described in Math for 3D GP and CG, page 185
  345. float m = (dot(cross(world.normal, world.tangent),
  346. world.binormal) < 0.0) ? -1.0 : 1.0;
  347. v_tangent = vec4(world.tangent, m);
  348. # endif
  349. # if USE_TBN_SHADING
  350. v_shade_tang = world.shade_tang;
  351. # endif
  352. #endif // NODES && ALPHA
  353. vec4 pos_view = vec4(tsr9_transform(view_tsr, world.position), 1.0);
  354. vec4 pos_clip = u_proj_matrix * pos_view;
  355. #if SMAA_JITTER
  356. pos_clip.xy += u_subpixel_jitter * pos_clip.w;
  357. #endif
  358. #if NODES && ALPHA
  359. # if REFLECTION_TYPE == REFL_PLANE || USE_POSITION_CLIP
  360. v_tex_pos_clip = clip_to_tex(pos_clip);
  361. # endif
  362. // NOTE: this can never be, because refraction needs a blend material, which
  363. // doesn't receive shadows
  364. # if USE_NODE_B4W_REFRACTION && REFRACTIVE
  365. v_view_depth = -pos_view.z / u_view_max_depth;
  366. # endif
  367. nodes_main();
  368. #else
  369. # if TEXTURE_COLOR
  370. v_texcoord = scale_texcoord(a_texcoord, u_texture_scale);
  371. # endif
  372. #endif // NODES && ALPHA
  373. #if SHADOW_USAGE == SHADOW_MASK_GENERATION
  374. get_shadow_coords(world.position, world.normal);
  375. #endif
  376. #if SHADOW_USAGE == SHADOW_MASK_GENERATION || NODES && ALPHA
  377. v_pos_view = pos_view;
  378. #endif
  379. #if SHADOW_USAGE == SHADOW_CASTING
  380. // NOTE: shift coords to remove shadow map panning
  381. // NOTE: view_tsr[0] is world space origin translated into light space
  382. vec2 shift = (u_proj_matrix * vec4(view_tsr[0], 1.0)).xy;
  383. float half_tex_res = SHADOW_TEX_RES / 2.0;
  384. shift = floor(shift * half_tex_res + 0.5) / half_tex_res - shift;
  385. pos_clip.xy += shift;
  386. #endif
  387. gl_Position = pos_clip;
  388. }