particles_material.cpp 64 KB

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