2018-08-24 22:25:06 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* noise_texture.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-08-24 22:25:06 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#include "noise_texture.h"
|
|
|
|
|
|
|
|
#include "core/core_string_names.h"
|
|
|
|
|
|
|
|
NoiseTexture::NoiseTexture() {
|
|
|
|
update_queued = false;
|
2020-04-01 23:20:12 +00:00
|
|
|
noise_thread = nullptr;
|
2018-08-24 22:25:06 +00:00
|
|
|
regen_queued = false;
|
|
|
|
first_time = true;
|
|
|
|
|
|
|
|
size = Vector2i(512, 512);
|
|
|
|
seamless = false;
|
2020-12-23 09:34:26 +00:00
|
|
|
as_normal_map = false;
|
2019-03-04 15:59:08 +00:00
|
|
|
bump_strength = 8.0;
|
2018-08-24 22:25:06 +00:00
|
|
|
|
2018-09-28 11:30:07 +00:00
|
|
|
noise = Ref<OpenSimplexNoise>();
|
2018-08-24 22:25:06 +00:00
|
|
|
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
NoiseTexture::~NoiseTexture() {
|
2019-06-11 18:43:37 +00:00
|
|
|
if (texture.is_valid()) {
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->free(texture);
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2020-01-07 10:35:34 +00:00
|
|
|
if (noise_thread) {
|
|
|
|
Thread::wait_to_finish(noise_thread);
|
|
|
|
memdelete(noise_thread);
|
|
|
|
}
|
2018-08-24 22:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NoiseTexture::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_width", "width"), &NoiseTexture::set_width);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_height", "height"), &NoiseTexture::set_height);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_noise", "noise"), &NoiseTexture::set_noise);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_noise"), &NoiseTexture::get_noise);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_seamless", "seamless"), &NoiseTexture::set_seamless);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_seamless"), &NoiseTexture::get_seamless);
|
|
|
|
|
2020-12-23 09:34:26 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_as_normal_map", "as_normal_map"), &NoiseTexture::set_as_normal_map);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_normal_map"), &NoiseTexture::is_normal_map);
|
2018-08-24 22:25:06 +00:00
|
|
|
|
2019-03-03 18:50:55 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_bump_strength", "bump_strength"), &NoiseTexture::set_bump_strength);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bump_strength"), &NoiseTexture::get_bump_strength);
|
|
|
|
|
2018-08-24 22:25:06 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_update_texture"), &NoiseTexture::_update_texture);
|
|
|
|
ClassDB::bind_method(D_METHOD("_generate_texture"), &NoiseTexture::_generate_texture);
|
|
|
|
ClassDB::bind_method(D_METHOD("_thread_done", "image"), &NoiseTexture::_thread_done);
|
|
|
|
|
2018-10-02 14:01:24 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,1,or_greater"), "set_width", "get_width");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,1,or_greater"), "set_height", "get_height");
|
2018-08-24 22:25:06 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "seamless"), "set_seamless", "get_seamless");
|
2020-12-23 09:34:26 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "as_normal_map"), "set_as_normal_map", "is_normal_map");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bump_strength", PROPERTY_HINT_RANGE, "0,32,0.1,or_greater"), "set_bump_strength", "get_bump_strength");
|
2018-09-28 11:30:07 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "noise", PROPERTY_HINT_RESOURCE_TYPE, "OpenSimplexNoise"), "set_noise", "get_noise");
|
2018-08-24 22:25:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 18:50:55 +00:00
|
|
|
void NoiseTexture::_validate_property(PropertyInfo &property) const {
|
|
|
|
if (property.name == "bump_strength") {
|
2020-12-23 09:34:26 +00:00
|
|
|
if (!as_normal_map) {
|
2019-03-03 18:50:55 +00:00
|
|
|
property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 22:25:06 +00:00
|
|
|
void NoiseTexture::_set_texture_data(const Ref<Image> &p_image) {
|
|
|
|
data = p_image;
|
|
|
|
if (data.is_valid()) {
|
2019-06-11 18:43:37 +00:00
|
|
|
if (texture.is_valid()) {
|
2020-03-27 18:21:27 +00:00
|
|
|
RID new_texture = RS::get_singleton()->texture_2d_create(p_image);
|
|
|
|
RS::get_singleton()->texture_replace(texture, new_texture);
|
2019-06-11 18:43:37 +00:00
|
|
|
} else {
|
2020-03-27 18:21:27 +00:00
|
|
|
texture = RS::get_singleton()->texture_2d_create(p_image);
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2018-08-24 22:25:06 +00:00
|
|
|
}
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NoiseTexture::_thread_done(const Ref<Image> &p_image) {
|
|
|
|
_set_texture_data(p_image);
|
|
|
|
Thread::wait_to_finish(noise_thread);
|
|
|
|
memdelete(noise_thread);
|
2020-04-01 23:20:12 +00:00
|
|
|
noise_thread = nullptr;
|
2018-08-24 22:25:06 +00:00
|
|
|
if (regen_queued) {
|
|
|
|
noise_thread = Thread::create(_thread_function, this);
|
|
|
|
regen_queued = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NoiseTexture::_thread_function(void *p_ud) {
|
|
|
|
NoiseTexture *tex = (NoiseTexture *)p_ud;
|
|
|
|
tex->call_deferred("_thread_done", tex->_generate_texture());
|
|
|
|
}
|
|
|
|
|
|
|
|
void NoiseTexture::_queue_update() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (update_queued) {
|
2018-08-24 22:25:06 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-08-24 22:25:06 +00:00
|
|
|
|
|
|
|
update_queued = true;
|
|
|
|
call_deferred("_update_texture");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Image> NoiseTexture::_generate_texture() {
|
2020-04-06 11:22:31 +00:00
|
|
|
// Prevent memdelete due to unref() on other thread.
|
|
|
|
Ref<OpenSimplexNoise> ref_noise = noise;
|
|
|
|
|
|
|
|
if (ref_noise.is_null()) {
|
|
|
|
return Ref<Image>();
|
|
|
|
}
|
2018-08-24 22:25:06 +00:00
|
|
|
|
|
|
|
Ref<Image> image;
|
|
|
|
|
|
|
|
if (seamless) {
|
2020-04-06 11:22:31 +00:00
|
|
|
image = ref_noise->get_seamless_image(size.x);
|
2018-08-24 22:25:06 +00:00
|
|
|
} else {
|
2020-04-06 11:22:31 +00:00
|
|
|
image = ref_noise->get_image(size.x, size.y);
|
2018-08-24 22:25:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 09:34:26 +00:00
|
|
|
if (as_normal_map) {
|
|
|
|
image->bump_map_to_normal_map(bump_strength);
|
2018-08-24 22:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NoiseTexture::_update_texture() {
|
|
|
|
bool use_thread = true;
|
|
|
|
if (first_time) {
|
|
|
|
use_thread = false;
|
|
|
|
first_time = false;
|
|
|
|
}
|
|
|
|
#ifdef NO_THREADS
|
|
|
|
use_thread = false;
|
|
|
|
#endif
|
|
|
|
if (use_thread) {
|
|
|
|
if (!noise_thread) {
|
|
|
|
noise_thread = Thread::create(_thread_function, this);
|
|
|
|
regen_queued = false;
|
|
|
|
} else {
|
|
|
|
regen_queued = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Ref<Image> image = _generate_texture();
|
|
|
|
_set_texture_data(image);
|
|
|
|
}
|
2020-01-07 10:35:34 +00:00
|
|
|
update_queued = false;
|
2018-08-24 22:25:06 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 11:30:07 +00:00
|
|
|
void NoiseTexture::set_noise(Ref<OpenSimplexNoise> p_noise) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_noise == noise) {
|
2018-08-24 22:25:06 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-08-24 22:25:06 +00:00
|
|
|
if (noise.is_valid()) {
|
2020-02-21 22:26:13 +00:00
|
|
|
noise->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture::_queue_update));
|
2018-08-24 22:25:06 +00:00
|
|
|
}
|
|
|
|
noise = p_noise;
|
|
|
|
if (noise.is_valid()) {
|
2020-02-21 22:26:13 +00:00
|
|
|
noise->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NoiseTexture::_queue_update));
|
2018-08-24 22:25:06 +00:00
|
|
|
}
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
2018-09-28 11:30:07 +00:00
|
|
|
Ref<OpenSimplexNoise> NoiseTexture::get_noise() {
|
2018-08-24 22:25:06 +00:00
|
|
|
return noise;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NoiseTexture::set_width(int p_width) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_width == size.x) {
|
2020-05-10 10:56:01 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-08-24 22:25:06 +00:00
|
|
|
size.x = p_width;
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NoiseTexture::set_height(int p_height) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_height == size.y) {
|
2020-05-10 10:56:01 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-08-24 22:25:06 +00:00
|
|
|
size.y = p_height;
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NoiseTexture::set_seamless(bool p_seamless) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_seamless == seamless) {
|
2020-05-10 10:56:01 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-08-24 22:25:06 +00:00
|
|
|
seamless = p_seamless;
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NoiseTexture::get_seamless() {
|
|
|
|
return seamless;
|
|
|
|
}
|
|
|
|
|
2020-12-23 09:34:26 +00:00
|
|
|
void NoiseTexture::set_as_normal_map(bool p_as_normal_map) {
|
|
|
|
if (p_as_normal_map == as_normal_map) {
|
2020-05-10 10:56:01 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-12-23 09:34:26 +00:00
|
|
|
as_normal_map = p_as_normal_map;
|
2018-08-24 22:25:06 +00:00
|
|
|
_queue_update();
|
2019-03-03 18:50:55 +00:00
|
|
|
_change_notify();
|
2018-08-24 22:25:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 09:34:26 +00:00
|
|
|
bool NoiseTexture::is_normal_map() {
|
|
|
|
return as_normal_map;
|
2018-08-24 22:25:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 18:50:55 +00:00
|
|
|
void NoiseTexture::set_bump_strength(float p_bump_strength) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_bump_strength == bump_strength) {
|
2020-05-10 10:56:01 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-03-03 18:50:55 +00:00
|
|
|
bump_strength = p_bump_strength;
|
2020-12-23 09:34:26 +00:00
|
|
|
if (as_normal_map) {
|
2019-03-03 18:50:55 +00:00
|
|
|
_queue_update();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-03-03 18:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float NoiseTexture::get_bump_strength() {
|
|
|
|
return bump_strength;
|
|
|
|
}
|
|
|
|
|
2018-08-24 22:25:06 +00:00
|
|
|
int NoiseTexture::get_width() const {
|
|
|
|
return size.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NoiseTexture::get_height() const {
|
|
|
|
return size.y;
|
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
RID NoiseTexture::get_rid() const {
|
|
|
|
if (!texture.is_valid()) {
|
2020-03-27 18:21:27 +00:00
|
|
|
texture = RS::get_singleton()->texture_2d_placeholder_create();
|
2019-06-11 18:43:37 +00:00
|
|
|
}
|
2018-08-24 22:25:06 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
return texture;
|
2018-08-24 22:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Image> NoiseTexture::get_data() const {
|
|
|
|
return data;
|
|
|
|
}
|