Path: blob/master/editor/scene/3d/gizmos/voxel_gi_gizmo_plugin.cpp
9912 views
/**************************************************************************/1/* voxel_gi_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 "voxel_gi_gizmo_plugin.h"3132#include "editor/editor_node.h"33#include "editor/editor_string_names.h"34#include "editor/scene/3d/gizmos/gizmo_3d_helper.h"35#include "editor/settings/editor_settings.h"36#include "scene/3d/voxel_gi.h"3738VoxelGIGizmoPlugin::VoxelGIGizmoPlugin() {39helper.instantiate();4041Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/voxel_gi");4243create_material("voxel_gi_material", gizmo_color);4445// This gizmo draws a lot of lines. Use a low opacity to make it not too intrusive.46gizmo_color.a = 0.02;47create_material("voxel_gi_internal_material", gizmo_color);4849create_icon_material("voxel_gi_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoVoxelGI"), EditorStringName(EditorIcons)));50create_handle_material("handles");51}5253bool VoxelGIGizmoPlugin::has_gizmo(Node3D *p_spatial) {54return Object::cast_to<VoxelGI>(p_spatial) != nullptr;55}5657String VoxelGIGizmoPlugin::get_gizmo_name() const {58return "VoxelGI";59}6061int VoxelGIGizmoPlugin::get_priority() const {62return -1;63}6465String VoxelGIGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {66return helper->box_get_handle_name(p_id);67}6869Variant VoxelGIGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {70VoxelGI *probe = Object::cast_to<VoxelGI>(p_gizmo->get_node_3d());71return probe->get_size();72}7374void VoxelGIGizmoPlugin::begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) {75helper->initialize_handle_action(get_handle_value(p_gizmo, p_id, p_secondary), p_gizmo->get_node_3d()->get_global_transform());76}7778void VoxelGIGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {79VoxelGI *probe = Object::cast_to<VoxelGI>(p_gizmo->get_node_3d());8081Vector3 sg[2];82helper->get_segment(p_camera, p_point, sg);8384Vector3 size = probe->get_size();85Vector3 position;86helper->box_set_handle(sg, p_id, size, position);87probe->set_size(size);88probe->set_global_position(position);89}9091void VoxelGIGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {92helper->box_commit_handle(TTR("Change Probe Size"), p_cancel, p_gizmo->get_node_3d());93}9495void VoxelGIGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {96p_gizmo->clear();9798if (p_gizmo->is_selected()) {99VoxelGI *probe = Object::cast_to<VoxelGI>(p_gizmo->get_node_3d());100Ref<Material> material = get_material("voxel_gi_material", p_gizmo);101Ref<Material> material_internal = get_material("voxel_gi_internal_material", p_gizmo);102103Vector<Vector3> lines;104Vector3 size = probe->get_size();105106static const int subdivs[VoxelGI::SUBDIV_MAX] = { 64, 128, 256, 512 };107108AABB aabb = AABB(-size / 2, size);109int subdiv = subdivs[probe->get_subdiv()];110float cell_size = aabb.get_longest_axis_size() / subdiv;111112for (int i = 0; i < 12; i++) {113Vector3 a, b;114aabb.get_edge(i, a, b);115lines.push_back(a);116lines.push_back(b);117}118119p_gizmo->add_lines(lines, material);120121lines.clear();122123for (int i = 1; i < subdiv; i++) {124for (int j = 0; j < 3; j++) {125if (cell_size * i > aabb.size[j]) {126continue;127}128129int j_n1 = (j + 1) % 3;130int j_n2 = (j + 2) % 3;131132for (int k = 0; k < 4; k++) {133Vector3 from = aabb.position, to = aabb.position;134from[j] += cell_size * i;135to[j] += cell_size * i;136137if (k & 1) {138to[j_n1] += aabb.size[j_n1];139} else {140to[j_n2] += aabb.size[j_n2];141}142143if (k & 2) {144from[j_n1] += aabb.size[j_n1];145from[j_n2] += aabb.size[j_n2];146}147148lines.push_back(from);149lines.push_back(to);150}151}152}153154p_gizmo->add_lines(lines, material_internal);155156Vector<Vector3> handles = helper->box_get_handles(probe->get_size());157158p_gizmo->add_handles(handles, get_material("handles"));159}160161Ref<Material> icon = get_material("voxel_gi_icon", p_gizmo);162p_gizmo->add_unscaled_billboard(icon, 0.05);163}164165166