Path: blob/master/editor/scene/3d/gizmos/reflection_probe_gizmo_plugin.cpp
21635 views
/**************************************************************************/1/* reflection_probe_gizmo_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 "reflection_probe_gizmo_plugin.h"3132#include "core/math/geometry_3d.h"33#include "editor/editor_node.h"34#include "editor/editor_string_names.h"35#include "editor/editor_undo_redo_manager.h"36#include "editor/scene/3d/gizmos/gizmo_3d_helper.h"37#include "editor/scene/3d/node_3d_editor_plugin.h"38#include "editor/settings/editor_settings.h"39#include "scene/3d/reflection_probe.h"4041ReflectionProbeGizmoPlugin::ReflectionProbeGizmoPlugin() {42helper.instantiate();43Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/reflection_probe");4445create_material("reflection_probe_material", gizmo_color);4647gizmo_color.a = 0.5;48create_material("reflection_internal_material", gizmo_color);4950create_icon_material("reflection_probe_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoReflectionProbe"), EditorStringName(EditorIcons)));51create_handle_material("handles");52}5354bool ReflectionProbeGizmoPlugin::has_gizmo(Node3D *p_spatial) {55return Object::cast_to<ReflectionProbe>(p_spatial) != nullptr;56}5758String ReflectionProbeGizmoPlugin::get_gizmo_name() const {59return "ReflectionProbe";60}6162int ReflectionProbeGizmoPlugin::get_priority() const {63return -1;64}6566String ReflectionProbeGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {67if (p_id < 6) {68return helper->box_get_handle_name(p_id);69}70switch (p_id) {71case 6:72return "Origin X";73case 7:74return "Origin Y";75case 8:76return "Origin Z";77}78return "";79}8081Variant ReflectionProbeGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {82ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_node_3d());83return AABB(probe->get_origin_offset(), probe->get_size());84}8586void ReflectionProbeGizmoPlugin::begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) {87// The initial value is only used for resizing the box, so we only need AABB size.88AABB aabb = get_handle_value(p_gizmo, p_id, p_secondary);89helper->initialize_handle_action(aabb.size, p_gizmo->get_node_3d()->get_global_transform());90}9192void ReflectionProbeGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {93ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_node_3d());9495Vector3 sg[2];96helper->get_segment(p_camera, p_point, sg);9798if (p_id < 6) {99Vector3 size = probe->get_size();100Vector3 position;101helper->box_set_handle(sg, p_id, size, position);102probe->set_size(size);103probe->set_global_position(position);104} else {105p_id -= 6;106107Vector3 origin = probe->get_origin_offset();108origin[p_id] = 0;109110Vector3 axis;111axis[p_id] = 1.0;112113Vector3 ra, rb;114Geometry3D::get_closest_points_between_segments(origin - axis * 16384, origin + axis * 16384, sg[0], sg[1], ra, rb);115// Adjust the actual position to account for the gizmo handle position116float d = ra[p_id] + 0.25;117if (Node3DEditor::get_singleton()->is_snap_enabled()) {118d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());119}120121origin[p_id] = d;122probe->set_origin_offset(origin);123}124}125126void ReflectionProbeGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {127ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_node_3d());128129if (p_id < 6) {130helper->box_commit_handle(TTR("Change Probe Size"), p_cancel, probe);131return;132}133134AABB restore = p_restore;135136if (p_cancel) {137probe->set_origin_offset(restore.position);138probe->set_size(restore.size);139return;140}141142EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();143ur->create_action(TTR("Change Probe Origin Offset"));144ur->add_do_method(probe, "set_origin_offset", probe->get_origin_offset());145ur->add_undo_method(probe, "set_origin_offset", restore.position);146ur->commit_action();147}148149void ReflectionProbeGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {150p_gizmo->clear();151152if (p_gizmo->is_selected()) {153ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_node_3d());154Vector<Vector3> lines;155Vector<Vector3> internal_lines;156Vector3 size = probe->get_size();157158AABB aabb;159aabb.position = -size / 2;160aabb.size = size;161162AABB blend_aabb;163for (int i = 0; i < 3; i++) {164blend_aabb.position[i] = aabb.position[i] + probe->get_blend_distance();165blend_aabb.size[i] = aabb.size[i] - probe->get_blend_distance() * 2.0;166if (blend_aabb.size[i] < blend_aabb.position[i]) {167blend_aabb.position[i] = aabb.position[i] + aabb.size[i] / 2.0;168blend_aabb.size[i] = 0.0;169}170}171172if (probe->get_blend_distance() != 0.0) {173for (int i = 0; i < 12; i++) {174Vector3 a;175Vector3 b;176blend_aabb.get_edge(i, a, b);177lines.push_back(a);178lines.push_back(b);179}180181for (int i = 0; i < 8; i++) {182Vector3 ep = aabb.get_endpoint(i);183internal_lines.push_back(blend_aabb.get_endpoint(i));184internal_lines.push_back(ep);185}186}187188Vector<Vector3> handles = helper->box_get_handles(probe->get_size());189190if (probe->get_origin_offset() != Vector3(0.0, 0.0, 0.0)) {191for (int i = 0; i < 3; i++) {192Vector3 orig_handle = probe->get_origin_offset();193orig_handle[i] -= 0.25;194lines.push_back(orig_handle);195196orig_handle[i] += 0.5;197lines.push_back(orig_handle);198}199}200201Ref<Material> material = get_material("reflection_probe_material", p_gizmo);202Ref<Material> material_internal = get_material("reflection_internal_material", p_gizmo);203204p_gizmo->add_lines(lines, material);205p_gizmo->add_lines(internal_lines, material_internal);206207p_gizmo->add_handles(handles, get_material("handles"));208}209210Ref<Material> icon = get_material("reflection_probe_icon", p_gizmo);211p_gizmo->add_unscaled_billboard(icon, 0.05);212}213214215