particles_material.cpp 54 KB

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