Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Texture.get_data() to get_image() #47435

Merged
merged 1 commit into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/classes/ImageTexture.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
var texture = load("res://icon.png")
$Sprite2D.texture = texture
[/codeblock]
This is because images have to be imported as [StreamTexture2D] first to be loaded with [method @GDScript.load]. If you'd still like to load an image file just like any other [Resource], import it as an [Image] resource instead, and then load it normally using the [method @GDScript.load] method.
But do note that the image data can still be retrieved from an imported texture as well using the [method Texture2D.get_data] method, which returns a copy of the data:
This is because images have to be imported as a [StreamTexture2D] first to be loaded with [method @GDScript.load]. If you'd still like to load an image file just like any other [Resource], import it as an [Image] resource instead, and then load it normally using the [method @GDScript.load] method.
[b]Note:[/b] The image can be retrieved from an imported texture using the [method Texture2D.get_image] method, which returns a copy of the image:
[codeblock]
var texture = load("res://icon.png")
var image : Image = texture.get_data()
var image : Image = texture.get_image()
[/codeblock]
An [ImageTexture] is not meant to be operated from within the editor interface directly, and is mostly useful for rendering images on screen dynamically via code. If you need to generate images procedurally from within the editor, consider saving and importing images as custom texture resources implementing a new [EditorImportPlugin].
[b]Note:[/b] The maximum texture size is 16384×16384 pixels due to graphics hardware limitations.
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/Texture2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
Draws a part of the texture using a [CanvasItem] with the [RenderingServer] API.
</description>
</method>
<method name="get_data" qualifiers="const">
<method name="get_image" qualifiers="const">
<return type="Image">
</return>
<description>
Expand Down
4 changes: 2 additions & 2 deletions doc/classes/Viewport.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
</return>
<description>
Returns the viewport's texture.
[b]Note:[/b] Due to the way OpenGL works, the resulting [ViewportTexture] is flipped vertically. You can use [method Image.flip_y] on the result of [method Texture2D.get_data] to flip it back, for example:
[b]Note:[/b] Due to the way OpenGL works, the resulting [ViewportTexture] is flipped vertically. You can use [method Image.flip_y] on the result of [method Texture2D.get_image] to flip it back, for example:
[codeblock]
var img = get_viewport().get_texture().get_data()
var img = get_viewport().get_texture().get_image()
img.flip_y()
[/codeblock]
</description>
Expand Down
2 changes: 1 addition & 1 deletion drivers/png/resource_saver_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Error ResourceSaverPNG::save(const String &p_path, const RES &p_resource, uint32
ERR_FAIL_COND_V_MSG(!texture.is_valid(), ERR_INVALID_PARAMETER, "Can't save invalid texture as PNG.");
ERR_FAIL_COND_V_MSG(!texture->get_width(), ERR_INVALID_PARAMETER, "Can't save empty texture as PNG.");

Ref<Image> img = texture->get_data();
Ref<Image> img = texture->get_image();

Error err = save_image(p_path, img);

Expand Down
10 changes: 5 additions & 5 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1381,14 +1381,14 @@ void EditorNode::_save_scene_with_preview(String p_file, int p_idx) {
} else if (c3d < c2d) {
Ref<ViewportTexture> viewport_texture = scene_root->get_texture();
if (viewport_texture->get_width() > 0 && viewport_texture->get_height() > 0) {
img = viewport_texture->get_data();
img = viewport_texture->get_image();
}
} else {
// The 3D editor may be disabled as a feature, but scenes can still be opened.
// This check prevents the preview from regenerating in case those scenes are then saved.
Ref<EditorFeatureProfile> profile = feature_profile_manager->get_current_profile();
if (profile.is_valid() && !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D)) {
img = Node3DEditor::get_singleton()->get_editor_viewport(0)->get_viewport_node()->get_texture()->get_data();
img = Node3DEditor::get_singleton()->get_editor_viewport(0)->get_viewport_node()->get_texture()->get_image();
}
}

Expand Down Expand Up @@ -2835,7 +2835,7 @@ void EditorNode::_save_screenshot(NodePath p_path) {
ERR_FAIL_COND_MSG(!viewport, "Cannot get editor main control viewport.");
Ref<ViewportTexture> texture = viewport->get_texture();
ERR_FAIL_COND_MSG(texture.is_null(), "Cannot get editor main control viewport texture.");
Ref<Image> img = texture->get_data();
Ref<Image> img = texture->get_image();
ERR_FAIL_COND_MSG(img.is_null(), "Cannot get editor main control viewport texture image.");
Error error = img->save_png(p_path);
ERR_FAIL_COND_MSG(error != OK, "Cannot save screenshot to file '" + p_path + "'.");
Expand Down Expand Up @@ -5108,8 +5108,8 @@ Variant EditorNode::drag_resource(const Ref<Resource> &p_res, Control *p_from) {

{
//todo make proper previews
Ref<ImageTexture> pic = gui_base->get_theme_icon("FileBigThumb", "EditorIcons");
Ref<Image> img = pic->get_data();
Ref<ImageTexture> texture = gui_base->get_theme_icon("FileBigThumb", "EditorIcons");
Ref<Image> img = texture->get_image();
img = img->duplicate();
img->resize(48, 48); //meh
Ref<ImageTexture> resized_pic = Ref<ImageTexture>(memnew(ImageTexture));
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_resource_preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void EditorResourcePreview::_generate_preview(Ref<ImageTexture> &r_texture, Ref<
}

if (!r_small_texture.is_valid() && r_texture.is_valid() && preview_generators[i]->generate_small_preview_automatically()) {
Ref<Image> small_image = r_texture->get_data();
Ref<Image> small_image = r_texture->get_image();
small_image = small_image->duplicate();
small_image->resize(small_thumbnail_size, small_thumbnail_size, Image::INTERPOLATE_CUBIC);
r_small_texture.instance();
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_run_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void EditorRunNative::_notification(int p_what) {
}
Ref<ImageTexture> icon = eep->get_run_icon();
if (!icon.is_null()) {
Ref<Image> im = icon->get_data();
Ref<Image> im = icon->get_image();
im = im->duplicate();
im->clear_mipmaps();
if (!im->is_empty()) {
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_themes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static Ref<Texture2D> flip_icon(Ref<Texture2D> p_texture, bool p_flip_y = false,
}

Ref<ImageTexture> texture(memnew(ImageTexture));
Ref<Image> img = p_texture->get_data();
Ref<Image> img = p_texture->get_image();
img = img->duplicate();

if (p_flip_y) {
Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/animation_player_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ void AnimationPlayerEditor::_notification(int p_what) {
autoplay_icon = get_theme_icon("AutoPlay", "EditorIcons");
reset_icon = get_theme_icon("Reload", "EditorIcons");
{
Ref<Image> autoplay_img = autoplay_icon->get_data();
Ref<Image> reset_img = reset_icon->get_data();
Ref<Image> autoplay_img = autoplay_icon->get_image();
Ref<Image> reset_img = reset_icon->get_image();
Ref<Image> autoplay_reset_img;
Size2 icon_size = Size2(autoplay_img->get_width(), autoplay_img->get_height());
autoplay_reset_img.instance();
Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/asset_library_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ void EditorAssetLibraryItemDescription::set_image(int p_type, int p_index, const
for (int i = 0; i < preview_images.size(); i++) {
if (preview_images[i].id == p_index) {
if (preview_images[i].is_video) {
Ref<Image> overlay = previews->get_theme_icon("PlayOverlay", "EditorIcons")->get_data();
Ref<Image> thumbnail = p_image->get_data();
Ref<Image> overlay = previews->get_theme_icon("PlayOverlay", "EditorIcons")->get_image();
Ref<Image> thumbnail = p_image->get_image();
thumbnail = thumbnail->duplicate();
Point2 overlay_pos = Point2((thumbnail->get_width() - overlay->get_width()) / 2, (thumbnail->get_height() - overlay->get_height()) / 2);

Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/editor_preview_plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Ref<Texture2D> EditorTexturePreviewPlugin::generate(const RES &p_from, const Siz
return Ref<Texture2D>();
}

Ref<Image> atlas = tex->get_data();
Ref<Image> atlas = tex->get_image();
if (!atlas.is_valid()) {
return Ref<Texture2D>();
}
Expand All @@ -99,7 +99,7 @@ Ref<Texture2D> EditorTexturePreviewPlugin::generate(const RES &p_from, const Siz
} else {
Ref<Texture2D> tex = p_from;
if (tex.is_valid()) {
img = tex->get_data();
img = tex->get_image();
if (img.is_valid()) {
img = img->duplicate();
}
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/sprite_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void Sprite2DEditor::_update_mesh_data() {
return;
}

Ref<Image> image = texture->get_data();
Ref<Image> image = texture->get_image();
ERR_FAIL_COND(image.is_null());

if (image->is_compressed()) {
Expand Down
2 changes: 1 addition & 1 deletion modules/fbx/tools/import_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ class ImportUtils {
// } else {
// Ref<Texture> texture = ResourceLoader::load(p_path);
// ERR_FAIL_COND_V(texture.is_null(), Ref<Image>());
// Ref<Image> image = texture->get_data();
// Ref<Image> image = texture->get_image();
// ERR_FAIL_COND_V(image.is_null(), Ref<Image>());
// state.path_to_image_cache.insert(p_path, image);
// return image;
Expand Down
38 changes: 19 additions & 19 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2821,7 +2821,7 @@ Error GLTFDocument::_serialize_images(Ref<GLTFState> state, const String &p_path

ERR_CONTINUE(state->images[i].is_null());

Ref<Image> image = state->images[i]->get_data();
Ref<Image> image = state->images[i]->get_image();
ERR_CONTINUE(image.is_null());

if (p_path.to_lower().ends_with("glb")) {
Expand All @@ -2838,7 +2838,7 @@ Error GLTFDocument::_serialize_images(Ref<GLTFState> state, const String &p_path
Vector<uint8_t> buffer;
Ref<ImageTexture> img_tex = image;
if (img_tex.is_valid()) {
image = img_tex->get_data();
image = img_tex->get_image();
}
Error err = PNGDriverCommon::image_to_png(image, buffer);
ERR_FAIL_COND_V_MSG(err, err, "Can't convert image to PNG.");
Expand Down Expand Up @@ -3068,7 +3068,7 @@ GLTFTextureIndex GLTFDocument::_set_texture(Ref<GLTFState> state, Ref<Texture2D>
ERR_FAIL_COND_V(p_texture.is_null(), -1);
Ref<GLTFTexture> gltf_texture;
gltf_texture.instance();
ERR_FAIL_COND_V(p_texture->get_data().is_null(), -1);
ERR_FAIL_COND_V(p_texture->get_image().is_null(), -1);
GLTFImageIndex gltf_src_image_i = state->images.size();
state->images.push_back(p_texture);
gltf_texture->set_src_image(gltf_src_image_i);
Expand Down Expand Up @@ -3115,7 +3115,7 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> state) {
Ref<Texture2D> albedo_texture = material->get_texture(BaseMaterial3D::TEXTURE_ALBEDO);
GLTFTextureIndex gltf_texture_index = -1;

if (albedo_texture.is_valid() && albedo_texture->get_data().is_valid()) {
if (albedo_texture.is_valid() && albedo_texture->get_image().is_valid()) {
albedo_texture->set_name(material->get_name() + "_albedo");
gltf_texture_index = _set_texture(state, albedo_texture);
}
Expand All @@ -3128,9 +3128,9 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> state) {

mr["metallicFactor"] = material->get_metallic();
mr["roughnessFactor"] = material->get_roughness();
bool has_roughness = material->get_texture(BaseMaterial3D::TEXTURE_ROUGHNESS).is_valid() && material->get_texture(BaseMaterial3D::TEXTURE_ROUGHNESS)->get_data().is_valid();
bool has_roughness = material->get_texture(BaseMaterial3D::TEXTURE_ROUGHNESS).is_valid() && material->get_texture(BaseMaterial3D::TEXTURE_ROUGHNESS)->get_image().is_valid();
bool has_ao = material->get_feature(BaseMaterial3D::FEATURE_AMBIENT_OCCLUSION) && material->get_texture(BaseMaterial3D::TEXTURE_AMBIENT_OCCLUSION).is_valid();
bool has_metalness = material->get_texture(BaseMaterial3D::TEXTURE_METALLIC).is_valid() && material->get_texture(BaseMaterial3D::TEXTURE_METALLIC)->get_data().is_valid();
bool has_metalness = material->get_texture(BaseMaterial3D::TEXTURE_METALLIC).is_valid() && material->get_texture(BaseMaterial3D::TEXTURE_METALLIC)->get_image().is_valid();
if (has_ao || has_roughness || has_metalness) {
Dictionary mrt;
Ref<Texture2D> roughness_texture = material->get_texture(BaseMaterial3D::TEXTURE_ROUGHNESS);
Expand All @@ -3149,10 +3149,10 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> state) {
if (has_ao) {
height = ao_texture->get_height();
width = ao_texture->get_width();
ao_image = ao_texture->get_data();
ao_image = ao_texture->get_image();
Ref<ImageTexture> img_tex = ao_image;
if (img_tex.is_valid()) {
ao_image = img_tex->get_data();
ao_image = img_tex->get_image();
}
if (ao_image->is_compressed()) {
ao_image->decompress();
Expand All @@ -3162,10 +3162,10 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> state) {
if (has_roughness) {
height = roughness_texture->get_height();
width = roughness_texture->get_width();
roughness_image = roughness_texture->get_data();
roughness_image = roughness_texture->get_image();
Ref<ImageTexture> img_tex = roughness_image;
if (img_tex.is_valid()) {
roughness_image = img_tex->get_data();
roughness_image = img_tex->get_image();
}
if (roughness_image->is_compressed()) {
roughness_image->decompress();
Expand All @@ -3175,17 +3175,17 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> state) {
if (has_metalness) {
height = metallic_texture->get_height();
width = metallic_texture->get_width();
metallness_image = metallic_texture->get_data();
metallness_image = metallic_texture->get_image();
Ref<ImageTexture> img_tex = metallness_image;
if (img_tex.is_valid()) {
metallness_image = img_tex->get_data();
metallness_image = img_tex->get_image();
}
if (metallness_image->is_compressed()) {
metallness_image->decompress();
}
}
Ref<Texture2D> albedo_texture = material->get_texture(BaseMaterial3D::TEXTURE_ALBEDO);
if (albedo_texture.is_valid() && albedo_texture->get_data().is_valid()) {
if (albedo_texture.is_valid() && albedo_texture->get_image().is_valid()) {
height = albedo_texture->get_height();
width = albedo_texture->get_width();
}
Expand Down Expand Up @@ -3266,10 +3266,10 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> state) {
{
Ref<Texture2D> normal_texture = material->get_texture(BaseMaterial3D::TEXTURE_NORMAL);
// Code for uncompressing RG normal maps
Ref<Image> img = normal_texture->get_data();
Ref<Image> img = normal_texture->get_image();
Ref<ImageTexture> img_tex = img;
if (img_tex.is_valid()) {
img = img_tex->get_data();
img = img_tex->get_image();
}
img->decompress();
img->convert(Image::FORMAT_RGBA8);
Expand All @@ -3288,7 +3288,7 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> state) {
}
Ref<Texture2D> normal_texture = material->get_texture(BaseMaterial3D::TEXTURE_NORMAL);
GLTFTextureIndex gltf_texture_index = -1;
if (tex.is_valid() && tex->get_data().is_valid()) {
if (tex.is_valid() && tex->get_image().is_valid()) {
tex->set_name(material->get_name() + "_normal");
gltf_texture_index = _set_texture(state, tex);
}
Expand All @@ -3311,7 +3311,7 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> state) {
Dictionary et;
Ref<Texture2D> emission_texture = material->get_texture(BaseMaterial3D::TEXTURE_EMISSION);
GLTFTextureIndex gltf_texture_index = -1;
if (emission_texture.is_valid() && emission_texture->get_data().is_valid()) {
if (emission_texture.is_valid() && emission_texture->get_image().is_valid()) {
emission_texture->set_name(material->get_name() + "_emission");
gltf_texture_index = _set_texture(state, emission_texture);
}
Expand Down Expand Up @@ -3370,7 +3370,7 @@ Error GLTFDocument::_parse_materials(Ref<GLTFState> state) {
if (diffuse_texture_dict.has("index")) {
Ref<Texture2D> diffuse_texture = _get_texture(state, diffuse_texture_dict["index"]);
if (diffuse_texture.is_valid()) {
spec_gloss->diffuse_img = diffuse_texture->get_data();
spec_gloss->diffuse_img = diffuse_texture->get_image();
material->set_texture(BaseMaterial3D::TEXTURE_ALBEDO, diffuse_texture);
}
}
Expand Down Expand Up @@ -3398,7 +3398,7 @@ Error GLTFDocument::_parse_materials(Ref<GLTFState> state) {
if (spec_gloss_texture.has("index")) {
const Ref<Texture2D> orig_texture = _get_texture(state, spec_gloss_texture["index"]);
if (orig_texture.is_valid()) {
spec_gloss->spec_gloss_img = orig_texture->get_data();
spec_gloss->spec_gloss_img = orig_texture->get_image();
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions modules/opensimplex/doc_classes/NoiseTexture.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
<description>
Uses an [OpenSimplexNoise] to fill the texture data. You can specify the texture size but keep in mind that larger textures will take longer to generate and seamless noise only works with square sized textures.
NoiseTexture can also generate normal map textures.
The class uses [Thread]s to generate the texture data internally, so [method Texture2D.get_data] may return [code]null[/code] if the generation process has not completed yet. In that case, you need to wait for the texture to be generated before accessing the data:
The class uses [Thread]s to generate the texture data internally, so [method Texture2D.get_image] may return [code]null[/code] if the generation process has not completed yet. In that case, you need to wait for the texture to be generated before accessing the image and the generated byte data:
[codeblock]
var texture = preload("res://noise.tres")
yield(texture, "changed")
var image = texture.get_data()
var image = texture.get_image()
var data = image.get_data()
[/codeblock]
</description>
<tutorials>
Expand Down
14 changes: 7 additions & 7 deletions modules/opensimplex/noise_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ void NoiseTexture::_validate_property(PropertyInfo &property) const {
}
}

void NoiseTexture::_set_texture_data(const Ref<Image> &p_image) {
data = p_image;
if (data.is_valid()) {
void NoiseTexture::_set_texture_image(const Ref<Image> &p_image) {
image = p_image;
if (image.is_valid()) {
if (texture.is_valid()) {
RID new_texture = RS::get_singleton()->texture_2d_create(p_image);
RS::get_singleton()->texture_replace(texture, new_texture);
Expand All @@ -95,7 +95,7 @@ void NoiseTexture::_set_texture_data(const Ref<Image> &p_image) {
}

void NoiseTexture::_thread_done(const Ref<Image> &p_image) {
_set_texture_data(p_image);
_set_texture_image(p_image);
noise_thread.wait_to_finish();
if (regen_queued) {
noise_thread.start(_thread_function, this);
Expand Down Expand Up @@ -159,7 +159,7 @@ void NoiseTexture::_update_texture() {

} else {
Ref<Image> image = _generate_texture();
_set_texture_data(image);
_set_texture_image(image);
}
update_queued = false;
}
Expand Down Expand Up @@ -253,6 +253,6 @@ RID NoiseTexture::get_rid() const {
return texture;
}

Ref<Image> NoiseTexture::get_data() const {
return data;
Ref<Image> NoiseTexture::get_image() const {
return image;
}
6 changes: 3 additions & 3 deletions modules/opensimplex/noise_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class NoiseTexture : public Texture2D {
GDCLASS(NoiseTexture, Texture2D);

private:
Ref<Image> data;
Ref<Image> image;

Thread noise_thread;

Expand All @@ -66,7 +66,7 @@ class NoiseTexture : public Texture2D {
void _queue_update();
Ref<Image> _generate_texture();
void _update_texture();
void _set_texture_data(const Ref<Image> &p_image);
void _set_texture_image(const Ref<Image> &p_image);

protected:
static void _bind_methods();
Expand Down Expand Up @@ -94,7 +94,7 @@ class NoiseTexture : public Texture2D {
virtual RID get_rid() const override;
virtual bool has_alpha() const override { return false; }

virtual Ref<Image> get_data() const override;
virtual Ref<Image> get_image() const override;

NoiseTexture();
virtual ~NoiseTexture();
Expand Down