Path: blob/master/scene/resources/gradient_texture.cpp
9903 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"3334GradientTexture1D::GradientTexture1D() {35_queue_update();36}3738GradientTexture1D::~GradientTexture1D() {39if (texture.is_valid()) {40ERR_FAIL_NULL(RenderingServer::get_singleton());41RS::get_singleton()->free(texture);42}43}4445void GradientTexture1D::_bind_methods() {46ClassDB::bind_method(D_METHOD("set_gradient", "gradient"), &GradientTexture1D::set_gradient);47ClassDB::bind_method(D_METHOD("get_gradient"), &GradientTexture1D::get_gradient);4849ClassDB::bind_method(D_METHOD("set_width", "width"), &GradientTexture1D::set_width);50// The `get_width()` method is already exposed by the parent class Texture2D.5152ClassDB::bind_method(D_METHOD("set_use_hdr", "enabled"), &GradientTexture1D::set_use_hdr);53ClassDB::bind_method(D_METHOD("is_using_hdr"), &GradientTexture1D::is_using_hdr);5455ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_gradient", "get_gradient");56ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,16384,suffix:px"), "set_width", "get_width");57ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hdr"), "set_use_hdr", "is_using_hdr");58}5960void GradientTexture1D::set_gradient(Ref<Gradient> p_gradient) {61if (p_gradient == gradient) {62return;63}64if (gradient.is_valid()) {65gradient->disconnect_changed(callable_mp(this, &GradientTexture1D::_queue_update));66}67gradient = p_gradient;68if (gradient.is_valid()) {69gradient->connect_changed(callable_mp(this, &GradientTexture1D::_queue_update));70}71_queue_update();72emit_changed();73}7475Ref<Gradient> GradientTexture1D::get_gradient() const {76return gradient;77}7879void GradientTexture1D::_queue_update() {80if (update_pending) {81return;82}83update_pending = true;84callable_mp(this, &GradientTexture1D::update_now).call_deferred();85}8687void GradientTexture1D::_update() const {88update_pending = false;8990if (gradient.is_null()) {91return;92}9394if (use_hdr) {95// High dynamic range.96Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBAF));97Gradient &g = **gradient;98// `create()` isn't available for non-uint8_t data, so fill in the data manually.99for (int i = 0; i < width; i++) {100float ofs = float(i) / (width - 1);101image->set_pixel(i, 0, g.get_color_at_offset(ofs));102}103104if (texture.is_valid()) {105RID new_texture = RS::get_singleton()->texture_2d_create(image);106RS::get_singleton()->texture_replace(texture, new_texture);107} else {108texture = RS::get_singleton()->texture_2d_create(image);109}110} else {111// Low dynamic range. "Overbright" colors will be clamped.112Vector<uint8_t> data;113data.resize(width * 4);114{115uint8_t *wd8 = data.ptrw();116Gradient &g = **gradient;117118for (int i = 0; i < width; i++) {119float ofs = float(i) / (width - 1);120Color color = g.get_color_at_offset(ofs);121122wd8[i * 4 + 0] = uint8_t(CLAMP(color.r * 255.0, 0, 255));123wd8[i * 4 + 1] = uint8_t(CLAMP(color.g * 255.0, 0, 255));124wd8[i * 4 + 2] = uint8_t(CLAMP(color.b * 255.0, 0, 255));125wd8[i * 4 + 3] = uint8_t(CLAMP(color.a * 255.0, 0, 255));126}127}128129Ref<Image> image = memnew(Image(width, 1, false, Image::FORMAT_RGBA8, data));130131if (texture.is_valid()) {132RID new_texture = RS::get_singleton()->texture_2d_create(image);133RS::get_singleton()->texture_replace(texture, new_texture);134} else {135texture = RS::get_singleton()->texture_2d_create(image);136}137}138RS::get_singleton()->texture_set_path(texture, get_path());139}140141void GradientTexture1D::set_width(int p_width) {142ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range.");143width = p_width;144_queue_update();145emit_changed();146}147148int GradientTexture1D::get_width() const {149return width;150}151152void GradientTexture1D::set_use_hdr(bool p_enabled) {153if (p_enabled == use_hdr) {154return;155}156157use_hdr = p_enabled;158_queue_update();159emit_changed();160}161162bool GradientTexture1D::is_using_hdr() const {163return use_hdr;164}165166RID GradientTexture1D::get_rid() const {167if (!texture.is_valid()) {168texture = RS::get_singleton()->texture_2d_placeholder_create();169}170return texture;171}172173Ref<Image> GradientTexture1D::get_image() const {174update_now();175if (!texture.is_valid()) {176return Ref<Image>();177}178return RenderingServer::get_singleton()->texture_2d_get(texture);179}180181void GradientTexture1D::update_now() const {182if (update_pending) {183_update();184}185}186187//////////////////188189GradientTexture2D::GradientTexture2D() {190_queue_update();191}192193GradientTexture2D::~GradientTexture2D() {194if (texture.is_valid()) {195ERR_FAIL_NULL(RenderingServer::get_singleton());196RS::get_singleton()->free(texture);197}198}199200void GradientTexture2D::set_gradient(Ref<Gradient> p_gradient) {201if (gradient == p_gradient) {202return;203}204if (gradient.is_valid()) {205gradient->disconnect_changed(callable_mp(this, &GradientTexture2D::_queue_update));206}207gradient = p_gradient;208if (gradient.is_valid()) {209gradient->connect_changed(callable_mp(this, &GradientTexture2D::_queue_update));210}211_queue_update();212emit_changed();213}214215Ref<Gradient> GradientTexture2D::get_gradient() const {216return gradient;217}218219void GradientTexture2D::_queue_update() {220if (update_pending) {221return;222}223update_pending = true;224callable_mp(this, &GradientTexture2D::update_now).call_deferred();225}226227void GradientTexture2D::_update() const {228update_pending = false;229230if (gradient.is_null()) {231return;232}233Ref<Image> image;234image.instantiate();235236if (gradient->get_point_count() <= 1) { // No need to interpolate.237image->initialize_data(width, height, false, (use_hdr) ? Image::FORMAT_RGBAF : Image::FORMAT_RGBA8);238image->fill((gradient->get_point_count() == 1) ? gradient->get_color(0) : Color(0, 0, 0, 1));239} else {240if (use_hdr) {241image->initialize_data(width, height, false, Image::FORMAT_RGBAF);242Gradient &g = **gradient;243// `create()` isn't available for non-uint8_t data, so fill in the data manually.244for (int y = 0; y < height; y++) {245for (int x = 0; x < width; x++) {246float ofs = _get_gradient_offset_at(x, y);247image->set_pixel(x, y, g.get_color_at_offset(ofs));248}249}250} else {251Vector<uint8_t> data;252data.resize(width * height * 4);253{254uint8_t *wd8 = data.ptrw();255Gradient &g = **gradient;256for (int y = 0; y < height; y++) {257for (int x = 0; x < width; x++) {258float ofs = _get_gradient_offset_at(x, y);259const Color &c = g.get_color_at_offset(ofs);260261wd8[(x + (y * width)) * 4 + 0] = uint8_t(CLAMP(c.r * 255.0, 0, 255));262wd8[(x + (y * width)) * 4 + 1] = uint8_t(CLAMP(c.g * 255.0, 0, 255));263wd8[(x + (y * width)) * 4 + 2] = uint8_t(CLAMP(c.b * 255.0, 0, 255));264wd8[(x + (y * width)) * 4 + 3] = uint8_t(CLAMP(c.a * 255.0, 0, 255));265}266}267}268image->set_data(width, height, false, Image::FORMAT_RGBA8, data);269}270}271272if (texture.is_valid()) {273RID new_texture = RS::get_singleton()->texture_2d_create(image);274RS::get_singleton()->texture_replace(texture, new_texture);275} else {276texture = RS::get_singleton()->texture_2d_create(image);277}278RS::get_singleton()->texture_set_path(texture, get_path());279}280281float GradientTexture2D::_get_gradient_offset_at(int x, int y) const {282if (fill_to == fill_from) {283return 0;284}285float ofs = 0;286Vector2 pos;287if (width > 1) {288pos.x = static_cast<float>(x) / (width - 1);289}290if (height > 1) {291pos.y = static_cast<float>(y) / (height - 1);292}293if (fill == Fill::FILL_LINEAR) {294const Vector2 closest = Geometry2D::get_closest_point_to_segment_uncapped(pos, fill_from, fill_to);295ofs = (closest - fill_from).length() / (fill_to - fill_from).length();296if ((closest - fill_from).dot(fill_to - fill_from) < 0) {297ofs *= -1;298}299} else if (fill == Fill::FILL_RADIAL) {300ofs = (pos - fill_from).length() / (fill_to - fill_from).length();301} else if (fill == Fill::FILL_SQUARE) {302ofs = 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));303}304if (repeat == Repeat::REPEAT_NONE) {305ofs = CLAMP(ofs, 0.0, 1.0);306} else if (repeat == Repeat::REPEAT) {307ofs = Math::fmod(ofs, 1.0f);308if (ofs < 0) {309ofs = 1 + ofs;310}311} else if (repeat == Repeat::REPEAT_MIRROR) {312ofs = Math::abs(ofs);313ofs = Math::fmod(ofs, 2.0f);314if (ofs > 1.0) {315ofs = 2.0 - ofs;316}317}318return ofs;319}320321void GradientTexture2D::set_width(int p_width) {322ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range.");323width = p_width;324_queue_update();325emit_changed();326}327328int GradientTexture2D::get_width() const {329return width;330}331332void GradientTexture2D::set_height(int p_height) {333ERR_FAIL_COND_MSG(p_height <= 0 || p_height > 16384, "Texture dimensions have to be within 1 to 16384 range.");334height = p_height;335_queue_update();336emit_changed();337}338int GradientTexture2D::get_height() const {339return height;340}341342void GradientTexture2D::set_use_hdr(bool p_enabled) {343if (p_enabled == use_hdr) {344return;345}346347use_hdr = p_enabled;348_queue_update();349emit_changed();350}351352bool GradientTexture2D::is_using_hdr() const {353return use_hdr;354}355356void GradientTexture2D::set_fill_from(Vector2 p_fill_from) {357fill_from = p_fill_from;358_queue_update();359emit_changed();360}361362Vector2 GradientTexture2D::get_fill_from() const {363return fill_from;364}365366void GradientTexture2D::set_fill_to(Vector2 p_fill_to) {367fill_to = p_fill_to;368_queue_update();369emit_changed();370}371372Vector2 GradientTexture2D::get_fill_to() const {373return fill_to;374}375376void GradientTexture2D::set_fill(Fill p_fill) {377fill = p_fill;378_queue_update();379emit_changed();380}381382GradientTexture2D::Fill GradientTexture2D::get_fill() const {383return fill;384}385386void GradientTexture2D::set_repeat(Repeat p_repeat) {387repeat = p_repeat;388_queue_update();389emit_changed();390}391392GradientTexture2D::Repeat GradientTexture2D::get_repeat() const {393return repeat;394}395396RID GradientTexture2D::get_rid() const {397if (!texture.is_valid()) {398texture = RS::get_singleton()->texture_2d_placeholder_create();399}400return texture;401}402403Ref<Image> GradientTexture2D::get_image() const {404update_now();405if (!texture.is_valid()) {406return Ref<Image>();407}408return RenderingServer::get_singleton()->texture_2d_get(texture);409}410411void GradientTexture2D::update_now() const {412if (update_pending) {413_update();414}415}416417void GradientTexture2D::_bind_methods() {418ClassDB::bind_method(D_METHOD("set_gradient", "gradient"), &GradientTexture2D::set_gradient);419ClassDB::bind_method(D_METHOD("get_gradient"), &GradientTexture2D::get_gradient);420421ClassDB::bind_method(D_METHOD("set_width", "width"), &GradientTexture2D::set_width);422ClassDB::bind_method(D_METHOD("set_height", "height"), &GradientTexture2D::set_height);423424ClassDB::bind_method(D_METHOD("set_use_hdr", "enabled"), &GradientTexture2D::set_use_hdr);425ClassDB::bind_method(D_METHOD("is_using_hdr"), &GradientTexture2D::is_using_hdr);426427ClassDB::bind_method(D_METHOD("set_fill", "fill"), &GradientTexture2D::set_fill);428ClassDB::bind_method(D_METHOD("get_fill"), &GradientTexture2D::get_fill);429ClassDB::bind_method(D_METHOD("set_fill_from", "fill_from"), &GradientTexture2D::set_fill_from);430ClassDB::bind_method(D_METHOD("get_fill_from"), &GradientTexture2D::get_fill_from);431ClassDB::bind_method(D_METHOD("set_fill_to", "fill_to"), &GradientTexture2D::set_fill_to);432ClassDB::bind_method(D_METHOD("get_fill_to"), &GradientTexture2D::get_fill_to);433434ClassDB::bind_method(D_METHOD("set_repeat", "repeat"), &GradientTexture2D::set_repeat);435ClassDB::bind_method(D_METHOD("get_repeat"), &GradientTexture2D::get_repeat);436437ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_gradient", "get_gradient");438ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_width", "get_width");439ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_height", "get_height");440ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hdr"), "set_use_hdr", "is_using_hdr");441442ADD_GROUP("Fill", "fill_");443ADD_PROPERTY(PropertyInfo(Variant::INT, "fill", PROPERTY_HINT_ENUM, "Linear,Radial,Square"), "set_fill", "get_fill");444ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "fill_from"), "set_fill_from", "get_fill_from");445ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "fill_to"), "set_fill_to", "get_fill_to");446447ADD_GROUP("Repeat", "repeat_");448ADD_PROPERTY(PropertyInfo(Variant::INT, "repeat", PROPERTY_HINT_ENUM, "No Repeat,Repeat,Mirror Repeat"), "set_repeat", "get_repeat");449450BIND_ENUM_CONSTANT(FILL_LINEAR);451BIND_ENUM_CONSTANT(FILL_RADIAL);452BIND_ENUM_CONSTANT(FILL_SQUARE);453454BIND_ENUM_CONSTANT(REPEAT_NONE);455BIND_ENUM_CONSTANT(REPEAT);456BIND_ENUM_CONSTANT(REPEAT_MIRROR);457}458459460