Path: blob/master/modules/noise/noise_texture_3d.cpp
21175 views
/**************************************************************************/1/* noise_texture_3d.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_3d.h"3132#include "noise.h"3334#include "servers/rendering/rendering_server.h"3536NoiseTexture3D::NoiseTexture3D() {37noise = Ref<Noise>();3839_queue_update();40}4142NoiseTexture3D::~NoiseTexture3D() {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 NoiseTexture3D::_bind_methods() {53ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture3D::set_width);54ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture3D::set_height);55ClassDB::bind_method(D_METHOD("set_depth", "depth"), &NoiseTexture3D::set_depth);5657ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture3D::set_noise);58ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture3D::get_noise);5960ClassDB::bind_method(D_METHOD("set_color_ramp", "gradient"), &NoiseTexture3D::set_color_ramp);61ClassDB::bind_method(D_METHOD("get_color_ramp"), &NoiseTexture3D::get_color_ramp);6263ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture3D::set_seamless);64ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture3D::get_seamless);6566ClassDB::bind_method(D_METHOD("set_invert", "invert"), &NoiseTexture3D::set_invert);67ClassDB::bind_method(D_METHOD("get_invert"), &NoiseTexture3D::get_invert);6869ClassDB::bind_method(D_METHOD("set_normalize", "normalize"), &NoiseTexture3D::set_normalize);70ClassDB::bind_method(D_METHOD("is_normalized"), &NoiseTexture3D::is_normalized);7172ClassDB::bind_method(D_METHOD("set_seamless_blend_skirt", "seamless_blend_skirt"), &NoiseTexture3D::set_seamless_blend_skirt);73ClassDB::bind_method(D_METHOD("get_seamless_blend_skirt"), &NoiseTexture3D::get_seamless_blend_skirt);7475ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_width", "get_width");76ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_height", "get_height");77ADD_PROPERTY(PropertyInfo(Variant::INT, "depth", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_depth", "get_depth");78ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, Noise::get_class_static()), "set_noise", "get_noise");79ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, Gradient::get_class_static()), "set_color_ramp", "get_color_ramp");80ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");81ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert"), "set_invert", "get_invert");82ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalize"), "set_normalize", "is_normalized");83ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "seamless_blend_skirt", PROPERTY_HINT_RANGE, "0.05,1,0.001"), "set_seamless_blend_skirt", "get_seamless_blend_skirt");84}8586void NoiseTexture3D::_validate_property(PropertyInfo &p_property) const {87if (!Engine::get_singleton()->is_editor_hint()) {88return;89}90if (p_property.name == "seamless_blend_skirt") {91if (!seamless) {92p_property.usage = PROPERTY_USAGE_NO_EDITOR;93}94}95}9697void NoiseTexture3D::_set_texture_data(const TypedArray<Image> &p_data) {98if (!p_data.is_empty()) {99Vector<Ref<Image>> data;100101data.resize(p_data.size());102103for (int i = 0; i < data.size(); i++) {104data.write[i] = p_data[i];105}106107if (texture.is_valid()) {108RID new_texture = RS::get_singleton()->texture_3d_create(data[0]->get_format(), data[0]->get_width(), data[0]->get_height(), data.size(), false, data);109RS::get_singleton()->texture_replace(texture, new_texture);110} else {111texture = RS::get_singleton()->texture_3d_create(data[0]->get_format(), data[0]->get_width(), data[0]->get_height(), data.size(), false, data);112}113format = data[0]->get_format();114}115emit_changed();116}117118void NoiseTexture3D::_thread_done(const TypedArray<Image> &p_data) {119_set_texture_data(p_data);120noise_thread.wait_to_finish();121if (regen_queued) {122noise_thread.start(_thread_function, this);123regen_queued = false;124}125}126127void NoiseTexture3D::_thread_function(void *p_ud) {128NoiseTexture3D *tex = static_cast<NoiseTexture3D *>(p_ud);129callable_mp(tex, &NoiseTexture3D::_thread_done).call_deferred(tex->_generate_texture());130}131132void NoiseTexture3D::_queue_update() {133if (update_queued) {134return;135}136137update_queued = true;138callable_mp(this, &NoiseTexture3D::_update_texture).call_deferred();139}140141TypedArray<Image> NoiseTexture3D::_generate_texture() {142// Prevent memdelete due to unref() on other thread.143Ref<Noise> ref_noise = noise;144145if (ref_noise.is_null()) {146return TypedArray<Image>();147}148149ERR_FAIL_COND_V_MSG((int64_t)width * height * depth > Image::MAX_PIXELS, TypedArray<Image>(), "The NoiseTexture3D is too big, consider lowering its width, height, or depth.");150151Vector<Ref<Image>> images;152153if (seamless) {154images = ref_noise->_get_seamless_image(width, height, depth, invert, true, seamless_blend_skirt, normalize);155} else {156images = ref_noise->_get_image(width, height, depth, invert, true, normalize);157}158159if (color_ramp.is_valid()) {160for (int i = 0; i < images.size(); i++) {161images.write[i] = _modulate_with_gradient(images[i], color_ramp);162}163}164165TypedArray<Image> new_data;166new_data.resize(images.size());167168for (int i = 0; i < new_data.size(); i++) {169new_data[i] = images[i];170}171172return new_data;173}174175Ref<Image> NoiseTexture3D::_modulate_with_gradient(Ref<Image> p_image, Ref<Gradient> p_gradient) {176int w = p_image->get_width();177int h = p_image->get_height();178179Ref<Image> new_image = Image::create_empty(w, h, false, Image::FORMAT_RGBA8);180181for (int row = 0; row < h; row++) {182for (int col = 0; col < w; col++) {183Color pixel_color = p_image->get_pixel(col, row);184Color ramp_color = p_gradient->get_color_at_offset(pixel_color.get_luminance());185new_image->set_pixel(col, row, ramp_color);186}187}188189return new_image;190}191192void NoiseTexture3D::_update_texture() {193bool use_thread = true;194#ifndef THREADS_ENABLED195use_thread = false;196#endif197if (first_time) {198use_thread = false;199first_time = false;200}201if (use_thread) {202if (!noise_thread.is_started()) {203noise_thread.start(_thread_function, this);204regen_queued = false;205} else {206regen_queued = true;207}208209} else {210TypedArray<Image> new_data = _generate_texture();211_set_texture_data(new_data);212}213update_queued = false;214}215216void NoiseTexture3D::set_noise(Ref<Noise> p_noise) {217if (p_noise == noise) {218return;219}220if (noise.is_valid()) {221noise->disconnect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));222}223noise = p_noise;224if (noise.is_valid()) {225noise->connect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));226}227_queue_update();228}229230Ref<Noise> NoiseTexture3D::get_noise() {231return noise;232}233234void NoiseTexture3D::set_width(int p_width) {235ERR_FAIL_COND(p_width <= 0);236if (p_width == width) {237return;238}239width = p_width;240_queue_update();241}242243void NoiseTexture3D::set_height(int p_height) {244ERR_FAIL_COND(p_height <= 0);245if (p_height == height) {246return;247}248height = p_height;249_queue_update();250}251252void NoiseTexture3D::set_depth(int p_depth) {253ERR_FAIL_COND(p_depth <= 0);254if (p_depth == depth) {255return;256}257depth = p_depth;258_queue_update();259}260261void NoiseTexture3D::set_invert(bool p_invert) {262if (p_invert == invert) {263return;264}265invert = p_invert;266_queue_update();267}268269bool NoiseTexture3D::get_invert() const {270return invert;271}272273void NoiseTexture3D::set_seamless(bool p_seamless) {274if (p_seamless == seamless) {275return;276}277seamless = p_seamless;278_queue_update();279notify_property_list_changed();280}281282bool NoiseTexture3D::get_seamless() {283return seamless;284}285286void NoiseTexture3D::set_seamless_blend_skirt(real_t p_blend_skirt) {287ERR_FAIL_COND(p_blend_skirt < 0.05 || p_blend_skirt > 1);288289if (p_blend_skirt == seamless_blend_skirt) {290return;291}292seamless_blend_skirt = p_blend_skirt;293_queue_update();294}295real_t NoiseTexture3D::get_seamless_blend_skirt() {296return seamless_blend_skirt;297}298299void NoiseTexture3D::set_color_ramp(const Ref<Gradient> &p_gradient) {300if (p_gradient == color_ramp) {301return;302}303if (color_ramp.is_valid()) {304color_ramp->disconnect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));305}306color_ramp = p_gradient;307if (color_ramp.is_valid()) {308color_ramp->connect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));309}310_queue_update();311}312313void NoiseTexture3D::set_normalize(bool p_normalize) {314if (normalize == p_normalize) {315return;316}317normalize = p_normalize;318_queue_update();319}320321bool NoiseTexture3D::is_normalized() const {322return normalize;323}324325Ref<Gradient> NoiseTexture3D::get_color_ramp() const {326return color_ramp;327}328329int NoiseTexture3D::get_width() const {330return width;331}332333int NoiseTexture3D::get_height() const {334return height;335}336337int NoiseTexture3D::get_depth() const {338return depth;339}340341bool NoiseTexture3D::has_mipmaps() const {342return false;343}344345RID NoiseTexture3D::get_rid() const {346if (!texture.is_valid()) {347texture = RS::get_singleton()->texture_3d_placeholder_create();348}349350return texture;351}352353Vector<Ref<Image>> NoiseTexture3D::get_data() const {354ERR_FAIL_COND_V(!texture.is_valid(), Vector<Ref<Image>>());355return RS::get_singleton()->texture_3d_get(texture);356}357358Image::Format NoiseTexture3D::get_format() const {359return format;360}361362363