Path: blob/master/editor/scene/3d/gizmos/lightmap_probe_gizmo_plugin.cpp
9904 views
/**************************************************************************/1/* lightmap_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 "lightmap_probe_gizmo_plugin.h"3132#include "editor/editor_node.h"33#include "editor/editor_string_names.h"34#include "editor/settings/editor_settings.h"35#include "scene/3d/lightmap_probe.h"3637LightmapProbeGizmoPlugin::LightmapProbeGizmoPlugin() {38// NOTE: This gizmo only renders LightmapProbe nodes as wireframes.39// The solid sphere representation is handled in LightmapGIGizmoPlugin.40create_icon_material("lightmap_probe_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoLightmapProbe"), EditorStringName(EditorIcons)));4142Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/lightprobe_lines");43probe_size = EDITOR_GET("editors/3d_gizmos/gizmo_settings/lightmap_gi_probe_size");4445gizmo_color.a = 0.3;46create_material("lightprobe_lines", gizmo_color);47}4849bool LightmapProbeGizmoPlugin::has_gizmo(Node3D *p_spatial) {50return Object::cast_to<LightmapProbe>(p_spatial) != nullptr;51}5253String LightmapProbeGizmoPlugin::get_gizmo_name() const {54return "LightmapProbe";55}5657int LightmapProbeGizmoPlugin::get_priority() const {58return -1;59}6061void LightmapProbeGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {62Ref<Material> material_lines = get_material("lightprobe_lines", p_gizmo);6364p_gizmo->clear();6566Vector<Vector3> lines;6768int stack_count = 8;69int sector_count = 16;7071float sector_step = (Math::PI * 2.0) / sector_count;72float stack_step = Math::PI / stack_count;7374Vector<Vector3> vertices;75// Make the lines' radius slightly smaller than its mesh representation to avoid Z-fighting.76float radius = probe_size * 0.495f;7778if (!Math::is_zero_approx(radius)) {79for (int i = 0; i <= stack_count; ++i) {80float stack_angle = Math::PI / 2 - i * stack_step; // starting from pi/2 to -pi/281float xy = radius * Math::cos(stack_angle); // r * cos(u)82float z = radius * Math::sin(stack_angle); // r * sin(u)8384// add (sector_count+1) vertices per stack85// the first and last vertices have same position and normal, but different tex coords86for (int j = 0; j <= sector_count; ++j) {87float sector_angle = j * sector_step; // starting from 0 to 2pi8889// vertex position (x, y, z)90float x = xy * Math::cos(sector_angle); // r * cos(u) * cos(v)91float y = xy * Math::sin(sector_angle); // r * cos(u) * sin(v)9293Vector3 n = Vector3(x, z, y);94vertices.push_back(n);95}96}9798for (int i = 0; i < stack_count; ++i) {99int k1 = i * (sector_count + 1); // beginning of current stack100int k2 = k1 + sector_count + 1; // beginning of next stack101102for (int j = 0; j < sector_count; ++j, ++k1, ++k2) {103// 2 triangles per sector excluding first and last stacks104// k1 => k2 => k1+1105if (i != 0) {106lines.push_back(vertices[k1]);107lines.push_back(vertices[k2]);108lines.push_back(vertices[k1]);109lines.push_back(vertices[k1 + 1]);110}111112if (i != (stack_count - 1)) {113lines.push_back(vertices[k1 + 1]);114lines.push_back(vertices[k2]);115lines.push_back(vertices[k2]);116lines.push_back(vertices[k2 + 1]);117}118}119}120121p_gizmo->add_lines(lines, material_lines);122}123const Ref<Material> icon = get_material("lightmap_probe_icon", p_gizmo);124p_gizmo->add_unscaled_billboard(icon, 0.05);125}126127128