diff options
Diffstat (limited to 'scene/resources')
26 files changed, 821 insertions, 193 deletions
diff --git a/scene/resources/audio_stream_polyphonic.cpp b/scene/resources/audio_stream_polyphonic.cpp new file mode 100644 index 0000000000..f7299b0789 --- /dev/null +++ b/scene/resources/audio_stream_polyphonic.cpp @@ -0,0 +1,274 @@ +/**************************************************************************/ +/* audio_stream_polyphonic.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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 "audio_stream_polyphonic.h" +#include "scene/main/scene_tree.h" + +Ref<AudioStreamPlayback> AudioStreamPolyphonic::instantiate_playback() { + Ref<AudioStreamPlaybackPolyphonic> playback; + playback.instantiate(); + playback->streams.resize(polyphony); + return playback; +} + +String AudioStreamPolyphonic::get_stream_name() const { + return "AudioStreamPolyphonic"; +} + +bool AudioStreamPolyphonic::is_monophonic() const { + return true; // This avoids stream players to instantiate more than one of these. +} + +void AudioStreamPolyphonic::set_polyphony(int p_voices) { + ERR_FAIL_COND(p_voices < 0 || p_voices > 128); + polyphony = p_voices; +} +int AudioStreamPolyphonic::get_polyphony() const { + return polyphony; +} + +void AudioStreamPolyphonic::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_polyphony", "voices"), &AudioStreamPolyphonic::set_polyphony); + ClassDB::bind_method(D_METHOD("get_polyphony"), &AudioStreamPolyphonic::get_polyphony); + + ADD_PROPERTY(PropertyInfo(Variant::INT, "polyphony", PROPERTY_HINT_RANGE, "1,128,1"), "set_polyphony", "get_polyphony"); +} + +AudioStreamPolyphonic::AudioStreamPolyphonic() { +} + +//////////////////////// + +void AudioStreamPlaybackPolyphonic::start(double p_from_pos) { + if (active) { + stop(); + } + + active = true; +} + +void AudioStreamPlaybackPolyphonic::stop() { + if (!active) { + return; + } + + bool locked = false; + for (Stream &s : streams) { + if (s.active.is_set()) { + // Need locking because something may still be mixing. + locked = true; + AudioServer::get_singleton()->lock(); + } + s.active.clear(); + s.finish_request.clear(); + s.stream_playback.unref(); + s.stream.unref(); + } + if (locked) { + AudioServer::get_singleton()->unlock(); + } + + active = false; +} + +bool AudioStreamPlaybackPolyphonic::is_playing() const { + return active; +} + +int AudioStreamPlaybackPolyphonic::get_loop_count() const { + return 0; +} + +double AudioStreamPlaybackPolyphonic::get_playback_position() const { + return 0; +} +void AudioStreamPlaybackPolyphonic::seek(double p_time) { + // Ignored. +} + +void AudioStreamPlaybackPolyphonic::tag_used_streams() { + for (Stream &s : streams) { + if (s.active.is_set()) { + s.stream_playback->tag_used_streams(); + } + } +} + +int AudioStreamPlaybackPolyphonic::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) { + if (!active) { + return 0; + } + + // Pre-clear buffer. + for (int i = 0; i < p_frames; i++) { + p_buffer[i] = AudioFrame(0, 0); + } + + for (Stream &s : streams) { + if (!s.active.is_set()) { + continue; + } + + float volume_db = s.volume_db; // Copy because it can be overridden at any time. + float next_volume = Math::db_to_linear(volume_db); + s.prev_volume_db = volume_db; + + if (s.finish_request.is_set()) { + if (s.pending_play.is_set()) { + // Did not get the chance to play, was finalized too soon. + s.active.clear(); + continue; + } + next_volume = 0; + } + + if (s.pending_play.is_set()) { + s.stream_playback->start(s.play_offset); + s.pending_play.clear(); + } + float prev_volume = Math::db_to_linear(s.prev_volume_db); + + float volume_inc = (next_volume - prev_volume) / float(p_frames); + + int todo = p_frames; + int offset = 0; + float volume = prev_volume; + + bool stream_done = false; + + while (todo) { + int to_mix = MIN(todo, int(INTERNAL_BUFFER_LEN)); + int mixed = s.stream_playback->mix(internal_buffer, s.pitch_scale, to_mix); + + for (int i = 0; i < to_mix; i++) { + p_buffer[offset + i] += internal_buffer[i] * volume; + volume += volume_inc; + } + + if (mixed < to_mix) { + // Stream is done. + s.active.clear(); + stream_done = true; + break; + } + + todo -= to_mix; + offset += to_mix; + } + + if (stream_done) { + continue; + } + + if (s.finish_request.is_set()) { + s.active.clear(); + } + } + + return p_frames; +} + +AudioStreamPlaybackPolyphonic::ID AudioStreamPlaybackPolyphonic::play_stream(const Ref<AudioStream> &p_stream, float p_from_offset, float p_volume_db, float p_pitch_scale) { + ERR_FAIL_COND_V(p_stream.is_null(), INVALID_ID); + for (uint32_t i = 0; i < streams.size(); i++) { + if (!streams[i].active.is_set()) { + // Can use this stream, as it's not active. + streams[i].stream = p_stream; + streams[i].stream_playback = streams[i].stream->instantiate_playback(); + streams[i].play_offset = p_from_offset; + streams[i].volume_db = p_volume_db; + streams[i].prev_volume_db = p_volume_db; + streams[i].pitch_scale = p_pitch_scale; + streams[i].id = id_counter++; + streams[i].finish_request.clear(); + streams[i].pending_play.set(); + streams[i].active.set(); + return (ID(i) << INDEX_SHIFT) | ID(streams[i].id); + } + } + + return INVALID_ID; +} + +AudioStreamPlaybackPolyphonic::Stream *AudioStreamPlaybackPolyphonic::_find_stream(int64_t p_id) { + uint32_t index = p_id >> INDEX_SHIFT; + if (index >= streams.size()) { + return nullptr; + } + if (!streams[index].active.is_set()) { + return nullptr; // Not active, no longer exists. + } + int64_t id = p_id & ID_MASK; + if (streams[index].id != id) { + return nullptr; + } + return &streams[index]; +} + +void AudioStreamPlaybackPolyphonic::set_stream_volume(ID p_stream_id, float p_volume_db) { + Stream *s = _find_stream(p_stream_id); + if (!s) { + return; + } + s->volume_db = p_volume_db; +} + +void AudioStreamPlaybackPolyphonic::set_stream_pitch_scale(ID p_stream_id, float p_pitch_scale) { + Stream *s = _find_stream(p_stream_id); + if (!s) { + return; + } + s->pitch_scale = p_pitch_scale; +} + +bool AudioStreamPlaybackPolyphonic::is_stream_playing(ID p_stream_id) const { + return const_cast<AudioStreamPlaybackPolyphonic *>(this)->_find_stream(p_stream_id) != nullptr; +} + +void AudioStreamPlaybackPolyphonic::stop_stream(ID p_stream_id) { + Stream *s = _find_stream(p_stream_id); + if (!s) { + return; + } + s->finish_request.set(); +} + +void AudioStreamPlaybackPolyphonic::_bind_methods() { + ClassDB::bind_method(D_METHOD("play_stream", "stream", "from_offset", "volume_db", "pitch_scale"), &AudioStreamPlaybackPolyphonic::play_stream, DEFVAL(0), DEFVAL(0), DEFVAL(1.0)); + ClassDB::bind_method(D_METHOD("set_stream_volume", "stream", "volume_db"), &AudioStreamPlaybackPolyphonic::set_stream_volume); + ClassDB::bind_method(D_METHOD("set_stream_pitch_scale", "stream", "pitch_scale"), &AudioStreamPlaybackPolyphonic::set_stream_pitch_scale); + ClassDB::bind_method(D_METHOD("is_stream_playing", "stream"), &AudioStreamPlaybackPolyphonic::is_stream_playing); + ClassDB::bind_method(D_METHOD("stop_stream", "stream"), &AudioStreamPlaybackPolyphonic::stop_stream); + + BIND_CONSTANT(INVALID_ID); +} + +AudioStreamPlaybackPolyphonic::AudioStreamPlaybackPolyphonic() { +} diff --git a/scene/resources/audio_stream_polyphonic.h b/scene/resources/audio_stream_polyphonic.h new file mode 100644 index 0000000000..e414401b6f --- /dev/null +++ b/scene/resources/audio_stream_polyphonic.h @@ -0,0 +1,119 @@ +/**************************************************************************/ +/* audio_stream_polyphonic.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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. */ +/**************************************************************************/ + +#ifndef AUDIO_STREAM_POLYPHONIC_H +#define AUDIO_STREAM_POLYPHONIC_H + +#include "core/templates/local_vector.h" +#include "servers/audio/audio_stream.h" + +class AudioStreamPolyphonic : public AudioStream { + GDCLASS(AudioStreamPolyphonic, AudioStream) + int polyphony = 32; + + static void _bind_methods(); + +public: + virtual Ref<AudioStreamPlayback> instantiate_playback() override; + virtual String get_stream_name() const override; + virtual bool is_monophonic() const override; + + void set_polyphony(int p_voices); + int get_polyphony() const; + + AudioStreamPolyphonic(); +}; + +class AudioStreamPlaybackPolyphonic : public AudioStreamPlayback { + GDCLASS(AudioStreamPlaybackPolyphonic, AudioStreamPlayback) + + enum { + INTERNAL_BUFFER_LEN = 128, + ID_MASK = 0xFFFFFFFF, + INDEX_SHIFT = 32 + }; + struct Stream { + SafeFlag active; + SafeFlag pending_play; + SafeFlag finish_request; + float play_offset = 0; + float pitch_scale = 1.0; + Ref<AudioStream> stream; + Ref<AudioStreamPlayback> stream_playback; + float prev_volume_db = 0; + float volume_db = 0; + uint32_t id = 0; + + Stream() : + active(false), pending_play(false), finish_request(false) {} + }; + + LocalVector<Stream> streams; + AudioFrame internal_buffer[INTERNAL_BUFFER_LEN]; + + bool active = false; + uint32_t id_counter = 1; + + _FORCE_INLINE_ Stream *_find_stream(int64_t p_id); + + friend class AudioStreamPolyphonic; + +protected: + static void _bind_methods(); + +public: + typedef int64_t ID; + enum { + INVALID_ID = -1 + }; + + virtual void start(double p_from_pos = 0.0) override; + virtual void stop() override; + virtual bool is_playing() const override; + + virtual int get_loop_count() const override; //times it looped + + virtual double get_playback_position() const override; + virtual void seek(double p_time) override; + + virtual void tag_used_streams() override; + + virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override; + + ID play_stream(const Ref<AudioStream> &p_stream, float p_from_offset = 0, float p_volume_db = 0, float p_pitch_scale = 1.0); + void set_stream_volume(ID p_stream_id, float p_volume_db); + void set_stream_pitch_scale(ID p_stream_id, float p_pitch_scale); + bool is_stream_playing(ID p_stream_id) const; + void stop_stream(ID p_stream_id); + + AudioStreamPlaybackPolyphonic(); +}; + +#endif // AUDIO_STREAM_POLYPHONIC_H diff --git a/scene/resources/camera_attributes.cpp b/scene/resources/camera_attributes.cpp index b5e302cce5..61df56523d 100644 --- a/scene/resources/camera_attributes.cpp +++ b/scene/resources/camera_attributes.cpp @@ -286,10 +286,10 @@ void CameraAttributesPractical::_bind_methods() { ADD_GROUP("DOF Blur", "dof_blur_"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dof_blur_far_enabled"), "set_dof_blur_far_enabled", "is_dof_blur_far_enabled"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dof_blur_far_distance", PROPERTY_HINT_RANGE, "0.01,8192,0.01,exp,suffix:m"), "set_dof_blur_far_distance", "get_dof_blur_far_distance"); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dof_blur_far_transition", PROPERTY_HINT_RANGE, "0.01,8192,0.01,exp"), "set_dof_blur_far_transition", "get_dof_blur_far_transition"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dof_blur_far_transition", PROPERTY_HINT_RANGE, "-1,8192,0.01,exp"), "set_dof_blur_far_transition", "get_dof_blur_far_transition"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dof_blur_near_enabled"), "set_dof_blur_near_enabled", "is_dof_blur_near_enabled"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dof_blur_near_distance", PROPERTY_HINT_RANGE, "0.01,8192,0.01,exp,suffix:m"), "set_dof_blur_near_distance", "get_dof_blur_near_distance"); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dof_blur_near_transition", PROPERTY_HINT_RANGE, "0.01,8192,0.01,exp"), "set_dof_blur_near_transition", "get_dof_blur_near_transition"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dof_blur_near_transition", PROPERTY_HINT_RANGE, "-1,8192,0.01,exp"), "set_dof_blur_near_transition", "get_dof_blur_near_transition"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dof_blur_amount", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_dof_blur_amount", "get_dof_blur_amount"); ADD_GROUP("Auto Exposure", "auto_exposure_"); diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index 4d3eec6333..7a865691d9 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -53,7 +53,7 @@ static const int default_corner_radius = 3; static Ref<StyleBoxFlat> make_flat_stylebox(Color p_color, float p_margin_left = default_margin, float p_margin_top = default_margin, float p_margin_right = default_margin, float p_margin_bottom = default_margin, int p_corner_radius = default_corner_radius, bool p_draw_center = true, int p_border_width = 0) { Ref<StyleBoxFlat> style(memnew(StyleBoxFlat)); style->set_bg_color(p_color); - style->set_default_margin_individual(p_margin_left * scale, p_margin_top * scale, p_margin_right * scale, p_margin_bottom * scale); + style->set_content_margin_individual(p_margin_left * scale, p_margin_top * scale, p_margin_right * scale, p_margin_bottom * scale); style->set_corner_radius_all(p_corner_radius); style->set_anti_aliased(true); @@ -67,10 +67,10 @@ static Ref<StyleBoxFlat> make_flat_stylebox(Color p_color, float p_margin_left = } static Ref<StyleBoxFlat> sb_expand(Ref<StyleBoxFlat> p_sbox, float p_left, float p_top, float p_right, float p_bottom) { - p_sbox->set_expand_margin_size(SIDE_LEFT, p_left * scale); - p_sbox->set_expand_margin_size(SIDE_TOP, p_top * scale); - p_sbox->set_expand_margin_size(SIDE_RIGHT, p_right * scale); - p_sbox->set_expand_margin_size(SIDE_BOTTOM, p_bottom * scale); + p_sbox->set_expand_margin(SIDE_LEFT, p_left * scale); + p_sbox->set_expand_margin(SIDE_TOP, p_top * scale); + p_sbox->set_expand_margin(SIDE_RIGHT, p_right * scale); + p_sbox->set_expand_margin(SIDE_BOTTOM, p_bottom * scale); return p_sbox; } @@ -93,7 +93,7 @@ static Ref<ImageTexture> generate_icon(int p_index) { static Ref<StyleBox> make_empty_stylebox(float p_margin_left = -1, float p_margin_top = -1, float p_margin_right = -1, float p_margin_bottom = -1) { Ref<StyleBox> style(memnew(StyleBoxEmpty)); - style->set_default_margin_individual(p_margin_left * scale, p_margin_top * scale, p_margin_right * scale, p_margin_bottom * scale); + style->set_content_margin_individual(p_margin_left * scale, p_margin_top * scale, p_margin_right * scale, p_margin_bottom * scale); return style; } @@ -148,7 +148,7 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const const Ref<StyleBoxFlat> button_disabled = make_flat_stylebox(style_disabled_color); Ref<StyleBoxFlat> focus = make_flat_stylebox(style_focus_color, default_margin, default_margin, default_margin, default_margin, default_corner_radius, false, 2); // Make the focus outline appear to be flush with the buttons it's focusing. - focus->set_expand_margin_size_all(2 * scale); + focus->set_expand_margin_all(2 * scale); theme->set_stylebox("normal", "Button", button_normal); theme->set_stylebox("hover", "Button", button_hover); @@ -279,9 +279,9 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const // CheckBox Ref<StyleBox> cbx_empty = memnew(StyleBoxEmpty); - cbx_empty->set_default_margin_all(4 * scale); + cbx_empty->set_content_margin_all(4 * scale); Ref<StyleBox> cbx_focus = focus; - cbx_focus->set_default_margin_all(4 * scale); + cbx_focus->set_content_margin_all(4 * scale); theme->set_stylebox("normal", "CheckBox", cbx_empty); theme->set_stylebox("pressed", "CheckBox", cbx_empty); @@ -317,7 +317,7 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const // CheckButton Ref<StyleBox> cb_empty = memnew(StyleBoxEmpty); - cb_empty->set_default_margin_individual(6 * scale, 4 * scale, 6 * scale, 4 * scale); + cb_empty->set_content_margin_individual(6 * scale, 4 * scale, 6 * scale, 4 * scale); theme->set_stylebox("normal", "CheckButton", cb_empty); theme->set_stylebox("pressed", "CheckButton", cb_empty); @@ -634,10 +634,10 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const Ref<StyleBoxLine> separator_horizontal = memnew(StyleBoxLine); separator_horizontal->set_thickness(Math::round(scale)); separator_horizontal->set_color(style_separator_color); - separator_horizontal->set_default_margin_individual(default_margin, 0, default_margin, 0); + separator_horizontal->set_content_margin_individual(default_margin, 0, default_margin, 0); Ref<StyleBoxLine> separator_vertical = separator_horizontal->duplicate(); separator_vertical->set_vertical(true); - separator_vertical->set_default_margin_individual(0, default_margin, 0, default_margin); + separator_vertical->set_content_margin_individual(0, default_margin, 0, default_margin); // Always display a border for PopupMenus so they can be distinguished from their background. Ref<StyleBoxFlat> style_popup_panel = make_flat_stylebox(style_popup_color); diff --git a/scene/resources/font.cpp b/scene/resources/font.cpp index deee187e8e..e5a1adff20 100644 --- a/scene/resources/font.cpp +++ b/scene/resources/font.cpp @@ -2868,6 +2868,12 @@ void SystemFont::_bind_methods() { ClassDB::bind_method(D_METHOD("set_multichannel_signed_distance_field", "msdf"), &SystemFont::set_multichannel_signed_distance_field); ClassDB::bind_method(D_METHOD("is_multichannel_signed_distance_field"), &SystemFont::is_multichannel_signed_distance_field); + ClassDB::bind_method(D_METHOD("set_msdf_pixel_range", "msdf_pixel_range"), &SystemFont::set_msdf_pixel_range); + ClassDB::bind_method(D_METHOD("get_msdf_pixel_range"), &SystemFont::get_msdf_pixel_range); + + ClassDB::bind_method(D_METHOD("set_msdf_size", "msdf_size"), &SystemFont::set_msdf_size); + ClassDB::bind_method(D_METHOD("get_msdf_size"), &SystemFont::get_msdf_size); + ClassDB::bind_method(D_METHOD("set_oversampling", "oversampling"), &SystemFont::set_oversampling); ClassDB::bind_method(D_METHOD("get_oversampling"), &SystemFont::get_oversampling); @@ -2890,6 +2896,8 @@ void SystemFont::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::INT, "hinting", PROPERTY_HINT_ENUM, "None,Light,Normal"), "set_hinting", "get_hinting"); ADD_PROPERTY(PropertyInfo(Variant::INT, "subpixel_positioning", PROPERTY_HINT_ENUM, "Disabled,Auto,One Half of a Pixel,One Quarter of a Pixel"), "set_subpixel_positioning", "get_subpixel_positioning"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "multichannel_signed_distance_field"), "set_multichannel_signed_distance_field", "is_multichannel_signed_distance_field"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "msdf_pixel_range"), "set_msdf_pixel_range", "get_msdf_pixel_range"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "msdf_size"), "set_msdf_size", "get_msdf_size"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "oversampling", PROPERTY_HINT_RANGE, "0,10,0.1"), "set_oversampling", "get_oversampling"); ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("Font")), "set_fallbacks", "get_fallbacks"); } @@ -2987,6 +2995,8 @@ void SystemFont::_update_base_font() { file->set_hinting(hinting); file->set_subpixel_positioning(subpixel_positioning); file->set_multichannel_signed_distance_field(msdf); + file->set_msdf_pixel_range(msdf_pixel_range); + file->set_msdf_size(msdf_size); file->set_oversampling(oversampling); base_font = file; @@ -3186,6 +3196,34 @@ bool SystemFont::is_multichannel_signed_distance_field() const { return msdf; } +void SystemFont::set_msdf_pixel_range(int p_msdf_pixel_range) { + if (msdf_pixel_range != p_msdf_pixel_range) { + msdf_pixel_range = p_msdf_pixel_range; + if (base_font.is_valid()) { + base_font->set_msdf_pixel_range(msdf_pixel_range); + } + emit_changed(); + } +} + +int SystemFont::get_msdf_pixel_range() const { + return msdf_pixel_range; +} + +void SystemFont::set_msdf_size(int p_msdf_size) { + if (msdf_size != p_msdf_size) { + msdf_size = p_msdf_size; + if (base_font.is_valid()) { + base_font->set_msdf_size(msdf_size); + } + emit_changed(); + } +} + +int SystemFont::get_msdf_size() const { + return msdf_size; +} + void SystemFont::set_oversampling(real_t p_oversampling) { if (oversampling != p_oversampling) { oversampling = p_oversampling; diff --git a/scene/resources/font.h b/scene/resources/font.h index 4d468a8841..ef79f8bd02 100644 --- a/scene/resources/font.h +++ b/scene/resources/font.h @@ -452,6 +452,8 @@ class SystemFont : public Font { TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO; real_t oversampling = 0.f; bool msdf = false; + int msdf_pixel_range = 16; + int msdf_size = 48; protected: static void _bind_methods(); @@ -488,6 +490,12 @@ public: virtual void set_multichannel_signed_distance_field(bool p_msdf); virtual bool is_multichannel_signed_distance_field() const; + virtual void set_msdf_pixel_range(int p_msdf_pixel_range); + virtual int get_msdf_pixel_range() const; + + virtual void set_msdf_size(int p_msdf_size); + virtual int get_msdf_size() const; + virtual void set_font_names(const PackedStringArray &p_names); virtual PackedStringArray get_font_names() const; diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp index db7385428b..f4aa0637f7 100644 --- a/scene/resources/material.cpp +++ b/scene/resources/material.cpp @@ -113,6 +113,12 @@ bool Material::_can_use_render_priority() const { return ret; } +Ref<Resource> Material::create_placeholder() const { + Ref<PlaceholderMaterial> placeholder; + placeholder.instantiate(); + return placeholder; +} + void Material::_bind_methods() { ClassDB::bind_method(D_METHOD("set_next_pass", "next_pass"), &Material::set_next_pass); ClassDB::bind_method(D_METHOD("get_next_pass"), &Material::get_next_pass); @@ -123,6 +129,8 @@ void Material::_bind_methods() { ClassDB::bind_method(D_METHOD("inspect_native_shader_code"), &Material::inspect_native_shader_code); ClassDB::set_method_flags(get_class_static(), _scs_create("inspect_native_shader_code"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR); + ClassDB::bind_method(D_METHOD("create_placeholder"), &Material::create_placeholder); + ADD_PROPERTY(PropertyInfo(Variant::INT, "render_priority", PROPERTY_HINT_RANGE, itos(RENDER_PRIORITY_MIN) + "," + itos(RENDER_PRIORITY_MAX) + ",1"), "set_render_priority", "get_render_priority"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "next_pass", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_next_pass", "get_next_pass"); @@ -188,7 +196,7 @@ bool ShaderMaterial::_get(const StringName &p_name, Variant &r_ret) const { if (shader.is_valid()) { const StringName *sn = remap_cache.getptr(p_name); if (sn) { - // Only return a parameter if it was previosly set. + // Only return a parameter if it was previously set. r_ret = get_shader_parameter(*sn); return true; } diff --git a/scene/resources/material.h b/scene/resources/material.h index 83c7a09cc4..fe1448cd4c 100644 --- a/scene/resources/material.h +++ b/scene/resources/material.h @@ -74,6 +74,9 @@ public: virtual RID get_rid() const override; virtual RID get_shader_rid() const; virtual Shader::Mode get_shader_mode() const; + + virtual Ref<Resource> create_placeholder() const; + Material(); virtual ~Material(); }; diff --git a/scene/resources/mesh.cpp b/scene/resources/mesh.cpp index cedf4319f8..cf9baa2907 100644 --- a/scene/resources/mesh.cpp +++ b/scene/resources/mesh.cpp @@ -189,6 +189,7 @@ Ref<TriangleMesh> Mesh::generate_triangle_mesh() const { if (primitive == PRIMITIVE_TRIANGLES) { for (int j = 0; j < ic; j++) { int index = ir[j]; + ERR_FAIL_COND_V(index >= vc, Ref<TriangleMesh>()); facesw[widx++] = vr[index]; } } else { // PRIMITIVE_TRIANGLE_STRIP @@ -614,6 +615,13 @@ Size2i Mesh::get_lightmap_size_hint() const { return lightmap_size_hint; } +Ref<Resource> Mesh::create_placeholder() const { + Ref<PlaceholderMesh> placeholder; + placeholder.instantiate(); + placeholder->set_aabb(get_aabb()); + return placeholder; +} + void Mesh::_bind_methods() { ClassDB::bind_method(D_METHOD("set_lightmap_size_hint", "size"), &Mesh::set_lightmap_size_hint); ClassDB::bind_method(D_METHOD("get_lightmap_size_hint"), &Mesh::get_lightmap_size_hint); @@ -626,6 +634,7 @@ void Mesh::_bind_methods() { ClassDB::bind_method(D_METHOD("surface_get_blend_shape_arrays", "surf_idx"), &Mesh::surface_get_blend_shape_arrays); ClassDB::bind_method(D_METHOD("surface_set_material", "surf_idx", "material"), &Mesh::surface_set_material); ClassDB::bind_method(D_METHOD("surface_get_material", "surf_idx"), &Mesh::surface_get_material); + ClassDB::bind_method(D_METHOD("create_placeholder"), &Mesh::create_placeholder); BIND_ENUM_CONSTANT(PRIMITIVE_POINTS); BIND_ENUM_CONSTANT(PRIMITIVE_LINES); @@ -1121,17 +1130,6 @@ bool ArrayMesh::_set(const StringName &p_name, const Variant &p_value) { } int idx = sname.substr(8, sl - 8).to_int(); - // This is a bit of a hack to ensure compatibility with older material - // overrides that start indexing at 1. - // We assume that idx 0 is always read first, if its not, this won't work. - if (idx == 0) { - surface_index_0 = true; - } - if (!surface_index_0) { - // This means the file was created when the indexing started at 1, so decrease by one. - idx--; - } - String what = sname.get_slicec('/', 1); if (what == "material") { surface_set_material(idx, p_value); diff --git a/scene/resources/mesh.h b/scene/resources/mesh.h index 1baa466312..1b870d996a 100644 --- a/scene/resources/mesh.h +++ b/scene/resources/mesh.h @@ -220,6 +220,8 @@ public: virtual int get_builtin_bind_pose_count() const; virtual Transform3D get_builtin_bind_pose(int p_index) const; + virtual Ref<Resource> create_placeholder() const; + Mesh(); }; diff --git a/scene/resources/packed_scene.cpp b/scene/resources/packed_scene.cpp index c24186a109..e497a628aa 100644 --- a/scene/resources/packed_scene.cpp +++ b/scene/resources/packed_scene.cpp @@ -1046,6 +1046,25 @@ Ref<SceneState> SceneState::get_base_scene_state() const { return Ref<SceneState>(); } +void SceneState::update_instance_resource(String p_path, Ref<PackedScene> p_packed_scene) { + ERR_FAIL_COND(p_packed_scene.is_null()); + + for (const NodeData &nd : nodes) { + if (nd.instance >= 0) { + if (!(nd.instance & FLAG_INSTANCE_IS_PLACEHOLDER)) { + int instance_id = nd.instance & FLAG_MASK; + Ref<PackedScene> original_packed_scene = variants[instance_id]; + if (original_packed_scene.is_valid()) { + if (original_packed_scene->get_path() == p_path) { + variants.remove_at(instance_id); + variants.insert(instance_id, p_packed_scene); + } + } + } + } + } +} + int SceneState::find_node_by_path(const NodePath &p_node) const { ERR_FAIL_COND_V_MSG(node_path_cache.size() == 0, -1, "This operation requires the node cache to have been built."); diff --git a/scene/resources/packed_scene.h b/scene/resources/packed_scene.h index ef7363dd44..5c53ffdb45 100644 --- a/scene/resources/packed_scene.h +++ b/scene/resources/packed_scene.h @@ -150,6 +150,8 @@ public: Ref<SceneState> get_base_scene_state() const; + void update_instance_resource(String p_path, Ref<PackedScene> p_packed_scene); + //unbuild API int get_node_count() const; diff --git a/scene/resources/polygon_path_finder.cpp b/scene/resources/polygon_path_finder.cpp index 85106883f9..3bbd0a0b5e 100644 --- a/scene/resources/polygon_path_finder.cpp +++ b/scene/resources/polygon_path_finder.cpp @@ -325,7 +325,7 @@ Vector<Vector2> PolygonPathFinder::find_path(const Vector2 &p_from, const Vector } const Point &np = points[least_cost_point]; - //open the neighbours for search + //open the neighbors for search for (const int &E : np.connections) { Point &p = points.write[E]; @@ -339,7 +339,7 @@ Vector<Vector2> PolygonPathFinder::find_path(const Vector2 &p_from, const Vector p.distance = distance; } } else { - //add to open neighbours + //add to open neighbors p.prev = least_cost_point; p.distance = distance; diff --git a/scene/resources/shader.cpp b/scene/resources/shader.cpp index b3952e745f..a09dbd50cf 100644 --- a/scene/resources/shader.cpp +++ b/scene/resources/shader.cpp @@ -42,9 +42,12 @@ Shader::Mode Shader::get_mode() const { } void Shader::_dependency_changed() { - RenderingServer::get_singleton()->shader_set_code(shader, RenderingServer::get_singleton()->shader_get_code(shader)); + // Preprocess and compile the code again because a dependency has changed. It also calls emit_changed() for us. + _recompile(); +} - emit_changed(); +void Shader::_recompile() { + set_code(get_code()); } void Shader::set_path(const String &p_path, bool p_take_over) { diff --git a/scene/resources/shader.h b/scene/resources/shader.h index 75c490e912..55608b6c11 100644 --- a/scene/resources/shader.h +++ b/scene/resources/shader.h @@ -60,6 +60,7 @@ private: HashMap<StringName, HashMap<int, Ref<Texture2D>>> default_textures; void _dependency_changed(); + void _recompile(); virtual void _update_shader() const; //used for visual shader Array _get_shader_uniform_list(bool p_get_groups = false); diff --git a/scene/resources/skeleton_modification_2d_stackholder.cpp b/scene/resources/skeleton_modification_2d_stackholder.cpp index 121108965b..34d31bac8a 100644 --- a/scene/resources/skeleton_modification_2d_stackholder.cpp +++ b/scene/resources/skeleton_modification_2d_stackholder.cpp @@ -64,7 +64,7 @@ bool SkeletonModification2DStackHolder::_get(const StringName &p_path, Variant & } void SkeletonModification2DStackHolder::_get_property_list(List<PropertyInfo> *p_list) const { - p_list->push_back(PropertyInfo(Variant::OBJECT, "held_modification_stack", PROPERTY_HINT_RESOURCE_TYPE, "SkeletonModificationStack2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE)); + p_list->push_back(PropertyInfo(Variant::OBJECT, "held_modification_stack", PROPERTY_HINT_RESOURCE_TYPE, "SkeletonModificationStack2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ALWAYS_DUPLICATE)); #ifdef TOOLS_ENABLED if (Engine::get_singleton()->is_editor_hint()) { diff --git a/scene/resources/skeleton_modification_stack_2d.cpp b/scene/resources/skeleton_modification_stack_2d.cpp index 4fa287e7b6..71ddbc0898 100644 --- a/scene/resources/skeleton_modification_stack_2d.cpp +++ b/scene/resources/skeleton_modification_stack_2d.cpp @@ -37,7 +37,7 @@ void SkeletonModificationStack2D::_get_property_list(List<PropertyInfo> *p_list) PropertyInfo(Variant::OBJECT, "modifications/" + itos(i), PROPERTY_HINT_RESOURCE_TYPE, "SkeletonModification2D", - PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DEFERRED_SET_RESOURCE | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE)); + PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DEFERRED_SET_RESOURCE | PROPERTY_USAGE_ALWAYS_DUPLICATE)); } } diff --git a/scene/resources/style_box.cpp b/scene/resources/style_box.cpp index 6390850b24..fbbe98d022 100644 --- a/scene/resources/style_box.cpp +++ b/scene/resources/style_box.cpp @@ -34,76 +34,65 @@ #include <limits.h> -float StyleBox::get_style_margin(Side p_side) const { - float ret = 0; - GDVIRTUAL_REQUIRED_CALL(_get_style_margin, p_side, ret); - return ret; -} +Size2 StyleBox::get_minimum_size() const { + Size2 min_size = Size2(get_margin(SIDE_LEFT) + get_margin(SIDE_RIGHT), get_margin(SIDE_TOP) + get_margin(SIDE_BOTTOM)); + Size2 custom_size; + GDVIRTUAL_CALL(_get_minimum_size, custom_size); -bool StyleBox::test_mask(const Point2 &p_point, const Rect2 &p_rect) const { - bool ret = true; - GDVIRTUAL_CALL(_test_mask, p_point, p_rect, ret); - return ret; -} + if (min_size.x < custom_size.x) { + min_size.x = custom_size.x; + } + if (min_size.y < custom_size.y) { + min_size.y = custom_size.y; + } -void StyleBox::draw(RID p_canvas_item, const Rect2 &p_rect) const { - GDVIRTUAL_REQUIRED_CALL(_draw, p_canvas_item, p_rect); + return min_size; } -void StyleBox::set_default_margin(Side p_side, float p_value) { +void StyleBox::set_content_margin(Side p_side, float p_value) { ERR_FAIL_INDEX((int)p_side, 4); - margin[p_side] = p_value; + content_margin[p_side] = p_value; emit_changed(); } -void StyleBox::set_default_margin_all(float p_value) { +void StyleBox::set_content_margin_all(float p_value) { for (int i = 0; i < 4; i++) { - margin[i] = p_value; + content_margin[i] = p_value; } emit_changed(); } -void StyleBox::set_default_margin_individual(float p_left, float p_top, float p_right, float p_bottom) { - margin[SIDE_LEFT] = p_left; - margin[SIDE_TOP] = p_top; - margin[SIDE_RIGHT] = p_right; - margin[SIDE_BOTTOM] = p_bottom; +void StyleBox::set_content_margin_individual(float p_left, float p_top, float p_right, float p_bottom) { + content_margin[SIDE_LEFT] = p_left; + content_margin[SIDE_TOP] = p_top; + content_margin[SIDE_RIGHT] = p_right; + content_margin[SIDE_BOTTOM] = p_bottom; emit_changed(); } -float StyleBox::get_default_margin(Side p_side) const { +float StyleBox::get_content_margin(Side p_side) const { ERR_FAIL_INDEX_V((int)p_side, 4, 0.0); - return margin[p_side]; + return content_margin[p_side]; } float StyleBox::get_margin(Side p_side) const { ERR_FAIL_INDEX_V((int)p_side, 4, 0.0); - if (margin[p_side] < 0) { + if (content_margin[p_side] < 0) { return get_style_margin(p_side); } else { - return margin[p_side]; + return content_margin[p_side]; } } -CanvasItem *StyleBox::get_current_item_drawn() const { - return CanvasItem::get_current_item_drawn(); -} - -Size2 StyleBox::get_minimum_size() const { - return Size2(get_margin(SIDE_LEFT) + get_margin(SIDE_RIGHT), get_margin(SIDE_TOP) + get_margin(SIDE_BOTTOM)); -} - Point2 StyleBox::get_offset() const { return Point2(get_margin(SIDE_LEFT), get_margin(SIDE_TOP)); } -Size2 StyleBox::get_center_size() const { - Size2 ret; - GDVIRTUAL_CALL(_get_center_size, ret); - return ret; +void StyleBox::draw(RID p_canvas_item, const Rect2 &p_rect) const { + GDVIRTUAL_REQUIRED_CALL(_draw, p_canvas_item, p_rect); } Rect2 StyleBox::get_draw_rect(const Rect2 &p_rect) const { @@ -114,37 +103,46 @@ Rect2 StyleBox::get_draw_rect(const Rect2 &p_rect) const { return p_rect; } +CanvasItem *StyleBox::get_current_item_drawn() const { + return CanvasItem::get_current_item_drawn(); +} + +bool StyleBox::test_mask(const Point2 &p_point, const Rect2 &p_rect) const { + bool ret = true; + GDVIRTUAL_CALL(_test_mask, p_point, p_rect, ret); + return ret; +} + void StyleBox::_bind_methods() { - ClassDB::bind_method(D_METHOD("test_mask", "point", "rect"), &StyleBox::test_mask); + ClassDB::bind_method(D_METHOD("get_minimum_size"), &StyleBox::get_minimum_size); - ClassDB::bind_method(D_METHOD("set_default_margin", "margin", "offset"), &StyleBox::set_default_margin); - ClassDB::bind_method(D_METHOD("set_default_margin_all", "offset"), &StyleBox::set_default_margin_all); - ClassDB::bind_method(D_METHOD("get_default_margin", "margin"), &StyleBox::get_default_margin); + ClassDB::bind_method(D_METHOD("set_content_margin", "margin", "offset"), &StyleBox::set_content_margin); + ClassDB::bind_method(D_METHOD("set_content_margin_all", "offset"), &StyleBox::set_content_margin_all); + ClassDB::bind_method(D_METHOD("get_content_margin", "margin"), &StyleBox::get_content_margin); ClassDB::bind_method(D_METHOD("get_margin", "margin"), &StyleBox::get_margin); - ClassDB::bind_method(D_METHOD("get_minimum_size"), &StyleBox::get_minimum_size); - ClassDB::bind_method(D_METHOD("get_center_size"), &StyleBox::get_center_size); ClassDB::bind_method(D_METHOD("get_offset"), &StyleBox::get_offset); - ClassDB::bind_method(D_METHOD("get_current_item_drawn"), &StyleBox::get_current_item_drawn); ClassDB::bind_method(D_METHOD("draw", "canvas_item", "rect"), &StyleBox::draw); + ClassDB::bind_method(D_METHOD("get_current_item_drawn"), &StyleBox::get_current_item_drawn); + + ClassDB::bind_method(D_METHOD("test_mask", "point", "rect"), &StyleBox::test_mask); ADD_GROUP("Content Margins", "content_margin_"); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_left", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_default_margin", "get_default_margin", SIDE_LEFT); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_top", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_default_margin", "get_default_margin", SIDE_TOP); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_right", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_default_margin", "get_default_margin", SIDE_RIGHT); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_bottom", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_default_margin", "get_default_margin", SIDE_BOTTOM); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_left", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_content_margin", "get_content_margin", SIDE_LEFT); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_top", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_content_margin", "get_content_margin", SIDE_TOP); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_right", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_content_margin", "get_content_margin", SIDE_RIGHT); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "content_margin_bottom", PROPERTY_HINT_RANGE, "-1,2048,1,suffix:px"), "set_content_margin", "get_content_margin", SIDE_BOTTOM); - GDVIRTUAL_BIND(_get_style_margin, "side") - GDVIRTUAL_BIND(_test_mask, "point", "rect") - GDVIRTUAL_BIND(_get_center_size) - GDVIRTUAL_BIND(_get_draw_rect, "rect") GDVIRTUAL_BIND(_draw, "to_canvas_item", "rect") + GDVIRTUAL_BIND(_get_draw_rect, "rect") + GDVIRTUAL_BIND(_get_minimum_size) + GDVIRTUAL_BIND(_test_mask, "point", "rect") } StyleBox::StyleBox() { for (int i = 0; i < 4; i++) { - margin[i] = -1; + content_margin[i] = -1; } } @@ -153,11 +151,6 @@ void StyleBoxTexture::set_texture(Ref<Texture2D> p_texture) { return; } texture = p_texture; - if (p_texture.is_null()) { - region_rect = Rect2(0, 0, 0, 0); - } else { - region_rect = Rect2(Point2(), texture->get_size()); - } emit_changed(); } @@ -165,38 +158,38 @@ Ref<Texture2D> StyleBoxTexture::get_texture() const { return texture; } -void StyleBoxTexture::set_margin_size(Side p_side, float p_size) { +void StyleBoxTexture::set_texture_margin(Side p_side, float p_size) { ERR_FAIL_INDEX((int)p_side, 4); - margin[p_side] = p_size; + texture_margin[p_side] = p_size; emit_changed(); } -void StyleBoxTexture::set_margin_size_all(float p_size) { +void StyleBoxTexture::set_texture_margin_all(float p_size) { for (int i = 0; i < 4; i++) { - margin[i] = p_size; + texture_margin[i] = p_size; } emit_changed(); } -void StyleBoxTexture::set_margin_size_individual(float p_left, float p_top, float p_right, float p_bottom) { - margin[SIDE_LEFT] = p_left; - margin[SIDE_TOP] = p_top; - margin[SIDE_RIGHT] = p_right; - margin[SIDE_BOTTOM] = p_bottom; +void StyleBoxTexture::set_texture_margin_individual(float p_left, float p_top, float p_right, float p_bottom) { + texture_margin[SIDE_LEFT] = p_left; + texture_margin[SIDE_TOP] = p_top; + texture_margin[SIDE_RIGHT] = p_right; + texture_margin[SIDE_BOTTOM] = p_bottom; emit_changed(); } -float StyleBoxTexture::get_margin_size(Side p_side) const { +float StyleBoxTexture::get_texture_margin(Side p_side) const { ERR_FAIL_INDEX_V((int)p_side, 4, 0.0); - return margin[p_side]; + return texture_margin[p_side]; } float StyleBoxTexture::get_style_margin(Side p_side) const { ERR_FAIL_INDEX_V((int)p_side, 4, 0.0); - return margin[p_side]; + return texture_margin[p_side]; } Rect2 StyleBoxTexture::get_draw_rect(const Rect2 &p_rect) const { @@ -218,7 +211,10 @@ void StyleBoxTexture::draw(RID p_canvas_item, const Rect2 &p_rect) const { rect.size.x += expand_margin[SIDE_LEFT] + expand_margin[SIDE_RIGHT]; rect.size.y += expand_margin[SIDE_TOP] + expand_margin[SIDE_BOTTOM]; - RenderingServer::get_singleton()->canvas_item_add_nine_patch(p_canvas_item, rect, src_rect, texture->get_rid(), Vector2(margin[SIDE_LEFT], margin[SIDE_TOP]), Vector2(margin[SIDE_RIGHT], margin[SIDE_BOTTOM]), RS::NinePatchAxisMode(axis_h), RS::NinePatchAxisMode(axis_v), draw_center, modulate); + Vector2 start_offset = Vector2(texture_margin[SIDE_LEFT], texture_margin[SIDE_TOP]); + Vector2 end_offset = Vector2(texture_margin[SIDE_RIGHT], texture_margin[SIDE_BOTTOM]); + + RenderingServer::get_singleton()->canvas_item_add_nine_patch(p_canvas_item, rect, src_rect, texture->get_rid(), start_offset, end_offset, RS::NinePatchAxisMode(axis_h), RS::NinePatchAxisMode(axis_v), draw_center, modulate); } void StyleBoxTexture::set_draw_center(bool p_enabled) { @@ -230,21 +226,13 @@ bool StyleBoxTexture::is_draw_center_enabled() const { return draw_center; } -Size2 StyleBoxTexture::get_center_size() const { - if (texture.is_null()) { - return Size2(); - } - - return region_rect.size - get_minimum_size(); -} - -void StyleBoxTexture::set_expand_margin_size(Side p_side, float p_size) { +void StyleBoxTexture::set_expand_margin(Side p_side, float p_size) { ERR_FAIL_INDEX((int)p_side, 4); expand_margin[p_side] = p_size; emit_changed(); } -void StyleBoxTexture::set_expand_margin_size_individual(float p_left, float p_top, float p_right, float p_bottom) { +void StyleBoxTexture::set_expand_margin_individual(float p_left, float p_top, float p_right, float p_bottom) { expand_margin[SIDE_LEFT] = p_left; expand_margin[SIDE_TOP] = p_top; expand_margin[SIDE_RIGHT] = p_right; @@ -252,14 +240,14 @@ void StyleBoxTexture::set_expand_margin_size_individual(float p_left, float p_to emit_changed(); } -void StyleBoxTexture::set_expand_margin_size_all(float p_expand_margin_size) { +void StyleBoxTexture::set_expand_margin_all(float p_expand_margin_size) { for (int i = 0; i < 4; i++) { expand_margin[i] = p_expand_margin_size; } emit_changed(); } -float StyleBoxTexture::get_expand_margin_size(Side p_side) const { +float StyleBoxTexture::get_expand_margin(Side p_side) const { ERR_FAIL_INDEX_V((int)p_side, 4, 0); return expand_margin[p_side]; } @@ -313,13 +301,13 @@ void StyleBoxTexture::_bind_methods() { ClassDB::bind_method(D_METHOD("set_texture", "texture"), &StyleBoxTexture::set_texture); ClassDB::bind_method(D_METHOD("get_texture"), &StyleBoxTexture::get_texture); - ClassDB::bind_method(D_METHOD("set_margin_size", "margin", "size"), &StyleBoxTexture::set_margin_size); - ClassDB::bind_method(D_METHOD("set_margin_size_all", "size"), &StyleBoxTexture::set_margin_size_all); - ClassDB::bind_method(D_METHOD("get_margin_size", "margin"), &StyleBoxTexture::get_margin_size); + ClassDB::bind_method(D_METHOD("set_texture_margin", "margin", "size"), &StyleBoxTexture::set_texture_margin); + ClassDB::bind_method(D_METHOD("set_texture_margin_all", "size"), &StyleBoxTexture::set_texture_margin_all); + ClassDB::bind_method(D_METHOD("get_texture_margin", "margin"), &StyleBoxTexture::get_texture_margin); - ClassDB::bind_method(D_METHOD("set_expand_margin_size", "margin", "size"), &StyleBoxTexture::set_expand_margin_size); - ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxTexture::set_expand_margin_size_all); - ClassDB::bind_method(D_METHOD("get_expand_margin_size", "margin"), &StyleBoxTexture::get_expand_margin_size); + ClassDB::bind_method(D_METHOD("set_expand_margin", "margin", "size"), &StyleBoxTexture::set_expand_margin); + ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxTexture::set_expand_margin_all); + ClassDB::bind_method(D_METHOD("get_expand_margin", "margin"), &StyleBoxTexture::get_expand_margin); ClassDB::bind_method(D_METHOD("set_region_rect", "region"), &StyleBoxTexture::set_region_rect); ClassDB::bind_method(D_METHOD("get_region_rect"), &StyleBoxTexture::get_region_rect); @@ -338,17 +326,17 @@ void StyleBoxTexture::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture"); - ADD_GROUP("Margins", "margin_"); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "margin_left", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_margin_size", "get_margin_size", SIDE_LEFT); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "margin_top", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_margin_size", "get_margin_size", SIDE_TOP); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "margin_right", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_margin_size", "get_margin_size", SIDE_RIGHT); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_margin_size", "get_margin_size", SIDE_BOTTOM); + ADD_GROUP("Texture Margins", "texture_margin_"); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_left", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_LEFT); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_top", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_TOP); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_right", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_RIGHT); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "texture_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_texture_margin", "get_texture_margin", SIDE_BOTTOM); ADD_GROUP("Expand Margins", "expand_margin_"); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_left", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin_size", "get_expand_margin_size", SIDE_LEFT); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_top", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin_size", "get_expand_margin_size", SIDE_TOP); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_right", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin_size", "get_expand_margin_size", SIDE_RIGHT); - ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin_size", "get_expand_margin_size", SIDE_BOTTOM); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_left", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_LEFT); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_top", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_TOP); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_right", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_RIGHT); + ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "expand_margin_bottom", PROPERTY_HINT_RANGE, "0,2048,1,suffix:px"), "set_expand_margin", "get_expand_margin", SIDE_BOTTOM); ADD_GROUP("Axis Stretch", "axis_stretch_"); ADD_PROPERTY(PropertyInfo(Variant::INT, "axis_stretch_horizontal", PROPERTY_HINT_ENUM, "Stretch,Tile,Tile Fit"), "set_h_axis_stretch_mode", "get_h_axis_stretch_mode"); @@ -450,13 +438,13 @@ int StyleBoxFlat::get_corner_radius(const Corner p_corner) const { return corner_radius[p_corner]; } -void StyleBoxFlat::set_expand_margin_size(Side p_side, float p_size) { +void StyleBoxFlat::set_expand_margin(Side p_side, float p_size) { ERR_FAIL_INDEX((int)p_side, 4); expand_margin[p_side] = p_size; emit_changed(); } -void StyleBoxFlat::set_expand_margin_size_individual(float p_left, float p_top, float p_right, float p_bottom) { +void StyleBoxFlat::set_expand_margin_individual(float p_left, float p_top, float p_right, float p_bottom) { expand_margin[SIDE_LEFT] = p_left; expand_margin[SIDE_TOP] = p_top; expand_margin[SIDE_RIGHT] = p_right; @@ -464,14 +452,14 @@ void StyleBoxFlat::set_expand_margin_size_individual(float p_left, float p_top, emit_changed(); } -void StyleBoxFlat::set_expand_margin_size_all(float p_expand_margin_size) { +void StyleBoxFlat::set_expand_margin_all(float p_expand_margin_size) { for (int i = 0; i < 4; i++) { expand_margin[i] = p_expand_margin_size; } emit_changed(); } -float StyleBoxFlat::get_expand_margin_size(Side p_side) const { +float StyleBoxFlat::get_expand_margin(Side p_side) const { ERR_FAIL_INDEX_V((int)p_side, 4, 0.0); return expand_margin[p_side]; } @@ -549,10 +537,6 @@ int StyleBoxFlat::get_corner_detail() const { return corner_detail; } -Size2 StyleBoxFlat::get_center_size() const { - return Size2(); -} - inline void set_inner_corner_radius(const Rect2 style_rect, const Rect2 inner_rect, const real_t corner_radius[4], real_t *inner_corner_radius) { real_t border_left = inner_rect.position.x - style_rect.position.x; real_t border_top = inner_rect.position.y - style_rect.position.y; @@ -891,9 +875,9 @@ void StyleBoxFlat::_bind_methods() { ClassDB::bind_method(D_METHOD("set_corner_radius", "corner", "radius"), &StyleBoxFlat::set_corner_radius); ClassDB::bind_method(D_METHOD("get_corner_radius", "corner"), &StyleBoxFlat::get_corner_radius); - ClassDB::bind_method(D_METHOD("set_expand_margin", "margin", "size"), &StyleBoxFlat::set_expand_margin_size); - ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxFlat::set_expand_margin_size_all); - ClassDB::bind_method(D_METHOD("get_expand_margin", "margin"), &StyleBoxFlat::get_expand_margin_size); + ClassDB::bind_method(D_METHOD("set_expand_margin", "margin", "size"), &StyleBoxFlat::set_expand_margin); + ClassDB::bind_method(D_METHOD("set_expand_margin_all", "size"), &StyleBoxFlat::set_expand_margin_all); + ClassDB::bind_method(D_METHOD("get_expand_margin", "margin"), &StyleBoxFlat::get_expand_margin); ClassDB::bind_method(D_METHOD("set_draw_center", "draw_center"), &StyleBoxFlat::set_draw_center); ClassDB::bind_method(D_METHOD("is_draw_center_enabled"), &StyleBoxFlat::is_draw_center_enabled); @@ -1041,10 +1025,6 @@ float StyleBoxLine::get_style_margin(Side p_side) const { return 0; } -Size2 StyleBoxLine::get_center_size() const { - return Size2(); -} - void StyleBoxLine::draw(RID p_canvas_item, const Rect2 &p_rect) const { RenderingServer *vs = RenderingServer::get_singleton(); Rect2i r = p_rect; diff --git a/scene/resources/style_box.h b/scene/resources/style_box.h index 5a80b4d4e2..17acfd773e 100644 --- a/scene/resources/style_box.h +++ b/scene/resources/style_box.h @@ -41,36 +41,34 @@ class StyleBox : public Resource { GDCLASS(StyleBox, Resource); RES_BASE_EXTENSION("stylebox"); OBJ_SAVE_TYPE(StyleBox); - float margin[4]; + float content_margin[4]; protected: - virtual float get_style_margin(Side p_side) const; + virtual float get_style_margin(Side p_side) const { return 0; } static void _bind_methods(); - GDVIRTUAL1RC(float, _get_style_margin, Side) - GDVIRTUAL2RC(bool, _test_mask, Point2, Rect2) - GDVIRTUAL0RC(Size2, _get_center_size) - GDVIRTUAL1RC(Rect2, _get_draw_rect, Rect2) GDVIRTUAL2C(_draw, RID, Rect2) + GDVIRTUAL1RC(Rect2, _get_draw_rect, Rect2) + GDVIRTUAL0RC(Size2, _get_minimum_size) + GDVIRTUAL2RC(bool, _test_mask, Point2, Rect2) public: - virtual bool test_mask(const Point2 &p_point, const Rect2 &p_rect) const; + virtual Size2 get_minimum_size() const; - void set_default_margin(Side p_side, float p_value); - void set_default_margin_all(float p_value); - void set_default_margin_individual(float p_left, float p_top, float p_right, float p_bottom); - float get_default_margin(Side p_side) const; + void set_content_margin(Side p_side, float p_value); + void set_content_margin_all(float p_value); + void set_content_margin_individual(float p_left, float p_top, float p_right, float p_bottom); + float get_content_margin(Side p_side) const; float get_margin(Side p_side) const; - virtual Size2 get_center_size() const; + Point2 get_offset() const; - virtual Rect2 get_draw_rect(const Rect2 &p_rect) const; virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const; + virtual Rect2 get_draw_rect(const Rect2 &p_rect) const; CanvasItem *get_current_item_drawn() const; - Size2 get_minimum_size() const; - Point2 get_offset() const; + virtual bool test_mask(const Point2 &p_point, const Rect2 &p_rect) const; StyleBox(); }; @@ -96,7 +94,7 @@ public: private: float expand_margin[4] = {}; - float margin[4] = {}; + float texture_margin[4] = {}; Rect2 region_rect; Ref<Texture2D> texture; bool draw_center = true; @@ -109,15 +107,15 @@ protected: static void _bind_methods(); public: - void set_expand_margin_size(Side p_expand_side, float p_size); - void set_expand_margin_size_all(float p_expand_margin_size); - void set_expand_margin_size_individual(float p_left, float p_top, float p_right, float p_bottom); - float get_expand_margin_size(Side p_expand_side) const; + void set_expand_margin(Side p_expand_side, float p_size); + void set_expand_margin_all(float p_expand_margin_size); + void set_expand_margin_individual(float p_left, float p_top, float p_right, float p_bottom); + float get_expand_margin(Side p_expand_side) const; - void set_margin_size(Side p_side, float p_size); - void set_margin_size_all(float p_size); - void set_margin_size_individual(float p_left, float p_top, float p_right, float p_bottom); - float get_margin_size(Side p_side) const; + void set_texture_margin(Side p_side, float p_size); + void set_texture_margin_all(float p_size); + void set_texture_margin_individual(float p_left, float p_top, float p_right, float p_bottom); + float get_texture_margin(Side p_side) const; void set_region_rect(const Rect2 &p_region_rect); Rect2 get_region_rect() const; @@ -127,7 +125,6 @@ public: void set_draw_center(bool p_enabled); bool is_draw_center_enabled() const; - virtual Size2 get_center_size() const override; void set_h_axis_stretch_mode(AxisStretchMode p_mode); AxisStretchMode get_h_axis_stretch_mode() const; @@ -198,10 +195,10 @@ public: void set_corner_detail(const int &p_corner_detail); int get_corner_detail() const; - void set_expand_margin_size(Side p_expand_side, float p_size); - void set_expand_margin_size_all(float p_expand_margin_size); - void set_expand_margin_size_individual(float p_left, float p_top, float p_right, float p_bottom); - float get_expand_margin_size(Side p_expand_side) const; + void set_expand_margin(Side p_expand_side, float p_size); + void set_expand_margin_all(float p_expand_margin_size); + void set_expand_margin_individual(float p_left, float p_top, float p_right, float p_bottom); + float get_expand_margin(Side p_expand_side) const; void set_draw_center(bool p_enabled); bool is_draw_center_enabled() const; @@ -223,8 +220,6 @@ public: void set_aa_size(const real_t p_aa_size); real_t get_aa_size() const; - virtual Size2 get_center_size() const override; - virtual Rect2 get_draw_rect(const Rect2 &p_rect) const override; virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const override; @@ -261,8 +256,6 @@ public: void set_grow_end(float p_grow); float get_grow_end() const; - virtual Size2 get_center_size() const override; - virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const override; StyleBoxLine(); diff --git a/scene/resources/surface_tool.cpp b/scene/resources/surface_tool.cpp index 17e92ddfca..5a2b917b9a 100644 --- a/scene/resources/surface_tool.cpp +++ b/scene/resources/surface_tool.cpp @@ -146,6 +146,25 @@ uint32_t SurfaceTool::VertexHasher::hash(const Vertex &p_vtx) { return h; } +bool SurfaceTool::SmoothGroupVertex::operator==(const SmoothGroupVertex &p_vertex) const { + if (vertex != p_vertex.vertex) { + return false; + } + + if (smooth_group != p_vertex.smooth_group) { + return false; + } + + return true; +} + +uint32_t SurfaceTool::SmoothGroupVertexHasher::hash(const SmoothGroupVertex &p_vtx) { + uint32_t h = hash_djb2_buffer((const uint8_t *)&p_vtx.vertex, sizeof(real_t) * 3); + h = hash_murmur3_one_32(p_vtx.smooth_group, h); + h = hash_fmix32(h); + return h; +} + uint32_t SurfaceTool::TriangleHasher::hash(const int *p_triangle) { int t0 = p_triangle[0]; int t1 = p_triangle[1]; @@ -1152,7 +1171,7 @@ void SurfaceTool::generate_normals(bool p_flip) { ERR_FAIL_COND((vertex_array.size() % 3) != 0); - HashMap<Vertex, Vector3, VertexHasher> vertex_hash; + HashMap<SmoothGroupVertex, Vector3, SmoothGroupVertexHasher> smooth_hash; for (uint32_t vi = 0; vi < vertex_array.size(); vi += 3) { Vertex *v = &vertex_array[vi]; @@ -1165,21 +1184,28 @@ void SurfaceTool::generate_normals(bool p_flip) { } for (int i = 0; i < 3; i++) { - Vector3 *lv = vertex_hash.getptr(v[i]); - if (!lv) { - vertex_hash.insert(v[i], normal); + // Add face normal to smooth vertex influence if vertex is member of a smoothing group + if (v[i].smooth_group != UINT32_MAX) { + Vector3 *lv = smooth_hash.getptr(v[i]); + if (!lv) { + smooth_hash.insert(v[i], normal); + } else { + (*lv) += normal; + } } else { - (*lv) += normal; + v[i].normal = normal; } } } for (Vertex &vertex : vertex_array) { - Vector3 *lv = vertex_hash.getptr(vertex); - if (!lv) { - vertex.normal = Vector3(); - } else { - vertex.normal = lv->normalized(); + if (vertex.smooth_group != UINT32_MAX) { + Vector3 *lv = smooth_hash.getptr(vertex); + if (!lv) { + vertex.normal = Vector3(); + } else { + vertex.normal = lv->normalized(); + } } } diff --git a/scene/resources/surface_tool.h b/scene/resources/surface_tool.h index 25e078d2ff..00438c4a53 100644 --- a/scene/resources/surface_tool.h +++ b/scene/resources/surface_tool.h @@ -97,6 +97,21 @@ private: static _FORCE_INLINE_ uint32_t hash(const Vertex &p_vtx); }; + struct SmoothGroupVertex { + Vector3 vertex; + uint32_t smooth_group = 0; + bool operator==(const SmoothGroupVertex &p_vertex) const; + + SmoothGroupVertex(const Vertex &p_vertex) { + vertex = p_vertex.vertex; + smooth_group = p_vertex.smooth_group; + }; + }; + + struct SmoothGroupVertexHasher { + static _FORCE_INLINE_ uint32_t hash(const SmoothGroupVertex &p_vtx); + }; + struct TriangleHasher { static _FORCE_INLINE_ uint32_t hash(const int *p_triangle); static _FORCE_INLINE_ bool compare(const int *p_lhs, const int *p_rhs); diff --git a/scene/resources/texture.cpp b/scene/resources/texture.cpp index f106eebff5..85e21d6056 100644 --- a/scene/resources/texture.cpp +++ b/scene/resources/texture.cpp @@ -94,6 +94,13 @@ bool Texture2D::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Re return true; } +Ref<Resource> Texture2D::create_placeholder() const { + Ref<PlaceholderTexture2D> placeholder; + placeholder.instantiate(); + placeholder->set_size(get_size()); + return placeholder; +} + void Texture2D::_bind_methods() { ClassDB::bind_method(D_METHOD("get_width"), &Texture2D::get_width); ClassDB::bind_method(D_METHOD("get_height"), &Texture2D::get_height); @@ -103,6 +110,7 @@ void Texture2D::_bind_methods() { ClassDB::bind_method(D_METHOD("draw_rect", "canvas_item", "rect", "tile", "modulate", "transpose"), &Texture2D::draw_rect, DEFVAL(Color(1, 1, 1)), DEFVAL(false)); ClassDB::bind_method(D_METHOD("draw_rect_region", "canvas_item", "rect", "src_rect", "modulate", "transpose", "clip_uv"), &Texture2D::draw_rect_region, DEFVAL(Color(1, 1, 1)), DEFVAL(false), DEFVAL(true)); ClassDB::bind_method(D_METHOD("get_image"), &Texture2D::get_image); + ClassDB::bind_method(D_METHOD("create_placeholder"), &Texture2D::create_placeholder); ADD_GROUP("", ""); @@ -1137,6 +1145,7 @@ void Texture3D::_bind_methods() { ClassDB::bind_method(D_METHOD("get_depth"), &Texture3D::get_depth); ClassDB::bind_method(D_METHOD("has_mipmaps"), &Texture3D::has_mipmaps); ClassDB::bind_method(D_METHOD("get_data"), &Texture3D::_get_datai); + ClassDB::bind_method(D_METHOD("create_placeholder"), &Texture3D::create_placeholder); GDVIRTUAL_BIND(_get_format); GDVIRTUAL_BIND(_get_width); @@ -1145,6 +1154,14 @@ void Texture3D::_bind_methods() { GDVIRTUAL_BIND(_has_mipmaps); GDVIRTUAL_BIND(_get_data); } + +Ref<Resource> Texture3D::create_placeholder() const { + Ref<PlaceholderTexture3D> placeholder; + placeholder.instantiate(); + placeholder->set_size(Vector3i(get_width(), get_height(), get_depth())); + return placeholder; +} + ////////////////////////////////////////// Image::Format ImageTexture3D::get_format() const { @@ -3048,6 +3065,42 @@ ImageTextureLayered::~ImageTextureLayered() { } } +void Texture2DArray::_bind_methods() { + ClassDB::bind_method(D_METHOD("create_placeholder"), &Texture2DArray::create_placeholder); +} + +Ref<Resource> Texture2DArray::create_placeholder() const { + Ref<PlaceholderTexture2DArray> placeholder; + placeholder.instantiate(); + placeholder->set_size(Size2i(get_width(), get_height())); + placeholder->set_layers(get_layers()); + return placeholder; +} + +void Cubemap::_bind_methods() { + ClassDB::bind_method(D_METHOD("create_placeholder"), &Cubemap::create_placeholder); +} + +Ref<Resource> Cubemap::create_placeholder() const { + Ref<PlaceholderCubemap> placeholder; + placeholder.instantiate(); + placeholder->set_size(Size2i(get_width(), get_height())); + placeholder->set_layers(get_layers()); + return placeholder; +} + +void CubemapArray::_bind_methods() { + ClassDB::bind_method(D_METHOD("create_placeholder"), &CubemapArray::create_placeholder); +} + +Ref<Resource> CubemapArray::create_placeholder() const { + Ref<PlaceholderCubemapArray> placeholder; + placeholder.instantiate(); + placeholder->set_size(Size2i(get_width(), get_height())); + placeholder->set_layers(get_layers()); + return placeholder; +} + /////////////////////////////////////////// void CompressedTextureLayered::set_path(const String &p_path, bool p_take_over) { diff --git a/scene/resources/texture.h b/scene/resources/texture.h index bb86910c0c..7f74ae6941 100644 --- a/scene/resources/texture.h +++ b/scene/resources/texture.h @@ -82,6 +82,8 @@ public: virtual Ref<Image> get_image() const { return Ref<Image>(); } + virtual Ref<Resource> create_placeholder() const; + Texture2D(); }; @@ -450,25 +452,41 @@ public: class Texture2DArray : public ImageTextureLayered { GDCLASS(Texture2DArray, ImageTextureLayered) + +protected: + static void _bind_methods(); + public: Texture2DArray() : ImageTextureLayered(LAYERED_TYPE_2D_ARRAY) {} + + virtual Ref<Resource> create_placeholder() const; }; class Cubemap : public ImageTextureLayered { GDCLASS(Cubemap, ImageTextureLayered); +protected: + static void _bind_methods(); + public: Cubemap() : ImageTextureLayered(LAYERED_TYPE_CUBEMAP) {} + + virtual Ref<Resource> create_placeholder() const; }; class CubemapArray : public ImageTextureLayered { GDCLASS(CubemapArray, ImageTextureLayered); +protected: + static void _bind_methods(); + public: CubemapArray() : ImageTextureLayered(LAYERED_TYPE_CUBEMAP_ARRAY) {} + + virtual Ref<Resource> create_placeholder() const; }; class CompressedTextureLayered : public TextureLayered { @@ -580,6 +598,7 @@ public: virtual int get_depth() const; virtual bool has_mipmaps() const; virtual Vector<Ref<Image>> get_data() const; + virtual Ref<Resource> create_placeholder() const; }; class ImageTexture3D : public Texture3D { diff --git a/scene/resources/visual_shader.cpp b/scene/resources/visual_shader.cpp index 1cbeaae428..bfcf5cb137 100644 --- a/scene/resources/visual_shader.cpp +++ b/scene/resources/visual_shader.cpp @@ -1561,7 +1561,7 @@ void VisualShader::_get_property_list(List<PropertyInfo> *p_list) const { prop_name += "/" + itos(E.key); if (E.key != NODE_ID_OUTPUT) { - p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "VisualShaderNode", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE)); + p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "VisualShaderNode", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_ALWAYS_DUPLICATE)); } p_list->push_back(PropertyInfo(Variant::VECTOR2, prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR)); diff --git a/scene/resources/visual_shader_nodes.cpp b/scene/resources/visual_shader_nodes.cpp index e78d9b924d..12be0f46a6 100644 --- a/scene/resources/visual_shader_nodes.cpp +++ b/scene/resources/visual_shader_nodes.cpp @@ -3690,16 +3690,47 @@ String VisualShaderNodeDerivativeFunc::get_output_port_name(int p_port) const { String VisualShaderNodeDerivativeFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const { static const char *functions[FUNC_MAX] = { - "fwidth($)", - "dFdx($)", - "dFdy($)" + "fwidth$($)", + "dFdx$($)", + "dFdy$($)" + }; + + static const char *precisions[PRECISION_MAX] = { + "", + "Coarse", + "Fine" }; String code; - code += " " + p_output_vars[0] + " = " + String(functions[func]).replace("$", p_input_vars[0]) + ";\n"; + if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") { + code += " " + p_output_vars[0] + " = " + String(functions[func]).replace_first("$", "").replace_first("$", p_input_vars[0]) + ";\n"; + return code; + } + + code += " " + p_output_vars[0] + " = " + String(functions[func]).replace_first("$", String(precisions[precision])).replace_first("$", p_input_vars[0]) + ";\n"; return code; } +String VisualShaderNodeDerivativeFunc::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const { + if (precision != PRECISION_NONE && OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") { + String precision_str; + switch (precision) { + case PRECISION_COARSE: { + precision_str = "Coarse"; + } break; + case PRECISION_FINE: { + precision_str = "Fine"; + } break; + default: { + } break; + } + + return vformat(RTR("`%s` precision mode is not available for `gl_compatibility` profile.\nReverted to `None` precision."), precision_str); + } + + return String(); +} + void VisualShaderNodeDerivativeFunc::set_op_type(OpType p_op_type) { ERR_FAIL_INDEX((int)p_op_type, int(OP_TYPE_MAX)); if (op_type == p_op_type) { @@ -3742,10 +3773,24 @@ VisualShaderNodeDerivativeFunc::Function VisualShaderNodeDerivativeFunc::get_fun return func; } +void VisualShaderNodeDerivativeFunc::set_precision(Precision p_precision) { + ERR_FAIL_INDEX(int(p_precision), int(PRECISION_MAX)); + if (precision == p_precision) { + return; + } + precision = p_precision; + emit_changed(); +} + +VisualShaderNodeDerivativeFunc::Precision VisualShaderNodeDerivativeFunc::get_precision() const { + return precision; +} + Vector<StringName> VisualShaderNodeDerivativeFunc::get_editable_properties() const { Vector<StringName> props; props.push_back("op_type"); props.push_back("function"); + props.push_back("precision"); return props; } @@ -3756,8 +3801,12 @@ void VisualShaderNodeDerivativeFunc::_bind_methods() { ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeDerivativeFunc::set_function); ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeDerivativeFunc::get_function); + ClassDB::bind_method(D_METHOD("set_precision", "precision"), &VisualShaderNodeDerivativeFunc::set_precision); + ClassDB::bind_method(D_METHOD("get_precision"), &VisualShaderNodeDerivativeFunc::get_precision); + ADD_PROPERTY(PropertyInfo(Variant::INT, "op_type", PROPERTY_HINT_ENUM, "Scalar,Vector2,Vector3,Vector4"), "set_op_type", "get_op_type"); ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Sum,X,Y"), "set_function", "get_function"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "precision", PROPERTY_HINT_ENUM, "None,Coarse,Fine"), "set_precision", "get_precision"); BIND_ENUM_CONSTANT(OP_TYPE_SCALAR); BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_2D); @@ -3769,6 +3818,11 @@ void VisualShaderNodeDerivativeFunc::_bind_methods() { BIND_ENUM_CONSTANT(FUNC_X); BIND_ENUM_CONSTANT(FUNC_Y); BIND_ENUM_CONSTANT(FUNC_MAX); + + BIND_ENUM_CONSTANT(PRECISION_NONE); + BIND_ENUM_CONSTANT(PRECISION_COARSE); + BIND_ENUM_CONSTANT(PRECISION_FINE); + BIND_ENUM_CONSTANT(PRECISION_MAX); } VisualShaderNodeDerivativeFunc::VisualShaderNodeDerivativeFunc() { diff --git a/scene/resources/visual_shader_nodes.h b/scene/resources/visual_shader_nodes.h index e3b101cf84..fa6b134526 100644 --- a/scene/resources/visual_shader_nodes.h +++ b/scene/resources/visual_shader_nodes.h @@ -1536,9 +1536,17 @@ public: FUNC_MAX, }; + enum Precision { + PRECISION_NONE, + PRECISION_COARSE, + PRECISION_FINE, + PRECISION_MAX, + }; + protected: OpType op_type = OP_TYPE_SCALAR; Function func = FUNC_SUM; + Precision precision = PRECISION_NONE; protected: static void _bind_methods(); @@ -1555,6 +1563,7 @@ public: virtual String get_output_port_name(int p_port) const override; virtual String generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview = false) const override; + virtual String get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const override; void set_op_type(OpType p_op_type); OpType get_op_type() const; @@ -1562,6 +1571,9 @@ public: void set_function(Function p_func); Function get_function() const; + void set_precision(Precision p_precision); + Precision get_precision() const; + virtual Vector<StringName> get_editable_properties() const override; VisualShaderNodeDerivativeFunc(); @@ -1569,6 +1581,7 @@ public: VARIANT_ENUM_CAST(VisualShaderNodeDerivativeFunc::OpType) VARIANT_ENUM_CAST(VisualShaderNodeDerivativeFunc::Function) +VARIANT_ENUM_CAST(VisualShaderNodeDerivativeFunc::Precision) /////////////////////////////////////// /// FACEFORWARD |