Path: blob/master/scene/resources/gradient_texture.cpp
20843 views
/**************************************************************************/1/* gradient_texture.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 "gradient_texture.h"3132#include "core/math/geometry_2d.h"33#include "servers/rendering/rendering_server.h"3435GradientTexture1D::GradientTexture1D() {36_queue_update();37}3839GradientTexture1D::~GradientTexture1D() {40if (texture.is_valid()) {41ERR_FAIL_NULL(RenderingServer::get_singleton());42RS::get_singleton()->free_rid(texture);43}44}4546void GradientTexture1D::_bind_methods() {47ClassDB::bind_method(D_METHOD("set_gradient", "gradient"), &GradientTexture1D::set_gradient);48ClassDB::bind_method(D_METHOD("get_gradient"), &GradientTexture1D::get_gradient);4950ClassDB::bind_method(D_METHOD("set_width", "width"), &GradientTexture1D::set_width);51// The `get_width()` method is already exposed by the parent class Texture2D.5253ClassDB::bind_method(D_METHOD("set_use_hdr", "enabled"), &GradientTexture1D::set_use_hdr);54ClassDB::bind_method(D_METHOD("is_using_hdr"), &GradientTexture1D::is_using_hdr);5556ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, Gradient::get_class_static(), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_gradient", "get_gradient");57ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,16384,suffix:px"), "set_width", "get_width");58ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hdr"), "set_use_hdr", "is_using_hdr");59}6061void GradientTexture1D::set_gradient(Ref<Gradient> p_gradient) {62if (p_gradient == gradient) {63return;64}65if (gradient.is_valid()) {66gradient->disconnect_changed(callable_mp(this, &GradientTexture1D::_queue_update));67}68gradient = p_gradient;69if (gradient.is_valid()) {70gradient->connect_changed(callable_mp(this, &GradientTexture1D::_queue_update));71}72_queue_update();73emit_changed();74}7576Ref<Gradient> GradientTexture1D::get_gradient() const {77return gradient;78}7980void GradientTexture1D::_queue_update() {81if (update_pending) {82return;83}84update_pending = true;85callable_mp(this, &GradientTexture1D::update_now).call_deferred();86}8788void GradientTexture1D::_update() const {89update_pending = false;9091if (gradient.is_null()) {92return;93}9495if (use_hdr) {96// High dynamic range.97Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBAF));98Gradient &g = **gradient;99// `create()` isn't available for non-uint8_t data, so fill in the data manually.100for (int i = 0; i < width; i++) {101float ofs = float(i) / (width - 1);102image->set_pixel(i, 0, g.get_color_at_offset(ofs));103}104105if (texture.is_valid()) {106RID new_texture = RS::get_singleton()->texture_2d_create(image);107RS::get_singleton()->texture_replace(texture, new_texture);108} else {109texture = RS::get_singleton()->texture_2d_create(image);110}111} else {112// Low dynamic range. "Overbright" colors will be clamped.113Vector<uint8_t> data;114data.resize(width * 4);115{116uint8_t *wd8 = data.ptrw();117Gradient &g = **gradient;118119for (int i = 0; i < width; i++) {120float ofs = float(i) / (width - 1);121Color color = g.get_color_at_offset(ofs);122123wd8[i * 4 + 0] = uint8_t(color.get_r8());124wd8[i * 4 + 1] = uint8_t(color.get_g8());125wd8[i * 4 + 2] = uint8_t(color.get_b8());126wd8[i * 4 + 3] = uint8_t(color.get_a8());127}128}129130Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBA8, data));131132if (texture.is_valid()) {133RID new_texture = RS::get_singleton()->texture_2d_create(image);134RS::get_singleton()->texture_replace(texture, new_texture);135} else {136texture = RS::get_singleton()->texture_2d_create(image);137}138}139RS::get_singleton()->texture_set_path(texture, get_path());140}141142void GradientTexture1D::set_width(int p_width) {143ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range.");144width = p_width;145_queue_update();146emit_changed();147}148149int GradientTexture1D::get_width() const {150return width;151}152153void GradientTexture1D::set_use_hdr(bool p_enabled) {154if (p_enabled == use_hdr) {155return;156}157158use_hdr = p_enabled;159_queue_update();160emit_changed();161}162163bool GradientTexture1D::is_using_hdr() const {164return use_hdr;165}166167RID GradientTexture1D::get_rid() const {168if (!texture.is_valid()) {169texture = RS::get_singleton()->texture_2d_placeholder_create();170}171return texture;172}173174Ref<Image> GradientTexture1D::get_image() const {175update_now();176if (!texture.is_valid()) {177return Ref<Image>();178}179return RenderingServer::get_singleton()->texture_2d_get(texture);180}181182void GradientTexture1D::update_now() const {183if (update_pending) {184_update();185}186}187188//////////////////189190GradientTexture2D::GradientTexture2D() {191_queue_update();192}193194GradientTexture2D::~GradientTexture2D() {195if (texture.is_valid()) {196ERR_FAIL_NULL(RenderingServer::get_singleton());197RS::get_singleton()->free_rid(texture);198}199}200201void GradientTexture2D::set_gradient(Ref<Gradient> p_gradient) {202if (gradient == p_gradient) {203return;204}205if (gradient.is_valid()) {206gradient->disconnect_changed(callable_mp(this, &GradientTexture2D::_queue_update));207}208gradient = p_gradient;209if (gradient.is_valid()) {210gradient->connect_changed(callable_mp(this, &GradientTexture2D::_queue_update));211}212_queue_update();213emit_changed();214}215216Ref<Gradient> GradientTexture2D::get_gradient() const {217return gradient;218}219220void GradientTexture2D::_queue_update() {221if (update_pending) {222return;223}224update_pending = true;225callable_mp(this, &GradientTexture2D::update_now).call_deferred();226}227228void GradientTexture2D::_update() const {229update_pending = false;230231if (gradient.is_null()) {232return;233}234Ref<Image> image;235image.instantiate();236237if (gradient->get_point_count() <= 1) { // No need to interpolate.238image->initialize_data(width, height, false, (use_hdr) ? Image::FORMAT_RGBAF : Image::FORMAT_RGBA8);239image->fill((gradient->get_point_count() == 1) ? gradient->get_color(0) : Color(0, 0, 0, 1));240} else {241if (use_hdr) {242image->initialize_data(width, height, false, Image::FORMAT_RGBAF);243Gradient &g = **gradient;244// `create()` isn't available for non-uint8_t data, so fill in the data manually.245for (int y = 0; y < height; y++) {246for (int x = 0; x < width; x++) {247float ofs = _get_gradient_offset_at(x, y);248image->set_pixel(x, y, g.get_color_at_offset(ofs));249}250}251} else {252Vector<uint8_t> data;253data.resize(width * height * 4);254{255uint8_t *wd8 = data.ptrw();256Gradient &g = **gradient;257for (int y = 0; y < height; y++) {258for (int x = 0; x < width; x++) {259float ofs = _get_gradient_offset_at(x, y);260const Color &c = g.get_color_at_offset(ofs);261262wd8[(x + (y * width)) * 4 + 0] = uint8_t(c.get_r8());263wd8[(x + (y * width)) * 4 + 1] = uint8_t(c.get_g8());264wd8[(x + (y * width)) * 4 + 2] = uint8_t(c.get_b8());265wd8[(x + (y * width)) * 4 + 3] = uint8_t(c.get_a8());266}267}268}269image->set_data(width, height, false, Image::FORMAT_RGBA8, data);270}271}272273if (texture.is_valid()) {274RID new_texture = RS::get_singleton()->texture_2d_create(image);275RS::get_singleton()->texture_replace(texture, new_texture);276} else {277texture = RS::get_singleton()->texture_2d_create(image);278}279RS::get_singleton()->texture_set_path(texture, get_path());280}281282float GradientTexture2D::_get_gradient_offset_at(int x, int y) const {283if (fill_to == fill_from) {284return 0;285}286float ofs = 0;287Vector2 pos;288if (width > 1) {289pos.x = static_cast<float>(x) / (width - 1);290}291if (height > 1) {292pos.y = static_cast<float>(y) / (height - 1);293}294if (fill == Fill::FILL_LINEAR) {295const Vector2 closest = Geometry2D::get_closest_point_to_segment_uncapped(pos, fill_from, fill_to);296ofs = (closest - fill_from).length() / (fill_to - fill_from).length();297if ((closest - fill_from).dot(fill_to - fill_from) < 0) {298ofs *= -1;299}300} else if (fill == Fill::FILL_RADIAL) {301ofs = (pos - fill_from).length() / (fill_to - fill_from).length();302} else if (fill == Fill::FILL_SQUARE) {303ofs = MAX(Math::abs(pos.x - fill_from.x), Math::abs(pos.y - fill_from.y)) / MAX(Math::abs(fill_to.x - fill_from.x), Math::abs(fill_to.y - fill_from.y));304}305if (repeat == Repeat::REPEAT_NONE) {306ofs = CLAMP(ofs, 0.0, 1.0);307} else if (repeat == Repeat::REPEAT) {308ofs = Math::fmod(ofs, 1.0f);309if (ofs < 0) {310ofs = 1 + ofs;311}312} else if (repeat == Repeat::REPEAT_MIRROR) {313ofs = Math::abs(ofs);314ofs = Math::fmod(ofs, 2.0f);315if (ofs > 1.0) {316ofs = 2.0 - ofs;317}318}319return ofs;320}321322void GradientTexture2D::set_width(int p_width) {323ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range.");324width = p_width;325_queue_update();326emit_changed();327}328329int GradientTexture2D::get_width() const {330return width;331}332333void GradientTexture2D::set_height(int p_height) {334ERR_FAIL_COND_MSG(p_height <= 0 || p_height > 16384, "Texture dimensions have to be within 1 to 16384 range.");335height = p_height;336_queue_update();337emit_changed();338}339int GradientTexture2D::get_height() const {340return height;341}342343void GradientTexture2D::set_use_hdr(bool p_enabled) {344if (p_enabled == use_hdr) {345return;346}347348use_hdr = p_enabled;349_queue_update();350emit_changed();351}352353bool GradientTexture2D::is_using_hdr() const {354return use_hdr;355}356357void GradientTexture2D::set_fill_from(Vector2 p_fill_from) {358fill_from = p_fill_from;359_queue_update();360emit_changed();361}362363Vector2 GradientTexture2D::get_fill_from() const {364return fill_from;365}366367void GradientTexture2D::set_fill_to(Vector2 p_fill_to) {368fill_to = p_fill_to;369_queue_update();370emit_changed();371}372373Vector2 GradientTexture2D::get_fill_to() const {374return fill_to;375}376377void GradientTexture2D::set_fill(Fill p_fill) {378fill = p_fill;379_queue_update();380emit_changed();381}382383GradientTexture2D::Fill GradientTexture2D::get_fill() const {384return fill;385}386387void GradientTexture2D::set_repeat(Repeat p_repeat) {388repeat = p_repeat;389_queue_update();390emit_changed();391}392393GradientTexture2D::Repeat GradientTexture2D::get_repeat() const {394return repeat;395}396397RID GradientTexture2D::get_rid() const {398if (!texture.is_valid()) {399texture = RS::get_singleton()->texture_2d_placeholder_create();400}401return texture;402}403404Ref<Image> GradientTexture2D::get_image() const {405update_now();406if (!texture.is_valid()) {407return Ref<Image>();408}409return RenderingServer::get_singleton()->texture_2d_get(texture);410}411412void GradientTexture2D::update_now() const {413if (update_pending) {414_update();415}416}417418void GradientTexture2D::_bind_methods() {419ClassDB::bind_method(D_METHOD("set_gradient", "gradient"), &GradientTexture2D::set_gradient);420ClassDB::bind_method(D_METHOD("get_gradient"), &GradientTexture2D::get_gradient);421422ClassDB::bind_method(D_METHOD("set_width", "width"), &GradientTexture2D::set_width);423ClassDB::bind_method(D_METHOD("set_height", "height"), &GradientTexture2D::set_height);424425ClassDB::bind_method(D_METHOD("set_use_hdr", "enabled"), &GradientTexture2D::set_use_hdr);426ClassDB::bind_method(D_METHOD("is_using_hdr"), &GradientTexture2D::is_using_hdr);427428ClassDB::bind_method(D_METHOD("set_fill", "fill"), &GradientTexture2D::set_fill);429ClassDB::bind_method(D_METHOD("get_fill"), &GradientTexture2D::get_fill);430ClassDB::bind_method(D_METHOD("set_fill_from", "fill_from"), &GradientTexture2D::set_fill_from);431ClassDB::bind_method(D_METHOD("get_fill_from"), &GradientTexture2D::get_fill_from);432ClassDB::bind_method(D_METHOD("set_fill_to", "fill_to"), &GradientTexture2D::set_fill_to);433ClassDB::bind_method(D_METHOD("get_fill_to"), &GradientTexture2D::get_fill_to);434435ClassDB::bind_method(D_METHOD("set_repeat", "repeat"), &GradientTexture2D::set_repeat);436ClassDB::bind_method(D_METHOD("get_repeat"), &GradientTexture2D::get_repeat);437438ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, Gradient::get_class_static(), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_gradient", "get_gradient");439ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_width", "get_width");440ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_height", "get_height");441ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hdr"), "set_use_hdr", "is_using_hdr");442443ADD_GROUP("Fill", "fill_");444ADD_PROPERTY(PropertyInfo(Variant::INT, "fill", PROPERTY_HINT_ENUM, "Linear,Radial,Square"), "set_fill", "get_fill");445ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "fill_from"), "set_fill_from", "get_fill_from");446ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "fill_to"), "set_fill_to", "get_fill_to");447448ADD_GROUP("Repeat", "repeat_");449ADD_PROPERTY(PropertyInfo(Variant::INT, "repeat", PROPERTY_HINT_ENUM, "No Repeat,Repeat,Mirror Repeat"), "set_repeat", "get_repeat");450451BIND_ENUM_CONSTANT(FILL_LINEAR);452BIND_ENUM_CONSTANT(FILL_RADIAL);453BIND_ENUM_CONSTANT(FILL_SQUARE);454455BIND_ENUM_CONSTANT(REPEAT_NONE);456BIND_ENUM_CONSTANT(REPEAT);457BIND_ENUM_CONSTANT(REPEAT_MIRROR);458}459460461