main.glslv 14 KB

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