| 91 | } |
| 92 | |
| 93 | Ref<Texture2D> EditorTexturePreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const { |
| 94 | Ref<Image> img; |
| 95 | |
| 96 | Ref<AtlasTexture> tex_atlas = p_from; |
| 97 | Ref<Texture3D> tex_3d = p_from; |
| 98 | Ref<TextureLayered> tex_lyr = p_from; |
| 99 | |
| 100 | if (tex_atlas.is_valid()) { |
| 101 | Ref<Texture2D> tex = tex_atlas->get_atlas(); |
| 102 | if (tex.is_null()) { |
| 103 | return Ref<Texture2D>(); |
| 104 | } |
| 105 | |
| 106 | Ref<Image> atlas = tex->get_image(); |
| 107 | if (atlas.is_null()) { |
| 108 | return Ref<Texture2D>(); |
| 109 | } |
| 110 | |
| 111 | if (atlas->is_compressed()) { |
| 112 | atlas = atlas->duplicate(); |
| 113 | if (atlas->decompress() != OK) { |
| 114 | return Ref<Texture2D>(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (!tex_atlas->get_region().has_area()) { |
| 119 | return Ref<Texture2D>(); |
| 120 | } |
| 121 | |
| 122 | img = atlas->get_region(tex_atlas->get_region()); |
| 123 | |
| 124 | } else if (tex_3d.is_valid()) { |
| 125 | if (tex_3d->get_depth() == 0) { |
| 126 | return Ref<Texture2D>(); |
| 127 | } |
| 128 | |
| 129 | Vector<Ref<Image>> data = tex_3d->get_data(); |
| 130 | if (data.size() != tex_3d->get_depth()) { |
| 131 | return Ref<Texture2D>(); |
| 132 | } |
| 133 | |
| 134 | // Use the middle slice for the thumbnail. |
| 135 | const int mid_depth = (tex_3d->get_depth() - 1) / 2; |
| 136 | if (!data.is_empty() && data[mid_depth].is_valid()) { |
| 137 | img = data[mid_depth]->duplicate(); |
| 138 | } |
| 139 | |
| 140 | } else if (tex_lyr.is_valid()) { |
| 141 | if (tex_lyr->get_layers() == 0) { |
| 142 | return Ref<Texture2D>(); |
| 143 | } |
| 144 | |
| 145 | // Use the middle slice for the thumbnail. |
| 146 | const int mid_layer = (tex_lyr->get_layers() - 1) / 2; |
| 147 | |
| 148 | Ref<Image> data = tex_lyr->get_layer_data(mid_layer); |
| 149 | if (data.is_valid()) { |
| 150 | img = data->duplicate(); |
no test coverage detected