Path: blob/master/modules/noise/noise_texture_3d.cpp
10277 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"3334NoiseTexture3D::NoiseTexture3D() {35noise = Ref<Noise>();3637_queue_update();38}3940NoiseTexture3D::~NoiseTexture3D() {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 NoiseTexture3D::_bind_methods() {51ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture3D::set_width);52ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture3D::set_height);53ClassDB::bind_method(D_METHOD("set_depth", "depth"), &NoiseTexture3D::set_depth);5455ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture3D::set_noise);56ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture3D::get_noise);5758ClassDB::bind_method(D_METHOD("set_color_ramp", "gradient"), &NoiseTexture3D::set_color_ramp);59ClassDB::bind_method(D_METHOD("get_color_ramp"), &NoiseTexture3D::get_color_ramp);6061ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture3D::set_seamless);62ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture3D::get_seamless);6364ClassDB::bind_method(D_METHOD("set_invert", "invert"), &NoiseTexture3D::set_invert);65ClassDB::bind_method(D_METHOD("get_invert"), &NoiseTexture3D::get_invert);6667ClassDB::bind_method(D_METHOD("set_normalize", "normalize"), &NoiseTexture3D::set_normalize);68ClassDB::bind_method(D_METHOD("is_normalized"), &NoiseTexture3D::is_normalized);6970ClassDB::bind_method(D_METHOD("set_seamless_blend_skirt", "seamless_blend_skirt"), &NoiseTexture3D::set_seamless_blend_skirt);71ClassDB::bind_method(D_METHOD("get_seamless_blend_skirt"), &NoiseTexture3D::get_seamless_blend_skirt);7273ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_width", "get_width");74ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_height", "get_height");75ADD_PROPERTY(PropertyInfo(Variant::INT, "depth", PROPERTY_HINT_RANGE, "1,2048,1,or_greater,suffix:px"), "set_depth", "get_depth");76ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "Noise"), "set_noise", "get_noise");77ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp");78ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");79ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert"), "set_invert", "get_invert");80ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalize"), "set_normalize", "is_normalized");81ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "seamless_blend_skirt", PROPERTY_HINT_RANGE, "0.05,1,0.001"), "set_seamless_blend_skirt", "get_seamless_blend_skirt");82}8384void NoiseTexture3D::_validate_property(PropertyInfo &p_property) const {85if (!Engine::get_singleton()->is_editor_hint()) {86return;87}88if (p_property.name == "seamless_blend_skirt") {89if (!seamless) {90p_property.usage = PROPERTY_USAGE_NO_EDITOR;91}92}93}9495void NoiseTexture3D::_set_texture_data(const TypedArray<Image> &p_data) {96if (!p_data.is_empty()) {97Vector<Ref<Image>> data;9899data.resize(p_data.size());100101for (int i = 0; i < data.size(); i++) {102data.write[i] = p_data[i];103}104105if (texture.is_valid()) {106RID new_texture = RS::get_singleton()->texture_3d_create(data[0]->get_format(), data[0]->get_width(), data[0]->get_height(), data.size(), false, data);107RS::get_singleton()->texture_replace(texture, new_texture);108} else {109texture = RS::get_singleton()->texture_3d_create(data[0]->get_format(), data[0]->get_width(), data[0]->get_height(), data.size(), false, data);110}111format = data[0]->get_format();112}113emit_changed();114}115116void NoiseTexture3D::_thread_done(const TypedArray<Image> &p_data) {117_set_texture_data(p_data);118noise_thread.wait_to_finish();119if (regen_queued) {120noise_thread.start(_thread_function, this);121regen_queued = false;122}123}124125void NoiseTexture3D::_thread_function(void *p_ud) {126NoiseTexture3D *tex = static_cast<NoiseTexture3D *>(p_ud);127callable_mp(tex, &NoiseTexture3D::_thread_done).call_deferred(tex->_generate_texture());128}129130void NoiseTexture3D::_queue_update() {131if (update_queued) {132return;133}134135update_queued = true;136callable_mp(this, &NoiseTexture3D::_update_texture).call_deferred();137}138139TypedArray<Image> NoiseTexture3D::_generate_texture() {140// Prevent memdelete due to unref() on other thread.141Ref<Noise> ref_noise = noise;142143if (ref_noise.is_null()) {144return TypedArray<Image>();145}146147ERR_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.");148149Vector<Ref<Image>> images;150151if (seamless) {152images = ref_noise->_get_seamless_image(width, height, depth, invert, true, seamless_blend_skirt, normalize);153} else {154images = ref_noise->_get_image(width, height, depth, invert, true, normalize);155}156157if (color_ramp.is_valid()) {158for (int i = 0; i < images.size(); i++) {159images.write[i] = _modulate_with_gradient(images[i], color_ramp);160}161}162163TypedArray<Image> new_data;164new_data.resize(images.size());165166for (int i = 0; i < new_data.size(); i++) {167new_data[i] = images[i];168}169170return new_data;171}172173Ref<Image> NoiseTexture3D::_modulate_with_gradient(Ref<Image> p_image, Ref<Gradient> p_gradient) {174int w = p_image->get_width();175int h = p_image->get_height();176177Ref<Image> new_image = Image::create_empty(w, h, false, Image::FORMAT_RGBA8);178179for (int row = 0; row < h; row++) {180for (int col = 0; col < w; col++) {181Color pixel_color = p_image->get_pixel(col, row);182Color ramp_color = p_gradient->get_color_at_offset(pixel_color.get_luminance());183new_image->set_pixel(col, row, ramp_color);184}185}186187return new_image;188}189190void NoiseTexture3D::_update_texture() {191bool use_thread = true;192#ifndef THREADS_ENABLED193use_thread = false;194#endif195if (first_time) {196use_thread = false;197first_time = false;198}199if (use_thread) {200if (!noise_thread.is_started()) {201noise_thread.start(_thread_function, this);202regen_queued = false;203} else {204regen_queued = true;205}206207} else {208TypedArray<Image> new_data = _generate_texture();209_set_texture_data(new_data);210}211update_queued = false;212}213214void NoiseTexture3D::set_noise(Ref<Noise> p_noise) {215if (p_noise == noise) {216return;217}218if (noise.is_valid()) {219noise->disconnect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));220}221noise = p_noise;222if (noise.is_valid()) {223noise->connect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));224}225_queue_update();226}227228Ref<Noise> NoiseTexture3D::get_noise() {229return noise;230}231232void NoiseTexture3D::set_width(int p_width) {233ERR_FAIL_COND(p_width <= 0);234if (p_width == width) {235return;236}237width = p_width;238_queue_update();239}240241void NoiseTexture3D::set_height(int p_height) {242ERR_FAIL_COND(p_height <= 0);243if (p_height == height) {244return;245}246height = p_height;247_queue_update();248}249250void NoiseTexture3D::set_depth(int p_depth) {251ERR_FAIL_COND(p_depth <= 0);252if (p_depth == depth) {253return;254}255depth = p_depth;256_queue_update();257}258259void NoiseTexture3D::set_invert(bool p_invert) {260if (p_invert == invert) {261return;262}263invert = p_invert;264_queue_update();265}266267bool NoiseTexture3D::get_invert() const {268return invert;269}270271void NoiseTexture3D::set_seamless(bool p_seamless) {272if (p_seamless == seamless) {273return;274}275seamless = p_seamless;276_queue_update();277notify_property_list_changed();278}279280bool NoiseTexture3D::get_seamless() {281return seamless;282}283284void NoiseTexture3D::set_seamless_blend_skirt(real_t p_blend_skirt) {285ERR_FAIL_COND(p_blend_skirt < 0.05 || p_blend_skirt > 1);286287if (p_blend_skirt == seamless_blend_skirt) {288return;289}290seamless_blend_skirt = p_blend_skirt;291_queue_update();292}293real_t NoiseTexture3D::get_seamless_blend_skirt() {294return seamless_blend_skirt;295}296297void NoiseTexture3D::set_color_ramp(const Ref<Gradient> &p_gradient) {298if (p_gradient == color_ramp) {299return;300}301if (color_ramp.is_valid()) {302color_ramp->disconnect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));303}304color_ramp = p_gradient;305if (color_ramp.is_valid()) {306color_ramp->connect_changed(callable_mp(this, &NoiseTexture3D::_queue_update));307}308_queue_update();309}310311void NoiseTexture3D::set_normalize(bool p_normalize) {312if (normalize == p_normalize) {313return;314}315normalize = p_normalize;316_queue_update();317}318319bool NoiseTexture3D::is_normalized() const {320return normalize;321}322323Ref<Gradient> NoiseTexture3D::get_color_ramp() const {324return color_ramp;325}326327int NoiseTexture3D::get_width() const {328return width;329}330331int NoiseTexture3D::get_height() const {332return height;333}334335int NoiseTexture3D::get_depth() const {336return depth;337}338339bool NoiseTexture3D::has_mipmaps() const {340return false;341}342343RID NoiseTexture3D::get_rid() const {344if (!texture.is_valid()) {345texture = RS::get_singleton()->texture_3d_placeholder_create();346}347348return texture;349}350351Vector<Ref<Image>> NoiseTexture3D::get_data() const {352ERR_FAIL_COND_V(!texture.is_valid(), Vector<Ref<Image>>());353return RS::get_singleton()->texture_3d_get(texture);354}355356Image::Format NoiseTexture3D::get_format() const {357return format;358}359360361