particles.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*************************************************************************/
  2. /* particles.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "particles.h"
  30. #include "servers/visual_server.h"
  31. #include "scene/resources/surface_tool.h"
  32. /*
  33. static const char* _var_names[Particles::VAR_MAX]={
  34. "vars/lifetime",
  35. "vars/spread",
  36. "vars/gravity",
  37. "vars/linear_vel",
  38. "vars/angular_vel",
  39. "vars/linear_accel",
  40. "vars/radial_accel",
  41. "vars/tan_accel",
  42. "vars/initial_size",
  43. "vars/final_size",
  44. "vars/initial_angle",
  45. "vars/height",
  46. "vars/height_speed_scale",
  47. };
  48. */
  49. static const char* _rand_names[Particles::VAR_MAX]={
  50. "rand/lifetime",
  51. "rand/spread",
  52. "rand/gravity",
  53. "rand/linear_vel",
  54. "rand/angular_vel",
  55. "rand/linear_accel",
  56. "rand/radial_accel",
  57. "rand/tan_accel",
  58. "rand/damping",
  59. "rand/initial_size",
  60. "rand/final_size",
  61. "rand/initial_angle",
  62. "rand/height",
  63. "rand/height_speed_scale",
  64. };
  65. static const Particles::Variable _var_indices[Particles::VAR_MAX]={
  66. Particles::VAR_LIFETIME,
  67. Particles::VAR_SPREAD,
  68. Particles::VAR_GRAVITY,
  69. Particles::VAR_LINEAR_VELOCITY,
  70. Particles::VAR_ANGULAR_VELOCITY,
  71. Particles::VAR_LINEAR_ACCELERATION,
  72. Particles::VAR_DRAG,
  73. Particles::VAR_TANGENTIAL_ACCELERATION,
  74. Particles::VAR_DAMPING,
  75. Particles::VAR_INITIAL_SIZE,
  76. Particles::VAR_FINAL_SIZE,
  77. Particles::VAR_INITIAL_ANGLE,
  78. Particles::VAR_HEIGHT,
  79. Particles::VAR_HEIGHT_SPEED_SCALE,
  80. };
  81. AABB Particles::get_aabb() const {
  82. return AABB( Vector3(-1,-1,-1), Vector3(2, 2, 2 ) );
  83. }
  84. DVector<Face3> Particles::get_faces(uint32_t p_usage_flags) const {
  85. return DVector<Face3>();
  86. }
  87. void Particles::set_amount(int p_amount) {
  88. ERR_FAIL_INDEX(p_amount,1024);
  89. amount=p_amount;
  90. VisualServer::get_singleton()->particles_set_amount(particles,p_amount);
  91. }
  92. int Particles::get_amount() const {
  93. return amount;
  94. }
  95. void Particles::set_emitting(bool p_emitting) {
  96. emitting=p_emitting;
  97. VisualServer::get_singleton()->particles_set_emitting(particles,p_emitting);
  98. setup_timer();
  99. }
  100. bool Particles::is_emitting() const {
  101. return emitting;
  102. }
  103. void Particles::set_visibility_aabb(const AABB& p_aabb) {
  104. visibility_aabb=p_aabb;
  105. VisualServer::get_singleton()->particles_set_visibility_aabb(particles,p_aabb);
  106. update_gizmo();
  107. }
  108. AABB Particles::get_visibility_aabb() const {
  109. return visibility_aabb;
  110. }
  111. void Particles::set_emission_points(const DVector<Vector3>& p_points) {
  112. using_points = p_points.size();
  113. VisualServer::get_singleton()->particles_set_emission_points(particles,p_points);
  114. }
  115. DVector<Vector3> Particles::get_emission_points() const {
  116. if (!using_points)
  117. return DVector<Vector3>();
  118. return VisualServer::get_singleton()->particles_get_emission_points(particles);
  119. }
  120. void Particles::set_emission_half_extents(const Vector3& p_half_extents) {
  121. emission_half_extents=p_half_extents;
  122. VisualServer::get_singleton()->particles_set_emission_half_extents(particles,p_half_extents);
  123. }
  124. Vector3 Particles::get_emission_half_extents() const {
  125. return emission_half_extents;
  126. }
  127. void Particles::set_emission_base_velocity(const Vector3& p_base_velocity) {
  128. emission_base_velocity=p_base_velocity;
  129. VisualServer::get_singleton()->particles_set_emission_base_velocity(particles,p_base_velocity);
  130. }
  131. Vector3 Particles::get_emission_base_velocity() const {
  132. return emission_base_velocity;
  133. }
  134. void Particles::set_gravity_normal(const Vector3& p_normal) {
  135. gravity_normal=p_normal;
  136. VisualServer::get_singleton()->particles_set_gravity_normal(particles,p_normal);
  137. }
  138. Vector3 Particles::get_gravity_normal() const {
  139. return gravity_normal;
  140. }
  141. void Particles::set_variable(Variable p_variable,float p_value) {
  142. ERR_FAIL_INDEX(p_variable,VAR_MAX);
  143. var[p_variable]=p_value;
  144. VisualServer::get_singleton()->particles_set_variable(particles,(VS::ParticleVariable)p_variable,p_value);
  145. if (p_variable==VAR_SPREAD)
  146. update_gizmo();
  147. }
  148. float Particles::get_variable(Variable p_variable) const {
  149. ERR_FAIL_INDEX_V(p_variable,VAR_MAX,-1);
  150. return var[p_variable];
  151. }
  152. void Particles::set_randomness(Variable p_variable,float p_randomness) {
  153. ERR_FAIL_INDEX(p_variable,VAR_MAX);
  154. var_random[p_variable]=p_randomness;
  155. VisualServer::get_singleton()->particles_set_randomness(particles,(VS::ParticleVariable)p_variable,p_randomness);
  156. }
  157. float Particles::get_randomness(Variable p_variable) const {
  158. ERR_FAIL_INDEX_V(p_variable,VAR_MAX,-1);
  159. return var_random[p_variable];
  160. }
  161. void Particles::set_color_phase_pos(int p_phase, float p_pos) {
  162. ERR_FAIL_INDEX(p_phase,VS::MAX_PARTICLE_COLOR_PHASES);
  163. color_phase[p_phase].pos=p_pos;
  164. VisualServer::get_singleton()->particles_set_color_phase_pos(particles,p_phase,p_pos);
  165. }
  166. float Particles::get_color_phase_pos(int p_phase) const {
  167. ERR_FAIL_INDEX_V(p_phase,VS::MAX_PARTICLE_COLOR_PHASES,-1);
  168. return color_phase[p_phase].pos;
  169. }
  170. void Particles::set_color_phase_color(int p_phase, const Color& p_color) {
  171. ERR_FAIL_INDEX(p_phase,VS::MAX_PARTICLE_COLOR_PHASES);
  172. color_phase[p_phase].color=p_color;
  173. VisualServer::get_singleton()->particles_set_color_phase_color(particles,p_phase,p_color);
  174. }
  175. Color Particles::get_color_phase_color(int p_phase) const {
  176. ERR_FAIL_INDEX_V(p_phase,VS::MAX_PARTICLE_COLOR_PHASES,Color());
  177. return color_phase[p_phase].color;
  178. }
  179. void Particles::set_material(const Ref<Material>& p_material) {
  180. material=p_material;
  181. if(material.is_null()) {
  182. VisualServer::get_singleton()->particles_set_material(particles,RID());
  183. } else {
  184. VisualServer::get_singleton()->particles_set_material(particles,material->get_rid());
  185. }
  186. }
  187. void Particles::setup_timer() {
  188. if (emitting && emit_timeout > 0) {
  189. timer->set_wait_time(emit_timeout);
  190. timer->start();
  191. timer->set_one_shot(true);
  192. };
  193. };
  194. void Particles::set_emit_timeout(float p_timeout) {
  195. emit_timeout = p_timeout;
  196. setup_timer();
  197. };
  198. float Particles::get_emit_timeout() const {
  199. return emit_timeout;
  200. };
  201. Ref<Material> Particles::get_material() const {
  202. return material;
  203. }
  204. void Particles::set_height_from_velocity(bool p_enable) {
  205. height_from_velocity=p_enable;
  206. VisualServer::get_singleton()->particles_set_height_from_velocity(particles,height_from_velocity);
  207. }
  208. bool Particles::has_height_from_velocity() const {
  209. return height_from_velocity;
  210. }
  211. void Particles::set_color_phases(int p_phases) {
  212. color_phase_count=p_phases;
  213. VisualServer::get_singleton()->particles_set_color_phases(particles,p_phases);
  214. }
  215. int Particles::get_color_phases() const{
  216. return color_phase_count;
  217. }
  218. bool Particles::_can_gizmo_scale() const {
  219. return false;
  220. }
  221. void Particles::set_use_local_coordinates(bool p_use) {
  222. local_coordinates=p_use;
  223. VisualServer::get_singleton()->particles_set_use_local_coordinates(particles,local_coordinates);
  224. }
  225. bool Particles::is_using_local_coordinates() const{
  226. return local_coordinates;
  227. }
  228. RES Particles::_get_gizmo_geometry() const {
  229. Ref<SurfaceTool> surface_tool( memnew( SurfaceTool ));
  230. Ref<FixedMaterial> mat( memnew( FixedMaterial ));
  231. mat->set_parameter( FixedMaterial::PARAM_DIFFUSE,Color(0.0,0.6,0.7,0.2) );
  232. mat->set_parameter( FixedMaterial::PARAM_EMISSION,Color(0.5,0.7,0.8) );
  233. mat->set_blend_mode( Material::BLEND_MODE_ADD );
  234. mat->set_flag(Material::FLAG_DOUBLE_SIDED,true);
  235. // mat->set_hint(Material::HINT_NO_DEPTH_DRAW,true);
  236. surface_tool->begin(Mesh::PRIMITIVE_TRIANGLES);
  237. surface_tool->set_material(mat);
  238. int sides=16;
  239. int sections=24;
  240. // float len=1;
  241. float deg=Math::deg2rad(var[VAR_SPREAD]*180);
  242. if (deg==180)
  243. deg=179.5;
  244. Vector3 to=Vector3(0,0,-1);
  245. for(int j=0;j<sections;j++) {
  246. Vector3 p1=Matrix3(Vector3(1,0,0),deg*j/sections).xform(to);
  247. Vector3 p2=Matrix3(Vector3(1,0,0),deg*(j+1)/sections).xform(to);
  248. for(int i=0;i<sides;i++) {
  249. Vector3 p1r = Matrix3(Vector3(0,0,1),Math_PI*2*float(i)/sides).xform(p1);
  250. Vector3 p1s = Matrix3(Vector3(0,0,1),Math_PI*2*float(i+1)/sides).xform(p1);
  251. Vector3 p2s = Matrix3(Vector3(0,0,1),Math_PI*2*float(i+1)/sides).xform(p2);
  252. Vector3 p2r = Matrix3(Vector3(0,0,1),Math_PI*2*float(i)/sides).xform(p2);
  253. surface_tool->add_normal(p1r.normalized());
  254. surface_tool->add_vertex(p1r);
  255. surface_tool->add_normal(p1s.normalized());
  256. surface_tool->add_vertex(p1s);
  257. surface_tool->add_normal(p2s.normalized());
  258. surface_tool->add_vertex(p2s);
  259. surface_tool->add_normal(p1r.normalized());
  260. surface_tool->add_vertex(p1r);
  261. surface_tool->add_normal(p2s.normalized());
  262. surface_tool->add_vertex(p2s);
  263. surface_tool->add_normal(p2r.normalized());
  264. surface_tool->add_vertex(p2r);
  265. if (j==sections-1) {
  266. surface_tool->add_normal(p2r.normalized());
  267. surface_tool->add_vertex(p2r);
  268. surface_tool->add_normal(p2s.normalized());
  269. surface_tool->add_vertex(p2s);
  270. surface_tool->add_normal(Vector3(0,0,1));
  271. surface_tool->add_vertex(Vector3());
  272. }
  273. }
  274. }
  275. Ref<Mesh> mesh = surface_tool->commit();
  276. Ref<FixedMaterial> mat_aabb( memnew( FixedMaterial ));
  277. mat_aabb->set_parameter( FixedMaterial::PARAM_DIFFUSE,Color(0.8,0.8,0.9,0.7) );
  278. mat_aabb->set_line_width(3);
  279. mat_aabb->set_flag( Material::FLAG_UNSHADED, true );
  280. surface_tool->begin(Mesh::PRIMITIVE_LINES);
  281. surface_tool->set_material(mat_aabb);
  282. for(int i=0;i<12;i++) {
  283. Vector3 f,t;
  284. visibility_aabb.get_edge(i,f,t);
  285. surface_tool->add_vertex(f);
  286. surface_tool->add_vertex(t);
  287. }
  288. return surface_tool->commit(mesh);
  289. }
  290. void Particles::_bind_methods() {
  291. ObjectTypeDB::bind_method(_MD("set_amount","amount"),&Particles::set_amount);
  292. ObjectTypeDB::bind_method(_MD("get_amount"),&Particles::get_amount);
  293. ObjectTypeDB::bind_method(_MD("set_emitting","enabled"),&Particles::set_emitting);
  294. ObjectTypeDB::bind_method(_MD("is_emitting"),&Particles::is_emitting);
  295. ObjectTypeDB::bind_method(_MD("set_visibility_aabb","aabb"),&Particles::set_visibility_aabb);
  296. ObjectTypeDB::bind_method(_MD("get_visibility_aabb"),&Particles::get_visibility_aabb);
  297. ObjectTypeDB::bind_method(_MD("set_emission_half_extents","half_extents"),&Particles::set_emission_half_extents);
  298. ObjectTypeDB::bind_method(_MD("get_emission_half_extents"),&Particles::get_emission_half_extents);
  299. ObjectTypeDB::bind_method(_MD("set_emission_base_velocity","base_velocity"),&Particles::set_emission_base_velocity);
  300. ObjectTypeDB::bind_method(_MD("get_emission_base_velocity"),&Particles::get_emission_base_velocity);
  301. ObjectTypeDB::bind_method(_MD("set_emission_points","points"),&Particles::set_emission_points);
  302. ObjectTypeDB::bind_method(_MD("get_emission_points"),&Particles::get_emission_points);
  303. ObjectTypeDB::bind_method(_MD("set_gravity_normal","normal"),&Particles::set_gravity_normal);
  304. ObjectTypeDB::bind_method(_MD("get_gravity_normal"),&Particles::get_gravity_normal);
  305. ObjectTypeDB::bind_method(_MD("set_variable","variable","value"),&Particles::set_variable);
  306. ObjectTypeDB::bind_method(_MD("get_variable","variable"),&Particles::get_variable);
  307. ObjectTypeDB::bind_method(_MD("set_randomness","variable","randomness"),&Particles::set_randomness);
  308. ObjectTypeDB::bind_method(_MD("get_randomness","variable"),&Particles::get_randomness);
  309. ObjectTypeDB::bind_method(_MD("set_color_phase_pos","phase","pos"),&Particles::set_color_phase_pos);
  310. ObjectTypeDB::bind_method(_MD("get_color_phase_pos","phase"),&Particles::get_color_phase_pos);
  311. ObjectTypeDB::bind_method(_MD("set_color_phase_color","phase","color"),&Particles::set_color_phase_color);
  312. ObjectTypeDB::bind_method(_MD("get_color_phase_color","phase"),&Particles::get_color_phase_color);
  313. ObjectTypeDB::bind_method(_MD("set_material","material:Material"),&Particles::set_material);
  314. ObjectTypeDB::bind_method(_MD("get_material:Material"),&Particles::get_material);
  315. ObjectTypeDB::bind_method(_MD("set_emit_timeout","timeout"),&Particles::set_emit_timeout);
  316. ObjectTypeDB::bind_method(_MD("get_emit_timeout"),&Particles::get_emit_timeout);
  317. ObjectTypeDB::bind_method(_MD("set_height_from_velocity","enable"),&Particles::set_height_from_velocity);
  318. ObjectTypeDB::bind_method(_MD("has_height_from_velocity"),&Particles::has_height_from_velocity);
  319. ObjectTypeDB::bind_method(_MD("set_use_local_coordinates","enable"),&Particles::set_use_local_coordinates);
  320. ObjectTypeDB::bind_method(_MD("is_using_local_coordinates"),&Particles::is_using_local_coordinates);
  321. ObjectTypeDB::bind_method(_MD("set_color_phases","count"),&Particles::set_color_phases);
  322. ObjectTypeDB::bind_method(_MD("get_color_phases"),&Particles::get_color_phases);
  323. ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "Material" ), _SCS("set_material"), _SCS("get_material") );
  324. ADD_PROPERTY( PropertyInfo( Variant::INT, "amount", PROPERTY_HINT_RANGE, "1,1024,1" ), _SCS("set_amount"), _SCS("get_amount") );
  325. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "emitting" ), _SCS("set_emitting"), _SCS("is_emitting") );
  326. ADD_PROPERTY( PropertyInfo( Variant::_AABB, "visibility" ), _SCS("set_visibility_aabb"), _SCS("get_visibility_aabb") );
  327. ADD_PROPERTY( PropertyInfo( Variant::VECTOR3, "emission_extents" ), _SCS("set_emission_half_extents"), _SCS("get_emission_half_extents") );
  328. ADD_PROPERTY( PropertyInfo( Variant::VECTOR3, "emission_base_velocity" ), _SCS("set_emission_base_velocity"), _SCS("get_emission_base_velocity") );
  329. ADD_PROPERTY( PropertyInfo( Variant::VECTOR3_ARRAY, "emission_points" ), _SCS("set_emission_points"), _SCS("get_emission_points") );
  330. ADD_PROPERTY( PropertyInfo( Variant::VECTOR3, "gravity_normal" ), _SCS("set_gravity_normal"), _SCS("get_gravity_normal") );
  331. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "local_coords" ), _SCS("set_use_local_coordinates"), _SCS("is_using_local_coordinates") );
  332. ADD_PROPERTY( PropertyInfo( Variant::REAL, "emit_timeout",PROPERTY_HINT_RANGE,"0,256,0.01"), _SCS("set_emit_timeout"), _SCS("get_emit_timeout") );
  333. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "vars/lifetime", PROPERTY_HINT_RANGE,"0.1,60,0.01"), _SCS("set_variable"), _SCS("get_variable"), VAR_LIFETIME );
  334. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "vars/spread", PROPERTY_HINT_RANGE,"0,1,0.01"), _SCS("set_variable"), _SCS("get_variable"), VAR_SPREAD );
  335. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "vars/gravity", PROPERTY_HINT_RANGE,"-48,48,0.01"), _SCS("set_variable"), _SCS("get_variable"), VAR_GRAVITY );
  336. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "vars/linear_vel", PROPERTY_HINT_RANGE,"-100,100,0.01"), _SCS("set_variable"), _SCS("get_variable"), VAR_LINEAR_VELOCITY );
  337. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "vars/angular_vel", PROPERTY_HINT_RANGE,"-100,100,0.01"), _SCS("set_variable"), _SCS("get_variable"), VAR_ANGULAR_VELOCITY );
  338. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "vars/linear_accel", PROPERTY_HINT_RANGE,"-100,100,0.01"), _SCS("set_variable"), _SCS("get_variable"), VAR_LINEAR_ACCELERATION );
  339. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "vars/radial_accel", PROPERTY_HINT_RANGE,"-100,100,0.01"), _SCS("set_variable"), _SCS("get_variable"), VAR_DRAG );
  340. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "vars/tan_accel", PROPERTY_HINT_RANGE,"-100,100,0.01"), _SCS("set_variable"), _SCS("get_variable"), VAR_TANGENTIAL_ACCELERATION );
  341. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "vars/damping", PROPERTY_HINT_RANGE,"0,128,0.01"), _SCS("set_variable"), _SCS("get_variable"), VAR_DAMPING );
  342. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "vars/initial_size", PROPERTY_HINT_RANGE,"0,100,0.01"), _SCS("set_variable"), _SCS("get_variable"), VAR_INITIAL_SIZE );
  343. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "vars/final_size", PROPERTY_HINT_RANGE,"0,100,0.01"), _SCS("set_variable"), _SCS("get_variable"), VAR_FINAL_SIZE );
  344. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "vars/initial_angle",PROPERTY_HINT_RANGE,"0,1,0.01"), _SCS("set_variable"), _SCS("get_variable"), VAR_INITIAL_ANGLE );
  345. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "vars/height_from_velocity"), _SCS("set_height_from_velocity"), _SCS("has_height_from_velocity") );
  346. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "vars/height",PROPERTY_HINT_RANGE,"0,4096,0.01"), _SCS("set_variable"), _SCS("get_variable"), VAR_HEIGHT);
  347. ADD_PROPERTYI( PropertyInfo( Variant::REAL, "vars/height_speed_scale",PROPERTY_HINT_RANGE,"0,4096,0.01"), _SCS("set_variable"), _SCS("get_variable"), VAR_HEIGHT_SPEED_SCALE );
  348. for(int i=0;i<VAR_MAX;i++)
  349. ADD_PROPERTYI( PropertyInfo( Variant::REAL, _rand_names[i], PROPERTY_HINT_RANGE,"-16.0,16.0,0.01"),_SCS("set_randomness"), _SCS("get_randomness"),_var_indices[i] );
  350. ADD_PROPERTY( PropertyInfo( Variant::INT, "color_phases/count",PROPERTY_HINT_RANGE,"0,4,1"), _SCS("set_color_phases"), _SCS("get_color_phases"));
  351. for(int i=0;i<VS::MAX_PARTICLE_COLOR_PHASES;i++) {
  352. String phase="phase_"+itos(i)+"/";
  353. ADD_PROPERTYI( PropertyInfo( Variant::REAL, phase+"pos", PROPERTY_HINT_RANGE,"0,1,0.01"),_SCS("set_color_phase_pos"),_SCS("get_color_phase_pos"),i );
  354. ADD_PROPERTYI( PropertyInfo( Variant::COLOR, phase+"color"),_SCS("set_color_phase_color"),_SCS("get_color_phase_color"),i );
  355. }
  356. BIND_CONSTANT( VAR_LIFETIME );
  357. BIND_CONSTANT( VAR_SPREAD );
  358. BIND_CONSTANT( VAR_GRAVITY );
  359. BIND_CONSTANT( VAR_LINEAR_VELOCITY );
  360. BIND_CONSTANT( VAR_ANGULAR_VELOCITY );
  361. BIND_CONSTANT( VAR_LINEAR_ACCELERATION );
  362. BIND_CONSTANT( VAR_DRAG );
  363. BIND_CONSTANT( VAR_TANGENTIAL_ACCELERATION );
  364. BIND_CONSTANT( VAR_INITIAL_SIZE );
  365. BIND_CONSTANT( VAR_FINAL_SIZE );
  366. BIND_CONSTANT( VAR_INITIAL_ANGLE );
  367. BIND_CONSTANT( VAR_HEIGHT );
  368. BIND_CONSTANT( VAR_HEIGHT_SPEED_SCALE );
  369. BIND_CONSTANT( VAR_MAX );
  370. }
  371. Particles::Particles() {
  372. particles = VisualServer::get_singleton()->particles_create();
  373. timer = memnew(Timer);
  374. add_child(timer);
  375. emit_timeout = 0;
  376. set_amount(64);
  377. set_emitting(true);
  378. set_visibility_aabb(AABB( Vector3(-4,-4,-4), Vector3(8,8,8) ) );
  379. for (int i=0;i<VAR_MAX;i++) {
  380. set_randomness((Variable)i,0.0);
  381. }
  382. set_variable( VAR_LIFETIME, 5.0);
  383. set_variable( VAR_SPREAD, 0.2);
  384. set_variable( VAR_GRAVITY, 9.8);
  385. set_variable( VAR_LINEAR_VELOCITY, 0.2);
  386. set_variable( VAR_ANGULAR_VELOCITY, 0.0);
  387. set_variable( VAR_LINEAR_ACCELERATION, 0.0);
  388. set_variable( VAR_DRAG, 0.0);
  389. set_variable( VAR_TANGENTIAL_ACCELERATION, 0.0);
  390. set_variable( VAR_DAMPING, 0.0);
  391. set_variable( VAR_INITIAL_SIZE, 1.0);
  392. set_variable( VAR_FINAL_SIZE, 1.0);
  393. set_variable( VAR_INITIAL_ANGLE, 0.0);
  394. set_variable( VAR_HEIGHT, 1.0);
  395. set_variable( VAR_HEIGHT_SPEED_SCALE, 0.0);
  396. color_phase_count=0;
  397. set_color_phase_pos(0,0.0);
  398. set_color_phase_pos(1,1.0);
  399. set_color_phase_pos(2,1.0);
  400. set_color_phase_pos(3,1.0);
  401. set_color_phase_color(0,Color(1,1,1));
  402. set_color_phase_color(1,Color(0,0,0));
  403. set_color_phase_color(2,Color(0,0,0));
  404. set_color_phase_color(3,Color(0,0,0));
  405. set_gravity_normal(Vector3(0,-1.0,0));
  406. set_emission_half_extents(Vector3(0.1,0.1,0.1));
  407. height_from_velocity=false;
  408. Vector<Variant> pars;
  409. pars.push_back(false);
  410. timer->connect("timeout", this, "set_emitting", pars);
  411. set_base(particles);
  412. local_coordinates=false;
  413. }
  414. Particles::~Particles() {
  415. VisualServer::get_singleton()->free(particles);
  416. }