| 
									
										
										
										
											2016-06-18 14:46:12 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  mesh_editor_plugin.cpp                                               */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2016-06-18 14:46:12 +02:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2022-01-03 21:27:34 +01:00
										 |  |  | /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2016-06-18 14:46:12 +02:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | #include "mesh_editor_plugin.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 15:17:23 +08:00
										 |  |  | #include "editor/editor_scale.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-22 12:37:22 -03:00
										 |  |  | void MeshEditor::gui_input(const Ref<InputEvent> &p_event) { | 
					
						
							| 
									
										
										
										
											2021-04-05 08:52:21 +02:00
										 |  |  | 	ERR_FAIL_COND(p_event.is_null()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	Ref<InputEventMouseMotion> mm = p_event; | 
					
						
							| 
									
										
										
										
											2021-08-13 16:31:57 -05:00
										 |  |  | 	if (mm.is_valid() && (mm->get_button_mask() & MouseButton::MASK_LEFT) != MouseButton::NONE) { | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 		rot_x -= mm->get_relative().y * 0.01; | 
					
						
							|  |  |  | 		rot_y -= mm->get_relative().x * 0.01; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (rot_x < -Math_PI / 2) { | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 			rot_x = -Math_PI / 2; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} else if (rot_x > Math_PI / 2) { | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 			rot_x = Math_PI / 2; | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		_update_rotation(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | void MeshEditor::_notification(int p_what) { | 
					
						
							| 
									
										
										
										
											2022-02-15 21:44:22 -05:00
										 |  |  | 	switch (p_what) { | 
					
						
							|  |  |  | 		case NOTIFICATION_READY: { | 
					
						
							|  |  |  | 			//get_scene()->connect("node_removed",this,"_node_removed");
 | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 21:44:22 -05:00
										 |  |  | 			if (first_enter) { | 
					
						
							|  |  |  | 				//it's in propertyeditor so. could be moved around
 | 
					
						
							| 
									
										
										
										
											2015-11-22 20:08:50 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 21:44:22 -05:00
										 |  |  | 				light_1_switch->set_normal_texture(get_theme_icon(SNAME("MaterialPreviewLight1"), SNAME("EditorIcons"))); | 
					
						
							|  |  |  | 				light_1_switch->set_pressed_texture(get_theme_icon(SNAME("MaterialPreviewLight1Off"), SNAME("EditorIcons"))); | 
					
						
							|  |  |  | 				light_2_switch->set_normal_texture(get_theme_icon(SNAME("MaterialPreviewLight2"), SNAME("EditorIcons"))); | 
					
						
							|  |  |  | 				light_2_switch->set_pressed_texture(get_theme_icon(SNAME("MaterialPreviewLight2Off"), SNAME("EditorIcons"))); | 
					
						
							|  |  |  | 				first_enter = false; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | void MeshEditor::_update_rotation() { | 
					
						
							| 
									
										
										
										
											2020-10-17 01:08:21 -04:00
										 |  |  | 	Transform3D t; | 
					
						
							| 
									
										
										
										
											2016-10-18 15:50:21 -05:00
										 |  |  | 	t.basis.rotate(Vector3(0, 1, 0), -rot_y); | 
					
						
							|  |  |  | 	t.basis.rotate(Vector3(1, 0, 0), -rot_x); | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	rotation->set_transform(t); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | void MeshEditor::edit(Ref<Mesh> p_mesh) { | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	mesh = p_mesh; | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	mesh_instance->set_mesh(mesh); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 15:52:39 -03:00
										 |  |  | 	rot_x = Math::deg2rad(-15.0); | 
					
						
							|  |  |  | 	rot_y = Math::deg2rad(30.0); | 
					
						
							|  |  |  | 	_update_rotation(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	AABB aabb = mesh->get_aabb(); | 
					
						
							| 
									
										
										
										
											2021-09-21 00:33:52 +05:45
										 |  |  | 	Vector3 ofs = aabb.get_center(); | 
					
						
							| 
									
										
										
										
											2019-03-04 15:52:39 -03:00
										 |  |  | 	float m = aabb.get_longest_axis_size(); | 
					
						
							|  |  |  | 	if (m != 0) { | 
					
						
							|  |  |  | 		m = 1.0 / m; | 
					
						
							|  |  |  | 		m *= 0.5; | 
					
						
							| 
									
										
										
										
											2020-10-17 01:08:21 -04:00
										 |  |  | 		Transform3D xform; | 
					
						
							| 
									
										
										
										
											2019-03-04 15:52:39 -03:00
										 |  |  | 		xform.basis.scale(Vector3(m, m, m)); | 
					
						
							|  |  |  | 		xform.origin = -xform.basis.xform(ofs); //-ofs*m;
 | 
					
						
							|  |  |  | 		//xform.origin.z -= aabb.get_longest_axis_size() * 2;
 | 
					
						
							|  |  |  | 		mesh_instance->set_transform(xform); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | void MeshEditor::_button_pressed(Node *p_button) { | 
					
						
							|  |  |  | 	if (p_button == light_1_switch) { | 
					
						
							|  |  |  | 		light1->set_visible(!light_1_switch->is_pressed()); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	if (p_button == light_2_switch) { | 
					
						
							|  |  |  | 		light2->set_visible(!light_2_switch->is_pressed()); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-02 11:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | MeshEditor::MeshEditor() { | 
					
						
							| 
									
										
										
										
											2020-03-03 22:51:12 -03:00
										 |  |  | 	viewport = memnew(SubViewport); | 
					
						
							| 
									
										
										
										
											2020-04-18 11:00:51 +02:00
										 |  |  | 	Ref<World3D> world_3d; | 
					
						
							| 
									
										
										
										
											2021-06-17 16:03:09 -06:00
										 |  |  | 	world_3d.instantiate(); | 
					
						
							| 
									
										
										
										
											2020-04-18 11:00:51 +02:00
										 |  |  | 	viewport->set_world_3d(world_3d); //use own world
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	add_child(viewport); | 
					
						
							| 
									
										
										
										
											2016-08-16 12:10:44 -03:00
										 |  |  | 	viewport->set_disable_input(true); | 
					
						
							| 
									
										
										
										
											2021-11-04 19:27:23 +01:00
										 |  |  | 	viewport->set_msaa(Viewport::MSAA_4X); | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	set_stretch(true); | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	camera = memnew(Camera3D); | 
					
						
							| 
									
										
										
										
											2020-10-17 01:08:21 -04:00
										 |  |  | 	camera->set_transform(Transform3D(Basis(), Vector3(0, 0, 1.1))); | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	camera->set_perspective(45, 0.1, 10); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	viewport->add_child(camera); | 
					
						
							| 
									
										
										
										
											2014-11-02 11:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	light1 = memnew(DirectionalLight3D); | 
					
						
							| 
									
										
										
										
											2020-10-17 01:08:21 -04:00
										 |  |  | 	light1->set_transform(Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0))); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	viewport->add_child(light1); | 
					
						
							| 
									
										
										
										
											2014-11-02 11:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	light2 = memnew(DirectionalLight3D); | 
					
						
							| 
									
										
										
										
											2020-10-17 01:08:21 -04:00
										 |  |  | 	light2->set_transform(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1))); | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	light2->set_color(Color(0.7, 0.7, 0.7)); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	viewport->add_child(light2); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	rotation = memnew(Node3D); | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	viewport->add_child(rotation); | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	mesh_instance = memnew(MeshInstance3D); | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	rotation->add_child(mesh_instance); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	set_custom_minimum_size(Size2(1, 150) * EDSCALE); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	HBoxContainer *hb = memnew(HBoxContainer); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	add_child(hb); | 
					
						
							| 
									
										
										
										
											2022-03-18 19:02:57 -05:00
										 |  |  | 	hb->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 2); | 
					
						
							| 
									
										
										
										
											2015-08-29 17:16:11 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	hb->add_spacer(); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	VBoxContainer *vb_light = memnew(VBoxContainer); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	hb->add_child(vb_light); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	light_1_switch = memnew(TextureButton); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	light_1_switch->set_toggle_mode(true); | 
					
						
							|  |  |  | 	vb_light->add_child(light_1_switch); | 
					
						
							| 
									
										
										
										
											2022-07-28 22:56:41 +02:00
										 |  |  | 	light_1_switch->connect("pressed", callable_mp(this, &MeshEditor::_button_pressed).bind(light_1_switch)); | 
					
						
							| 
									
										
										
										
											2015-10-18 13:31:44 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	light_2_switch = memnew(TextureButton); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | 	light_2_switch->set_toggle_mode(true); | 
					
						
							|  |  |  | 	vb_light->add_child(light_2_switch); | 
					
						
							| 
									
										
										
										
											2022-07-28 22:56:41 +02:00
										 |  |  | 	light_2_switch->connect("pressed", callable_mp(this, &MeshEditor::_button_pressed).bind(light_2_switch)); | 
					
						
							| 
									
										
										
										
											2014-11-02 11:31:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	first_enter = true; | 
					
						
							| 
									
										
										
										
											2015-04-03 10:28:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-23 18:48:05 -03:00
										 |  |  | 	rot_x = 0; | 
					
						
							|  |  |  | 	rot_y = 0; | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 15:52:39 -03:00
										 |  |  | ///////////////////////
 | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 15:52:39 -03:00
										 |  |  | bool EditorInspectorPluginMesh::can_handle(Object *p_object) { | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	return Object::cast_to<Mesh>(p_object) != nullptr; | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 15:52:39 -03:00
										 |  |  | void EditorInspectorPluginMesh::parse_begin(Object *p_object) { | 
					
						
							|  |  |  | 	Mesh *mesh = Object::cast_to<Mesh>(p_object); | 
					
						
							|  |  |  | 	if (!mesh) { | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-04 15:52:39 -03:00
										 |  |  | 	Ref<Mesh> m(mesh); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	MeshEditor *editor = memnew(MeshEditor); | 
					
						
							|  |  |  | 	editor->edit(m); | 
					
						
							|  |  |  | 	add_custom_control(editor); | 
					
						
							| 
									
										
										
										
											2014-10-12 02:13:22 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-27 10:36:51 +01:00
										 |  |  | MeshEditorPlugin::MeshEditorPlugin() { | 
					
						
							| 
									
										
										
										
											2019-03-04 15:52:39 -03:00
										 |  |  | 	Ref<EditorInspectorPluginMesh> plugin; | 
					
						
							| 
									
										
										
										
											2021-06-17 16:03:09 -06:00
										 |  |  | 	plugin.instantiate(); | 
					
						
							| 
									
										
										
										
											2019-03-04 15:52:39 -03:00
										 |  |  | 	add_inspector_plugin(plugin); | 
					
						
							| 
									
										
										
										
											2016-05-23 17:10:26 -03:00
										 |  |  | } |