/*************************************************************************/ /* editor_plugin.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* 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. */ /*************************************************************************/ #include "editor_plugin.h" #include "editor/debugger/editor_debugger_node.h" #include "editor/editor_command_palette.h" #include "editor/editor_node.h" #include "editor/editor_paths.h" #include "editor/editor_resource_preview.h" #include "editor/editor_settings.h" #include "editor/editor_translation_parser.h" #include "editor/editor_undo_redo_manager.h" #include "editor/export/editor_export.h" #include "editor/filesystem_dock.h" #include "editor/import/editor_import_plugin.h" #include "editor/import/resource_importer_scene.h" #include "editor/plugins/canvas_item_editor_plugin.h" #include "editor/plugins/node_3d_editor_plugin.h" #include "editor/plugins/script_editor_plugin.h" #include "editor/project_settings_editor.h" #include "editor/scene_tree_dock.h" #include "main/main.h" #include "scene/3d/camera_3d.h" #include "scene/gui/popup_menu.h" #include "servers/rendering_server.h" TypedArray EditorInterface::_make_mesh_previews(const TypedArray &p_meshes, int p_preview_size) { Vector> meshes; for (int i = 0; i < p_meshes.size(); i++) { meshes.push_back(p_meshes[i]); } Vector> textures = make_mesh_previews(meshes, nullptr, p_preview_size); TypedArray ret; for (int i = 0; i < textures.size(); i++) { ret.push_back(textures[i]); } return ret; } Vector> EditorInterface::make_mesh_previews(const Vector> &p_meshes, Vector *p_transforms, int p_preview_size) { int size = p_preview_size; RID scenario = RS::get_singleton()->scenario_create(); RID viewport = RS::get_singleton()->viewport_create(); RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_ALWAYS); RS::get_singleton()->viewport_set_scenario(viewport, scenario); RS::get_singleton()->viewport_set_size(viewport, size, size); RS::get_singleton()->viewport_set_transparent_background(viewport, true); RS::get_singleton()->viewport_set_active(viewport, true); RID viewport_texture = RS::get_singleton()->viewport_get_texture(viewport); RID camera = RS::get_singleton()->camera_create(); RS::get_singleton()->viewport_attach_camera(viewport, camera); RID light = RS::get_singleton()->directional_light_create(); RID light_instance = RS::get_singleton()->instance_create2(light, scenario); RID light2 = RS::get_singleton()->directional_light_create(); RS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7)); RID light_instance2 = RS::get_singleton()->instance_create2(light2, scenario); EditorProgress ep("mlib", TTR("Creating Mesh Previews"), p_meshes.size()); Vector> textures; for (int i = 0; i < p_meshes.size(); i++) { Ref mesh = p_meshes[i]; if (!mesh.is_valid()) { textures.push_back(Ref()); continue; } Transform3D mesh_xform; if (p_transforms != nullptr) { mesh_xform = (*p_transforms)[i]; } RID inst = RS::get_singleton()->instance_create2(mesh->get_rid(), scenario); RS::get_singleton()->instance_set_transform(inst, mesh_xform); AABB aabb = mesh->get_aabb(); Vector3 ofs = aabb.get_center(); aabb.position -= ofs; Transform3D xform; xform.basis = Basis().rotated(Vector3(0, 1, 0), -Math_PI / 6); xform.basis = Basis().rotated(Vector3(1, 0, 0), Math_PI / 6) * xform.basis; AABB rot_aabb = xform.xform(aabb); float m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5; if (m == 0) { textures.push_back(Ref()); continue; } xform.origin = -xform.basis.xform(ofs); //-ofs*m; xform.origin.z -= rot_aabb.size.z * 2; xform.invert(); xform = mesh_xform * xform; RS::get_singleton()->camera_set_transform(camera, xform * Transform3D(Basis(), Vector3(0, 0, 3))); RS::get_singleton()->camera_set_orthogonal(camera, m * 2, 0.01, 1000.0); RS::get_singleton()->instance_set_transform(light_instance, xform * Transform3D().looking_at(Vector3(-2, -1, -1), Vector3(0, 1, 0))); RS::get_singleton()->instance_set_transform(light_instance2, xform * Transform3D().looking_at(Vector3(+1, -1, -2), Vector3(0, 1, 0))); ep.step(TTR("Thumbnail..."), i); Main::iteration(); Main::iteration(); Ref img = RS::get_singleton()->texture_2d_get(viewport_texture); ERR_CONTINUE(!img.is_valid() || img->is_empty()); Ref it = ImageTexture::create_from_image(img); RS::get_singleton()->free(inst); textures.push_back(it); } RS::get_singleton()->free(viewport); RS::get_singleton()->free(light); RS::get_singleton()->free(light_instance); RS::get_singleton()->free(light2); RS::get_singleton()->free(light_instance2); RS::get_singleton()->free(camera); RS::get_singleton()->free(scenario); return textures; } void EditorInterface::set_main_screen_editor(const String &p_name) { EditorNode::get_singleton()->select_editor_by_name(p_name); } VBoxContainer *EditorInterface::get_editor_main_screen() { return EditorNode::get_singleton()->get_main_screen_control(); } void EditorInterface::edit_resource(const Ref &p_resource) { EditorNode::get_singleton()->edit_resource(p_resource); } void EditorInterface::edit_node(Node *p_node) { EditorNode::get_singleton()->edit_node(p_node); } void EditorInterface::edit_script(const Ref