|
| 1 | +/**************************************************************************/ |
| 2 | +/* rasterized_mesh_texture.cpp */ |
| 3 | +/**************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* GODOT ENGINE */ |
| 6 | +/* https://godotengine.org */ |
| 7 | +/**************************************************************************/ |
| 8 | +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | +/* */ |
| 11 | +/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | +/* a copy of this software and associated documentation files (the */ |
| 13 | +/* "Software"), to deal in the Software without restriction, including */ |
| 14 | +/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | +/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | +/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | +/* the following conditions: */ |
| 18 | +/* */ |
| 19 | +/* The above copyright notice and this permission notice shall be */ |
| 20 | +/* included in all copies or substantial portions of the Software. */ |
| 21 | +/* */ |
| 22 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | +/**************************************************************************/ |
| 30 | + |
| 31 | +#include "rasterized_mesh_texture.h" |
| 32 | +#include "scene/resources/mesh.h" |
| 33 | + |
| 34 | +int RasterizedMeshTexture::get_width() const { |
| 35 | + return size.width; |
| 36 | +} |
| 37 | + |
| 38 | +int RasterizedMeshTexture::get_height() const { |
| 39 | + return size.height; |
| 40 | +} |
| 41 | + |
| 42 | +bool RasterizedMeshTexture::has_alpha() const { |
| 43 | + return true; |
| 44 | +} |
| 45 | + |
| 46 | +RID RasterizedMeshTexture::get_rid() const { |
| 47 | + return texture; |
| 48 | +} |
| 49 | + |
| 50 | +Ref<Image> RasterizedMeshTexture::get_image() const { |
| 51 | + if (texture.is_null()) { |
| 52 | + return Ref<Image>(); |
| 53 | + } |
| 54 | + return RenderingServer::get_singleton()->texture_2d_get(texture); |
| 55 | +} |
| 56 | + |
| 57 | +void RasterizedMeshTexture::set_width(int p_width) { |
| 58 | + ERR_FAIL_COND(p_width <= 0 || p_width > 16384); |
| 59 | + size.width = p_width; |
| 60 | + rasterizer_dirty = true; |
| 61 | + queue_update_texture(); |
| 62 | +} |
| 63 | + |
| 64 | +void RasterizedMeshTexture::set_height(int p_height) { |
| 65 | + ERR_FAIL_COND(p_height <= 0 || p_height > 16384); |
| 66 | + size.height = p_height; |
| 67 | + rasterizer_dirty = true; |
| 68 | + queue_update_texture(); |
| 69 | +} |
| 70 | + |
| 71 | +void RasterizedMeshTexture::set_mesh(const Ref<Mesh> &p_mesh) { |
| 72 | + mesh = p_mesh; |
| 73 | + mesh_drity = true; |
| 74 | + queue_update_texture(); |
| 75 | +} |
| 76 | + |
| 77 | +Ref<Mesh> RasterizedMeshTexture::get_mesh() const { |
| 78 | + return mesh; |
| 79 | +} |
| 80 | + |
| 81 | +void RasterizedMeshTexture::set_bg_color(const Color &p_color) { |
| 82 | + bg_color = p_color; |
| 83 | + queue_update_texture(); |
| 84 | +} |
| 85 | + |
| 86 | +Color RasterizedMeshTexture::get_bg_color() const { |
| 87 | + return bg_color; |
| 88 | +} |
| 89 | + |
| 90 | +void RasterizedMeshTexture::set_material(const Ref<ShaderMaterial> &p_material) { |
| 91 | + material = p_material; |
| 92 | + material_drity = true; |
| 93 | + queue_update_texture(); |
| 94 | +} |
| 95 | + |
| 96 | +Ref<ShaderMaterial> RasterizedMeshTexture::get_material() const { |
| 97 | + return material; |
| 98 | +} |
| 99 | + |
| 100 | +void RasterizedMeshTexture::set_surface_index(int p_surface_index) { |
| 101 | + surface_index = p_surface_index; |
| 102 | + mesh_drity = true; |
| 103 | + RS::get_singleton()->mesh_rasterizer_draw(mesh_rasterizer); |
| 104 | +} |
| 105 | + |
| 106 | +int RasterizedMeshTexture::get_surface_index() const { |
| 107 | + return surface_index; |
| 108 | +} |
| 109 | + |
| 110 | +void RasterizedMeshTexture::set_texture_format(RS::RasterizedTextureFormat p_texture_format) { |
| 111 | + texture_format = p_texture_format; |
| 112 | + rasterizer_dirty = true; |
| 113 | + queue_update_texture(); |
| 114 | +} |
| 115 | + |
| 116 | +RS::RasterizedTextureFormat RasterizedMeshTexture::get_texture_format() const { |
| 117 | + return texture_format; |
| 118 | +} |
| 119 | + |
| 120 | +void RasterizedMeshTexture::set_generate_mipmaps(bool p_generate_mipmaps) { |
| 121 | + generate_mipmaps = p_generate_mipmaps; |
| 122 | + rasterizer_dirty = true; |
| 123 | + queue_update_texture(); |
| 124 | +} |
| 125 | + |
| 126 | +bool RasterizedMeshTexture::is_generating_mipmaps() const { |
| 127 | + return generate_mipmaps; |
| 128 | +} |
| 129 | + |
| 130 | +RasterizedMeshTexture::RasterizedMeshTexture() { |
| 131 | + mesh_rasterizer = RS::get_singleton()->mesh_rasterizer_create(size.width, size.height, texture_format, generate_mipmaps); |
| 132 | + texture = RS::get_singleton()->texture_rd_create(RS::get_singleton()->mesh_rasterizer_get_rd_texture(mesh_rasterizer)); |
| 133 | +} |
| 134 | + |
| 135 | +RasterizedMeshTexture::~RasterizedMeshTexture() { |
| 136 | + RS::get_singleton()->free(texture); |
| 137 | + RS::get_singleton()->free(mesh_rasterizer); |
| 138 | +} |
| 139 | + |
| 140 | +void RasterizedMeshTexture::update_texture() { |
| 141 | + if (rasterizer_dirty) { |
| 142 | + RID new_mesh_rasterizer = RS::get_singleton()->mesh_rasterizer_create(size.width, size.height, texture_format, generate_mipmaps); |
| 143 | + RID new_texture = RS::get_singleton()->texture_rd_create(RS::get_singleton()->mesh_rasterizer_get_rd_texture(new_mesh_rasterizer)); |
| 144 | + RS::get_singleton()->texture_replace(texture, new_texture); |
| 145 | + RS::get_singleton()->free(mesh_rasterizer); |
| 146 | + mesh_rasterizer = new_mesh_rasterizer; |
| 147 | + } |
| 148 | + if (rasterizer_dirty || mesh_drity) { |
| 149 | + RS::get_singleton()->mesh_rasterizer_set_mesh(mesh_rasterizer, mesh.is_valid() ? mesh->get_rid() : RID(), surface_index); |
| 150 | + mesh_drity = false; |
| 151 | + } |
| 152 | + if (rasterizer_dirty || material_drity) { |
| 153 | + RS::get_singleton()->mesh_rasterizer_set_material(mesh_rasterizer, material.is_valid() ? material->get_rid() : RID()); |
| 154 | + material_drity = false; |
| 155 | + } |
| 156 | + RS::get_singleton()->mesh_rasterizer_set_bg_color(mesh_rasterizer, bg_color); |
| 157 | + RS::get_singleton()->mesh_rasterizer_draw(mesh_rasterizer); |
| 158 | + rasterizer_dirty = false; |
| 159 | + update_queued = false; |
| 160 | + emit_changed(); |
| 161 | +} |
| 162 | + |
| 163 | +void RasterizedMeshTexture::queue_update_texture() { |
| 164 | + if (update_queued) { |
| 165 | + return; |
| 166 | + } |
| 167 | + callable_mp(this, &RasterizedMeshTexture::update_texture).call_deferred(); |
| 168 | + update_queued = true; |
| 169 | +} |
| 170 | + |
| 171 | +void RasterizedMeshTexture::_bind_methods() { |
| 172 | + ClassDB::bind_method(D_METHOD("set_width", "width"), &RasterizedMeshTexture::set_width); |
| 173 | + ClassDB::bind_method(D_METHOD("set_height", "height"), &RasterizedMeshTexture::set_height); |
| 174 | + ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &RasterizedMeshTexture::set_mesh); |
| 175 | + ClassDB::bind_method(D_METHOD("get_mesh"), &RasterizedMeshTexture::get_mesh); |
| 176 | + ClassDB::bind_method(D_METHOD("set_bg_color", "color"), &RasterizedMeshTexture::set_bg_color); |
| 177 | + ClassDB::bind_method(D_METHOD("get_bg_color"), &RasterizedMeshTexture::get_bg_color); |
| 178 | + ClassDB::bind_method(D_METHOD("set_material", "material"), &RasterizedMeshTexture::set_material); |
| 179 | + ClassDB::bind_method(D_METHOD("get_material"), &RasterizedMeshTexture::get_material); |
| 180 | + ClassDB::bind_method(D_METHOD("set_surface_index", "surface_index"), &RasterizedMeshTexture::set_surface_index); |
| 181 | + ClassDB::bind_method(D_METHOD("get_surface_index"), &RasterizedMeshTexture::get_surface_index); |
| 182 | + ClassDB::bind_method(D_METHOD("set_texture_format", "texture_format"), &RasterizedMeshTexture::set_texture_format); |
| 183 | + ClassDB::bind_method(D_METHOD("get_texture_format"), &RasterizedMeshTexture::get_texture_format); |
| 184 | + ClassDB::bind_method(D_METHOD("set_generate_mipmaps", "generate_mipmaps"), &RasterizedMeshTexture::set_generate_mipmaps); |
| 185 | + ClassDB::bind_method(D_METHOD("is_generating_mipmaps"), &RasterizedMeshTexture::is_generating_mipmaps); |
| 186 | + |
| 187 | + ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_width", "get_width"); |
| 188 | + ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_height", "get_height"); |
| 189 | + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "bg_color"), "set_bg_color", "get_bg_color"); |
| 190 | + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh"); |
| 191 | + ADD_PROPERTY(PropertyInfo(Variant::INT, "surface_index", PROPERTY_HINT_RANGE, "0,10,1,or_greater"), "set_surface_index", "get_surface_index"); |
| 192 | + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial"), "set_material", "get_material"); |
| 193 | + ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_format", PROPERTY_HINT_ENUM, "RGBA8,RGBA8_SRGB,RGBAH,RGBAF"), "set_texture_format", "get_texture_format"); |
| 194 | + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps"), "set_generate_mipmaps", "is_generating_mipmaps"); |
| 195 | +} |
0 commit comments