Path: blob/master/editor/scene/3d/mesh_editor_plugin.cpp
9903 views
/**************************************************************************/1/* mesh_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 "mesh_editor_plugin.h"3132#include "core/config/project_settings.h"33#include "editor/themes/editor_scale.h"34#include "scene/gui/button.h"35#include "scene/main/viewport.h"3637void MeshEditor::gui_input(const Ref<InputEvent> &p_event) {38ERR_FAIL_COND(p_event.is_null());3940Ref<InputEventMouseMotion> mm = p_event;41if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {42rot_x -= mm->get_relative().y * 0.01;43rot_y -= mm->get_relative().x * 0.01;4445rot_x = CLAMP(rot_x, -Math::PI / 2, Math::PI / 2);46_update_rotation();47}48}4950void MeshEditor::_update_theme_item_cache() {51SubViewportContainer::_update_theme_item_cache();5253theme_cache.light_1_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight1"));54theme_cache.light_2_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight2"));55}5657void MeshEditor::_notification(int p_what) {58switch (p_what) {59case NOTIFICATION_THEME_CHANGED: {60light_1_switch->set_button_icon(theme_cache.light_1_icon);61light_2_switch->set_button_icon(theme_cache.light_2_icon);62} break;63}64}6566void MeshEditor::_update_rotation() {67Transform3D t;68t.basis.rotate(Vector3(0, 1, 0), -rot_y);69t.basis.rotate(Vector3(1, 0, 0), -rot_x);70rotation->set_transform(t);71}7273void MeshEditor::edit(Ref<Mesh> p_mesh) {74mesh = p_mesh;75mesh_instance->set_mesh(mesh);7677rot_x = Math::deg_to_rad(-15.0);78rot_y = Math::deg_to_rad(30.0);79_update_rotation();8081AABB aabb = mesh->get_aabb();82Vector3 ofs = aabb.get_center();83float m = aabb.get_longest_axis_size();84if (m != 0) {85m = 1.0 / m;86m *= 0.5;87Transform3D xform;88xform.basis.scale(Vector3(m, m, m));89xform.origin = -xform.basis.xform(ofs); //-ofs*m;90//xform.origin.z -= aabb.get_longest_axis_size() * 2;91mesh_instance->set_transform(xform);92}93}9495void MeshEditor::_on_light_1_switch_pressed() {96light1->set_visible(light_1_switch->is_pressed());97}9899void MeshEditor::_on_light_2_switch_pressed() {100light2->set_visible(light_2_switch->is_pressed());101}102103MeshEditor::MeshEditor() {104viewport = memnew(SubViewport);105Ref<World3D> world_3d;106world_3d.instantiate();107viewport->set_world_3d(world_3d); // Use own world.108add_child(viewport);109viewport->set_disable_input(true);110viewport->set_msaa_3d(Viewport::MSAA_4X);111set_stretch(true);112camera = memnew(Camera3D);113camera->set_transform(Transform3D(Basis(), Vector3(0, 0, 1.1)));114camera->set_perspective(45, 0.1, 10);115viewport->add_child(camera);116117if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {118camera_attributes.instantiate();119camera->set_attributes(camera_attributes);120}121122light1 = memnew(DirectionalLight3D);123light1->set_transform(Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));124viewport->add_child(light1);125126light2 = memnew(DirectionalLight3D);127light2->set_transform(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));128light2->set_color(Color(0.7, 0.7, 0.7));129viewport->add_child(light2);130131rotation = memnew(Node3D);132viewport->add_child(rotation);133mesh_instance = memnew(MeshInstance3D);134rotation->add_child(mesh_instance);135136set_custom_minimum_size(Size2(1, 150) * EDSCALE);137138HBoxContainer *hb = memnew(HBoxContainer);139add_child(hb);140hb->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 2);141142hb->add_spacer();143144VBoxContainer *vb_light = memnew(VBoxContainer);145hb->add_child(vb_light);146147light_1_switch = memnew(Button);148light_1_switch->set_theme_type_variation("PreviewLightButton");149light_1_switch->set_toggle_mode(true);150light_1_switch->set_pressed(true);151light_1_switch->set_accessibility_name(TTRC("First Light"));152vb_light->add_child(light_1_switch);153light_1_switch->connect(SceneStringName(pressed), callable_mp(this, &MeshEditor::_on_light_1_switch_pressed));154155light_2_switch = memnew(Button);156light_2_switch->set_theme_type_variation("PreviewLightButton");157light_2_switch->set_toggle_mode(true);158light_2_switch->set_pressed(true);159light_2_switch->set_accessibility_name(TTRC("Second Light"));160vb_light->add_child(light_2_switch);161light_2_switch->connect(SceneStringName(pressed), callable_mp(this, &MeshEditor::_on_light_2_switch_pressed));162163rot_x = 0;164rot_y = 0;165}166167///////////////////////168169bool EditorInspectorPluginMesh::can_handle(Object *p_object) {170return Object::cast_to<Mesh>(p_object) != nullptr;171}172173void EditorInspectorPluginMesh::parse_begin(Object *p_object) {174Mesh *mesh = Object::cast_to<Mesh>(p_object);175if (!mesh) {176return;177}178Ref<Mesh> m(mesh);179180MeshEditor *editor = memnew(MeshEditor);181editor->edit(m);182add_custom_control(editor);183}184185MeshEditorPlugin::MeshEditorPlugin() {186Ref<EditorInspectorPluginMesh> plugin;187plugin.instantiate();188add_inspector_plugin(plugin);189}190191192