Path: blob/master/editor/scene/3d/particles_3d_editor_plugin.cpp
9903 views
/**************************************************************************/1/* particles_3d_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 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/**************************************************************************/2930#include "particles_3d_editor_plugin.h"3132#include "editor/editor_node.h"33#include "editor/editor_undo_redo_manager.h"34#include "editor/scene/scene_tree_editor.h"35#include "scene/3d/cpu_particles_3d.h"36#include "scene/3d/gpu_particles_3d.h"37#include "scene/3d/mesh_instance_3d.h"38#include "scene/gui/box_container.h"39#include "scene/gui/option_button.h"40#include "scene/gui/spin_box.h"41#include "scene/resources/image_texture.h"42#include "scene/resources/particle_process_material.h"4344void Particles3DEditorPlugin::_generate_aabb() {45double time = generate_seconds->get_value();4647double running = 0.0;4849EditorProgress ep("gen_aabb", TTR("Generating Visibility AABB (Waiting for Particle Simulation)"), int(time));5051bool was_emitting = edited_node->get("emitting");52if (!was_emitting) {53edited_node->set("emitting", true);54OS::get_singleton()->delay_usec(1000);55}5657AABB rect;58Callable capture_aabb = Callable(edited_node, "capture_aabb");5960while (running < time) {61uint64_t ticks = OS::get_singleton()->get_ticks_usec();62ep.step(TTR("Generating..."), int(running), true);63OS::get_singleton()->delay_usec(1000);6465AABB capture = capture_aabb.call();66if (rect == AABB()) {67rect = capture;68} else {69rect.merge_with(capture);70}7172running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;73}7475if (!was_emitting) {76edited_node->set("emitting", false);77}7879EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();80ur->create_action(TTR("Generate Visibility AABB"));81ur->add_do_property(edited_node, "visibility_aabb", rect);82ur->add_undo_property(edited_node, "visibility_aabb", edited_node->get("visibility_aabb"));83ur->commit_action();84}8586void Particles3DEditorPlugin::_node_selected(const NodePath &p_path) {87Node *sel = get_node(p_path);88if (!sel) {89return;90}9192if (!sel->is_class("Node3D")) {93EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't inherit from Node3D."), sel->get_name()));94return;95}9697MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(sel);98if (!mi || mi->get_mesh().is_null()) {99EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't contain geometry."), sel->get_name()));100return;101}102103geometry = mi->get_mesh()->get_faces();104if (geometry.is_empty()) {105EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't contain face geometry."), sel->get_name()));106return;107}108109Transform3D geom_xform = edited_node->get("global_transform");110geom_xform = geom_xform.affine_inverse() * mi->get_global_transform();111int gc = geometry.size();112Face3 *w = geometry.ptrw();113114for (int i = 0; i < gc; i++) {115for (int j = 0; j < 3; j++) {116w[i].vertex[j] = geom_xform.xform(w[i].vertex[j]);117}118}119emission_dialog->popup_centered(Size2(300, 130));120}121122void Particles3DEditorPlugin::_menu_callback(int p_idx) {123switch (p_idx) {124case MENU_OPTION_GENERATE_AABB: {125if (need_show_lifetime_dialog(generate_seconds)) {126generate_aabb->popup_centered();127} else {128_generate_aabb();129}130} break;131132case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE: {133if (_can_generate_points()) {134emission_tree_dialog->popup_scenetree_dialog();135}136} break;137138default: {139ParticlesEditorPlugin::_menu_callback(p_idx);140}141}142}143144void Particles3DEditorPlugin::_add_menu_options(PopupMenu *p_menu) {145p_menu->add_item(TTR("Generate AABB"), MENU_OPTION_GENERATE_AABB);146p_menu->add_item(TTR("Create Emission Points From Node"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE);147}148149bool Particles3DEditorPlugin::_generate(Vector<Vector3> &r_points, Vector<Vector3> &r_normals) {150bool use_normals = emission_fill->get_selected() == 1;151152if (emission_fill->get_selected() < 2) {153float area_accum = 0;154RBMap<float, int> triangle_area_map;155156for (int i = 0; i < geometry.size(); i++) {157float area = geometry[i].get_area();158if (area < CMP_EPSILON) {159continue;160}161triangle_area_map[area_accum] = i;162area_accum += area;163}164165if (!triangle_area_map.size() || area_accum == 0) {166EditorNode::get_singleton()->show_warning(TTR("The geometry's faces don't contain any area."));167return false;168}169170int emissor_count = emission_amount->get_value();171172for (int i = 0; i < emissor_count; i++) {173float areapos = Math::random(0.0f, area_accum);174175RBMap<float, int>::Iterator E = triangle_area_map.find_closest(areapos);176ERR_FAIL_COND_V(!E, false);177int index = E->value;178ERR_FAIL_INDEX_V(index, geometry.size(), false);179180// ok FINALLY get face181Face3 face = geometry[index];182//now compute some position inside the face...183184Vector3 pos = face.get_random_point_inside();185186r_points.push_back(pos);187188if (use_normals) {189Vector3 normal = face.get_plane().normal;190r_normals.push_back(normal);191}192}193} else {194int gcount = geometry.size();195196if (gcount == 0) {197EditorNode::get_singleton()->show_warning(TTR("The geometry doesn't contain any faces."));198return false;199}200201const Face3 *r = geometry.ptr();202203AABB aabb;204205for (int i = 0; i < gcount; i++) {206for (int j = 0; j < 3; j++) {207if (i == 0 && j == 0) {208aabb.position = r[i].vertex[j];209} else {210aabb.expand_to(r[i].vertex[j]);211}212}213}214215int emissor_count = emission_amount->get_value();216217for (int i = 0; i < emissor_count; i++) {218int attempts = 5;219220for (int j = 0; j < attempts; j++) {221Vector3 dir;222dir[Math::rand() % 3] = 1.0;223Vector3 ofs = (Vector3(1, 1, 1) - dir) * Vector3(Math::randf(), Math::randf(), Math::randf()) * aabb.size + aabb.position;224225Vector3 ofsv = ofs + aabb.size * dir;226227//space it a little228ofs -= dir;229ofsv += dir;230231float max = -1e7, min = 1e7;232233for (int k = 0; k < gcount; k++) {234const Face3 &f3 = r[k];235236Vector3 res;237if (f3.intersects_segment(ofs, ofsv, &res)) {238res -= ofs;239float d = dir.dot(res);240241if (d < min) {242min = d;243}244if (d > max) {245max = d;246}247}248}249250if (max < min) {251continue; //lost attempt252}253254float val = min + (max - min) * Math::randf();255256Vector3 point = ofs + dir * val;257258r_points.push_back(point);259break;260}261}262}263return true;264}265266Particles3DEditorPlugin::Particles3DEditorPlugin() {267generate_aabb = memnew(ConfirmationDialog);268generate_aabb->set_title(TTR("Generate Visibility AABB"));269270VBoxContainer *genvb = memnew(VBoxContainer);271generate_aabb->add_child(genvb);272273generate_seconds = memnew(SpinBox);274generate_seconds->set_accessibility_name(TTRC("Generation Time (sec)"));275generate_seconds->set_min(0.1);276generate_seconds->set_max(25);277generate_seconds->set_value(2);278genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);279280EditorNode::get_singleton()->get_gui_base()->add_child(generate_aabb);281282generate_aabb->connect(SceneStringName(confirmed), callable_mp(this, &Particles3DEditorPlugin::_generate_aabb));283284emission_tree_dialog = memnew(SceneTreeDialog);285Vector<StringName> valid_types;286valid_types.push_back("MeshInstance3D");287emission_tree_dialog->set_valid_types(valid_types);288EditorNode::get_singleton()->get_gui_base()->add_child(emission_tree_dialog);289emission_tree_dialog->connect("selected", callable_mp(this, &Particles3DEditorPlugin::_node_selected));290291emission_dialog = memnew(ConfirmationDialog);292emission_dialog->set_title(TTR("Create Emitter"));293EditorNode::get_singleton()->get_gui_base()->add_child(emission_dialog);294295VBoxContainer *emd_vb = memnew(VBoxContainer);296emission_dialog->add_child(emd_vb);297298emission_amount = memnew(SpinBox);299emission_amount->set_accessibility_name(TTRC("Emission Points:"));300emission_amount->set_min(1);301emission_amount->set_max(100000);302emission_amount->set_value(512);303emd_vb->add_margin_child(TTR("Emission Points:"), emission_amount);304305emission_fill = memnew(OptionButton);306emission_fill->set_accessibility_name(TTRC("Emission Source:"));307emission_fill->add_item(TTR("Surface Points"));308emission_fill->add_item(TTR("Surface Points+Normal (Directed)"));309emission_fill->add_item(TTR("Volume"));310emd_vb->add_margin_child(TTR("Emission Source:"), emission_fill);311312emission_dialog->set_ok_button_text(TTR("Create"));313emission_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Particles3DEditorPlugin::_generate_emission_points));314}315316Node *GPUParticles3DEditorPlugin::_convert_particles() {317GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(edited_node);318319CPUParticles3D *cpu_particles = memnew(CPUParticles3D);320cpu_particles->convert_from_particles(particles);321cpu_particles->set_name(particles->get_name());322cpu_particles->set_transform(particles->get_transform());323cpu_particles->set_visible(particles->is_visible());324cpu_particles->set_process_mode(particles->get_process_mode());325return cpu_particles;326}327328bool GPUParticles3DEditorPlugin::_can_generate_points() const {329GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(edited_node);330Ref<ParticleProcessMaterial> mat = particles->get_process_material();331if (mat.is_null()) {332EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticleProcessMaterial' is required."));333return false;334}335return true;336}337338void GPUParticles3DEditorPlugin::_generate_emission_points() {339GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(edited_node);340341/// hacer codigo aca342Vector<Vector3> points;343Vector<Vector3> normals;344345if (!_generate(points, normals)) {346return;347}348349int point_count = points.size();350351int w = 2048;352int h = (point_count / 2048) + 1;353354Vector<uint8_t> point_img;355point_img.resize(w * h * 3 * sizeof(float));356357{358uint8_t *iw = point_img.ptrw();359memset(iw, 0, w * h * 3 * sizeof(float));360const Vector3 *r = points.ptr();361float *wf = reinterpret_cast<float *>(iw);362for (int i = 0; i < point_count; i++) {363wf[i * 3 + 0] = r[i].x;364wf[i * 3 + 1] = r[i].y;365wf[i * 3 + 2] = r[i].z;366}367}368369Ref<Image> image = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img));370Ref<ImageTexture> tex = ImageTexture::create_from_image(image);371372Ref<ParticleProcessMaterial> mat = particles->get_process_material();373ERR_FAIL_COND(mat.is_null());374375EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();376undo_redo->create_action(TTR("Create Emission Points"));377ParticleProcessMaterial *matptr = mat.ptr();378379if (!normals.is_empty()) {380undo_redo->add_do_property(matptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);381undo_redo->add_undo_property(matptr, "emission_shape", matptr->get_emission_shape());382383Vector<uint8_t> point_img2;384point_img2.resize(w * h * 3 * sizeof(float));385386{387uint8_t *iw = point_img2.ptrw();388memset(iw, 0, w * h * 3 * sizeof(float));389const Vector3 *r = normals.ptr();390float *wf = reinterpret_cast<float *>(iw);391for (int i = 0; i < point_count; i++) {392wf[i * 3 + 0] = r[i].x;393wf[i * 3 + 1] = r[i].y;394wf[i * 3 + 2] = r[i].z;395}396}397398Ref<Image> image2 = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img2));399undo_redo->add_do_property(matptr, "emission_normal_texture", ImageTexture::create_from_image(image2));400undo_redo->add_undo_property(matptr, "emission_normal_texture", matptr->get_emission_normal_texture());401} else {402undo_redo->add_do_property(matptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_POINTS);403undo_redo->add_undo_property(matptr, "emission_shape", matptr->get_emission_shape());404}405undo_redo->add_do_property(matptr, "emission_point_count", point_count);406undo_redo->add_undo_property(matptr, "emission_point_count", matptr->get_emission_point_count());407undo_redo->add_do_property(matptr, "emission_point_texture", tex);408undo_redo->add_undo_property(matptr, "emission_point_texture", matptr->get_emission_point_texture());409undo_redo->commit_action();410}411412GPUParticles3DEditorPlugin::GPUParticles3DEditorPlugin() {413handled_type = TTRC("GPUParticles3D");414conversion_option_name = TTR("Convert to CPUParticles3D");415}416417Node *CPUParticles3DEditorPlugin::_convert_particles() {418CPUParticles3D *particles = Object::cast_to<CPUParticles3D>(edited_node);419420GPUParticles3D *gpu_particles = memnew(GPUParticles3D);421gpu_particles->convert_from_particles(particles);422gpu_particles->set_name(particles->get_name());423gpu_particles->set_transform(particles->get_transform());424gpu_particles->set_visible(particles->is_visible());425gpu_particles->set_process_mode(particles->get_process_mode());426return gpu_particles;427}428429void CPUParticles3DEditorPlugin::_generate_emission_points() {430CPUParticles3D *particles = Object::cast_to<CPUParticles3D>(edited_node);431432/// hacer codigo aca433Vector<Vector3> points;434Vector<Vector3> normals;435436if (!_generate(points, normals)) {437return;438}439440EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();441undo_redo->create_action(TTR("Create Emission Points"));442443if (normals.is_empty()) {444undo_redo->add_do_property(particles, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_POINTS);445undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());446} else {447undo_redo->add_do_property(particles, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);448undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());449undo_redo->add_do_property(particles, "emission_normals", normals);450undo_redo->add_undo_property(particles, "emission_normals", particles->get_emission_normals());451}452undo_redo->add_do_property(particles, "emission_points", points);453undo_redo->add_undo_property(particles, "emission_points", particles->get_emission_points());454undo_redo->commit_action();455}456457CPUParticles3DEditorPlugin::CPUParticles3DEditorPlugin() {458handled_type = TTRC("CPUParticles3D");459conversion_option_name = TTR("Convert to GPUParticles3D");460}461462463