particles_material.cpp 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /*************************************************************************/
  2. /* particles_material.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "particles_material.h"
  31. Mutex *ParticlesMaterial::material_mutex = NULL;
  32. SelfList<ParticlesMaterial>::List *ParticlesMaterial::dirty_materials = NULL;
  33. Map<ParticlesMaterial::MaterialKey, ParticlesMaterial::ShaderData> ParticlesMaterial::shader_map;
  34. ParticlesMaterial::ShaderNames *ParticlesMaterial::shader_names = NULL;
  35. void ParticlesMaterial::init_shaders() {
  36. #ifndef NO_THREADS
  37. material_mutex = Mutex::create();
  38. #endif
  39. dirty_materials = memnew(SelfList<ParticlesMaterial>::List);
  40. shader_names = memnew(ShaderNames);
  41. shader_names->direction = "direction";
  42. shader_names->spread = "spread";
  43. shader_names->flatness = "flatness";
  44. shader_names->initial_linear_velocity = "initial_linear_velocity";
  45. shader_names->initial_angle = "initial_angle";
  46. shader_names->angular_velocity = "angular_velocity";
  47. shader_names->orbit_velocity = "orbit_velocity";
  48. shader_names->linear_accel = "linear_accel";
  49. shader_names->radial_accel = "radial_accel";
  50. shader_names->tangent_accel = "tangent_accel";
  51. shader_names->damping = "damping";
  52. shader_names->scale = "scale";
  53. shader_names->hue_variation = "hue_variation";
  54. shader_names->anim_speed = "anim_speed";
  55. shader_names->anim_offset = "anim_offset";
  56. shader_names->initial_linear_velocity_random = "initial_linear_velocity_random";
  57. shader_names->initial_angle_random = "initial_angle_random";
  58. shader_names->angular_velocity_random = "angular_velocity_random";
  59. shader_names->orbit_velocity_random = "orbit_velocity_random";
  60. shader_names->linear_accel_random = "linear_accel_random";
  61. shader_names->radial_accel_random = "radial_accel_random";
  62. shader_names->tangent_accel_random = "tangent_accel_random";
  63. shader_names->damping_random = "damping_random";
  64. shader_names->scale_random = "scale_random";
  65. shader_names->hue_variation_random = "hue_variation_random";
  66. shader_names->anim_speed_random = "anim_speed_random";
  67. shader_names->anim_offset_random = "anim_offset_random";
  68. shader_names->angle_texture = "angle_texture";
  69. shader_names->angular_velocity_texture = "angular_velocity_texture";
  70. shader_names->orbit_velocity_texture = "orbit_velocity_texture";
  71. shader_names->linear_accel_texture = "linear_accel_texture";
  72. shader_names->radial_accel_texture = "radial_accel_texture";
  73. shader_names->tangent_accel_texture = "tangent_accel_texture";
  74. shader_names->damping_texture = "damping_texture";
  75. shader_names->scale_texture = "scale_texture";
  76. shader_names->hue_variation_texture = "hue_variation_texture";
  77. shader_names->anim_speed_texture = "anim_speed_texture";
  78. shader_names->anim_offset_texture = "anim_offset_texture";
  79. shader_names->color = "color_value";
  80. shader_names->color_ramp = "color_ramp";
  81. shader_names->emission_sphere_radius = "emission_sphere_radius";
  82. shader_names->emission_box_extents = "emission_box_extents";
  83. shader_names->emission_texture_point_count = "emission_texture_point_count";
  84. shader_names->emission_texture_points = "emission_texture_points";
  85. shader_names->emission_texture_normal = "emission_texture_normal";
  86. shader_names->emission_texture_color = "emission_texture_color";
  87. shader_names->trail_divisor = "trail_divisor";
  88. shader_names->trail_size_modifier = "trail_size_modifier";
  89. shader_names->trail_color_modifier = "trail_color_modifier";
  90. shader_names->gravity = "gravity";
  91. shader_names->lifetime_randomness = "lifetime_randomness";
  92. }
  93. void ParticlesMaterial::finish_shaders() {
  94. #ifndef NO_THREADS
  95. memdelete(material_mutex);
  96. #endif
  97. memdelete(dirty_materials);
  98. dirty_materials = NULL;
  99. memdelete(shader_names);
  100. }
  101. void ParticlesMaterial::_update_shader() {
  102. dirty_materials->remove(&element);
  103. MaterialKey mk = _compute_key();
  104. if (mk.key == current_key.key)
  105. return; //no update required in the end
  106. if (shader_map.has(current_key)) {
  107. shader_map[current_key].users--;
  108. if (shader_map[current_key].users == 0) {
  109. //deallocate shader, as it's no longer in use
  110. VS::get_singleton()->free(shader_map[current_key].shader);
  111. shader_map.erase(current_key);
  112. }
  113. }
  114. current_key = mk;
  115. if (shader_map.has(mk)) {
  116. VS::get_singleton()->material_set_shader(_get_material(), shader_map[mk].shader);
  117. shader_map[mk].users++;
  118. return;
  119. }
  120. //must create a shader!
  121. String code = "shader_type particles;\n";
  122. code += "uniform vec3 direction;\n";
  123. code += "uniform float spread;\n";
  124. code += "uniform float flatness;\n";
  125. code += "uniform float initial_linear_velocity;\n";
  126. code += "uniform float initial_angle;\n";
  127. code += "uniform float angular_velocity;\n";
  128. code += "uniform float orbit_velocity;\n";
  129. code += "uniform float linear_accel;\n";
  130. code += "uniform float radial_accel;\n";
  131. code += "uniform float tangent_accel;\n";
  132. code += "uniform float damping;\n";
  133. code += "uniform float scale;\n";
  134. code += "uniform float hue_variation;\n";
  135. code += "uniform float anim_speed;\n";
  136. code += "uniform float anim_offset;\n";
  137. code += "uniform float initial_linear_velocity_random;\n";
  138. code += "uniform float initial_angle_random;\n";
  139. code += "uniform float angular_velocity_random;\n";
  140. code += "uniform float orbit_velocity_random;\n";
  141. code += "uniform float linear_accel_random;\n";
  142. code += "uniform float radial_accel_random;\n";
  143. code += "uniform float tangent_accel_random;\n";
  144. code += "uniform float damping_random;\n";
  145. code += "uniform float scale_random;\n";
  146. code += "uniform float hue_variation_random;\n";
  147. code += "uniform float anim_speed_random;\n";
  148. code += "uniform float anim_offset_random;\n";
  149. code += "uniform float lifetime_randomness;\n";
  150. switch (emission_shape) {
  151. case EMISSION_SHAPE_POINT: {
  152. //do none
  153. } break;
  154. case EMISSION_SHAPE_SPHERE: {
  155. code += "uniform float emission_sphere_radius;\n";
  156. } break;
  157. case EMISSION_SHAPE_BOX: {
  158. code += "uniform vec3 emission_box_extents;\n";
  159. } break;
  160. case EMISSION_SHAPE_DIRECTED_POINTS: {
  161. code += "uniform sampler2D emission_texture_normal : hint_black;\n";
  162. FALLTHROUGH;
  163. }
  164. case EMISSION_SHAPE_POINTS: {
  165. code += "uniform sampler2D emission_texture_points : hint_black;\n";
  166. code += "uniform int emission_texture_point_count;\n";
  167. if (emission_color_texture.is_valid()) {
  168. code += "uniform sampler2D emission_texture_color : hint_white;\n";
  169. }
  170. } break;
  171. case EMISSION_SHAPE_MAX: { // Max value for validity check.
  172. break;
  173. }
  174. }
  175. code += "uniform vec4 color_value : hint_color;\n";
  176. code += "uniform int trail_divisor;\n";
  177. code += "uniform vec3 gravity;\n";
  178. if (color_ramp.is_valid())
  179. code += "uniform sampler2D color_ramp;\n";
  180. if (tex_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid())
  181. code += "uniform sampler2D linear_velocity_texture;\n";
  182. if (tex_parameters[PARAM_ORBIT_VELOCITY].is_valid())
  183. code += "uniform sampler2D orbit_velocity_texture;\n";
  184. if (tex_parameters[PARAM_ANGULAR_VELOCITY].is_valid())
  185. code += "uniform sampler2D angular_velocity_texture;\n";
  186. if (tex_parameters[PARAM_LINEAR_ACCEL].is_valid())
  187. code += "uniform sampler2D linear_accel_texture;\n";
  188. if (tex_parameters[PARAM_RADIAL_ACCEL].is_valid())
  189. code += "uniform sampler2D radial_accel_texture;\n";
  190. if (tex_parameters[PARAM_TANGENTIAL_ACCEL].is_valid())
  191. code += "uniform sampler2D tangent_accel_texture;\n";
  192. if (tex_parameters[PARAM_DAMPING].is_valid())
  193. code += "uniform sampler2D damping_texture;\n";
  194. if (tex_parameters[PARAM_ANGLE].is_valid())
  195. code += "uniform sampler2D angle_texture;\n";
  196. if (tex_parameters[PARAM_SCALE].is_valid())
  197. code += "uniform sampler2D scale_texture;\n";
  198. if (tex_parameters[PARAM_HUE_VARIATION].is_valid())
  199. code += "uniform sampler2D hue_variation_texture;\n";
  200. if (tex_parameters[PARAM_ANIM_SPEED].is_valid())
  201. code += "uniform sampler2D anim_speed_texture;\n";
  202. if (tex_parameters[PARAM_ANIM_OFFSET].is_valid())
  203. code += "uniform sampler2D anim_offset_texture;\n";
  204. if (trail_size_modifier.is_valid()) {
  205. code += "uniform sampler2D trail_size_modifier;\n";
  206. }
  207. if (trail_color_modifier.is_valid()) {
  208. code += "uniform sampler2D trail_color_modifier;\n";
  209. }
  210. //need a random function
  211. code += "\n\n";
  212. code += "float rand_from_seed(inout uint seed) {\n";
  213. code += " int k;\n";
  214. code += " int s = int(seed);\n";
  215. code += " if (s == 0)\n";
  216. code += " s = 305420679;\n";
  217. code += " k = s / 127773;\n";
  218. code += " s = 16807 * (s - k * 127773) - 2836 * k;\n";
  219. code += " if (s < 0)\n";
  220. code += " s += 2147483647;\n";
  221. code += " seed = uint(s);\n";
  222. code += " return float(seed % uint(65536)) / 65535.0;\n";
  223. code += "}\n";
  224. code += "\n";
  225. code += "float rand_from_seed_m1_p1(inout uint seed) {\n";
  226. code += " return rand_from_seed(seed) * 2.0 - 1.0;\n";
  227. code += "}\n";
  228. code += "\n";
  229. //improve seed quality
  230. code += "uint hash(uint x) {\n";
  231. code += " x = ((x >> uint(16)) ^ x) * uint(73244475);\n";
  232. code += " x = ((x >> uint(16)) ^ x) * uint(73244475);\n";
  233. code += " x = (x >> uint(16)) ^ x;\n";
  234. code += " return x;\n";
  235. code += "}\n";
  236. code += "\n";
  237. code += "void vertex() {\n";
  238. code += " uint base_number = NUMBER / uint(trail_divisor);\n";
  239. code += " uint alt_seed = hash(base_number + uint(1) + RANDOM_SEED);\n";
  240. code += " float angle_rand = rand_from_seed(alt_seed);\n";
  241. code += " float scale_rand = rand_from_seed(alt_seed);\n";
  242. code += " float hue_rot_rand = rand_from_seed(alt_seed);\n";
  243. code += " float anim_offset_rand = rand_from_seed(alt_seed);\n";
  244. code += " float pi = 3.14159;\n";
  245. code += " float degree_to_rad = pi / 180.0;\n";
  246. code += "\n";
  247. if (emission_shape == EMISSION_SHAPE_POINTS || emission_shape == EMISSION_SHAPE_DIRECTED_POINTS) {
  248. code += " int point = min(emission_texture_point_count - 1, int(rand_from_seed(alt_seed) * float(emission_texture_point_count)));\n";
  249. code += " ivec2 emission_tex_size = textureSize(emission_texture_points, 0);\n";
  250. code += " ivec2 emission_tex_ofs = ivec2(point % emission_tex_size.x, point / emission_tex_size.x);\n";
  251. }
  252. code += " bool restart = false;\n";
  253. code += " if (CUSTOM.y > CUSTOM.w) {\n";
  254. code += " restart = true;\n";
  255. code += " }\n\n";
  256. code += " if (RESTART || restart) {\n";
  257. if (tex_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid())
  258. code += " float tex_linear_velocity = textureLod(linear_velocity_texture, vec2(0.0, 0.0), 0.0).r;\n";
  259. else
  260. code += " float tex_linear_velocity = 0.0;\n";
  261. if (tex_parameters[PARAM_ANGLE].is_valid())
  262. code += " float tex_angle = textureLod(angle_texture, vec2(0.0, 0.0), 0.0).r;\n";
  263. else
  264. code += " float tex_angle = 0.0;\n";
  265. if (tex_parameters[PARAM_ANIM_OFFSET].is_valid())
  266. code += " float tex_anim_offset = textureLod(anim_offset_texture, vec2(0.0, 0.0), 0.0).r;\n";
  267. else
  268. code += " float tex_anim_offset = 0.0;\n";
  269. code += " float spread_rad = spread * degree_to_rad;\n";
  270. if (flags[FLAG_DISABLE_Z]) {
  271. code += " float angle1_rad = rand_from_seed_m1_p1(alt_seed) * spread_rad;\n";
  272. code += " angle1_rad += direction.x != 0.0 ? atan(direction.y, direction.x) : sign(direction.y) * (pi / 2.0);\n";
  273. code += " vec3 rot = vec3(cos(angle1_rad), sin(angle1_rad), 0.0);\n";
  274. code += " VELOCITY = rot * initial_linear_velocity * mix(1.0, rand_from_seed(alt_seed), initial_linear_velocity_random);\n";
  275. } else {
  276. //initiate velocity spread in 3D
  277. code += " float angle1_rad = rand_from_seed_m1_p1(alt_seed) * spread_rad;\n";
  278. code += " float angle2_rad = rand_from_seed_m1_p1(alt_seed) * spread_rad * (1.0 - flatness);\n";
  279. code += " angle1_rad += direction.z != 0.0 ? atan(direction.x, direction.z) : sign(direction.x) * (pi / 2.0);\n";
  280. code += " angle2_rad += direction.z != 0.0 ? atan(direction.y, abs(direction.z)) : (direction.x != 0.0 ? atan(direction.y, abs(direction.x)) : sign(direction.y) * (pi / 2.0));\n";
  281. code += " vec3 direction_xz = vec3(sin(angle1_rad), 0.0, cos(angle1_rad));\n";
  282. code += " vec3 direction_yz = vec3(0.0, sin(angle2_rad), cos(angle2_rad));\n";
  283. code += " direction_yz.z = direction_yz.z / max(0.0001,sqrt(abs(direction_yz.z))); // better uniform distribution\n";
  284. code += " vec3 vec_direction = vec3(direction_xz.x * direction_yz.z, direction_yz.y, direction_xz.z * direction_yz.z);\n";
  285. code += " vec_direction = normalize(vec_direction);\n";
  286. code += " VELOCITY = vec_direction * initial_linear_velocity * mix(1.0, rand_from_seed(alt_seed), initial_linear_velocity_random);\n";
  287. }
  288. code += " float base_angle = (initial_angle + tex_angle) * mix(1.0, angle_rand, initial_angle_random);\n";
  289. code += " CUSTOM.x = base_angle * degree_to_rad;\n"; // angle
  290. code += " CUSTOM.y = 0.0;\n"; // phase
  291. code += " CUSTOM.w = (1.0 - lifetime_randomness * rand_from_seed(alt_seed));\n";
  292. code += " CUSTOM.z = (anim_offset + tex_anim_offset) * mix(1.0, anim_offset_rand, anim_offset_random);\n"; // animation offset (0-1)
  293. switch (emission_shape) {
  294. case EMISSION_SHAPE_POINT: {
  295. //do none
  296. } break;
  297. case EMISSION_SHAPE_SPHERE: {
  298. code += " float s = rand_from_seed(alt_seed) * 2.0 - 1.0;\n";
  299. code += " float t = rand_from_seed(alt_seed) * 2.0 * pi;\n";
  300. code += " float radius = emission_sphere_radius * sqrt(1.0 - s * s);\n";
  301. code += " TRANSFORM[3].xyz = vec3(radius * cos(t), radius * sin(t), emission_sphere_radius * s);\n";
  302. } break;
  303. case EMISSION_SHAPE_BOX: {
  304. code += " TRANSFORM[3].xyz = vec3(rand_from_seed(alt_seed) * 2.0 - 1.0, rand_from_seed(alt_seed) * 2.0 - 1.0, rand_from_seed(alt_seed) * 2.0 - 1.0) * emission_box_extents;\n";
  305. } break;
  306. case EMISSION_SHAPE_POINTS:
  307. case EMISSION_SHAPE_DIRECTED_POINTS: {
  308. code += " TRANSFORM[3].xyz = texelFetch(emission_texture_points, emission_tex_ofs, 0).xyz;\n";
  309. if (emission_shape == EMISSION_SHAPE_DIRECTED_POINTS) {
  310. if (flags[FLAG_DISABLE_Z]) {
  311. code += " mat2 rotm;";
  312. code += " rotm[0] = texelFetch(emission_texture_normal, emission_tex_ofs, 0).xy;\n";
  313. code += " rotm[1] = rotm[0].yx * vec2(1.0, -1.0);\n";
  314. code += " VELOCITY.xy = rotm * VELOCITY.xy;\n";
  315. } else {
  316. code += " vec3 normal = texelFetch(emission_texture_normal, emission_tex_ofs, 0).xyz;\n";
  317. code += " vec3 v0 = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);\n";
  318. code += " vec3 tangent = normalize(cross(v0, normal));\n";
  319. code += " vec3 bitangent = normalize(cross(tangent, normal));\n";
  320. code += " VELOCITY = mat3(tangent, bitangent, normal) * VELOCITY;\n";
  321. }
  322. }
  323. } break;
  324. case EMISSION_SHAPE_MAX: { // Max value for validity check.
  325. break;
  326. }
  327. }
  328. code += " VELOCITY = (EMISSION_TRANSFORM * vec4(VELOCITY, 0.0)).xyz;\n";
  329. code += " TRANSFORM = EMISSION_TRANSFORM * TRANSFORM;\n";
  330. if (flags[FLAG_DISABLE_Z]) {
  331. code += " VELOCITY.z = 0.0;\n";
  332. code += " TRANSFORM[3].z = 0.0;\n";
  333. }
  334. code += " } else {\n";
  335. code += " CUSTOM.y += DELTA / LIFETIME;\n";
  336. if (tex_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid())
  337. code += " float tex_linear_velocity = textureLod(linear_velocity_texture, vec2(CUSTOM.y, 0.0), 0.0).r;\n";
  338. else
  339. code += " float tex_linear_velocity = 0.0;\n";
  340. if (flags[FLAG_DISABLE_Z]) {
  341. if (tex_parameters[PARAM_ORBIT_VELOCITY].is_valid())
  342. code += " float tex_orbit_velocity = textureLod(orbit_velocity_texture, vec2(CUSTOM.y, 0.0), 0.0).r;\n";
  343. else
  344. code += " float tex_orbit_velocity = 0.0;\n";
  345. }
  346. if (tex_parameters[PARAM_ANGULAR_VELOCITY].is_valid())
  347. code += " float tex_angular_velocity = textureLod(angular_velocity_texture, vec2(CUSTOM.y, 0.0), 0.0).r;\n";
  348. else
  349. code += " float tex_angular_velocity = 0.0;\n";
  350. if (tex_parameters[PARAM_LINEAR_ACCEL].is_valid())
  351. code += " float tex_linear_accel = textureLod(linear_accel_texture, vec2(CUSTOM.y, 0.0), 0.0).r;\n";
  352. else
  353. code += " float tex_linear_accel = 0.0;\n";
  354. if (tex_parameters[PARAM_RADIAL_ACCEL].is_valid())
  355. code += " float tex_radial_accel = textureLod(radial_accel_texture, vec2(CUSTOM.y, 0.0), 0.0).r;\n";
  356. else
  357. code += " float tex_radial_accel = 0.0;\n";
  358. if (tex_parameters[PARAM_TANGENTIAL_ACCEL].is_valid())
  359. code += " float tex_tangent_accel = textureLod(tangent_accel_texture, vec2(CUSTOM.y, 0.0), 0.0).r;\n";
  360. else
  361. code += " float tex_tangent_accel = 0.0;\n";
  362. if (tex_parameters[PARAM_DAMPING].is_valid())
  363. code += " float tex_damping = textureLod(damping_texture, vec2(CUSTOM.y, 0.0), 0.0).r;\n";
  364. else
  365. code += " float tex_damping = 0.0;\n";
  366. if (tex_parameters[PARAM_ANGLE].is_valid())
  367. code += " float tex_angle = textureLod(angle_texture, vec2(CUSTOM.y, 0.0), 0.0).r;\n";
  368. else
  369. code += " float tex_angle = 0.0;\n";
  370. if (tex_parameters[PARAM_ANIM_SPEED].is_valid())
  371. code += " float tex_anim_speed = textureLod(anim_speed_texture, vec2(CUSTOM.y, 0.0), 0.0).r;\n";
  372. else
  373. code += " float tex_anim_speed = 0.0;\n";
  374. if (tex_parameters[PARAM_ANIM_OFFSET].is_valid())
  375. code += " float tex_anim_offset = textureLod(anim_offset_texture, vec2(CUSTOM.y, 0.0), 0.0).r;\n";
  376. else
  377. code += " float tex_anim_offset = 0.0;\n";
  378. code += " vec3 force = gravity;\n";
  379. code += " vec3 pos = TRANSFORM[3].xyz;\n";
  380. if (flags[FLAG_DISABLE_Z]) {
  381. code += " pos.z = 0.0;\n";
  382. }
  383. code += " // apply linear acceleration\n";
  384. code += " force += length(VELOCITY) > 0.0 ? normalize(VELOCITY) * (linear_accel + tex_linear_accel) * mix(1.0, rand_from_seed(alt_seed), linear_accel_random) : vec3(0.0);\n";
  385. code += " // apply radial acceleration\n";
  386. code += " vec3 org = EMISSION_TRANSFORM[3].xyz;\n";
  387. code += " vec3 diff = pos - org;\n";
  388. code += " force += length(diff) > 0.0 ? normalize(diff) * (radial_accel + tex_radial_accel) * mix(1.0, rand_from_seed(alt_seed), radial_accel_random) : vec3(0.0);\n";
  389. code += " // apply tangential acceleration;\n";
  390. if (flags[FLAG_DISABLE_Z]) {
  391. code += " force += length(diff.yx) > 0.0 ? vec3(normalize(diff.yx * vec2(-1.0, 1.0)), 0.0) * ((tangent_accel + tex_tangent_accel) * mix(1.0, rand_from_seed(alt_seed), tangent_accel_random)) : vec3(0.0);\n";
  392. } else {
  393. code += " vec3 crossDiff = cross(normalize(diff), normalize(gravity));\n";
  394. code += " force += length(crossDiff) > 0.0 ? normalize(crossDiff) * ((tangent_accel + tex_tangent_accel) * mix(1.0, rand_from_seed(alt_seed), tangent_accel_random)) : vec3(0.0);\n";
  395. }
  396. code += " // apply attractor forces\n";
  397. code += " VELOCITY += force * DELTA;\n";
  398. code += " // orbit velocity\n";
  399. if (flags[FLAG_DISABLE_Z]) {
  400. code += " float orbit_amount = (orbit_velocity + tex_orbit_velocity) * mix(1.0, rand_from_seed(alt_seed), orbit_velocity_random);\n";
  401. code += " if (orbit_amount != 0.0) {\n";
  402. code += " float ang = orbit_amount * DELTA * pi * 2.0;\n";
  403. code += " mat2 rot = mat2(vec2(cos(ang), -sin(ang)), vec2(sin(ang), cos(ang)));\n";
  404. code += " TRANSFORM[3].xy -= diff.xy;\n";
  405. code += " TRANSFORM[3].xy += rot * diff.xy;\n";
  406. code += " }\n";
  407. }
  408. if (tex_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) {
  409. code += " VELOCITY = normalize(VELOCITY) * tex_linear_velocity;\n";
  410. }
  411. code += " if (damping + tex_damping > 0.0) {\n";
  412. code += " float v = length(VELOCITY);\n";
  413. code += " float damp = (damping + tex_damping) * mix(1.0, rand_from_seed(alt_seed), damping_random);\n";
  414. code += " v -= damp * DELTA;\n";
  415. code += " if (v < 0.0) {\n";
  416. code += " VELOCITY = vec3(0.0);\n";
  417. code += " } else {\n";
  418. code += " VELOCITY = normalize(VELOCITY) * v;\n";
  419. code += " }\n";
  420. code += " }\n";
  421. code += " float base_angle = (initial_angle + tex_angle) * mix(1.0, angle_rand, initial_angle_random);\n";
  422. code += " base_angle += CUSTOM.y * LIFETIME * (angular_velocity + tex_angular_velocity) * mix(1.0, rand_from_seed(alt_seed) * 2.0 - 1.0, angular_velocity_random);\n";
  423. code += " CUSTOM.x = base_angle * degree_to_rad;\n"; // angle
  424. code += " CUSTOM.z = (anim_offset + tex_anim_offset) * mix(1.0, anim_offset_rand, anim_offset_random) + CUSTOM.y * (anim_speed + tex_anim_speed) * mix(1.0, rand_from_seed(alt_seed), anim_speed_random);\n"; // angle
  425. code += " }\n";
  426. // apply color
  427. // apply hue rotation
  428. if (tex_parameters[PARAM_SCALE].is_valid())
  429. code += " float tex_scale = textureLod(scale_texture, vec2(CUSTOM.y, 0.0), 0.0).r;\n";
  430. else
  431. code += " float tex_scale = 1.0;\n";
  432. if (tex_parameters[PARAM_HUE_VARIATION].is_valid())
  433. code += " float tex_hue_variation = textureLod(hue_variation_texture, vec2(CUSTOM.y, 0.0), 0.0).r;\n";
  434. else
  435. code += " float tex_hue_variation = 0.0;\n";
  436. code += " float hue_rot_angle = (hue_variation + tex_hue_variation) * pi * 2.0 * mix(1.0, hue_rot_rand * 2.0 - 1.0, hue_variation_random);\n";
  437. code += " float hue_rot_c = cos(hue_rot_angle);\n";
  438. code += " float hue_rot_s = sin(hue_rot_angle);\n";
  439. code += " mat4 hue_rot_mat = mat4(vec4(0.299, 0.587, 0.114, 0.0),\n";
  440. code += " vec4(0.299, 0.587, 0.114, 0.0),\n";
  441. code += " vec4(0.299, 0.587, 0.114, 0.0),\n";
  442. code += " vec4(0.000, 0.000, 0.000, 1.0)) +\n";
  443. code += " mat4(vec4(0.701, -0.587, -0.114, 0.0),\n";
  444. code += " vec4(-0.299, 0.413, -0.114, 0.0),\n";
  445. code += " vec4(-0.300, -0.588, 0.886, 0.0),\n";
  446. code += " vec4(0.000, 0.000, 0.000, 0.0)) * hue_rot_c +\n";
  447. code += " mat4(vec4(0.168, 0.330, -0.497, 0.0),\n";
  448. code += " vec4(-0.328, 0.035, 0.292, 0.0),\n";
  449. code += " vec4(1.250, -1.050, -0.203, 0.0),\n";
  450. code += " vec4(0.000, 0.000, 0.000, 0.0)) * hue_rot_s;\n";
  451. if (color_ramp.is_valid()) {
  452. code += " COLOR = hue_rot_mat * textureLod(color_ramp, vec2(CUSTOM.y, 0.0), 0.0);\n";
  453. } else {
  454. code += " COLOR = hue_rot_mat * color_value;\n";
  455. }
  456. if (emission_color_texture.is_valid() && (emission_shape == EMISSION_SHAPE_POINTS || emission_shape == EMISSION_SHAPE_DIRECTED_POINTS)) {
  457. code += " COLOR *= texelFetch(emission_texture_color, emission_tex_ofs, 0);\n";
  458. }
  459. if (trail_color_modifier.is_valid()) {
  460. code += " if (trail_divisor > 1) {\n";
  461. code += " COLOR *= textureLod(trail_color_modifier, vec2(float(int(NUMBER) % trail_divisor) / float(trail_divisor - 1), 0.0), 0.0);\n";
  462. code += " }\n";
  463. }
  464. code += "\n";
  465. if (flags[FLAG_DISABLE_Z]) {
  466. if (flags[FLAG_ALIGN_Y_TO_VELOCITY]) {
  467. code += " if (length(VELOCITY) > 0.0) {\n";
  468. code += " TRANSFORM[1].xyz = normalize(VELOCITY);\n";
  469. code += " } else {\n";
  470. code += " TRANSFORM[1].xyz = normalize(TRANSFORM[1].xyz);\n";
  471. code += " }\n";
  472. code += " TRANSFORM[0].xyz = normalize(cross(TRANSFORM[1].xyz, TRANSFORM[2].xyz));\n";
  473. code += " TRANSFORM[2] = vec4(0.0, 0.0, 1.0, 0.0);\n";
  474. } else {
  475. code += " TRANSFORM[0] = vec4(cos(CUSTOM.x), -sin(CUSTOM.x), 0.0, 0.0);\n";
  476. code += " TRANSFORM[1] = vec4(sin(CUSTOM.x), cos(CUSTOM.x), 0.0, 0.0);\n";
  477. code += " TRANSFORM[2] = vec4(0.0, 0.0, 1.0, 0.0);\n";
  478. }
  479. } else {
  480. // orient particle Y towards velocity
  481. if (flags[FLAG_ALIGN_Y_TO_VELOCITY]) {
  482. code += " if (length(VELOCITY) > 0.0) {\n";
  483. code += " TRANSFORM[1].xyz = normalize(VELOCITY);\n";
  484. code += " } else {\n";
  485. code += " TRANSFORM[1].xyz = normalize(TRANSFORM[1].xyz);\n";
  486. code += " }\n";
  487. code += " if (TRANSFORM[1].xyz == normalize(TRANSFORM[0].xyz)) {\n";
  488. code += " TRANSFORM[0].xyz = normalize(cross(normalize(TRANSFORM[1].xyz), normalize(TRANSFORM[2].xyz)));\n";
  489. code += " TRANSFORM[2].xyz = normalize(cross(normalize(TRANSFORM[0].xyz), normalize(TRANSFORM[1].xyz)));\n";
  490. code += " } else {\n";
  491. code += " TRANSFORM[2].xyz = normalize(cross(normalize(TRANSFORM[0].xyz), normalize(TRANSFORM[1].xyz)));\n";
  492. code += " TRANSFORM[0].xyz = normalize(cross(normalize(TRANSFORM[1].xyz), normalize(TRANSFORM[2].xyz)));\n";
  493. code += " }\n";
  494. } else {
  495. code += " TRANSFORM[0].xyz = normalize(TRANSFORM[0].xyz);\n";
  496. code += " TRANSFORM[1].xyz = normalize(TRANSFORM[1].xyz);\n";
  497. code += " TRANSFORM[2].xyz = normalize(TRANSFORM[2].xyz);\n";
  498. }
  499. // turn particle by rotation in Y
  500. if (flags[FLAG_ROTATE_Y]) {
  501. code += " TRANSFORM = TRANSFORM * mat4(vec4(cos(CUSTOM.x), 0.0, -sin(CUSTOM.x), 0.0), vec4(0.0, 1.0, 0.0, 0.0), vec4(sin(CUSTOM.x), 0.0, cos(CUSTOM.x), 0.0), vec4(0.0, 0.0, 0.0, 1.0));\n";
  502. }
  503. }
  504. //scale by scale
  505. code += " float base_scale = tex_scale * mix(scale, 1.0, scale_random * scale_rand);\n";
  506. code += " if (base_scale < 0.000001) {\n";
  507. code += " base_scale = 0.000001;\n";
  508. code += " }\n";
  509. if (trail_size_modifier.is_valid()) {
  510. code += " if (trail_divisor > 1) {\n";
  511. code += " base_scale *= textureLod(trail_size_modifier, vec2(float(int(NUMBER) % trail_divisor) / float(trail_divisor - 1), 0.0), 0.0).r;\n";
  512. code += " }\n";
  513. }
  514. code += " TRANSFORM[0].xyz *= base_scale;\n";
  515. code += " TRANSFORM[1].xyz *= base_scale;\n";
  516. code += " TRANSFORM[2].xyz *= base_scale;\n";
  517. if (flags[FLAG_DISABLE_Z]) {
  518. code += " VELOCITY.z = 0.0;\n";
  519. code += " TRANSFORM[3].z = 0.0;\n";
  520. }
  521. code += " if (CUSTOM.y > CUSTOM.w) {";
  522. code += " ACTIVE = false;\n";
  523. code += " }\n";
  524. code += "}\n";
  525. code += "\n";
  526. ShaderData shader_data;
  527. shader_data.shader = VS::get_singleton()->shader_create();
  528. shader_data.users = 1;
  529. VS::get_singleton()->shader_set_code(shader_data.shader, code);
  530. shader_map[mk] = shader_data;
  531. VS::get_singleton()->material_set_shader(_get_material(), shader_data.shader);
  532. }
  533. void ParticlesMaterial::flush_changes() {
  534. if (material_mutex)
  535. material_mutex->lock();
  536. while (dirty_materials->first()) {
  537. dirty_materials->first()->self()->_update_shader();
  538. }
  539. if (material_mutex)
  540. material_mutex->unlock();
  541. }
  542. void ParticlesMaterial::_queue_shader_change() {
  543. if (material_mutex)
  544. material_mutex->lock();
  545. if (!element.in_list()) {
  546. dirty_materials->add(&element);
  547. }
  548. if (material_mutex)
  549. material_mutex->unlock();
  550. }
  551. bool ParticlesMaterial::_is_shader_dirty() const {
  552. bool dirty = false;
  553. if (material_mutex)
  554. material_mutex->lock();
  555. dirty = element.in_list();
  556. if (material_mutex)
  557. material_mutex->unlock();
  558. return dirty;
  559. }
  560. void ParticlesMaterial::set_direction(Vector3 p_direction) {
  561. direction = p_direction;
  562. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->direction, direction);
  563. }
  564. Vector3 ParticlesMaterial::get_direction() const {
  565. return direction;
  566. }
  567. void ParticlesMaterial::set_spread(float p_spread) {
  568. spread = p_spread;
  569. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->spread, p_spread);
  570. }
  571. float ParticlesMaterial::get_spread() const {
  572. return spread;
  573. }
  574. void ParticlesMaterial::set_flatness(float p_flatness) {
  575. flatness = p_flatness;
  576. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->flatness, p_flatness);
  577. }
  578. float ParticlesMaterial::get_flatness() const {
  579. return flatness;
  580. }
  581. void ParticlesMaterial::set_param(Parameter p_param, float p_value) {
  582. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  583. parameters[p_param] = p_value;
  584. switch (p_param) {
  585. case PARAM_INITIAL_LINEAR_VELOCITY: {
  586. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->initial_linear_velocity, p_value);
  587. } break;
  588. case PARAM_ANGULAR_VELOCITY: {
  589. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->angular_velocity, p_value);
  590. } break;
  591. case PARAM_ORBIT_VELOCITY: {
  592. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->orbit_velocity, p_value);
  593. } break;
  594. case PARAM_LINEAR_ACCEL: {
  595. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->linear_accel, p_value);
  596. } break;
  597. case PARAM_RADIAL_ACCEL: {
  598. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->radial_accel, p_value);
  599. } break;
  600. case PARAM_TANGENTIAL_ACCEL: {
  601. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->tangent_accel, p_value);
  602. } break;
  603. case PARAM_DAMPING: {
  604. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->damping, p_value);
  605. } break;
  606. case PARAM_ANGLE: {
  607. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->initial_angle, p_value);
  608. } break;
  609. case PARAM_SCALE: {
  610. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->scale, p_value);
  611. } break;
  612. case PARAM_HUE_VARIATION: {
  613. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->hue_variation, p_value);
  614. } break;
  615. case PARAM_ANIM_SPEED: {
  616. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_speed, p_value);
  617. } break;
  618. case PARAM_ANIM_OFFSET: {
  619. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_offset, p_value);
  620. } break;
  621. case PARAM_MAX: break; // Can't happen, but silences warning
  622. }
  623. }
  624. float ParticlesMaterial::get_param(Parameter p_param) const {
  625. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  626. return parameters[p_param];
  627. }
  628. void ParticlesMaterial::set_param_randomness(Parameter p_param, float p_value) {
  629. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  630. randomness[p_param] = p_value;
  631. switch (p_param) {
  632. case PARAM_INITIAL_LINEAR_VELOCITY: {
  633. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->initial_linear_velocity_random, p_value);
  634. } break;
  635. case PARAM_ANGULAR_VELOCITY: {
  636. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->angular_velocity_random, p_value);
  637. } break;
  638. case PARAM_ORBIT_VELOCITY: {
  639. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->orbit_velocity_random, p_value);
  640. } break;
  641. case PARAM_LINEAR_ACCEL: {
  642. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->linear_accel_random, p_value);
  643. } break;
  644. case PARAM_RADIAL_ACCEL: {
  645. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->radial_accel_random, p_value);
  646. } break;
  647. case PARAM_TANGENTIAL_ACCEL: {
  648. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->tangent_accel_random, p_value);
  649. } break;
  650. case PARAM_DAMPING: {
  651. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->damping_random, p_value);
  652. } break;
  653. case PARAM_ANGLE: {
  654. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->initial_angle_random, p_value);
  655. } break;
  656. case PARAM_SCALE: {
  657. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->scale_random, p_value);
  658. } break;
  659. case PARAM_HUE_VARIATION: {
  660. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->hue_variation_random, p_value);
  661. } break;
  662. case PARAM_ANIM_SPEED: {
  663. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_speed_random, p_value);
  664. } break;
  665. case PARAM_ANIM_OFFSET: {
  666. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_offset_random, p_value);
  667. } break;
  668. case PARAM_MAX: break; // Can't happen, but silences warning
  669. }
  670. }
  671. float ParticlesMaterial::get_param_randomness(Parameter p_param) const {
  672. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0);
  673. return randomness[p_param];
  674. }
  675. static void _adjust_curve_range(const Ref<Texture> &p_texture, float p_min, float p_max) {
  676. Ref<CurveTexture> curve_tex = p_texture;
  677. if (!curve_tex.is_valid())
  678. return;
  679. curve_tex->ensure_default_setup(p_min, p_max);
  680. }
  681. void ParticlesMaterial::set_param_texture(Parameter p_param, const Ref<Texture> &p_texture) {
  682. ERR_FAIL_INDEX(p_param, PARAM_MAX);
  683. tex_parameters[p_param] = p_texture;
  684. switch (p_param) {
  685. case PARAM_INITIAL_LINEAR_VELOCITY: {
  686. //do none for this one
  687. } break;
  688. case PARAM_ANGULAR_VELOCITY: {
  689. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->angular_velocity_texture, p_texture);
  690. _adjust_curve_range(p_texture, -360, 360);
  691. } break;
  692. case PARAM_ORBIT_VELOCITY: {
  693. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->orbit_velocity_texture, p_texture);
  694. _adjust_curve_range(p_texture, -500, 500);
  695. } break;
  696. case PARAM_LINEAR_ACCEL: {
  697. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->linear_accel_texture, p_texture);
  698. _adjust_curve_range(p_texture, -200, 200);
  699. } break;
  700. case PARAM_RADIAL_ACCEL: {
  701. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->radial_accel_texture, p_texture);
  702. _adjust_curve_range(p_texture, -200, 200);
  703. } break;
  704. case PARAM_TANGENTIAL_ACCEL: {
  705. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->tangent_accel_texture, p_texture);
  706. _adjust_curve_range(p_texture, -200, 200);
  707. } break;
  708. case PARAM_DAMPING: {
  709. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->damping_texture, p_texture);
  710. _adjust_curve_range(p_texture, 0, 100);
  711. } break;
  712. case PARAM_ANGLE: {
  713. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->angle_texture, p_texture);
  714. _adjust_curve_range(p_texture, -360, 360);
  715. } break;
  716. case PARAM_SCALE: {
  717. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->scale_texture, p_texture);
  718. _adjust_curve_range(p_texture, 0, 1);
  719. } break;
  720. case PARAM_HUE_VARIATION: {
  721. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->hue_variation_texture, p_texture);
  722. _adjust_curve_range(p_texture, -1, 1);
  723. } break;
  724. case PARAM_ANIM_SPEED: {
  725. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_speed_texture, p_texture);
  726. _adjust_curve_range(p_texture, 0, 200);
  727. } break;
  728. case PARAM_ANIM_OFFSET: {
  729. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->anim_offset_texture, p_texture);
  730. } break;
  731. case PARAM_MAX: break; // Can't happen, but silences warning
  732. }
  733. _queue_shader_change();
  734. }
  735. Ref<Texture> ParticlesMaterial::get_param_texture(Parameter p_param) const {
  736. ERR_FAIL_INDEX_V(p_param, PARAM_MAX, Ref<Texture>());
  737. return tex_parameters[p_param];
  738. }
  739. void ParticlesMaterial::set_color(const Color &p_color) {
  740. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->color, p_color);
  741. color = p_color;
  742. }
  743. Color ParticlesMaterial::get_color() const {
  744. return color;
  745. }
  746. void ParticlesMaterial::set_color_ramp(const Ref<Texture> &p_texture) {
  747. color_ramp = p_texture;
  748. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->color_ramp, p_texture);
  749. _queue_shader_change();
  750. _change_notify();
  751. }
  752. Ref<Texture> ParticlesMaterial::get_color_ramp() const {
  753. return color_ramp;
  754. }
  755. void ParticlesMaterial::set_flag(Flags p_flag, bool p_enable) {
  756. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  757. flags[p_flag] = p_enable;
  758. _queue_shader_change();
  759. if (p_flag == FLAG_DISABLE_Z) {
  760. _change_notify();
  761. }
  762. }
  763. bool ParticlesMaterial::get_flag(Flags p_flag) const {
  764. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  765. return flags[p_flag];
  766. }
  767. void ParticlesMaterial::set_emission_shape(EmissionShape p_shape) {
  768. ERR_FAIL_INDEX(p_shape, EMISSION_SHAPE_MAX);
  769. emission_shape = p_shape;
  770. _change_notify();
  771. _queue_shader_change();
  772. }
  773. void ParticlesMaterial::set_emission_sphere_radius(float p_radius) {
  774. emission_sphere_radius = p_radius;
  775. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_sphere_radius, p_radius);
  776. }
  777. void ParticlesMaterial::set_emission_box_extents(Vector3 p_extents) {
  778. emission_box_extents = p_extents;
  779. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_box_extents, p_extents);
  780. }
  781. void ParticlesMaterial::set_emission_point_texture(const Ref<Texture> &p_points) {
  782. emission_point_texture = p_points;
  783. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_texture_points, p_points);
  784. }
  785. void ParticlesMaterial::set_emission_normal_texture(const Ref<Texture> &p_normals) {
  786. emission_normal_texture = p_normals;
  787. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_texture_normal, p_normals);
  788. }
  789. void ParticlesMaterial::set_emission_color_texture(const Ref<Texture> &p_colors) {
  790. emission_color_texture = p_colors;
  791. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_texture_color, p_colors);
  792. _queue_shader_change();
  793. }
  794. void ParticlesMaterial::set_emission_point_count(int p_count) {
  795. emission_point_count = p_count;
  796. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->emission_texture_point_count, p_count);
  797. }
  798. ParticlesMaterial::EmissionShape ParticlesMaterial::get_emission_shape() const {
  799. return emission_shape;
  800. }
  801. float ParticlesMaterial::get_emission_sphere_radius() const {
  802. return emission_sphere_radius;
  803. }
  804. Vector3 ParticlesMaterial::get_emission_box_extents() const {
  805. return emission_box_extents;
  806. }
  807. Ref<Texture> ParticlesMaterial::get_emission_point_texture() const {
  808. return emission_point_texture;
  809. }
  810. Ref<Texture> ParticlesMaterial::get_emission_normal_texture() const {
  811. return emission_normal_texture;
  812. }
  813. Ref<Texture> ParticlesMaterial::get_emission_color_texture() const {
  814. return emission_color_texture;
  815. }
  816. int ParticlesMaterial::get_emission_point_count() const {
  817. return emission_point_count;
  818. }
  819. void ParticlesMaterial::set_trail_divisor(int p_divisor) {
  820. trail_divisor = p_divisor;
  821. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->trail_divisor, p_divisor);
  822. }
  823. int ParticlesMaterial::get_trail_divisor() const {
  824. return trail_divisor;
  825. }
  826. void ParticlesMaterial::set_trail_size_modifier(const Ref<CurveTexture> &p_trail_size_modifier) {
  827. trail_size_modifier = p_trail_size_modifier;
  828. Ref<CurveTexture> curve = trail_size_modifier;
  829. if (curve.is_valid()) {
  830. curve->ensure_default_setup();
  831. }
  832. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->trail_size_modifier, curve);
  833. _queue_shader_change();
  834. }
  835. Ref<CurveTexture> ParticlesMaterial::get_trail_size_modifier() const {
  836. return trail_size_modifier;
  837. }
  838. void ParticlesMaterial::set_trail_color_modifier(const Ref<GradientTexture> &p_trail_color_modifier) {
  839. trail_color_modifier = p_trail_color_modifier;
  840. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->trail_color_modifier, p_trail_color_modifier);
  841. _queue_shader_change();
  842. }
  843. Ref<GradientTexture> ParticlesMaterial::get_trail_color_modifier() const {
  844. return trail_color_modifier;
  845. }
  846. void ParticlesMaterial::set_gravity(const Vector3 &p_gravity) {
  847. gravity = p_gravity;
  848. Vector3 gset = gravity;
  849. if (gset == Vector3()) {
  850. gset = Vector3(0, -0.000001, 0); //as gravity is used as upvector in some calculations
  851. }
  852. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->gravity, gset);
  853. }
  854. Vector3 ParticlesMaterial::get_gravity() const {
  855. return gravity;
  856. }
  857. void ParticlesMaterial::set_lifetime_randomness(float p_lifetime) {
  858. lifetime_randomness = p_lifetime;
  859. VisualServer::get_singleton()->material_set_param(_get_material(), shader_names->lifetime_randomness, lifetime_randomness);
  860. }
  861. float ParticlesMaterial::get_lifetime_randomness() const {
  862. return lifetime_randomness;
  863. }
  864. RID ParticlesMaterial::get_shader_rid() const {
  865. ERR_FAIL_COND_V(!shader_map.has(current_key), RID());
  866. return shader_map[current_key].shader;
  867. }
  868. void ParticlesMaterial::_validate_property(PropertyInfo &property) const {
  869. if (property.name == "color" && color_ramp.is_valid()) {
  870. property.usage = 0;
  871. }
  872. if (property.name == "emission_sphere_radius" && emission_shape != EMISSION_SHAPE_SPHERE) {
  873. property.usage = 0;
  874. }
  875. if (property.name == "emission_box_extents" && emission_shape != EMISSION_SHAPE_BOX) {
  876. property.usage = 0;
  877. }
  878. if ((property.name == "emission_point_texture" || property.name == "emission_color_texture") && (emission_shape < EMISSION_SHAPE_POINTS)) {
  879. property.usage = 0;
  880. }
  881. if (property.name == "emission_normal_texture" && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) {
  882. property.usage = 0;
  883. }
  884. if (property.name == "emission_point_count" && (emission_shape != EMISSION_SHAPE_POINTS && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS)) {
  885. property.usage = 0;
  886. }
  887. if (property.name.begins_with("orbit_") && !flags[FLAG_DISABLE_Z]) {
  888. property.usage = 0;
  889. }
  890. }
  891. Shader::Mode ParticlesMaterial::get_shader_mode() const {
  892. return Shader::MODE_PARTICLES;
  893. }
  894. void ParticlesMaterial::_bind_methods() {
  895. ClassDB::bind_method(D_METHOD("set_direction", "degrees"), &ParticlesMaterial::set_direction);
  896. ClassDB::bind_method(D_METHOD("get_direction"), &ParticlesMaterial::get_direction);
  897. ClassDB::bind_method(D_METHOD("set_spread", "degrees"), &ParticlesMaterial::set_spread);
  898. ClassDB::bind_method(D_METHOD("get_spread"), &ParticlesMaterial::get_spread);
  899. ClassDB::bind_method(D_METHOD("set_flatness", "amount"), &ParticlesMaterial::set_flatness);
  900. ClassDB::bind_method(D_METHOD("get_flatness"), &ParticlesMaterial::get_flatness);
  901. ClassDB::bind_method(D_METHOD("set_param", "param", "value"), &ParticlesMaterial::set_param);
  902. ClassDB::bind_method(D_METHOD("get_param", "param"), &ParticlesMaterial::get_param);
  903. ClassDB::bind_method(D_METHOD("set_param_randomness", "param", "randomness"), &ParticlesMaterial::set_param_randomness);
  904. ClassDB::bind_method(D_METHOD("get_param_randomness", "param"), &ParticlesMaterial::get_param_randomness);
  905. ClassDB::bind_method(D_METHOD("set_param_texture", "param", "texture"), &ParticlesMaterial::set_param_texture);
  906. ClassDB::bind_method(D_METHOD("get_param_texture", "param"), &ParticlesMaterial::get_param_texture);
  907. ClassDB::bind_method(D_METHOD("set_color", "color"), &ParticlesMaterial::set_color);
  908. ClassDB::bind_method(D_METHOD("get_color"), &ParticlesMaterial::get_color);
  909. ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &ParticlesMaterial::set_color_ramp);
  910. ClassDB::bind_method(D_METHOD("get_color_ramp"), &ParticlesMaterial::get_color_ramp);
  911. ClassDB::bind_method(D_METHOD("set_flag", "flag", "enable"), &ParticlesMaterial::set_flag);
  912. ClassDB::bind_method(D_METHOD("get_flag", "flag"), &ParticlesMaterial::get_flag);
  913. ClassDB::bind_method(D_METHOD("set_emission_shape", "shape"), &ParticlesMaterial::set_emission_shape);
  914. ClassDB::bind_method(D_METHOD("get_emission_shape"), &ParticlesMaterial::get_emission_shape);
  915. ClassDB::bind_method(D_METHOD("set_emission_sphere_radius", "radius"), &ParticlesMaterial::set_emission_sphere_radius);
  916. ClassDB::bind_method(D_METHOD("get_emission_sphere_radius"), &ParticlesMaterial::get_emission_sphere_radius);
  917. ClassDB::bind_method(D_METHOD("set_emission_box_extents", "extents"), &ParticlesMaterial::set_emission_box_extents);
  918. ClassDB::bind_method(D_METHOD("get_emission_box_extents"), &ParticlesMaterial::get_emission_box_extents);
  919. ClassDB::bind_method(D_METHOD("set_emission_point_texture", "texture"), &ParticlesMaterial::set_emission_point_texture);
  920. ClassDB::bind_method(D_METHOD("get_emission_point_texture"), &ParticlesMaterial::get_emission_point_texture);
  921. ClassDB::bind_method(D_METHOD("set_emission_normal_texture", "texture"), &ParticlesMaterial::set_emission_normal_texture);
  922. ClassDB::bind_method(D_METHOD("get_emission_normal_texture"), &ParticlesMaterial::get_emission_normal_texture);
  923. ClassDB::bind_method(D_METHOD("set_emission_color_texture", "texture"), &ParticlesMaterial::set_emission_color_texture);
  924. ClassDB::bind_method(D_METHOD("get_emission_color_texture"), &ParticlesMaterial::get_emission_color_texture);
  925. ClassDB::bind_method(D_METHOD("set_emission_point_count", "point_count"), &ParticlesMaterial::set_emission_point_count);
  926. ClassDB::bind_method(D_METHOD("get_emission_point_count"), &ParticlesMaterial::get_emission_point_count);
  927. ClassDB::bind_method(D_METHOD("set_trail_divisor", "divisor"), &ParticlesMaterial::set_trail_divisor);
  928. ClassDB::bind_method(D_METHOD("get_trail_divisor"), &ParticlesMaterial::get_trail_divisor);
  929. ClassDB::bind_method(D_METHOD("set_trail_size_modifier", "texture"), &ParticlesMaterial::set_trail_size_modifier);
  930. ClassDB::bind_method(D_METHOD("get_trail_size_modifier"), &ParticlesMaterial::get_trail_size_modifier);
  931. ClassDB::bind_method(D_METHOD("set_trail_color_modifier", "texture"), &ParticlesMaterial::set_trail_color_modifier);
  932. ClassDB::bind_method(D_METHOD("get_trail_color_modifier"), &ParticlesMaterial::get_trail_color_modifier);
  933. ClassDB::bind_method(D_METHOD("get_gravity"), &ParticlesMaterial::get_gravity);
  934. ClassDB::bind_method(D_METHOD("set_gravity", "accel_vec"), &ParticlesMaterial::set_gravity);
  935. ClassDB::bind_method(D_METHOD("set_lifetime_randomness", "randomness"), &ParticlesMaterial::set_lifetime_randomness);
  936. ClassDB::bind_method(D_METHOD("get_lifetime_randomness"), &ParticlesMaterial::get_lifetime_randomness);
  937. ADD_GROUP("Time", "");
  938. ADD_PROPERTY(PropertyInfo(Variant::REAL, "lifetime_randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_lifetime_randomness", "get_lifetime_randomness");
  939. ADD_GROUP("Trail", "trail_");
  940. ADD_PROPERTY(PropertyInfo(Variant::INT, "trail_divisor", PROPERTY_HINT_RANGE, "1,1000000,1"), "set_trail_divisor", "get_trail_divisor");
  941. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "trail_size_modifier", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_trail_size_modifier", "get_trail_size_modifier");
  942. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "trail_color_modifier", PROPERTY_HINT_RESOURCE_TYPE, "GradientTexture"), "set_trail_color_modifier", "get_trail_color_modifier");
  943. ADD_GROUP("Emission Shape", "emission_");
  944. ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_shape", PROPERTY_HINT_ENUM, "Point,Sphere,Box,Points,Directed Points"), "set_emission_shape", "get_emission_shape");
  945. ADD_PROPERTY(PropertyInfo(Variant::REAL, "emission_sphere_radius", PROPERTY_HINT_RANGE, "0.01,128,0.01,or_greater"), "set_emission_sphere_radius", "get_emission_sphere_radius");
  946. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "emission_box_extents"), "set_emission_box_extents", "get_emission_box_extents");
  947. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "emission_point_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_emission_point_texture", "get_emission_point_texture");
  948. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "emission_normal_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_emission_normal_texture", "get_emission_normal_texture");
  949. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "emission_color_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_emission_color_texture", "get_emission_color_texture");
  950. ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_point_count", PROPERTY_HINT_RANGE, "0,1000000,1"), "set_emission_point_count", "get_emission_point_count");
  951. ADD_GROUP("Flags", "flag_");
  952. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_align_y"), "set_flag", "get_flag", FLAG_ALIGN_Y_TO_VELOCITY);
  953. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_rotate_y"), "set_flag", "get_flag", FLAG_ROTATE_Y);
  954. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flag_disable_z"), "set_flag", "get_flag", FLAG_DISABLE_Z);
  955. ADD_GROUP("Direction", "");
  956. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "direction"), "set_direction", "get_direction");
  957. ADD_PROPERTY(PropertyInfo(Variant::REAL, "spread", PROPERTY_HINT_RANGE, "0,180,0.01"), "set_spread", "get_spread");
  958. ADD_PROPERTY(PropertyInfo(Variant::REAL, "flatness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_flatness", "get_flatness");
  959. ADD_GROUP("Gravity", "");
  960. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "gravity"), "set_gravity", "get_gravity");
  961. ADD_GROUP("Initial Velocity", "initial_");
  962. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "initial_velocity", PROPERTY_HINT_RANGE, "0,1000,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_INITIAL_LINEAR_VELOCITY);
  963. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "initial_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_INITIAL_LINEAR_VELOCITY);
  964. ADD_GROUP("Angular Velocity", "angular_");
  965. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_velocity", PROPERTY_HINT_RANGE, "-720,720,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_ANGULAR_VELOCITY);
  966. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angular_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGULAR_VELOCITY);
  967. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angular_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_ANGULAR_VELOCITY);
  968. ADD_GROUP("Orbit Velocity", "orbit_");
  969. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "orbit_velocity", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_ORBIT_VELOCITY);
  970. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "orbit_velocity_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ORBIT_VELOCITY);
  971. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "orbit_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_ORBIT_VELOCITY);
  972. ADD_GROUP("Linear Accel", "linear_");
  973. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_accel", PROPERTY_HINT_RANGE, "-100,100,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_LINEAR_ACCEL);
  974. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "linear_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_LINEAR_ACCEL);
  975. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "linear_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_LINEAR_ACCEL);
  976. ADD_GROUP("Radial Accel", "radial_");
  977. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "radial_accel", PROPERTY_HINT_RANGE, "-100,100,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_RADIAL_ACCEL);
  978. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "radial_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_RADIAL_ACCEL);
  979. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "radial_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_RADIAL_ACCEL);
  980. ADD_GROUP("Tangential Accel", "tangential_");
  981. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "tangential_accel", PROPERTY_HINT_RANGE, "-100,100,0.01,or_lesser,or_greater"), "set_param", "get_param", PARAM_TANGENTIAL_ACCEL);
  982. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "tangential_accel_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_TANGENTIAL_ACCEL);
  983. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "tangential_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_TANGENTIAL_ACCEL);
  984. ADD_GROUP("Damping", "");
  985. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "damping", PROPERTY_HINT_RANGE, "0,100,0.01,or_greater"), "set_param", "get_param", PARAM_DAMPING);
  986. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "damping_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_DAMPING);
  987. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_DAMPING);
  988. ADD_GROUP("Angle", "");
  989. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angle", PROPERTY_HINT_RANGE, "-720,720,0.1,or_lesser,or_greater"), "set_param", "get_param", PARAM_ANGLE);
  990. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "angle_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANGLE);
  991. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_ANGLE);
  992. ADD_GROUP("Scale", "");
  993. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param", "get_param", PARAM_SCALE);
  994. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "scale_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_SCALE);
  995. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_SCALE);
  996. ADD_GROUP("Color", "");
  997. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  998. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "GradientTexture"), "set_color_ramp", "get_color_ramp");
  999. ADD_GROUP("Hue Variation", "hue_");
  1000. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "hue_variation", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param", "get_param", PARAM_HUE_VARIATION);
  1001. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "hue_variation_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_HUE_VARIATION);
  1002. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "hue_variation_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_HUE_VARIATION);
  1003. ADD_GROUP("Animation", "anim_");
  1004. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_speed", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater"), "set_param", "get_param", PARAM_ANIM_SPEED);
  1005. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_speed_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANIM_SPEED);
  1006. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_speed_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_ANIM_SPEED);
  1007. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_offset", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param", "get_param", PARAM_ANIM_OFFSET);
  1008. ADD_PROPERTYI(PropertyInfo(Variant::REAL, "anim_offset_random", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_param_randomness", "get_param_randomness", PARAM_ANIM_OFFSET);
  1009. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_offset_curve", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_param_texture", "get_param_texture", PARAM_ANIM_OFFSET);
  1010. BIND_ENUM_CONSTANT(PARAM_INITIAL_LINEAR_VELOCITY);
  1011. BIND_ENUM_CONSTANT(PARAM_ANGULAR_VELOCITY);
  1012. BIND_ENUM_CONSTANT(PARAM_ORBIT_VELOCITY);
  1013. BIND_ENUM_CONSTANT(PARAM_LINEAR_ACCEL);
  1014. BIND_ENUM_CONSTANT(PARAM_RADIAL_ACCEL);
  1015. BIND_ENUM_CONSTANT(PARAM_TANGENTIAL_ACCEL);
  1016. BIND_ENUM_CONSTANT(PARAM_DAMPING);
  1017. BIND_ENUM_CONSTANT(PARAM_ANGLE);
  1018. BIND_ENUM_CONSTANT(PARAM_SCALE);
  1019. BIND_ENUM_CONSTANT(PARAM_HUE_VARIATION);
  1020. BIND_ENUM_CONSTANT(PARAM_ANIM_SPEED);
  1021. BIND_ENUM_CONSTANT(PARAM_ANIM_OFFSET);
  1022. BIND_ENUM_CONSTANT(PARAM_MAX);
  1023. BIND_ENUM_CONSTANT(FLAG_ALIGN_Y_TO_VELOCITY);
  1024. BIND_ENUM_CONSTANT(FLAG_ROTATE_Y);
  1025. BIND_ENUM_CONSTANT(FLAG_DISABLE_Z);
  1026. BIND_ENUM_CONSTANT(FLAG_MAX);
  1027. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINT);
  1028. BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE);
  1029. BIND_ENUM_CONSTANT(EMISSION_SHAPE_BOX);
  1030. BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINTS);
  1031. BIND_ENUM_CONSTANT(EMISSION_SHAPE_DIRECTED_POINTS);
  1032. BIND_ENUM_CONSTANT(EMISSION_SHAPE_MAX);
  1033. }
  1034. ParticlesMaterial::ParticlesMaterial() :
  1035. element(this) {
  1036. set_direction(Vector3(1, 0, 0));
  1037. set_spread(45);
  1038. set_flatness(0);
  1039. set_param(PARAM_INITIAL_LINEAR_VELOCITY, 0);
  1040. set_param(PARAM_ANGULAR_VELOCITY, 0);
  1041. set_param(PARAM_ORBIT_VELOCITY, 0);
  1042. set_param(PARAM_LINEAR_ACCEL, 0);
  1043. set_param(PARAM_RADIAL_ACCEL, 0);
  1044. set_param(PARAM_TANGENTIAL_ACCEL, 0);
  1045. set_param(PARAM_DAMPING, 0);
  1046. set_param(PARAM_ANGLE, 0);
  1047. set_param(PARAM_SCALE, 1);
  1048. set_param(PARAM_HUE_VARIATION, 0);
  1049. set_param(PARAM_ANIM_SPEED, 0);
  1050. set_param(PARAM_ANIM_OFFSET, 0);
  1051. set_emission_shape(EMISSION_SHAPE_POINT);
  1052. set_emission_sphere_radius(1);
  1053. set_emission_box_extents(Vector3(1, 1, 1));
  1054. set_trail_divisor(1);
  1055. set_gravity(Vector3(0, -9.8, 0));
  1056. set_lifetime_randomness(0);
  1057. emission_point_count = 1;
  1058. for (int i = 0; i < PARAM_MAX; i++) {
  1059. set_param_randomness(Parameter(i), 0);
  1060. }
  1061. for (int i = 0; i < FLAG_MAX; i++) {
  1062. flags[i] = false;
  1063. }
  1064. set_color(Color(1, 1, 1, 1));
  1065. current_key.key = 0;
  1066. current_key.invalid_key = 1;
  1067. _queue_shader_change();
  1068. }
  1069. ParticlesMaterial::~ParticlesMaterial() {
  1070. if (material_mutex)
  1071. material_mutex->lock();
  1072. if (shader_map.has(current_key)) {
  1073. shader_map[current_key].users--;
  1074. if (shader_map[current_key].users == 0) {
  1075. //deallocate shader, as it's no longer in use
  1076. VS::get_singleton()->free(shader_map[current_key].shader);
  1077. shader_map.erase(current_key);
  1078. }
  1079. VS::get_singleton()->material_set_shader(_get_material(), RID());
  1080. }
  1081. if (material_mutex)
  1082. material_mutex->unlock();
  1083. }