diff options
Diffstat (limited to 'editor/plugins/particles_editor_plugin.cpp')
| -rw-r--r-- | editor/plugins/particles_editor_plugin.cpp | 350 |
1 files changed, 192 insertions, 158 deletions
diff --git a/editor/plugins/particles_editor_plugin.cpp b/editor/plugins/particles_editor_plugin.cpp index fd26674a0..962403024 100644 --- a/editor/plugins/particles_editor_plugin.cpp +++ b/editor/plugins/particles_editor_plugin.cpp @@ -27,30 +27,24 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#if 0 #include "particles_editor_plugin.h" #include "editor/plugins/spatial_editor_plugin.h" #include "io/resource_loader.h" -#include "servers/visual/particle_system_sw.h" - void ParticlesEditor::_node_removed(Node *p_node) { - if(p_node==node) { - node=NULL; + if (p_node == node) { + node = NULL; hide(); } - } - -void ParticlesEditor::_resource_seleted(const String& p_res) { +void ParticlesEditor::_resource_seleted(const String &p_res) { //print_line("selected resource path: "+p_res); } -void ParticlesEditor::_node_selected(const NodePath& p_path){ - +void ParticlesEditor::_node_selected(const NodePath &p_path) { Node *sel = get_node(p_path); if (!sel) @@ -66,12 +60,11 @@ void ParticlesEditor::_node_selected(const NodePath& p_path){ geometry = vi->get_faces(VisualInstance::FACES_SOLID); - if (geometry.size()==0) { + if (geometry.size() == 0) { err_dialog->set_text(TTR("Node does not contain geometry (faces).")); err_dialog->popup_centered_minsize(); return; - } Transform geom_xform = node->get_global_transform().affine_inverse() * vi->get_global_transform(); @@ -79,20 +72,17 @@ void ParticlesEditor::_node_selected(const NodePath& p_path){ int gc = geometry.size(); PoolVector<Face3>::Write w = geometry.write(); - - for(int i=0;i<gc;i++) { - for(int j=0;j<3;j++) { - w[i].vertex[j] = geom_xform.xform( w[i].vertex[j] ); + for (int i = 0; i < gc; i++) { + for (int j = 0; j < 3; j++) { + w[i].vertex[j] = geom_xform.xform(w[i].vertex[j]); } } - w = PoolVector<Face3>::Write(); - emission_dialog->popup_centered(Size2(300,130)); + emission_dialog->popup_centered(Size2(300, 130)); } - /* void ParticlesEditor::_populate() { @@ -112,74 +102,77 @@ void ParticlesEditor::_populate() { void ParticlesEditor::_notification(int p_notification) { - if (p_notification==NOTIFICATION_ENTER_TREE) { - options->set_icon(options->get_popup()->get_icon("Particles","EditorIcons")); - + if (p_notification == NOTIFICATION_ENTER_TREE) { + options->set_icon(options->get_popup()->get_icon("Particles", "EditorIcons")); } } - void ParticlesEditor::_menu_option(int p_option) { - - switch(p_option) { + switch (p_option) { case MENU_OPTION_GENERATE_AABB: { - +#if 0 Transform globalizer = node->get_global_transform(); ParticleSystemSW pssw; - for(int i=0;i<VS::PARTICLE_VAR_MAX;i++) { + for (int i = 0; i < VS::PARTICLE_VAR_MAX; i++) { - pssw.particle_vars[i]=node->get_variable((Particles::Variable)i); - pssw.particle_randomness[i]=node->get_randomness((Particles::Variable)i); + pssw.particle_vars[i] = node->get_variable((Particles::Variable)i); + pssw.particle_randomness[i] = node->get_randomness((Particles::Variable)i); } - pssw.emission_half_extents=node->get_emission_half_extents(); - pssw.emission_points=node->get_emission_points(); - pssw.emission_base_velocity=node->get_emission_base_velocity(); - pssw.amount=node->get_amount(); - pssw.gravity_normal=node->get_gravity_normal(); - pssw.emitting=true; - pssw.height_from_velocity=node->has_height_from_velocity(); - pssw.color_phase_count=1; - + pssw.emission_half_extents = node->get_emission_half_extents(); + pssw.emission_points = node->get_emission_points(); + pssw.emission_base_velocity = node->get_emission_base_velocity(); + pssw.amount = node->get_amount(); + pssw.gravity_normal = node->get_gravity_normal(); + pssw.emitting = true; + pssw.height_from_velocity = node->has_height_from_velocity(); + pssw.color_phase_count = 1; ParticleSystemProcessSW pp; - float delta=0.01; - float lifetime=pssw.particle_vars[VS::PARTICLE_LIFETIME]; - + float delta = 0.01; + float lifetime = pssw.particle_vars[VS::PARTICLE_LIFETIME]; Transform localizer = globalizer.affine_inverse(); AABB aabb; - for(float t=0;t<lifetime;t+=delta) { + for (float t = 0; t < lifetime; t += delta) { - pp.process(&pssw,globalizer,delta); - for(int i=0;i<pp.particle_data.size();i++) { + pp.process(&pssw, globalizer, delta); + for (int i = 0; i < pp.particle_data.size(); i++) { Vector3 p = localizer.xform(pp.particle_data[i].pos); - if (t==0 && i==0) - aabb.pos=p; + if (t == 0 && i == 0) + aabb.pos = p; else aabb.expand_to(p); } } - aabb.grow_by( aabb.get_longest_axis_size()*0.2); + aabb.grow_by(aabb.get_longest_axis_size() * 0.2); node->set_visibility_aabb(aabb); - - +#endif } break; case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH: { - + Ref<ParticlesMaterial> material = node->get_process_material(); + if (material.is_null()) { + EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticlesMaterial' is required.")); + return; + } emission_file_dialog->popup_centered_ratio(); } break; case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE: { -/* + Ref<ParticlesMaterial> material = node->get_process_material(); + if (material.is_null()) { + EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticlesMaterial' is required.")); + return; + } + /* Node *root = get_scene()->get_root_node(); ERR_FAIL_COND(!root); EditorNode *en = root->cast_to<EditorNode>(); @@ -192,50 +185,50 @@ void ParticlesEditor::_menu_option(int p_option) { } } - void ParticlesEditor::edit(Particles *p_particles) { - node=p_particles; - + node = p_particles; } void ParticlesEditor::_generate_emission_points() { /// hacer codigo aca - PoolVector<Vector3> points; + PoolVector<float> points; + bool use_normals = emission_fill->get_selected() == 1; + PoolVector<float> normals; - if (emission_fill->get_selected()==0) { + if (emission_fill->get_selected() < 2) { - float area_accum=0; - Map<float,int> triangle_area_map; - print_line("geometry size: "+itos(geometry.size())); + float area_accum = 0; + Map<float, int> triangle_area_map; + print_line("geometry size: " + itos(geometry.size())); - for(int i=0;i<geometry.size();i++) { + for (int i = 0; i < geometry.size(); i++) { float area = geometry[i].get_area(); - if (area<CMP_EPSILON) + if (area < CMP_EPSILON) continue; - triangle_area_map[area_accum]=i; - area_accum+=area; + triangle_area_map[area_accum] = i; + area_accum += area; } - if (!triangle_area_map.size() || area_accum==0) { + if (!triangle_area_map.size() || area_accum == 0) { err_dialog->set_text(TTR("Faces contain no area!")); err_dialog->popup_centered_minsize(); return; } - int emissor_count=emission_amount->get_val(); + int emissor_count = emission_amount->get_value(); - for(int i=0;i<emissor_count;i++) { + for (int i = 0; i < emissor_count; i++) { - float areapos = Math::random(0,area_accum); + float areapos = Math::random(0.0f, area_accum); - Map<float,int>::Element *E = triangle_area_map.find_closest(areapos); + Map<float, int>::Element *E = triangle_area_map.find_closest(areapos); ERR_FAIL_COND(!E) int index = E->get(); - ERR_FAIL_INDEX(index,geometry.size()); + ERR_FAIL_INDEX(index, geometry.size()); // ok FINALLY get face Face3 face = geometry[index]; @@ -243,13 +236,22 @@ void ParticlesEditor::_generate_emission_points() { Vector3 pos = face.get_random_point_inside(); - points.push_back(pos); + points.push_back(pos.x); + points.push_back(pos.y); + points.push_back(pos.z); + + if (use_normals) { + Vector3 normal = face.get_plane().normal; + normals.push_back(normal.x); + normals.push_back(normal.y); + normals.push_back(normal.z); + } } } else { int gcount = geometry.size(); - if (gcount==0) { + if (gcount == 0) { err_dialog->set_text(TTR("No faces!")); err_dialog->popup_centered_minsize(); @@ -258,32 +260,32 @@ void ParticlesEditor::_generate_emission_points() { PoolVector<Face3>::Read r = geometry.read(); - AABB aabb; + Rect3 aabb; - for(int i=0;i<gcount;i++) { + for (int i = 0; i < gcount; i++) { - for(int j=0;j<3;j++) { + for (int j = 0; j < 3; j++) { - if (i==0 && j==0) - aabb.pos=r[i].vertex[j]; + if (i == 0 && j == 0) + aabb.pos = r[i].vertex[j]; else aabb.expand_to(r[i].vertex[j]); } } - int emissor_count=emission_amount->get_val(); + int emissor_count = emission_amount->get_value(); - for(int i=0;i<emissor_count;i++) { + for (int i = 0; i < emissor_count; i++) { - int attempts=5; + int attempts = 5; - for(int j=0;j<attempts;j++) { + for (int j = 0; j < attempts; j++) { Vector3 dir; - dir[Math::rand()%3]=1.0; - Vector3 ofs = Vector3(1,1,1)-dir; - ofs=(Vector3(1,1,1)-dir)*Vector3(Math::randf(),Math::randf(),Math::randf())*aabb.size; - ofs+=aabb.pos; + dir[Math::rand() % 3] = 1.0; + Vector3 ofs = Vector3(1, 1, 1) - dir; + ofs = (Vector3(1, 1, 1) - dir) * Vector3(Math::randf(), Math::randf(), Math::randf()) * aabb.size; + ofs += aabb.pos; Vector3 ofsv = ofs + aabb.size * dir; @@ -291,135 +293,172 @@ void ParticlesEditor::_generate_emission_points() { ofs -= dir; ofsv += dir; - float max=-1e7,min=1e7; + float max = -1e7, min = 1e7; - for(int k=0;k<gcount;k++) { + for (int k = 0; k < gcount; k++) { - const Face3& f3 = r[k]; + const Face3 &f3 = r[k]; Vector3 res; - if (f3.intersects_segment(ofs,ofsv,&res)) { + if (f3.intersects_segment(ofs, ofsv, &res)) { - res-=ofs; + res -= ofs; float d = dir.dot(res); - if (d<min) - min=d; - if (d>max) - max=d; - + if (d < min) + min = d; + if (d > max) + max = d; } } - - if (max<min) + if (max < min) continue; //lost attempt - float val = min + (max-min)*Math::randf(); + float val = min + (max - min) * Math::randf(); Vector3 point = ofs + dir * val; - points.push_back(point); + points.push_back(point.x); + points.push_back(point.y); + points.push_back(point.z); break; } } } - //print_line("point count: "+itos(points.size())); - node->set_emission_points(points); + int point_count = points.size() / 3; + + int w = 2048; + int h = (point_count / 2048) + 1; + PoolVector<uint8_t> point_img; + point_img.resize(w * h * 3 * sizeof(float)); + + { + PoolVector<uint8_t>::Write iw = point_img.write(); + zeromem(iw.ptr(), w * h * 3 * sizeof(float)); + PoolVector<float>::Read r = points.read(); + copymem(iw.ptr(), r.ptr(), point_count * sizeof(float) * 3); + } + + Image image(w, h, false, Image::FORMAT_RGBF, point_img); + + Ref<ImageTexture> tex; + tex.instance(); + tex->create_from_image(image, Texture::FLAG_FILTER); + + Ref<ParticlesMaterial> material = node->get_process_material(); + ERR_FAIL_COND(material.is_null()); + + if (use_normals) { + + material->set_emission_shape(ParticlesMaterial::EMISSION_SHAPE_DIRECTED_POINTS); + material->set_emission_point_count(point_count); + material->set_emission_point_texture(tex); + + PoolVector<uint8_t> point_img2; + point_img2.resize(w * h * 3 * sizeof(float)); + + { + PoolVector<uint8_t>::Write iw = point_img2.write(); + zeromem(iw.ptr(), w * h * 3 * sizeof(float)); + PoolVector<float>::Read r = normals.read(); + copymem(iw.ptr(), r.ptr(), point_count * sizeof(float) * 3); + } + + Image image2(w, h, false, Image::FORMAT_RGBF, point_img2); + + Ref<ImageTexture> tex2; + tex2.instance(); + tex2->create_from_image(image2, Texture::FLAG_FILTER); + + material->set_emission_normal_texture(tex2); + } else { + + material->set_emission_shape(ParticlesMaterial::EMISSION_SHAPE_POINTS); + material->set_emission_point_count(point_count); + material->set_emission_point_texture(tex); + } + + //print_line("point count: "+itos(points.size())); + //node->set_emission_points(points); } void ParticlesEditor::_bind_methods() { - ClassDB::bind_method("_menu_option",&ParticlesEditor::_menu_option); - ClassDB::bind_method("_resource_seleted",&ParticlesEditor::_resource_seleted); - ClassDB::bind_method("_node_selected",&ParticlesEditor::_node_selected); - ClassDB::bind_method("_generate_emission_points",&ParticlesEditor::_generate_emission_points); + ClassDB::bind_method("_menu_option", &ParticlesEditor::_menu_option); + ClassDB::bind_method("_resource_seleted", &ParticlesEditor::_resource_seleted); + ClassDB::bind_method("_node_selected", &ParticlesEditor::_node_selected); + ClassDB::bind_method("_generate_emission_points", &ParticlesEditor::_generate_emission_points); //ClassDB::bind_method("_populate",&ParticlesEditor::_populate); - } ParticlesEditor::ParticlesEditor() { - particles_editor_hb = memnew ( HBoxContainer ); + particles_editor_hb = memnew(HBoxContainer); SpatialEditor::get_singleton()->add_control_to_menu_panel(particles_editor_hb); - options = memnew( MenuButton ); + options = memnew(MenuButton); particles_editor_hb->add_child(options); particles_editor_hb->hide(); options->set_text("Particles"); - options->get_popup()->add_item(TTR("Generate AABB"),MENU_OPTION_GENERATE_AABB); + options->get_popup()->add_item(TTR("Generate AABB"), MENU_OPTION_GENERATE_AABB); options->get_popup()->add_separator(); - options->get_popup()->add_item(TTR("Create Emitter From Mesh"),MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH); - options->get_popup()->add_item(TTR("Create Emitter From Node"),MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE); - options->get_popup()->add_item(TTR("Clear Emitter"),MENU_OPTION_CLEAR_EMISSION_VOLUME); + options->get_popup()->add_item(TTR("Create Emission Points From Mesh"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH); + options->get_popup()->add_item(TTR("Create Emission Points From Node"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE); + // options->get_popup()->add_item(TTR("Clear Emitter"), MENU_OPTION_CLEAR_EMISSION_VOLUME); - options->get_popup()->connect("id_pressed", this,"_menu_option"); + options->get_popup()->connect("id_pressed", this, "_menu_option"); - emission_dialog = memnew( ConfirmationDialog ); + emission_dialog = memnew(ConfirmationDialog); emission_dialog->set_title(TTR("Create Emitter")); add_child(emission_dialog); - Label *l = memnew(Label); - l->set_pos(Point2(5,5)); - l->set_text(TTR("Emission Positions:")); - emission_dialog->add_child(l); - + VBoxContainer *emd_vb = memnew(VBoxContainer); + emission_dialog->add_child(emd_vb); - emission_amount = memnew( SpinBox ); - emission_amount->set_anchor(MARGIN_RIGHT,ANCHOR_END); - emission_amount->set_begin( Point2(20,23)); - emission_amount->set_end( Point2(5,25)); + emission_amount = memnew(SpinBox); emission_amount->set_min(1); - emission_amount->set_max(65536); - emission_amount->set_val(512); - emission_dialog->add_child(emission_amount); - emission_dialog->get_ok()->set_text(TTR("Create")); - emission_dialog->connect("confirmed",this,"_generate_emission_points"); - - l = memnew(Label); - l->set_pos(Point2(5,50)); - l->set_text(TTR("Emission Fill:")); - emission_dialog->add_child(l); + emission_amount->set_max(100000); + emission_amount->set_value(512); + emd_vb->add_margin_child(TTR("Emission Points:"), emission_amount); - emission_fill = memnew( OptionButton ); - emission_fill->set_anchor(MARGIN_RIGHT,ANCHOR_END); - emission_fill->set_begin( Point2(20,70)); - emission_fill->set_end( Point2(5,75)); - emission_fill->add_item(TTR("Surface")); + emission_fill = memnew(OptionButton); + emission_fill->add_item(TTR("Surface Points")); + emission_fill->add_item(TTR("Surface Points+Normal (Directed)")); emission_fill->add_item(TTR("Volume")); - emission_dialog->add_child(emission_fill); + emd_vb->add_margin_child(TTR("Emission Source: "), emission_fill); + + emission_dialog->get_ok()->set_text(TTR("Create")); + emission_dialog->connect("confirmed", this, "_generate_emission_points"); - err_dialog = memnew( ConfirmationDialog ); + err_dialog = memnew(ConfirmationDialog); //err_dialog->get_cancel()->hide(); add_child(err_dialog); - - emission_file_dialog = memnew( EditorFileDialog ); + emission_file_dialog = memnew(EditorFileDialog); add_child(emission_file_dialog); - emission_file_dialog->connect("file_selected",this,"_resource_seleted"); - emission_tree_dialog = memnew( SceneTreeDialog ); + emission_file_dialog->connect("file_selected", this, "_resource_seleted"); + emission_tree_dialog = memnew(SceneTreeDialog); add_child(emission_tree_dialog); - emission_tree_dialog->connect("selected",this,"_node_selected"); + emission_tree_dialog->connect("selected", this, "_node_selected"); List<String> extensions; - ResourceLoader::get_recognized_extensions_for_type("Mesh",&extensions); + ResourceLoader::get_recognized_extensions_for_type("Mesh", &extensions); emission_file_dialog->clear_filters(); - for(int i=0;i<extensions.size();i++) { + for (int i = 0; i < extensions.size(); i++) { - emission_file_dialog->add_filter("*."+extensions[i]+" ; "+extensions[i].to_upper()); + emission_file_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper()); } emission_file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE); //options->set_anchor(MARGIN_LEFT,Control::ANCHOR_END); //options->set_anchor(MARGIN_RIGHT,Control::ANCHOR_END); - } - void ParticlesEditorPlugin::edit(Object *p_object) { particles_editor->edit(p_object->cast_to<Particles>()); @@ -427,7 +466,7 @@ void ParticlesEditorPlugin::edit(Object *p_object) { bool ParticlesEditorPlugin::handles(Object *p_object) const { - return p_object->is_type("Particles"); + return p_object->is_class("Particles"); } void ParticlesEditorPlugin::make_visible(bool p_visible) { @@ -440,21 +479,16 @@ void ParticlesEditorPlugin::make_visible(bool p_visible) { particles_editor->hide(); particles_editor->edit(NULL); } - } ParticlesEditorPlugin::ParticlesEditorPlugin(EditorNode *p_node) { - editor=p_node; - particles_editor = memnew( ParticlesEditor ); + editor = p_node; + particles_editor = memnew(ParticlesEditor); editor->get_viewport()->add_child(particles_editor); particles_editor->hide(); } - -ParticlesEditorPlugin::~ParticlesEditorPlugin() -{ +ParticlesEditorPlugin::~ParticlesEditorPlugin() { } - -#endif |
