Path: blob/master/modules/noise/noise_texture_2d.cpp
20878 views
/**************************************************************************/1/* noise_texture_2d.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 "noise_texture_2d.h"3132#include "noise.h"3334#include "servers/rendering/rendering_server.h"3536NoiseTexture2D::NoiseTexture2D() {37noise = Ref<Noise>();3839_queue_update();40}4142NoiseTexture2D::~NoiseTexture2D() {43ERR_FAIL_NULL(RenderingServer::get_singleton());44if (texture.is_valid()) {45RS::get_singleton()->free_rid(texture);46}47if (noise_thread.is_started()) {48noise_thread.wait_to_finish();49}50}5152void NoiseTexture2D::_bind_methods() {53ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture2D::set_width);54ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture2D::set_height);5556ClassDB::bind_method(D_METHOD("set_generate_mipmaps", "invert"), &NoiseTexture2D::set_generate_mipmaps);57ClassDB::bind_method(D_METHOD("is_generating_mipmaps"), &NoiseTexture2D::is_generating_mipmaps);5859ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture2D::set_noise);60ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture2D::get_noise);6162ClassDB::bind_method(D_METHOD("set_color_ramp", "gradient"), &NoiseTexture2D::set_color_ramp);63ClassDB::bind_method(D_METHOD("get_color_ramp"), &NoiseTexture2D::get_color_ramp);6465ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture2D::set_seamless);66ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture2D::get_seamless);6768ClassDB::bind_method(D_METHOD("set_invert", "invert"), &NoiseTexture2D::set_invert);69ClassDB::bind_method(D_METHOD("get_invert"), &NoiseTexture2D::get_invert);7071ClassDB::bind_method(D_METHOD("set_in_3d_space", "enable"), &NoiseTexture2D::set_in_3d_space);72ClassDB::bind_method(D_METHOD("is_in_3d_space"), &NoiseTexture2D::is_in_3d_space);7374ClassDB::bind_method(D_METHOD("set_as_normal_map", "as_normal_map"), &NoiseTexture2D::set_as_normal_map);75ClassDB::bind_method(D_METHOD("is_normal_map"), &NoiseTexture2D::is_normal_map);7677ClassDB::bind_method(D_METHOD("set_normalize", "normalize"), &NoiseTexture2D::set_normalize);78ClassDB::bind_method(D_METHOD("is_normalized"), &NoiseTexture2D::is_normalized);7980ClassDB::bind_method(D_METHOD("set_seamless_blend_skirt", "seamless_blend_skirt"), &NoiseTexture2D::set_seamless_blend_skirt);81ClassDB::bind_method(D_METHOD("get_seamless_blend_skirt"), &NoiseTexture2D::get_seamless_blend_skirt);8283ClassDB::bind_method(D_METHOD("set_bump_strength", "bump_strength"), &NoiseTexture2D::set_bump_strength);84ClassDB::bind_method(D_METHOD("get_bump_strength"), &NoiseTexture2D::get_bump_strength);8586ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_width", "get_width");87ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_height", "get_height");88ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps"), "set_generate_mipmaps", "is_generating_mipmaps");89ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, Noise::get_class_static()), "set_noise", "get_noise");90ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, Gradient::get_class_static()), "set_color_ramp", "get_color_ramp");91ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");92ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert"), "set_invert", "get_invert");93ADD_PROPERTY(PropertyInfo(Variant::BOOL, "in_3d_space"), "set_in_3d_space", "is_in_3d_space");94ADD_PROPERTY(PropertyInfo(Variant::BOOL, "as_normal_map"), "set_as_normal_map", "is_normal_map");95ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalize"), "set_normalize", "is_normalized");96ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "seamless_blend_skirt", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_seamless_blend_skirt", "get_seamless_blend_skirt");97ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bump_strength", PROPERTY_HINT_RANGE, "0,32,0.1,or_greater"), "set_bump_strength", "get_bump_strength");98}99100void NoiseTexture2D::_validate_property(PropertyInfo &p_property) const {101if (!Engine::get_singleton()->is_editor_hint()) {102return;103}104if (p_property.name == "bump_strength") {105if (!as_normal_map) {106p_property.usage = PROPERTY_USAGE_NO_EDITOR;107}108}109110if (p_property.name == "seamless_blend_skirt") {111if (!seamless) {112p_property.usage = PROPERTY_USAGE_NO_EDITOR;113}114}115}116117void NoiseTexture2D::_set_texture_image(const Ref<Image> &p_image) {118image = p_image;119if (image.is_valid()) {120if (texture.is_valid()) {121RID new_texture = RS::get_singleton()->texture_2d_create(p_image);122RS::get_singleton()->texture_replace(texture, new_texture);123} else {124texture = RS::get_singleton()->texture_2d_create(p_image);125}126RS::get_singleton()->texture_set_path(texture, get_path());127}128emit_changed();129}130131void NoiseTexture2D::_thread_done(const Ref<Image> &p_image) {132_set_texture_image(p_image);133noise_thread.wait_to_finish();134if (regen_queued) {135noise_thread.start(_thread_function, this);136regen_queued = false;137}138}139140void NoiseTexture2D::_thread_function(void *p_ud) {141NoiseTexture2D *tex = static_cast<NoiseTexture2D *>(p_ud);142callable_mp(tex, &NoiseTexture2D::_thread_done).call_deferred(tex->_generate_texture());143}144145void NoiseTexture2D::_queue_update() {146if (update_queued) {147return;148}149150update_queued = true;151callable_mp(this, &NoiseTexture2D::_update_texture).call_deferred();152}153154Ref<Image> NoiseTexture2D::_generate_texture() {155// Prevent memdelete due to unref() on other thread.156Ref<Noise> ref_noise = noise;157158if (ref_noise.is_null()) {159return Ref<Image>();160}161162Ref<Image> new_image;163164if (seamless) {165new_image = ref_noise->get_seamless_image(size.x, size.y, invert, in_3d_space, seamless_blend_skirt, normalize);166} else {167new_image = ref_noise->get_image(size.x, size.y, invert, in_3d_space, normalize);168}169if (color_ramp.is_valid()) {170new_image = _modulate_with_gradient(new_image, color_ramp);171}172if (as_normal_map) {173new_image->bump_map_to_normal_map(bump_strength);174}175if (generate_mipmaps) {176new_image->generate_mipmaps();177}178179return new_image;180}181182Ref<Image> NoiseTexture2D::_modulate_with_gradient(Ref<Image> p_image, Ref<Gradient> p_gradient) {183int width = p_image->get_width();184int height = p_image->get_height();185186Ref<Image> new_image = Image::create_empty(width, height, false, Image::FORMAT_RGBA8);187188for (int row = 0; row < height; row++) {189for (int col = 0; col < width; col++) {190Color pixel_color = p_image->get_pixel(col, row);191Color ramp_color = p_gradient->get_color_at_offset(pixel_color.get_luminance());192new_image->set_pixel(col, row, ramp_color);193}194}195196return new_image;197}198199void NoiseTexture2D::_update_texture() {200bool use_thread = true;201#ifndef THREADS_ENABLED202use_thread = false;203#endif204if (first_time) {205use_thread = false;206first_time = false;207}208if (use_thread) {209if (!noise_thread.is_started()) {210noise_thread.start(_thread_function, this);211regen_queued = false;212} else {213regen_queued = true;214}215216} else {217Ref<Image> new_image = _generate_texture();218_set_texture_image(new_image);219}220update_queued = false;221}222223void NoiseTexture2D::set_noise(Ref<Noise> p_noise) {224if (p_noise == noise) {225return;226}227if (noise.is_valid()) {228noise->disconnect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));229}230noise = p_noise;231if (noise.is_valid()) {232noise->connect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));233}234_queue_update();235}236237Ref<Noise> NoiseTexture2D::get_noise() {238return noise;239}240241void NoiseTexture2D::set_width(int p_width) {242ERR_FAIL_COND(p_width <= 0);243if (p_width == size.x) {244return;245}246size.x = p_width;247_queue_update();248}249250void NoiseTexture2D::set_height(int p_height) {251ERR_FAIL_COND(p_height <= 0);252if (p_height == size.y) {253return;254}255size.y = p_height;256_queue_update();257}258259void NoiseTexture2D::set_invert(bool p_invert) {260if (p_invert == invert) {261return;262}263invert = p_invert;264_queue_update();265}266267bool NoiseTexture2D::get_invert() const {268return invert;269}270271void NoiseTexture2D::set_in_3d_space(bool p_enable) {272if (p_enable == in_3d_space) {273return;274}275in_3d_space = p_enable;276_queue_update();277}278bool NoiseTexture2D::is_in_3d_space() const {279return in_3d_space;280}281282void NoiseTexture2D::set_generate_mipmaps(bool p_enable) {283if (p_enable == generate_mipmaps) {284return;285}286generate_mipmaps = p_enable;287_queue_update();288}289290bool NoiseTexture2D::is_generating_mipmaps() const {291return generate_mipmaps;292}293294void NoiseTexture2D::set_seamless(bool p_seamless) {295if (p_seamless == seamless) {296return;297}298seamless = p_seamless;299_queue_update();300notify_property_list_changed();301}302303bool NoiseTexture2D::get_seamless() {304return seamless;305}306307void NoiseTexture2D::set_seamless_blend_skirt(real_t p_blend_skirt) {308ERR_FAIL_COND(p_blend_skirt < 0 || p_blend_skirt > 1);309310if (p_blend_skirt == seamless_blend_skirt) {311return;312}313seamless_blend_skirt = p_blend_skirt;314_queue_update();315}316real_t NoiseTexture2D::get_seamless_blend_skirt() {317return seamless_blend_skirt;318}319320void NoiseTexture2D::set_as_normal_map(bool p_as_normal_map) {321if (p_as_normal_map == as_normal_map) {322return;323}324as_normal_map = p_as_normal_map;325_queue_update();326notify_property_list_changed();327}328329bool NoiseTexture2D::is_normal_map() {330return as_normal_map;331}332333void NoiseTexture2D::set_bump_strength(float p_bump_strength) {334if (p_bump_strength == bump_strength) {335return;336}337bump_strength = p_bump_strength;338if (as_normal_map) {339_queue_update();340}341}342343float NoiseTexture2D::get_bump_strength() {344return bump_strength;345}346347void NoiseTexture2D::set_color_ramp(const Ref<Gradient> &p_gradient) {348if (p_gradient == color_ramp) {349return;350}351if (color_ramp.is_valid()) {352color_ramp->disconnect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));353}354color_ramp = p_gradient;355if (color_ramp.is_valid()) {356color_ramp->connect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));357}358_queue_update();359}360361void NoiseTexture2D::set_normalize(bool p_normalize) {362if (normalize == p_normalize) {363return;364}365normalize = p_normalize;366_queue_update();367}368369bool NoiseTexture2D::is_normalized() const {370return normalize;371}372373Ref<Gradient> NoiseTexture2D::get_color_ramp() const {374return color_ramp;375}376377int NoiseTexture2D::get_width() const {378return size.x;379}380381int NoiseTexture2D::get_height() const {382return size.y;383}384385RID NoiseTexture2D::get_rid() const {386if (!texture.is_valid()) {387texture = RS::get_singleton()->texture_2d_placeholder_create();388}389390return texture;391}392393Ref<Image> NoiseTexture2D::get_image() const {394return image;395}396397398