Path: blob/master/modules/noise/noise_texture_2d.cpp
10277 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"3334NoiseTexture2D::NoiseTexture2D() {35noise = Ref<Noise>();3637_queue_update();38}3940NoiseTexture2D::~NoiseTexture2D() {41ERR_FAIL_NULL(RenderingServer::get_singleton());42if (texture.is_valid()) {43RS::get_singleton()->free(texture);44}45if (noise_thread.is_started()) {46noise_thread.wait_to_finish();47}48}4950void NoiseTexture2D::_bind_methods() {51ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture2D::set_width);52ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture2D::set_height);5354ClassDB::bind_method(D_METHOD("set_generate_mipmaps", "invert"), &NoiseTexture2D::set_generate_mipmaps);55ClassDB::bind_method(D_METHOD("is_generating_mipmaps"), &NoiseTexture2D::is_generating_mipmaps);5657ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture2D::set_noise);58ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture2D::get_noise);5960ClassDB::bind_method(D_METHOD("set_color_ramp", "gradient"), &NoiseTexture2D::set_color_ramp);61ClassDB::bind_method(D_METHOD("get_color_ramp"), &NoiseTexture2D::get_color_ramp);6263ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture2D::set_seamless);64ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture2D::get_seamless);6566ClassDB::bind_method(D_METHOD("set_invert", "invert"), &NoiseTexture2D::set_invert);67ClassDB::bind_method(D_METHOD("get_invert"), &NoiseTexture2D::get_invert);6869ClassDB::bind_method(D_METHOD("set_in_3d_space", "enable"), &NoiseTexture2D::set_in_3d_space);70ClassDB::bind_method(D_METHOD("is_in_3d_space"), &NoiseTexture2D::is_in_3d_space);7172ClassDB::bind_method(D_METHOD("set_as_normal_map", "as_normal_map"), &NoiseTexture2D::set_as_normal_map);73ClassDB::bind_method(D_METHOD("is_normal_map"), &NoiseTexture2D::is_normal_map);7475ClassDB::bind_method(D_METHOD("set_normalize", "normalize"), &NoiseTexture2D::set_normalize);76ClassDB::bind_method(D_METHOD("is_normalized"), &NoiseTexture2D::is_normalized);7778ClassDB::bind_method(D_METHOD("set_seamless_blend_skirt", "seamless_blend_skirt"), &NoiseTexture2D::set_seamless_blend_skirt);79ClassDB::bind_method(D_METHOD("get_seamless_blend_skirt"), &NoiseTexture2D::get_seamless_blend_skirt);8081ClassDB::bind_method(D_METHOD("set_bump_strength", "bump_strength"), &NoiseTexture2D::set_bump_strength);82ClassDB::bind_method(D_METHOD("get_bump_strength"), &NoiseTexture2D::get_bump_strength);8384ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_width", "get_width");85ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_height", "get_height");86ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps"), "set_generate_mipmaps", "is_generating_mipmaps");87ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "Noise"), "set_noise", "get_noise");88ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");89ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");90ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert"), "set_invert", "get_invert");91ADD_PROPERTY(PropertyInfo(Variant::BOOL, "in_3d_space"), "set_in_3d_space", "is_in_3d_space");92ADD_PROPERTY(PropertyInfo(Variant::BOOL, "as_normal_map"), "set_as_normal_map", "is_normal_map");93ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalize"), "set_normalize", "is_normalized");94ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "seamless_blend_skirt", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_seamless_blend_skirt", "get_seamless_blend_skirt");95ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bump_strength", PROPERTY_HINT_RANGE, "0,32,0.1,or_greater"), "set_bump_strength", "get_bump_strength");96}9798void NoiseTexture2D::_validate_property(PropertyInfo &p_property) const {99if (!Engine::get_singleton()->is_editor_hint()) {100return;101}102if (p_property.name == "bump_strength") {103if (!as_normal_map) {104p_property.usage = PROPERTY_USAGE_NO_EDITOR;105}106}107108if (p_property.name == "seamless_blend_skirt") {109if (!seamless) {110p_property.usage = PROPERTY_USAGE_NO_EDITOR;111}112}113}114115void NoiseTexture2D::_set_texture_image(const Ref<Image> &p_image) {116image = p_image;117if (image.is_valid()) {118if (texture.is_valid()) {119RID new_texture = RS::get_singleton()->texture_2d_create(p_image);120RS::get_singleton()->texture_replace(texture, new_texture);121} else {122texture = RS::get_singleton()->texture_2d_create(p_image);123}124RS::get_singleton()->texture_set_path(texture, get_path());125}126emit_changed();127}128129void NoiseTexture2D::_thread_done(const Ref<Image> &p_image) {130_set_texture_image(p_image);131noise_thread.wait_to_finish();132if (regen_queued) {133noise_thread.start(_thread_function, this);134regen_queued = false;135}136}137138void NoiseTexture2D::_thread_function(void *p_ud) {139NoiseTexture2D *tex = static_cast<NoiseTexture2D *>(p_ud);140callable_mp(tex, &NoiseTexture2D::_thread_done).call_deferred(tex->_generate_texture());141}142143void NoiseTexture2D::_queue_update() {144if (update_queued) {145return;146}147148update_queued = true;149callable_mp(this, &NoiseTexture2D::_update_texture).call_deferred();150}151152Ref<Image> NoiseTexture2D::_generate_texture() {153// Prevent memdelete due to unref() on other thread.154Ref<Noise> ref_noise = noise;155156if (ref_noise.is_null()) {157return Ref<Image>();158}159160Ref<Image> new_image;161162if (seamless) {163new_image = ref_noise->get_seamless_image(size.x, size.y, invert, in_3d_space, seamless_blend_skirt, normalize);164} else {165new_image = ref_noise->get_image(size.x, size.y, invert, in_3d_space, normalize);166}167if (color_ramp.is_valid()) {168new_image = _modulate_with_gradient(new_image, color_ramp);169}170if (as_normal_map) {171new_image->bump_map_to_normal_map(bump_strength);172}173if (generate_mipmaps) {174new_image->generate_mipmaps();175}176177return new_image;178}179180Ref<Image> NoiseTexture2D::_modulate_with_gradient(Ref<Image> p_image, Ref<Gradient> p_gradient) {181int width = p_image->get_width();182int height = p_image->get_height();183184Ref<Image> new_image = Image::create_empty(width, height, false, Image::FORMAT_RGBA8);185186for (int row = 0; row < height; row++) {187for (int col = 0; col < width; col++) {188Color pixel_color = p_image->get_pixel(col, row);189Color ramp_color = p_gradient->get_color_at_offset(pixel_color.get_luminance());190new_image->set_pixel(col, row, ramp_color);191}192}193194return new_image;195}196197void NoiseTexture2D::_update_texture() {198bool use_thread = true;199#ifndef THREADS_ENABLED200use_thread = false;201#endif202if (first_time) {203use_thread = false;204first_time = false;205}206if (use_thread) {207if (!noise_thread.is_started()) {208noise_thread.start(_thread_function, this);209regen_queued = false;210} else {211regen_queued = true;212}213214} else {215Ref<Image> new_image = _generate_texture();216_set_texture_image(new_image);217}218update_queued = false;219}220221void NoiseTexture2D::set_noise(Ref<Noise> p_noise) {222if (p_noise == noise) {223return;224}225if (noise.is_valid()) {226noise->disconnect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));227}228noise = p_noise;229if (noise.is_valid()) {230noise->connect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));231}232_queue_update();233}234235Ref<Noise> NoiseTexture2D::get_noise() {236return noise;237}238239void NoiseTexture2D::set_width(int p_width) {240ERR_FAIL_COND(p_width <= 0);241if (p_width == size.x) {242return;243}244size.x = p_width;245_queue_update();246}247248void NoiseTexture2D::set_height(int p_height) {249ERR_FAIL_COND(p_height <= 0);250if (p_height == size.y) {251return;252}253size.y = p_height;254_queue_update();255}256257void NoiseTexture2D::set_invert(bool p_invert) {258if (p_invert == invert) {259return;260}261invert = p_invert;262_queue_update();263}264265bool NoiseTexture2D::get_invert() const {266return invert;267}268269void NoiseTexture2D::set_in_3d_space(bool p_enable) {270if (p_enable == in_3d_space) {271return;272}273in_3d_space = p_enable;274_queue_update();275}276bool NoiseTexture2D::is_in_3d_space() const {277return in_3d_space;278}279280void NoiseTexture2D::set_generate_mipmaps(bool p_enable) {281if (p_enable == generate_mipmaps) {282return;283}284generate_mipmaps = p_enable;285_queue_update();286}287288bool NoiseTexture2D::is_generating_mipmaps() const {289return generate_mipmaps;290}291292void NoiseTexture2D::set_seamless(bool p_seamless) {293if (p_seamless == seamless) {294return;295}296seamless = p_seamless;297_queue_update();298notify_property_list_changed();299}300301bool NoiseTexture2D::get_seamless() {302return seamless;303}304305void NoiseTexture2D::set_seamless_blend_skirt(real_t p_blend_skirt) {306ERR_FAIL_COND(p_blend_skirt < 0 || p_blend_skirt > 1);307308if (p_blend_skirt == seamless_blend_skirt) {309return;310}311seamless_blend_skirt = p_blend_skirt;312_queue_update();313}314real_t NoiseTexture2D::get_seamless_blend_skirt() {315return seamless_blend_skirt;316}317318void NoiseTexture2D::set_as_normal_map(bool p_as_normal_map) {319if (p_as_normal_map == as_normal_map) {320return;321}322as_normal_map = p_as_normal_map;323_queue_update();324notify_property_list_changed();325}326327bool NoiseTexture2D::is_normal_map() {328return as_normal_map;329}330331void NoiseTexture2D::set_bump_strength(float p_bump_strength) {332if (p_bump_strength == bump_strength) {333return;334}335bump_strength = p_bump_strength;336if (as_normal_map) {337_queue_update();338}339}340341float NoiseTexture2D::get_bump_strength() {342return bump_strength;343}344345void NoiseTexture2D::set_color_ramp(const Ref<Gradient> &p_gradient) {346if (p_gradient == color_ramp) {347return;348}349if (color_ramp.is_valid()) {350color_ramp->disconnect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));351}352color_ramp = p_gradient;353if (color_ramp.is_valid()) {354color_ramp->connect_changed(callable_mp(this, &NoiseTexture2D::_queue_update));355}356_queue_update();357}358359void NoiseTexture2D::set_normalize(bool p_normalize) {360if (normalize == p_normalize) {361return;362}363normalize = p_normalize;364_queue_update();365}366367bool NoiseTexture2D::is_normalized() const {368return normalize;369}370371Ref<Gradient> NoiseTexture2D::get_color_ramp() const {372return color_ramp;373}374375int NoiseTexture2D::get_width() const {376return size.x;377}378379int NoiseTexture2D::get_height() const {380return size.y;381}382383RID NoiseTexture2D::get_rid() const {384if (!texture.is_valid()) {385texture = RS::get_singleton()->texture_2d_placeholder_create();386}387388return texture;389}390391Ref<Image> NoiseTexture2D::get_image() const {392return image;393}394395396