Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Create Emission Points From Mesh" option to the particle editor plugin #84124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions editor/plugins/cpu_particles_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "editor/editor_node.h"
#include "editor/editor_undo_redo_manager.h"
#include "editor/gui/editor_file_dialog.h"
#include "editor/gui/scene_tree_editor.h"
#include "editor/plugins/node_3d_editor_plugin.h"
#include "editor/scene_tree_dock.h"
Expand All @@ -54,6 +55,10 @@ void CPUParticles3DEditor::_notification(int p_notification) {

void CPUParticles3DEditor::_menu_option(int p_option) {
switch (p_option) {
case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH: {
emission_file_dialog->popup_file_dialog();

} break;
case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE: {
emission_tree_dialog->popup_scenetree_dialog();

Expand Down Expand Up @@ -117,6 +122,7 @@ CPUParticles3DEditor::CPUParticles3DEditor() {

options->set_text(TTR("CPUParticles3D"));
options->get_popup()->add_item(TTR("Restart"), MENU_OPTION_RESTART);
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("Convert to GPUParticles3D"), MENU_OPTION_CONVERT_TO_GPU_PARTICLES);
options->get_popup()->connect("id_pressed", callable_mp(this, &CPUParticles3DEditor::_menu_option));
Expand Down
1 change: 1 addition & 0 deletions editor/plugins/cpu_particles_3d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class CPUParticles3DEditor : public GPUParticles3DEditorBase {

enum Menu {
MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE,
MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH,
MENU_OPTION_CLEAR_EMISSION_VOLUME,
MENU_OPTION_RESTART,
MENU_OPTION_CONVERT_TO_GPU_PARTICLES,
Expand Down
45 changes: 45 additions & 0 deletions editor/plugins/gpu_particles_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@

#include "gpu_particles_3d_editor_plugin.h"

#include "core/io/resource_importer.h"
#include "core/io/resource_loader.h"
#include "editor/editor_node.h"
#include "editor/editor_undo_redo_manager.h"
#include "editor/gui/editor_file_dialog.h"
#include "editor/plugins/node_3d_editor_plugin.h"
#include "editor/scene_tree_dock.h"
#include "scene/3d/cpu_particles_3d.h"
Expand Down Expand Up @@ -197,6 +199,31 @@ void GPUParticles3DEditorBase::_node_selected(const NodePath &p_path) {
emission_dialog->popup_centered(Size2(300, 130));
}

void GPUParticles3DEditorBase::_resource_selected(const String &p_path) {
Ref<Resource> loaded_resource = ResourceLoader::load(p_path);
ERR_FAIL_COND_MSG(loaded_resource.is_null(), "Cannot load resource from path '" + p_path + "'.");

geometry = Object::cast_to<ArrayMesh>(*loaded_resource)->get_faces();

if (geometry.size() == 0) {
EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't contain face geometry."), p_path));
return;
}

Transform3D geom_xform = base_node->get_global_transform();

int gc = geometry.size();
Face3 *w = geometry.ptrw();

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]);
}
}

emission_dialog->popup_centered(Size2(300, 130));
}

void GPUParticles3DEditorBase::_bind_methods() {
}

Expand Down Expand Up @@ -225,6 +252,13 @@ GPUParticles3DEditorBase::GPUParticles3DEditorBase() {
emission_tree_dialog = memnew(SceneTreeDialog);
add_child(emission_tree_dialog);
emission_tree_dialog->connect("selected", callable_mp(this, &GPUParticles3DEditorBase::_node_selected));

emission_file_dialog = memnew(EditorFileDialog);
emission_file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
Vector<String> filters = { "*.tres", "*.res", "*.mesh", "*.obj" };
emission_file_dialog->set_filters(filters);
add_child(emission_file_dialog);
emission_file_dialog->connect("file_selected", callable_mp(this, &GPUParticles3DEditorBase::_resource_selected));
}

void GPUParticles3DEditor::_node_removed(Node *p_node) {
Expand Down Expand Up @@ -257,6 +291,16 @@ void GPUParticles3DEditor::_menu_option(int p_option) {
_generate_aabb();
}
} break;
case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH: {
Ref<ParticleProcessMaterial> mat = node->get_process_material();
if (mat.is_null()) {
EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticleProcessMaterial' is required."));
return;
}

emission_file_dialog->popup_file_dialog();

} break;
case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE: {
Ref<ParticleProcessMaterial> mat = node->get_process_material();
if (mat.is_null()) {
Expand Down Expand Up @@ -413,6 +457,7 @@ GPUParticles3DEditor::GPUParticles3DEditor() {
options->set_text(TTR("GPUParticles3D"));
options->get_popup()->add_item(TTR("Restart"), MENU_OPTION_RESTART);
options->get_popup()->add_item(TTR("Generate AABB"), MENU_OPTION_GENERATE_AABB);
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("Convert to CPUParticles3D"), MENU_OPTION_CONVERT_TO_CPU_PARTICLES);

Expand Down
4 changes: 4 additions & 0 deletions editor/plugins/gpu_particles_3d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class HBoxContainer;
class MenuButton;
class OptionButton;
class SceneTreeDialog;
class EditorFileDialog;

class GPUParticles3DEditorBase : public Control {
GDCLASS(GPUParticles3DEditorBase, Control);
Expand All @@ -51,6 +52,7 @@ class GPUParticles3DEditorBase : public Control {
HBoxContainer *particles_editor_hb = nullptr;

SceneTreeDialog *emission_tree_dialog = nullptr;
EditorFileDialog *emission_file_dialog = nullptr;

ConfirmationDialog *emission_dialog = nullptr;
SpinBox *emission_amount = nullptr;
Expand All @@ -61,6 +63,7 @@ class GPUParticles3DEditorBase : public Control {
bool _generate(Vector<Vector3> &points, Vector<Vector3> &normals);
virtual void _generate_emission_points(){};
void _node_selected(const NodePath &p_path);
void _resource_selected(const String &p_path);

static void _bind_methods();

Expand All @@ -78,6 +81,7 @@ class GPUParticles3DEditor : public GPUParticles3DEditorBase {
enum Menu {
MENU_OPTION_GENERATE_AABB,
MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE,
MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH,
MENU_OPTION_CLEAR_EMISSION_VOLUME,
MENU_OPTION_CONVERT_TO_CPU_PARTICLES,
MENU_OPTION_RESTART,
Expand Down
Loading