Path: blob/master/editor/scene/3d/gizmos/lightmap_gi_gizmo_plugin.cpp
9904 views
/**************************************************************************/1/* lightmap_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 "lightmap_gi_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_gi.h"3637LightmapGIGizmoPlugin::LightmapGIGizmoPlugin() {38// NOTE: This gizmo only renders solid spheres for previewing indirect lighting on dynamic objects.39// The wireframe representation for LightmapProbe nodes is handled in LightmapProbeGizmoPlugin.40Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/lightmap_lines");41probe_size = EDITOR_GET("editors/3d_gizmos/gizmo_settings/lightmap_gi_probe_size");4243gizmo_color.a = 0.1;44create_material("lightmap_lines", gizmo_color);4546Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);47mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);48// Fade out probes when camera gets too close to them.49mat->set_distance_fade(StandardMaterial3D::DISTANCE_FADE_PIXEL_DITHER);50mat->set_distance_fade_min_distance(probe_size * 0.5);51mat->set_distance_fade_max_distance(probe_size * 1.5);52mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);53mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, false);54mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);5556add_material("lightmap_probe_material", mat);5758create_icon_material("baked_indirect_light_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoLightmapGI"), EditorStringName(EditorIcons)));59}6061bool LightmapGIGizmoPlugin::has_gizmo(Node3D *p_spatial) {62return Object::cast_to<LightmapGI>(p_spatial) != nullptr;63}6465String LightmapGIGizmoPlugin::get_gizmo_name() const {66return "LightmapGI";67}6869int LightmapGIGizmoPlugin::get_priority() const {70return -1;71}7273void LightmapGIGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {74Ref<Material> icon = get_material("baked_indirect_light_icon", p_gizmo);75LightmapGI *baker = Object::cast_to<LightmapGI>(p_gizmo->get_node_3d());76Ref<LightmapGIData> data = baker->get_light_data();7778p_gizmo->clear();7980p_gizmo->add_unscaled_billboard(icon, 0.05);8182if (data.is_null() || !p_gizmo->is_selected()) {83return;84}8586Ref<Material> material_lines = get_material("lightmap_lines", p_gizmo);87Ref<Material> material_probes = get_material("lightmap_probe_material", p_gizmo);8889Vector<Vector3> lines;90HashSet<Vector2i> lines_found;9192Vector<Vector3> points = data->get_capture_points();93if (points.is_empty()) {94return;95}96Vector<Color> sh = data->get_capture_sh();97if (sh.size() != points.size() * 9) {98return;99}100101Vector<int> tetrahedrons = data->get_capture_tetrahedra();102103for (int i = 0; i < tetrahedrons.size(); i += 4) {104for (int j = 0; j < 4; j++) {105for (int k = j + 1; k < 4; k++) {106Vector2i pair;107pair.x = tetrahedrons[i + j];108pair.y = tetrahedrons[i + k];109110if (pair.y < pair.x) {111SWAP(pair.x, pair.y);112}113if (lines_found.has(pair)) {114continue;115}116lines_found.insert(pair);117lines.push_back(points[pair.x]);118lines.push_back(points[pair.y]);119}120}121}122123p_gizmo->add_lines(lines, material_lines);124125int stack_count = 8;126int sector_count = 16;127128float sector_step = (Math::PI * 2.0) / sector_count;129float stack_step = Math::PI / stack_count;130131LocalVector<Vector3> vertices;132LocalVector<Color> colors;133LocalVector<int> indices;134float radius = probe_size * 0.5f;135136if (!Math::is_zero_approx(radius)) {137// L2 Spherical Harmonics evaluation and diffuse convolution coefficients.138const float sh_coeffs[5] = {139static_cast<float>(sqrt(1.0 / (4.0 * Math::PI)) * Math::PI),140static_cast<float>(sqrt(3.0 / (4.0 * Math::PI)) * Math::PI * 2.0 / 3.0),141static_cast<float>(sqrt(15.0 / (4.0 * Math::PI)) * Math::PI * 1.0 / 4.0),142static_cast<float>(sqrt(5.0 / (16.0 * Math::PI)) * Math::PI * 1.0 / 4.0),143static_cast<float>(sqrt(15.0 / (16.0 * Math::PI)) * Math::PI * 1.0 / 4.0)144};145146for (int p = 0; p < points.size(); p++) {147int vertex_base = vertices.size();148Vector3 sh_col[9];149for (int i = 0; i < 9; i++) {150sh_col[i].x = sh[p * 9 + i].r;151sh_col[i].y = sh[p * 9 + i].g;152sh_col[i].z = sh[p * 9 + i].b;153}154155for (int i = 0; i <= stack_count; ++i) {156float stack_angle = Math::PI / 2 - i * stack_step; // starting from pi/2 to -pi/2157float xy = radius * Math::cos(stack_angle); // r * cos(u)158float z = radius * Math::sin(stack_angle); // r * sin(u)159160// add (sector_count+1) vertices per stack161// the first and last vertices have same position and normal, but different tex coords162for (int j = 0; j <= sector_count; ++j) {163float sector_angle = j * sector_step; // starting from 0 to 2pi164165// vertex position (x, y, z)166float x = xy * Math::cos(sector_angle); // r * cos(u) * cos(v)167float y = xy * Math::sin(sector_angle); // r * cos(u) * sin(v)168169Vector3 n = Vector3(x, z, y);170vertices.push_back(points[p] + n);171n.normalize();172173const Vector3 light = (sh_coeffs[0] * sh_col[0] +174sh_coeffs[1] * sh_col[1] * n.y +175sh_coeffs[1] * sh_col[2] * n.z +176sh_coeffs[1] * sh_col[3] * n.x +177sh_coeffs[2] * sh_col[4] * n.x * n.y +178sh_coeffs[2] * sh_col[5] * n.y * n.z +179sh_coeffs[3] * sh_col[6] * (3.0 * n.z * n.z - 1.0) +180sh_coeffs[2] * sh_col[7] * n.x * n.z +181sh_coeffs[4] * sh_col[8] * (n.x * n.x - n.y * n.y));182183colors.push_back(Color(light.x, light.y, light.z, 1));184}185}186187for (int i = 0; i < stack_count; ++i) {188int k1 = i * (sector_count + 1); // beginning of current stack189int k2 = k1 + sector_count + 1; // beginning of next stack190191for (int j = 0; j < sector_count; ++j, ++k1, ++k2) {192// 2 triangles per sector excluding first and last stacks193// k1 => k2 => k1+1194if (i != 0) {195indices.push_back(vertex_base + k1);196indices.push_back(vertex_base + k2);197indices.push_back(vertex_base + k1 + 1);198}199200// k1+1 => k2 => k2+1201if (i != (stack_count - 1)) {202indices.push_back(vertex_base + k1 + 1);203indices.push_back(vertex_base + k2);204indices.push_back(vertex_base + k2 + 1);205}206}207}208}209210Array array;211array.resize(RS::ARRAY_MAX);212array[RS::ARRAY_VERTEX] = Vector<Vector3>(vertices);213array[RS::ARRAY_INDEX] = Vector<int>(indices);214array[RS::ARRAY_COLOR] = Vector<Color>(colors);215216Ref<ArrayMesh> mesh;217mesh.instantiate();218mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, array, Array(), Dictionary(), 0); //no compression219mesh->surface_set_material(0, material_probes);220221p_gizmo->add_mesh(mesh);222}223}224225226