/*************************************************************************/ /* editor_preview_plugins.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* http://www.godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2017 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 "editor_preview_plugins.h" #include "io/resource_loader.h" #include "tools/editor/editor_settings.h" #include "io/file_access_memory.h" #include "os/os.h" #include "scene/resources/material.h" #include "scene/resources/sample.h" #include "scene/resources/mesh.h" #include "scene/resources/bit_mask.h" #include "tools/editor/editor_scale.h" #if 0 bool EditorTexturePreviewPlugin::handles(const String& p_type) const { return (ClassDB::is_type(p_type,"ImageTexture") || ClassDB::is_type(p_type, "AtlasTexture")); } Ref EditorTexturePreviewPlugin::generate(const RES& p_from) { Image img; Ref atex = p_from; if (atex.is_valid()) { Ref tex = atex->get_atlas(); if (!tex.is_valid()) { return Ref(); } Image atlas = tex->get_data(); img = atlas.get_rect(atex->get_region()); } else { Ref tex = p_from; img = tex->get_data(); } if (img.empty()) return Ref(); img.clear_mipmaps(); int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size"); thumbnail_size*=EDSCALE; if (img.is_compressed()) { if (img.decompress()!=OK) return Ref(); } else if (img.get_format()!=Image::FORMAT_RGB8 && img.get_format()!=Image::FORMAT_RGBA8) { img.convert(Image::FORMAT_RGBA8); } int width,height; if (img.get_width() > thumbnail_size && img.get_width() >= img.get_height()) { width=thumbnail_size; height = img.get_height() * thumbnail_size / img.get_width(); } else if (img.get_height() > thumbnail_size && img.get_height() >= img.get_width()) { height=thumbnail_size; width = img.get_width() * thumbnail_size / img.get_height(); } else { width=img.get_width(); height=img.get_height(); } img.resize(width,height); Ref ptex = Ref( memnew( ImageTexture )); ptex->create_from_image(img,0); return ptex; } EditorTexturePreviewPlugin::EditorTexturePreviewPlugin() { } //////////////////////////////////////////////////////////////////////////// bool EditorBitmapPreviewPlugin::handles(const String& p_type) const { return ClassDB::is_type(p_type,"BitMap"); } Ref EditorBitmapPreviewPlugin::generate(const RES& p_from) { Ref bm =p_from; if (bm->get_size()==Size2()) { return Ref(); } PoolVector data; data.resize(bm->get_size().width*bm->get_size().height); { PoolVector::Write w=data.write(); for(int i=0;iget_size().width;i++) { for(int j=0;jget_size().height;j++) { if (bm->get_bit(Point2i(i,j))) { w[j*bm->get_size().width+i]=255; } else { w[j*bm->get_size().width+i]=0; } } } } Image img(bm->get_size().width,bm->get_size().height,0,Image::FORMAT_L8,data); int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size"); thumbnail_size*=EDSCALE; if (img.is_compressed()) { if (img.decompress()!=OK) return Ref(); } else if (img.get_format()!=Image::FORMAT_RGB8 && img.get_format()!=Image::FORMAT_RGBA8) { img.convert(Image::FORMAT_RGBA8); } int width,height; if (img.get_width() > thumbnail_size && img.get_width() >= img.get_height()) { width=thumbnail_size; height = img.get_height() * thumbnail_size / img.get_width(); } else if (img.get_height() > thumbnail_size && img.get_height() >= img.get_width()) { height=thumbnail_size; width = img.get_width() * thumbnail_size / img.get_height(); } else { width=img.get_width(); height=img.get_height(); } img.resize(width,height); Ref ptex = Ref( memnew( ImageTexture )); ptex->create_from_image(img,0); return ptex; } EditorBitmapPreviewPlugin::EditorBitmapPreviewPlugin() { } /////////////////////////////////////////////////////////////////////////// Ref EditorPackedScenePreviewPlugin::_gen_from_imd(Ref p_imd) { if (p_imd.is_null()) { return Ref(); } if (!p_imd->has_option("thumbnail")) return Ref(); Variant tn = p_imd->get_option("thumbnail"); //print_line(Variant::get_type_name(tn.get_type())); PoolVector thumbnail = tn; int len = thumbnail.size(); if (len==0) return Ref(); PoolVector::Read r = thumbnail.read(); Image img(r.ptr(),len); if (img.empty()) return Ref(); Ref ptex = Ref( memnew( ImageTexture )); ptex->create_from_image(img,0); return ptex; } bool EditorPackedScenePreviewPlugin::handles(const String& p_type) const { return ClassDB::is_type(p_type,"PackedScene"); } Ref EditorPackedScenePreviewPlugin::generate(const RES& p_from) { Ref imd = p_from->get_import_metadata(); return _gen_from_imd(imd); } Ref EditorPackedScenePreviewPlugin::generate_from_path(const String& p_path) { Ref imd = ResourceLoader::load_import_metadata(p_path); return _gen_from_imd(imd); } EditorPackedScenePreviewPlugin::EditorPackedScenePreviewPlugin() { } ////////////////////////////////////////////////////////////////// bool EditorMaterialPreviewPlugin::handles(const String& p_type) const { return ClassDB::is_type(p_type,"Material"); //any material } Ref EditorMaterialPreviewPlugin::generate(const RES& p_from) { Ref material = p_from; ERR_FAIL_COND_V(material.is_null(),Ref()); VS::get_singleton()->mesh_surface_set_material(sphere,0,material->get_rid()); VS::get_singleton()->viewport_queue_screen_capture(viewport); VS::get_singleton()->viewport_set_render_target_update_mode(viewport,VS::RENDER_TARGET_UPDATE_ONCE); //once used for capture // print_line("queue capture!"); Image img; int timeout=1000; while(timeout) { //print_line("try capture?"); OS::get_singleton()->delay_usec(10); img = VS::get_singleton()->viewport_get_screen_capture(viewport); if (!img.empty()) break; timeout--; } //print_line("captured!"); VS::get_singleton()->mesh_surface_set_material(sphere,0,RID()); int thumbnail_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size"); thumbnail_size*=EDSCALE; img.resize(thumbnail_size,thumbnail_size); Ref ptex = Ref( memnew( ImageTexture )); ptex->create_from_image(img,0); return ptex; } EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() { scenario = VS::get_singleton()->scenario_create(); viewport = VS::get_singleton()->viewport_create(); VS::get_singleton()->viewport_set_as_render_target(viewport,true); VS::get_singleton()->viewport_set_render_target_update_mode(viewport,VS::RENDER_TARGET_UPDATE_DISABLED); VS::get_singleton()->viewport_set_scenario(viewport,scenario); VS::ViewportRect vr; vr.x=0; vr.y=0; vr.width=128; vr.height=128; VS::get_singleton()->viewport_set_rect(viewport,vr); camera = VS::get_singleton()->camera_create(); VS::get_singleton()->viewport_attach_camera(viewport,camera); VS::get_singleton()->camera_set_transform(camera,Transform(Matrix3(),Vector3(0,0,3))); VS::get_singleton()->camera_set_perspective(camera,45,0.1,10); light = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL); light_instance = VS::get_singleton()->instance_create2(light,scenario); VS::get_singleton()->instance_set_transform(light_instance,Transform().looking_at(Vector3(-1,-1,-1),Vector3(0,1,0))); light2 = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL); VS::get_singleton()->light_set_color(light2,VS::LIGHT_COLOR_DIFFUSE,Color(0.7,0.7,0.7)); VS::get_singleton()->light_set_color(light2,VS::LIGHT_COLOR_SPECULAR,Color(0.0,0.0,0.0)); light_instance2 = VS::get_singleton()->instance_create2(light2,scenario); VS::get_singleton()->instance_set_transform(light_instance2,Transform().looking_at(Vector3(0,1,0),Vector3(0,0,1))); sphere = VS::get_singleton()->mesh_create(); sphere_instance = VS::get_singleton()->instance_create2(sphere,scenario); int lats=32; int lons=32; float radius=1.0; PoolVector vertices; PoolVector normals; PoolVector uvs; PoolVector tangents; Matrix3 tt = Matrix3(Vector3(0,1,0),Math_PI*0.5); for(int i = 1; i <= lats; i++) { double lat0 = Math_PI * (-0.5 + (double) (i - 1) / lats); double z0 = Math::sin(lat0); double zr0 = Math::cos(lat0); double lat1 = Math_PI * (-0.5 + (double) i / lats); double z1 = Math::sin(lat1); double zr1 = Math::cos(lat1); for(int j = lons; j >= 1; j--) { double lng0 = 2 * Math_PI * (double) (j - 1) / lons; double x0 = Math::cos(lng0); double y0 = Math::sin(lng0); double lng1 = 2 * Math_PI * (double) (j) / lons; double x1 = Math::cos(lng1); double y1 = Math::sin(lng1); Vector3 v[4]={ Vector3(x1 * zr0, z0, y1 *zr0), Vector3(x1 * zr1, z1, y1 *zr1), Vector3(x0 * zr1, z1, y0 *zr1), Vector3(x0 * zr0, z0, y0 *zr0) }; #define ADD_POINT(m_idx) \ normals.push_back(v[m_idx]);\ vertices.push_back(v[m_idx]*radius);\ { Vector2 uv(Math::atan2(v[m_idx].x,v[m_idx].z),Math::atan2(-v[m_idx].y,v[m_idx].z));\ uv/=Math_PI;\ uv*=4.0;\ uv=uv*0.5+Vector2(0.5,0.5);\ uvs.push_back(uv);\ }\ { Vector3 t = tt.xform(v[m_idx]);\ tangents.push_back(t.x);\ tangents.push_back(t.y);\ tangents.push_back(t.z);\ tangents.push_back(1.0);\ } ADD_POINT(0); ADD_POINT(1); ADD_POINT(2); ADD_POINT(2); ADD_POINT(3); ADD_POINT(0); } } Array arr; arr.resize(VS::ARRAY_MAX); arr[VS::ARRAY_VERTEX]=vertices; arr[VS::ARRAY_NORMAL]=normals; arr[VS::ARRAY_TANGENT]=tangents; arr[VS::ARRAY_TEX_UV]=uvs; VS::get_singleton()->mesh_add_surface(sphere,VS::PRIMITIVE_TRIANGLES,arr); } EditorMaterialPreviewPlugin::~EditorMaterialPreviewPlugin() { VS::get_singleton()->free(sphere); VS::get_singleton()->free(sphere_instance); VS::get_singleton()->free(viewport); VS::get_singleton()->free(light); VS::get_singleton()->free(light_instance); VS::get_singleton()->free(light2); VS::get_singleton()->free(light_instance2); VS::get_singleton()->free(camera); VS::get_singleton()->free(scenario); } /////////////////////////////////////////////////////////////////////////// static bool _is_text_char(CharType c) { return (c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='_'; } bool EditorScriptPreviewPlugin::handles(const String& p_type) const { return ClassDB::is_type(p_type,"Script"); } Ref EditorScriptPreviewPlugin::generate(const RES& p_from) { Ref