particle_system.glslv 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #version GLSL_VERSION
  2. /*==============================================================================
  3. VARS
  4. ==============================================================================*/
  5. #var BILLBOARD_ALIGN BILLBOARD_ALIGN_VIEW
  6. #var TEXTURE_NORM_CO TEXTURE_COORDS_NONE
  7. #var CALC_TBN_SPACE 0
  8. #var USE_TBN_SHADING 0
  9. #var NODES 0
  10. #var HALO_PARTICLES 0
  11. #var TEXTURE_COLOR 0
  12. #var PARTICLES_SHADELESS 0
  13. #var DISABLE_FOG 0
  14. #var SOFT_PARTICLES 0
  15. #var COLOR_RAMP_LENGTH 0
  16. #var REFLECTION_PASS REFL_PASS_NONE
  17. #var WORLD_SPACE 0
  18. #var USE_COLOR_RAMP 0
  19. #var USE_POSITION_CLIP 0
  20. #var CAUSTICS 0
  21. #var WIND_BEND 0
  22. #var MAIN_BEND_COL 0
  23. #var DETAIL_BEND 0
  24. /*==============================================================================
  25. INCLUDES
  26. ==============================================================================*/
  27. #include <std.glsl>
  28. /*==============================================================================
  29. SHADER INTERFACE
  30. ==============================================================================*/
  31. GLSL_IN vec3 a_position;
  32. GLSL_IN vec4 a_tbn_quat;
  33. GLSL_IN vec3 a_p_data;
  34. GLSL_IN vec4 a_p_vels;
  35. GLSL_IN vec2 a_p_bb_vertex;
  36. //------------------------------------------------------------------------------
  37. #if !NODES
  38. # if !HALO_PARTICLES
  39. GLSL_OUT float v_alpha;
  40. # endif
  41. GLSL_OUT vec3 v_color;
  42. #endif
  43. #if TEXTURE_NORM_CO != TEXTURE_COORDS_NONE || CALC_TBN_SPACE || USE_TBN_SHADING
  44. GLSL_OUT vec4 v_tangent;
  45. #endif
  46. #if TEXTURE_COLOR || HALO_PARTICLES || USE_NODE_TEX_COORD_UV || USE_NODE_UV_MERGED \
  47. || USE_NODE_UVMAP || USE_NODE_GEOMETRY_UV || USE_NODE_GEOMETRY_OR \
  48. || USE_NODE_TEX_COORD_GE
  49. GLSL_OUT vec2 v_texcoord;
  50. #endif
  51. GLSL_OUT vec3 v_pos_world;
  52. #if !PARTICLES_SHADELESS || !DISABLE_FOG
  53. GLSL_OUT vec3 v_eye_dir;
  54. #endif
  55. #if SOFT_PARTICLES || !DISABLE_FOG || NODES
  56. GLSL_OUT vec4 v_pos_view;
  57. #endif
  58. #if SOFT_PARTICLES || USE_POSITION_CLIP
  59. GLSL_OUT vec3 v_tex_pos_clip;
  60. #endif
  61. #if NODES
  62. GLSL_OUT vec3 v_normal;
  63. #endif
  64. #if HALO_PARTICLES
  65. GLSL_OUT float v_vertex_random;
  66. #endif
  67. /*==============================================================================
  68. UNIFORMS
  69. ==============================================================================*/
  70. #if COLOR_RAMP_LENGTH > 0
  71. uniform vec4 u_p_color_ramp[COLOR_RAMP_LENGTH];
  72. #endif
  73. uniform float u_p_time;
  74. uniform float u_p_length;
  75. uniform int u_p_cyclic;
  76. uniform float u_p_fade_in;
  77. uniform float u_p_fade_out;
  78. uniform float u_p_nfactor;
  79. uniform float u_p_gravity;
  80. uniform float u_p_mass;
  81. uniform float u_p_wind_fac;
  82. uniform float u_p_max_lifetime;
  83. uniform float u_p_tilt;
  84. uniform float u_p_tilt_rand;
  85. #if REFLECTION_PASS == REFL_PASS_PLANE
  86. uniform mat4 u_view_refl_matrix;
  87. #endif
  88. uniform mat3 u_view_tsr;
  89. uniform mat4 u_proj_matrix;
  90. uniform vec3 u_wind;
  91. uniform float u_p_size;
  92. uniform vec3 u_camera_eye;
  93. #if !WORLD_SPACE || NODES || HALO_PARTICLES
  94. uniform mat3 u_model_tsr;
  95. #endif
  96. #if USE_COLOR_RAMP
  97. uniform sampler2D u_color_ramp_tex;
  98. #endif
  99. /*==============================================================================
  100. INCLUDES
  101. ==============================================================================*/
  102. #include <math.glslv>
  103. #include <to_world.glslv>
  104. #include <particles.glslv>
  105. #if NODES
  106. #include <particles_nodes.glslv>
  107. #endif
  108. // 1->2
  109. float vec_vec_angle(vec2 v1, vec2 v2) {
  110. return (atan(v2.y, v2.x) - atan(v1.y, v1.x));
  111. }
  112. /*==============================================================================
  113. MAIN
  114. ==============================================================================*/
  115. void main(void) {
  116. #if REFLECTION_PASS == REFL_PASS_PLANE
  117. mat4 view_refl_matrix = u_view_refl_matrix;
  118. #else
  119. mat4 view_refl_matrix = mat4(0.0);
  120. #endif
  121. mat3 view_tsr = u_view_tsr;
  122. part_params pp;
  123. pp = calc_part_params();
  124. float rotation_angle;
  125. mat3 bb_tsr = tsr_identity();
  126. #if REFLECTION_PASS == REFL_PASS_PLANE
  127. vec3 c_pos = (view_refl_matrix * vec4(pp.position, 1.0)).xyz;
  128. #else
  129. vec3 c_pos = pp.position;
  130. #endif
  131. vec3 normal = qrot(a_tbn_quat, vec3(0.0, 1.0, 0.0));
  132. if (BILLBOARD_ALIGN == BILLBOARD_ALIGN_VIEW) {
  133. rotation_angle = pp.angle;
  134. // NOTE: there is no simple way to extract camera eye from view matrix
  135. bb_tsr = billboard_spherical(c_pos, view_tsr);
  136. } else if (BILLBOARD_ALIGN == BILLBOARD_ALIGN_XY) {
  137. // NOTE: scattering only in horizontal space
  138. rotation_angle = vec_vec_angle(vec2(EPSILON, 1.0), vec2(normal.x,
  139. -normal.y)) + pp.angle;
  140. bb_tsr[0] = c_pos;
  141. vec4 quat_right = qsetAxisAngle(RIGHT_VECTOR, -M_PI/2.0);
  142. bb_tsr = tsr_set_quat(quat_right, bb_tsr);
  143. } else if (BILLBOARD_ALIGN == BILLBOARD_ALIGN_YZ) {
  144. rotation_angle = pp.angle;
  145. bb_tsr[0] = c_pos;
  146. vec4 quat_up = qsetAxisAngle(UP_VECTOR, M_PI/2.0);
  147. bb_tsr = tsr_set_quat(quat_up, bb_tsr);
  148. } else if (BILLBOARD_ALIGN == BILLBOARD_ALIGN_ZX) {
  149. rotation_angle = pp.angle;
  150. bb_tsr[0] = c_pos;
  151. }
  152. vec3 pos_local = vec3((a_p_bb_vertex[0] * 2.0 - 1.0) * pp.size * u_p_size, 0.0,
  153. (a_p_bb_vertex[1] * 2.0 - 1.0) * pp.size * u_p_size);
  154. float bb_random_val = a_p_data[2];
  155. float random_tilt = u_p_tilt * u_p_tilt_rand * (2.0 * bb_random_val - 1.0);
  156. float init_rot_angle = (random_tilt + u_p_tilt) * M_PI;
  157. vec4 quat_tow = qsetAxisAngle(TOWARD_VECTOR, init_rot_angle + rotation_angle);
  158. vec3 pos_world = tsr9_transform(bb_tsr, qrot(quat_tow, pos_local));
  159. vec4 pos_view = vec4(tsr9_transform(view_tsr, pos_world), 1.0);
  160. vec4 pos_clip = u_proj_matrix * pos_view;
  161. #if HALO_PARTICLES
  162. v_vertex_random = bb_random_val;
  163. #endif
  164. #if NODES
  165. # if CALC_TBN_SPACE || !NODES && TEXTURE_NORM_CO == TEXTURE_COORDS_UV_ORCO \
  166. || USE_TBN_SHADING
  167. vec3 tangent = qrot(a_tbn_quat, vec3(1.0, 0.0, 0.0));
  168. vec3 binormal = sign(a_tbn_quat[3]) * cross(normal, tangent);
  169. # else
  170. vec3 tangent = vec3(0.0);
  171. vec3 binormal = vec3(0.0);
  172. # endif
  173. mat3 tsr_rotz = tsr_identity();
  174. quat_tow = qsetAxisAngle(TOWARD_VECTOR, rotation_angle);
  175. tsr_rotz = tsr_set_quat(quat_tow, tsr_rotz);
  176. vertex world = to_world(vec3(0.0), vec3(0.0), tangent, vec3(0.0), binormal,
  177. vec3(0.0), tsr_multiply(bb_tsr, tsr_rotz));
  178. # if TEXTURE_NORM_CO != TEXTURE_COORDS_NONE || CALC_TBN_SPACE || USE_TBN_SHADING
  179. // calculate handedness as described in Math for 3D GP and CG, page 185
  180. float m = (dot(cross(normal, world.tangent),
  181. world.binormal) < 0.0) ? -1.0 : 1.0;
  182. v_tangent = vec4(world.tangent, m);
  183. # endif
  184. # if !NODES || USE_NODE_MATERIAL_BEGIN || USE_NODE_GEOMETRY_NO \
  185. || CAUSTICS || CALC_TBN_SPACE || WIND_BEND && MAIN_BEND_COL && DETAIL_BEND \
  186. || USE_NODE_TEX_COORD_NO || USE_NODE_BSDF_BEGIN || USE_NODE_FRESNEL \
  187. || USE_NODE_TEX_COORD_RE || USE_NODE_LAYER_WEIGHT
  188. v_normal = normal;
  189. # endif
  190. nodes_main(pp.position, pp.velocity, pp.ang_velocity, pp.age, pp.size,
  191. a_p_bb_vertex, a_p_data[0]);
  192. #else //NODES
  193. # if TEXTURE_COLOR
  194. v_texcoord = a_p_bb_vertex;
  195. # endif
  196. # if HALO_PARTICLES
  197. v_texcoord = a_p_bb_vertex * 2.0 - 1.0;
  198. # else
  199. v_alpha = pp.alpha;
  200. # endif
  201. v_color = pp.color;
  202. #endif //NODES
  203. #if SOFT_PARTICLES || USE_POSITION_CLIP
  204. v_tex_pos_clip = clip_to_tex(pos_clip);
  205. #endif
  206. #if SOFT_PARTICLES || !DISABLE_FOG || NODES
  207. v_pos_view = pos_view;
  208. #endif
  209. gl_Position = pos_clip;
  210. v_pos_world = pos_world;
  211. #if !PARTICLES_SHADELESS || !DISABLE_FOG
  212. v_eye_dir = u_camera_eye - pos_world;
  213. #endif
  214. }