Path: blob/master/editor/scene/3d/gizmos/reflection_probe_gizmo_plugin.cpp
9904 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 "editor/editor_node.h"33#include "editor/editor_string_names.h"34#include "editor/editor_undo_redo_manager.h"35#include "editor/scene/3d/gizmos/gizmo_3d_helper.h"36#include "editor/scene/3d/node_3d_editor_plugin.h"37#include "editor/settings/editor_settings.h"38#include "scene/3d/reflection_probe.h"3940ReflectionProbeGizmoPlugin::ReflectionProbeGizmoPlugin() {41helper.instantiate();42Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/reflection_probe");4344create_material("reflection_probe_material", gizmo_color);4546gizmo_color.a = 0.5;47create_material("reflection_internal_material", gizmo_color);4849create_icon_material("reflection_probe_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoReflectionProbe"), EditorStringName(EditorIcons)));50create_handle_material("handles");51}5253bool ReflectionProbeGizmoPlugin::has_gizmo(Node3D *p_spatial) {54return Object::cast_to<ReflectionProbe>(p_spatial) != nullptr;55}5657String ReflectionProbeGizmoPlugin::get_gizmo_name() const {58return "ReflectionProbe";59}6061int ReflectionProbeGizmoPlugin::get_priority() const {62return -1;63}6465String ReflectionProbeGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {66if (p_id < 6) {67return helper->box_get_handle_name(p_id);68}69switch (p_id) {70case 6:71return "Origin X";72case 7:73return "Origin Y";74case 8:75return "Origin Z";76}77return "";78}7980Variant ReflectionProbeGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {81ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_node_3d());82return AABB(probe->get_origin_offset(), probe->get_size());83}8485void ReflectionProbeGizmoPlugin::begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) {86// The initial value is only used for resizing the box, so we only need AABB size.87AABB aabb = get_handle_value(p_gizmo, p_id, p_secondary);88helper->initialize_handle_action(aabb.size, p_gizmo->get_node_3d()->get_global_transform());89}9091void ReflectionProbeGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {92ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_node_3d());9394Vector3 sg[2];95helper->get_segment(p_camera, p_point, sg);9697if (p_id < 6) {98Vector3 size = probe->get_size();99Vector3 position;100helper->box_set_handle(sg, p_id, size, position);101probe->set_size(size);102probe->set_global_position(position);103} else {104p_id -= 6;105106Vector3 origin = probe->get_origin_offset();107origin[p_id] = 0;108109Vector3 axis;110axis[p_id] = 1.0;111112Vector3 ra, rb;113Geometry3D::get_closest_points_between_segments(origin - axis * 16384, origin + axis * 16384, sg[0], sg[1], ra, rb);114// Adjust the actual position to account for the gizmo handle position115float d = ra[p_id] + 0.25;116if (Node3DEditor::get_singleton()->is_snap_enabled()) {117d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());118}119120origin[p_id] = d;121probe->set_origin_offset(origin);122}123}124125void ReflectionProbeGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {126ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_node_3d());127128if (p_id < 6) {129helper->box_commit_handle(TTR("Change Probe Size"), p_cancel, probe);130return;131}132133AABB restore = p_restore;134135if (p_cancel) {136probe->set_origin_offset(restore.position);137probe->set_size(restore.size);138return;139}140141EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();142ur->create_action(TTR("Change Probe Origin Offset"));143ur->add_do_method(probe, "set_origin_offset", probe->get_origin_offset());144ur->add_undo_method(probe, "set_origin_offset", restore.position);145ur->commit_action();146}147148void ReflectionProbeGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {149p_gizmo->clear();150151if (p_gizmo->is_selected()) {152ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_node_3d());153Vector<Vector3> lines;154Vector<Vector3> internal_lines;155Vector3 size = probe->get_size();156157AABB aabb;158aabb.position = -size / 2;159aabb.size = size;160161AABB blend_aabb;162for (int i = 0; i < 3; i++) {163blend_aabb.position[i] = aabb.position[i] + probe->get_blend_distance();164blend_aabb.size[i] = aabb.size[i] - probe->get_blend_distance() * 2.0;165if (blend_aabb.size[i] < blend_aabb.position[i]) {166blend_aabb.position[i] = aabb.position[i] + aabb.size[i] / 2.0;167blend_aabb.size[i] = 0.0;168}169}170171if (probe->get_blend_distance() != 0.0) {172for (int i = 0; i < 12; i++) {173Vector3 a;174Vector3 b;175blend_aabb.get_edge(i, a, b);176lines.push_back(a);177lines.push_back(b);178}179180for (int i = 0; i < 8; i++) {181Vector3 ep = aabb.get_endpoint(i);182internal_lines.push_back(blend_aabb.get_endpoint(i));183internal_lines.push_back(ep);184}185}186187Vector<Vector3> handles = helper->box_get_handles(probe->get_size());188189if (probe->get_origin_offset() != Vector3(0.0, 0.0, 0.0)) {190for (int i = 0; i < 3; i++) {191Vector3 orig_handle = probe->get_origin_offset();192orig_handle[i] -= 0.25;193lines.push_back(orig_handle);194195orig_handle[i] += 0.5;196lines.push_back(orig_handle);197}198}199200Ref<Material> material = get_material("reflection_probe_material", p_gizmo);201Ref<Material> material_internal = get_material("reflection_internal_material", p_gizmo);202203p_gizmo->add_lines(lines, material);204p_gizmo->add_lines(internal_lines, material_internal);205206p_gizmo->add_handles(handles, get_material("handles"));207}208209Ref<Material> icon = get_material("reflection_probe_icon", p_gizmo);210p_gizmo->add_unscaled_billboard(icon, 0.05);211}212213214