2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* editor_preview_plugins.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:45:00 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 12:46:12 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2015-05-31 04:59:42 +00:00
|
|
|
#include "editor_preview_plugins.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "editor/editor_scale.h"
|
2017-03-18 22:45:45 +00:00
|
|
|
#include "editor/editor_settings.h"
|
2015-05-31 04:59:42 +00:00
|
|
|
#include "io/file_access_memory.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "io/resource_loader.h"
|
2015-05-31 04:59:42 +00:00
|
|
|
#include "os/os.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "scene/resources/bit_mask.h"
|
2015-05-31 04:59:42 +00:00
|
|
|
#include "scene/resources/material.h"
|
2015-06-01 00:13:24 +00:00
|
|
|
#include "scene/resources/mesh.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "scene/resources/sample.h"
|
2016-07-06 17:04:21 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool EditorTexturePreviewPlugin::handles(const String &p_type) const {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return (ObjectTypeDB::is_type(p_type, "ImageTexture") || ObjectTypeDB::is_type(p_type, "AtlasTexture"));
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<Texture> EditorTexturePreviewPlugin::generate(const RES &p_from) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2016-01-19 03:27:27 +00:00
|
|
|
Image img;
|
|
|
|
Ref<AtlasTexture> atex = p_from;
|
|
|
|
if (atex.is_valid()) {
|
|
|
|
Ref<ImageTexture> tex = atex->get_atlas();
|
|
|
|
if (!tex.is_valid()) {
|
|
|
|
return Ref<Texture>();
|
|
|
|
}
|
|
|
|
Image atlas = tex->get_data();
|
|
|
|
img = atlas.get_rect(atex->get_region());
|
2017-03-18 23:36:26 +00:00
|
|
|
} else {
|
2016-01-19 03:27:27 +00:00
|
|
|
Ref<ImageTexture> tex = p_from;
|
|
|
|
img = tex->get_data();
|
|
|
|
}
|
|
|
|
|
2015-05-31 04:59:42 +00:00
|
|
|
if (img.empty())
|
|
|
|
return Ref<Texture>();
|
|
|
|
|
|
|
|
img.clear_mipmaps();
|
|
|
|
|
|
|
|
int thumbnail_size = EditorSettings::get_singleton()->get("file_dialog/thumbnail_size");
|
2017-03-18 23:36:26 +00:00
|
|
|
thumbnail_size *= EDSCALE;
|
2015-05-31 04:59:42 +00:00
|
|
|
if (img.is_compressed()) {
|
2017-03-18 23:36:26 +00:00
|
|
|
if (img.decompress() != OK)
|
2015-05-31 04:59:42 +00:00
|
|
|
return Ref<Texture>();
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (img.get_format() != Image::FORMAT_RGB && img.get_format() != Image::FORMAT_RGBA) {
|
2015-05-31 04:59:42 +00:00
|
|
|
img.convert(Image::FORMAT_RGBA);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int width, height;
|
2015-05-31 04:59:42 +00:00
|
|
|
if (img.get_width() > thumbnail_size && img.get_width() >= img.get_height()) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
width = thumbnail_size;
|
2015-05-31 04:59:42 +00:00
|
|
|
height = img.get_height() * thumbnail_size / img.get_width();
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (img.get_height() > thumbnail_size && img.get_height() >= img.get_width()) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
height = thumbnail_size;
|
2015-05-31 04:59:42 +00:00
|
|
|
width = img.get_width() * thumbnail_size / img.get_height();
|
2017-03-18 23:36:26 +00:00
|
|
|
} else {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
width = img.get_width();
|
|
|
|
height = img.get_height();
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
img.resize(width, height);
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ptex->create_from_image(img, 0);
|
2015-05-31 04:59:42 +00:00
|
|
|
return ptex;
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorTexturePreviewPlugin::EditorTexturePreviewPlugin() {
|
2016-01-03 20:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool EditorBitmapPreviewPlugin::handles(const String &p_type) const {
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return ObjectTypeDB::is_type(p_type, "BitMap");
|
2016-01-03 20:14:28 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<Texture> EditorBitmapPreviewPlugin::generate(const RES &p_from) {
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<BitMap> bm = p_from;
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (bm->get_size() == Size2()) {
|
2016-01-03 20:14:28 +00:00
|
|
|
return Ref<Texture>();
|
|
|
|
}
|
|
|
|
|
|
|
|
DVector<uint8_t> data;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
data.resize(bm->get_size().width * bm->get_size().height);
|
2016-01-03 20:14:28 +00:00
|
|
|
|
|
|
|
{
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<uint8_t>::Write w = data.write();
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < bm->get_size().width; i++) {
|
|
|
|
for (int j = 0; j < bm->get_size().height; j++) {
|
|
|
|
if (bm->get_bit(Point2i(i, j))) {
|
|
|
|
w[j * bm->get_size().width + i] = 255;
|
2016-01-03 20:14:28 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
w[j * bm->get_size().width + i] = 0;
|
2016-01-03 20:14:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Image img(bm->get_size().width, bm->get_size().height, 0, Image::FORMAT_GRAYSCALE, data);
|
2016-01-03 20:14:28 +00:00
|
|
|
|
|
|
|
int thumbnail_size = EditorSettings::get_singleton()->get("file_dialog/thumbnail_size");
|
2017-03-18 23:36:26 +00:00
|
|
|
thumbnail_size *= EDSCALE;
|
2016-01-03 20:14:28 +00:00
|
|
|
if (img.is_compressed()) {
|
2017-03-18 23:36:26 +00:00
|
|
|
if (img.decompress() != OK)
|
2016-01-03 20:14:28 +00:00
|
|
|
return Ref<Texture>();
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (img.get_format() != Image::FORMAT_RGB && img.get_format() != Image::FORMAT_RGBA) {
|
2016-01-03 20:14:28 +00:00
|
|
|
img.convert(Image::FORMAT_RGBA);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int width, height;
|
2016-01-03 20:14:28 +00:00
|
|
|
if (img.get_width() > thumbnail_size && img.get_width() >= img.get_height()) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
width = thumbnail_size;
|
2016-01-03 20:14:28 +00:00
|
|
|
height = img.get_height() * thumbnail_size / img.get_width();
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (img.get_height() > thumbnail_size && img.get_height() >= img.get_width()) {
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
height = thumbnail_size;
|
2016-01-03 20:14:28 +00:00
|
|
|
width = img.get_width() * thumbnail_size / img.get_height();
|
2017-03-18 23:36:26 +00:00
|
|
|
} else {
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
width = img.get_width();
|
|
|
|
height = img.get_height();
|
2016-01-03 20:14:28 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
img.resize(width, height);
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
|
2016-01-03 20:14:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ptex->create_from_image(img, 0);
|
2016-01-03 20:14:28 +00:00
|
|
|
return ptex;
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorBitmapPreviewPlugin::EditorBitmapPreviewPlugin() {
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
Ref<Texture> EditorPackedScenePreviewPlugin::_gen_from_imd(Ref<ResourceImportMetadata> p_imd) {
|
|
|
|
|
2015-06-07 01:06:58 +00:00
|
|
|
if (p_imd.is_null()) {
|
2015-05-31 04:59:42 +00:00
|
|
|
return Ref<Texture>();
|
2015-06-07 01:06:58 +00:00
|
|
|
}
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
if (!p_imd->has_option("thumbnail"))
|
|
|
|
return Ref<Texture>();
|
|
|
|
|
|
|
|
Variant tn = p_imd->get_option("thumbnail");
|
2015-06-07 01:06:58 +00:00
|
|
|
//print_line(Variant::get_type_name(tn.get_type()));
|
2015-05-31 04:59:42 +00:00
|
|
|
DVector<uint8_t> thumbnail = tn;
|
|
|
|
|
|
|
|
int len = thumbnail.size();
|
2017-03-18 23:36:26 +00:00
|
|
|
if (len == 0)
|
2015-05-31 04:59:42 +00:00
|
|
|
return Ref<Texture>();
|
|
|
|
|
|
|
|
DVector<uint8_t>::Read r = thumbnail.read();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Image img(r.ptr(), len);
|
2015-05-31 04:59:42 +00:00
|
|
|
if (img.empty())
|
|
|
|
return Ref<Texture>();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
|
|
|
|
ptex->create_from_image(img, 0);
|
2015-05-31 04:59:42 +00:00
|
|
|
return ptex;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool EditorPackedScenePreviewPlugin::handles(const String &p_type) const {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return ObjectTypeDB::is_type(p_type, "PackedScene");
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<Texture> EditorPackedScenePreviewPlugin::generate(const RES &p_from) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
Ref<ResourceImportMetadata> imd = p_from->get_import_metadata();
|
|
|
|
return _gen_from_imd(imd);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<Texture> EditorPackedScenePreviewPlugin::generate_from_path(const String &p_path) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
Ref<ResourceImportMetadata> imd = ResourceLoader::load_import_metadata(p_path);
|
|
|
|
return _gen_from_imd(imd);
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorPackedScenePreviewPlugin::EditorPackedScenePreviewPlugin() {
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool EditorMaterialPreviewPlugin::handles(const String &p_type) const {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return ObjectTypeDB::is_type(p_type, "Material"); //any material
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<Texture> EditorMaterialPreviewPlugin::generate(const RES &p_from) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
Ref<Material> material = p_from;
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(material.is_null(), Ref<Texture>());
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
VS::get_singleton()->mesh_surface_set_material(sphere, 0, material->get_rid());
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
VS::get_singleton()->viewport_queue_screen_capture(viewport);
|
2017-03-18 23:36:26 +00:00
|
|
|
VS::get_singleton()->viewport_set_render_target_update_mode(viewport, VS::RENDER_TARGET_UPDATE_ONCE); //once used for capture
|
|
|
|
// print_line("queue capture!");
|
2015-05-31 04:59:42 +00:00
|
|
|
Image img;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int timeout = 1000;
|
|
|
|
while (timeout) {
|
2015-05-31 04:59:42 +00:00
|
|
|
//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!");
|
2017-03-18 23:36:26 +00:00
|
|
|
VS::get_singleton()->mesh_surface_set_material(sphere, 0, RID());
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
int thumbnail_size = EditorSettings::get_singleton()->get("file_dialog/thumbnail_size");
|
2017-03-18 23:36:26 +00:00
|
|
|
thumbnail_size *= EDSCALE;
|
|
|
|
img.resize(thumbnail_size, thumbnail_size);
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
|
|
|
|
ptex->create_from_image(img, 0);
|
2015-05-31 04:59:42 +00:00
|
|
|
return ptex;
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() {
|
|
|
|
|
|
|
|
scenario = VS::get_singleton()->scenario_create();
|
|
|
|
|
|
|
|
viewport = VS::get_singleton()->viewport_create();
|
2017-03-18 23:36:26 +00:00
|
|
|
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);
|
2015-05-31 04:59:42 +00:00
|
|
|
VS::ViewportRect vr;
|
2017-03-18 23:36:26 +00:00
|
|
|
vr.x = 0;
|
|
|
|
vr.y = 0;
|
|
|
|
vr.width = 128;
|
|
|
|
vr.height = 128;
|
|
|
|
VS::get_singleton()->viewport_set_rect(viewport, vr);
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
camera = VS::get_singleton()->camera_create();
|
2017-03-18 23:36:26 +00:00
|
|
|
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);
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
light = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL);
|
2017-03-18 23:36:26 +00:00
|
|
|
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)));
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
light2 = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL);
|
2017-03-18 23:36:26 +00:00
|
|
|
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);
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
VS::get_singleton()->instance_set_transform(light_instance2, Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
sphere = VS::get_singleton()->mesh_create();
|
2017-03-18 23:36:26 +00:00
|
|
|
sphere_instance = VS::get_singleton()->instance_create2(sphere, scenario);
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int lats = 32;
|
|
|
|
int lons = 32;
|
|
|
|
float radius = 1.0;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
DVector<Vector3> vertices;
|
|
|
|
DVector<Vector3> normals;
|
|
|
|
DVector<Vector2> uvs;
|
|
|
|
DVector<float> tangents;
|
2017-03-18 23:36:26 +00:00
|
|
|
Matrix3 tt = Matrix3(Vector3(0, 1, 0), Math_PI * 0.5);
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
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);
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
double lat1 = Math_PI * (-0.5 + (double)i / lats);
|
2015-05-31 04:59:42 +00:00
|
|
|
double z1 = Math::sin(lat1);
|
|
|
|
double zr1 = Math::cos(lat1);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = lons; j >= 1; j--) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
double lng0 = 2 * Math_PI * (double)(j - 1) / lons;
|
2015-05-31 04:59:42 +00:00
|
|
|
double x0 = Math::cos(lng0);
|
|
|
|
double y0 = Math::sin(lng0);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
double lng1 = 2 * Math_PI * (double)(j) / lons;
|
2015-05-31 04:59:42 +00:00
|
|
|
double x1 = Math::cos(lng1);
|
|
|
|
double y1 = Math::sin(lng1);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
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)
|
2015-05-31 04:59:42 +00:00
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
#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); \
|
|
|
|
}
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
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);
|
2017-03-18 23:36:26 +00:00
|
|
|
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);
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool EditorScriptPreviewPlugin::handles(const String &p_type) const {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return ObjectTypeDB::is_type(p_type, "Script");
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<Texture> EditorScriptPreviewPlugin::generate(const RES &p_from) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
Ref<Script> scr = p_from;
|
|
|
|
if (scr.is_null())
|
|
|
|
return Ref<Texture>();
|
|
|
|
|
|
|
|
String code = scr->get_source_code().strip_edges();
|
2017-03-18 23:36:26 +00:00
|
|
|
if (code == "")
|
2015-05-31 04:59:42 +00:00
|
|
|
return Ref<Texture>();
|
|
|
|
|
|
|
|
List<String> kwors;
|
|
|
|
scr->get_language()->get_reserved_words(&kwors);
|
|
|
|
|
|
|
|
Set<String> keywords;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (List<String>::Element *E = kwors.front(); E; E = E->next()) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
keywords.insert(E->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
int line = 0;
|
2017-03-18 23:36:26 +00:00
|
|
|
int col = 0;
|
2015-05-31 04:59:42 +00:00
|
|
|
int thumbnail_size = EditorSettings::get_singleton()->get("file_dialog/thumbnail_size");
|
2017-03-18 23:36:26 +00:00
|
|
|
thumbnail_size *= EDSCALE;
|
|
|
|
Image img(thumbnail_size, thumbnail_size, 0, Image::FORMAT_RGBA);
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
Color bg_color = EditorSettings::get_singleton()->get("text_editor/background_color");
|
2017-03-18 23:36:26 +00:00
|
|
|
bg_color.a = 1.0;
|
2015-05-31 04:59:42 +00:00
|
|
|
Color keyword_color = EditorSettings::get_singleton()->get("text_editor/keyword_color");
|
|
|
|
Color text_color = EditorSettings::get_singleton()->get("text_editor/text_color");
|
|
|
|
Color symbol_color = EditorSettings::get_singleton()->get("text_editor/symbol_color");
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < thumbnail_size; i++) {
|
|
|
|
for (int j = 0; j < thumbnail_size; j++) {
|
|
|
|
img.put_pixel(i, j, bg_color);
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool prev_is_text = false;
|
|
|
|
bool in_keyword = false;
|
|
|
|
for (int i = 0; i < code.length(); i++) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
CharType c = code[i];
|
2017-03-18 23:36:26 +00:00
|
|
|
if (c > 32) {
|
|
|
|
if (col < thumbnail_size) {
|
2015-05-31 04:59:42 +00:00
|
|
|
Color color = text_color;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (c != '_' && ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~') || c == '\t')) {
|
2015-05-31 04:59:42 +00:00
|
|
|
//make symbol a little visible
|
2017-03-18 23:36:26 +00:00
|
|
|
color = symbol_color;
|
|
|
|
in_keyword = false;
|
2015-05-31 04:59:42 +00:00
|
|
|
} else if (!prev_is_text && _is_text_char(c)) {
|
|
|
|
int pos = i;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (_is_text_char(code[pos])) {
|
2015-05-31 04:59:42 +00:00
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
///print_line("from "+itos(i)+" to "+itos(pos));
|
2017-03-18 23:36:26 +00:00
|
|
|
String word = code.substr(i, pos - i);
|
2015-05-31 04:59:42 +00:00
|
|
|
//print_line("found word: "+word);
|
|
|
|
if (keywords.has(word))
|
2017-03-18 23:36:26 +00:00
|
|
|
in_keyword = true;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
} else if (!_is_text_char(c)) {
|
2017-03-18 23:36:26 +00:00
|
|
|
in_keyword = false;
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (in_keyword)
|
2017-03-18 23:36:26 +00:00
|
|
|
color = keyword_color;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Color ul = color;
|
|
|
|
ul.a *= 0.5;
|
|
|
|
img.put_pixel(col, line * 2, bg_color.blend(ul));
|
|
|
|
img.put_pixel(col, line * 2 + 1, color);
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
prev_is_text = _is_text_char(c);
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
prev_is_text = false;
|
|
|
|
in_keyword = false;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (c == '\n') {
|
|
|
|
col = 0;
|
2015-05-31 04:59:42 +00:00
|
|
|
line++;
|
2017-03-18 23:36:26 +00:00
|
|
|
if (line >= thumbnail_size / 2)
|
2015-05-31 04:59:42 +00:00
|
|
|
break;
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (c == '\t') {
|
|
|
|
col += 3;
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
col++;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ptex->create_from_image(img, 0);
|
2015-05-31 04:59:42 +00:00
|
|
|
return ptex;
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorScriptPreviewPlugin::EditorScriptPreviewPlugin() {
|
|
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool EditorSamplePreviewPlugin::handles(const String &p_type) const {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return ObjectTypeDB::is_type(p_type, "Sample");
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<Texture> EditorSamplePreviewPlugin::generate(const RES &p_from) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<Sample> smp = p_from;
|
|
|
|
ERR_FAIL_COND_V(smp.is_null(), Ref<Texture>());
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
int thumbnail_size = EditorSettings::get_singleton()->get("file_dialog/thumbnail_size");
|
2017-03-18 23:36:26 +00:00
|
|
|
thumbnail_size *= EDSCALE;
|
2015-05-31 04:59:42 +00:00
|
|
|
DVector<uint8_t> img;
|
|
|
|
int w = thumbnail_size;
|
|
|
|
int h = thumbnail_size;
|
2017-03-18 23:36:26 +00:00
|
|
|
img.resize(w * h * 3);
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
DVector<uint8_t>::Write imgdata = img.write();
|
2017-03-18 23:36:26 +00:00
|
|
|
uint8_t *imgw = imgdata.ptr();
|
2015-05-31 04:59:42 +00:00
|
|
|
DVector<uint8_t> data = smp->get_data();
|
|
|
|
DVector<uint8_t>::Read sampledata = data.read();
|
2017-03-18 23:36:26 +00:00
|
|
|
const uint8_t *sdata = sampledata.ptr();
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
bool stereo = smp->is_stereo();
|
2017-03-18 23:36:26 +00:00
|
|
|
bool _16 = smp->get_format() == Sample::FORMAT_PCM16;
|
2015-05-31 04:59:42 +00:00
|
|
|
int len = smp->get_length();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (len < 1)
|
2015-05-31 04:59:42 +00:00
|
|
|
return Ref<Texture>();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (smp->get_format() == Sample::FORMAT_IMA_ADPCM) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
struct IMA_ADPCM_State {
|
|
|
|
|
|
|
|
int16_t step_index;
|
|
|
|
int32_t predictor;
|
|
|
|
/* values at loop point */
|
|
|
|
int16_t loop_step_index;
|
|
|
|
int32_t loop_predictor;
|
|
|
|
int32_t last_nibble;
|
|
|
|
int32_t loop_pos;
|
|
|
|
int32_t window_ofs;
|
|
|
|
const uint8_t *ptr;
|
|
|
|
} ima_adpcm;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ima_adpcm.step_index = 0;
|
|
|
|
ima_adpcm.predictor = 0;
|
|
|
|
ima_adpcm.loop_step_index = 0;
|
|
|
|
ima_adpcm.loop_predictor = 0;
|
|
|
|
ima_adpcm.last_nibble = -1;
|
|
|
|
ima_adpcm.loop_pos = 0x7FFFFFFF;
|
|
|
|
ima_adpcm.window_ofs = 0;
|
|
|
|
ima_adpcm.ptr = NULL;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < w; i++) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float max[2] = { -1e10, -1e10 };
|
|
|
|
float min[2] = { 1e10, 1e10 };
|
|
|
|
int from = i * len / w;
|
|
|
|
int to = (i + 1) * len / w;
|
|
|
|
if (to >= len)
|
|
|
|
to = len - 1;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = from; j < to; j++) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
while (j > ima_adpcm.last_nibble) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
static const int16_t _ima_adpcm_step_table[89] = {
|
|
|
|
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
|
|
|
|
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
|
|
|
|
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
|
|
|
|
130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
|
|
|
|
337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
|
|
|
|
876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
|
|
|
|
2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
|
|
|
|
5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
|
|
|
|
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int8_t _ima_adpcm_index_table[16] = {
|
|
|
|
-1, -1, -1, -1, 2, 4, 6, 8,
|
|
|
|
-1, -1, -1, -1, 2, 4, 6, 8
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int16_t nibble, diff, step;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
ima_adpcm.last_nibble++;
|
2017-03-18 23:36:26 +00:00
|
|
|
const uint8_t *src_ptr = sdata;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int ofs = ima_adpcm.last_nibble >> 1;
|
2015-11-09 03:49:18 +00:00
|
|
|
|
|
|
|
if (stereo)
|
2017-03-18 23:36:26 +00:00
|
|
|
ofs *= 2;
|
2015-11-09 03:49:18 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
nibble = (ima_adpcm.last_nibble & 1) ?
|
|
|
|
(src_ptr[ofs] >> 4) :
|
|
|
|
(src_ptr[ofs] & 0xF);
|
|
|
|
step = _ima_adpcm_step_table[ima_adpcm.step_index];
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
ima_adpcm.step_index += _ima_adpcm_index_table[nibble];
|
2017-03-18 23:36:26 +00:00
|
|
|
if (ima_adpcm.step_index < 0)
|
|
|
|
ima_adpcm.step_index = 0;
|
|
|
|
if (ima_adpcm.step_index > 88)
|
|
|
|
ima_adpcm.step_index = 88;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
diff = step >> 3;
|
2015-05-31 04:59:42 +00:00
|
|
|
if (nibble & 1)
|
2017-03-18 23:36:26 +00:00
|
|
|
diff += step >> 2;
|
2015-05-31 04:59:42 +00:00
|
|
|
if (nibble & 2)
|
2017-03-18 23:36:26 +00:00
|
|
|
diff += step >> 1;
|
2015-05-31 04:59:42 +00:00
|
|
|
if (nibble & 4)
|
2017-03-18 23:36:26 +00:00
|
|
|
diff += step;
|
2015-05-31 04:59:42 +00:00
|
|
|
if (nibble & 8)
|
2017-03-18 23:36:26 +00:00
|
|
|
diff = -diff;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ima_adpcm.predictor += diff;
|
|
|
|
if (ima_adpcm.predictor < -0x8000)
|
|
|
|
ima_adpcm.predictor = -0x8000;
|
|
|
|
else if (ima_adpcm.predictor > 0x7FFF)
|
|
|
|
ima_adpcm.predictor = 0x7FFF;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
/* store loop if there */
|
2017-03-18 23:36:26 +00:00
|
|
|
if (ima_adpcm.last_nibble == ima_adpcm.loop_pos) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
ima_adpcm.loop_step_index = ima_adpcm.step_index;
|
|
|
|
ima_adpcm.loop_predictor = ima_adpcm.predictor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float v = ima_adpcm.predictor / 32767.0;
|
|
|
|
if (v > max[0])
|
|
|
|
max[0] = v;
|
|
|
|
if (v < min[0])
|
|
|
|
min[0] = v;
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
max[0] *= 0.8;
|
|
|
|
min[0] *= 0.8;
|
|
|
|
|
|
|
|
for (int j = 0; j < h; j++) {
|
|
|
|
float v = (j / (float)h) * 2.0 - 1.0;
|
|
|
|
uint8_t *imgofs = &imgw[(uint64_t(j) * w + i) * 3];
|
|
|
|
if (v > min[0] && v < max[0]) {
|
|
|
|
imgofs[0] = 255;
|
|
|
|
imgofs[1] = 150;
|
|
|
|
imgofs[2] = 80;
|
2015-05-31 04:59:42 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
imgofs[0] = 0;
|
|
|
|
imgofs[1] = 0;
|
|
|
|
imgofs[2] = 0;
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < w; i++) {
|
2015-05-31 04:59:42 +00:00
|
|
|
// i trust gcc will optimize this loop
|
2017-03-18 23:36:26 +00:00
|
|
|
float max[2] = { -1e10, -1e10 };
|
|
|
|
float min[2] = { 1e10, 1e10 };
|
|
|
|
int c = stereo ? 2 : 1;
|
|
|
|
int from = uint64_t(i) * len / w;
|
|
|
|
int to = (uint64_t(i) + 1) * len / w;
|
|
|
|
if (to >= len)
|
|
|
|
to = len - 1;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
if (_16) {
|
2017-03-18 23:36:26 +00:00
|
|
|
const int16_t *src = (const int16_t *)sdata;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 0; j < c; j++) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int k = from; k <= to; k++) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float v = src[uint64_t(k) * c + j] / 32768.0;
|
|
|
|
if (v > max[j])
|
|
|
|
max[j] = v;
|
|
|
|
if (v < min[j])
|
|
|
|
min[j] = v;
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const int8_t *src = (const int8_t *)sdata;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 0; j < c; j++) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int k = from; k <= to; k++) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float v = src[uint64_t(k) * c + j] / 128.0;
|
|
|
|
if (v > max[j])
|
|
|
|
max[j] = v;
|
|
|
|
if (v < min[j])
|
|
|
|
min[j] = v;
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
max[0] *= 0.8;
|
|
|
|
max[1] *= 0.8;
|
|
|
|
min[0] *= 0.8;
|
|
|
|
min[1] *= 0.8;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
if (!stereo) {
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 0; j < h; j++) {
|
|
|
|
float v = (j / (float)h) * 2.0 - 1.0;
|
|
|
|
uint8_t *imgofs = &imgw[(j * w + i) * 3];
|
|
|
|
if (v > min[0] && v < max[0]) {
|
|
|
|
imgofs[0] = 255;
|
|
|
|
imgofs[1] = 150;
|
|
|
|
imgofs[2] = 80;
|
2015-05-31 04:59:42 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
imgofs[0] = 0;
|
|
|
|
imgofs[1] = 0;
|
|
|
|
imgofs[2] = 0;
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 0; j < h; j++) {
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2016-07-06 17:04:21 +00:00
|
|
|
int half;
|
2015-05-31 04:59:42 +00:00
|
|
|
float v;
|
2017-03-18 23:36:26 +00:00
|
|
|
if (j < (h / 2)) {
|
|
|
|
half = 0;
|
|
|
|
v = (j / (float)(h / 2)) * 2.0 - 1.0;
|
2015-05-31 04:59:42 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
half = 1;
|
|
|
|
if ((float)(h / 2) != 0) {
|
|
|
|
v = ((j - (h / 2)) / (float)(h / 2)) * 2.0 - 1.0;
|
2016-02-29 23:08:33 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
v = ((j - (h / 2)) / (float)(1 / 2)) * 2.0 - 1.0;
|
2016-02-29 23:08:33 +00:00
|
|
|
}
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
uint8_t *imgofs = &imgw[(j * w + i) * 3];
|
|
|
|
if (v > min[half] && v < max[half]) {
|
|
|
|
imgofs[0] = 255;
|
|
|
|
imgofs[1] = 150;
|
|
|
|
imgofs[2] = 80;
|
2015-05-31 04:59:42 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
imgofs[0] = 0;
|
|
|
|
imgofs[1] = 0;
|
|
|
|
imgofs[2] = 0;
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
imgdata = DVector<uint8_t>::Write();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
|
|
|
|
ptex->create_from_image(Image(w, h, 0, Image::FORMAT_RGB, img), 0);
|
2015-05-31 04:59:42 +00:00
|
|
|
return ptex;
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorSamplePreviewPlugin::EditorSamplePreviewPlugin() {
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2015-06-01 00:13:24 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool EditorMeshPreviewPlugin::handles(const String &p_type) const {
|
2015-06-01 00:13:24 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return ObjectTypeDB::is_type(p_type, "Mesh"); //any Mesh
|
2015-06-01 00:13:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<Texture> EditorMeshPreviewPlugin::generate(const RES &p_from) {
|
2015-06-01 00:13:24 +00:00
|
|
|
|
|
|
|
Ref<Mesh> mesh = p_from;
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(mesh.is_null(), Ref<Texture>());
|
2015-06-01 00:13:24 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
VS::get_singleton()->instance_set_base(mesh_instance, mesh->get_rid());
|
2015-06-01 00:13:24 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
AABB aabb = mesh->get_aabb();
|
|
|
|
Vector3 ofs = aabb.pos + aabb.size * 0.5;
|
|
|
|
aabb.pos -= ofs;
|
2015-06-01 00:13:24 +00:00
|
|
|
Transform xform;
|
2017-03-18 23:36:26 +00:00
|
|
|
xform.basis = Matrix3().rotated(Vector3(0, 1, 0), Math_PI * 0.125);
|
|
|
|
xform.basis = Matrix3().rotated(Vector3(1, 0, 0), -Math_PI * 0.125) * xform.basis;
|
2015-06-01 00:13:24 +00:00
|
|
|
AABB rot_aabb = xform.xform(aabb);
|
2017-03-18 23:36:26 +00:00
|
|
|
float m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5;
|
|
|
|
if (m == 0)
|
2015-06-01 00:13:24 +00:00
|
|
|
return Ref<Texture>();
|
2017-03-18 23:36:26 +00:00
|
|
|
m = 1.0 / m;
|
|
|
|
m *= 0.5;
|
2015-06-01 00:13:24 +00:00
|
|
|
//print_line("scale: "+rtos(m));
|
2017-03-18 23:36:26 +00:00
|
|
|
xform.basis.scale(Vector3(m, m, m));
|
|
|
|
xform.origin = -xform.basis.xform(ofs); //-ofs*m;
|
|
|
|
xform.origin.z -= rot_aabb.size.z * 2;
|
|
|
|
VS::get_singleton()->instance_set_transform(mesh_instance, xform);
|
2015-06-01 00:13:24 +00:00
|
|
|
|
|
|
|
VS::get_singleton()->viewport_queue_screen_capture(viewport);
|
2017-03-18 23:36:26 +00:00
|
|
|
VS::get_singleton()->viewport_set_render_target_update_mode(viewport, VS::RENDER_TARGET_UPDATE_ONCE); //once used for capture
|
|
|
|
// print_line("queue capture!");
|
2015-06-01 00:13:24 +00:00
|
|
|
Image img;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int timeout = 1000;
|
|
|
|
while (timeout) {
|
2015-06-01 00:13:24 +00:00
|
|
|
//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!");
|
2017-03-18 23:36:26 +00:00
|
|
|
VS::get_singleton()->instance_set_base(mesh_instance, RID());
|
2015-06-01 00:13:24 +00:00
|
|
|
|
|
|
|
int thumbnail_size = EditorSettings::get_singleton()->get("file_dialog/thumbnail_size");
|
2017-03-18 23:36:26 +00:00
|
|
|
thumbnail_size *= EDSCALE;
|
|
|
|
img.resize(thumbnail_size, thumbnail_size);
|
2015-06-01 00:13:24 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
|
|
|
|
ptex->create_from_image(img, 0);
|
2015-06-01 00:13:24 +00:00
|
|
|
return ptex;
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorMeshPreviewPlugin::EditorMeshPreviewPlugin() {
|
|
|
|
|
|
|
|
scenario = VS::get_singleton()->scenario_create();
|
|
|
|
viewport = VS::get_singleton()->viewport_create();
|
2017-03-18 23:36:26 +00:00
|
|
|
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);
|
2015-06-01 00:13:24 +00:00
|
|
|
VS::ViewportRect vr;
|
2017-03-18 23:36:26 +00:00
|
|
|
vr.x = 0;
|
|
|
|
vr.y = 0;
|
|
|
|
vr.width = 128;
|
|
|
|
vr.height = 128;
|
|
|
|
VS::get_singleton()->viewport_set_rect(viewport, vr);
|
2015-06-01 00:13:24 +00:00
|
|
|
|
|
|
|
camera = VS::get_singleton()->camera_create();
|
2017-03-18 23:36:26 +00:00
|
|
|
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);
|
|
|
|
VS::get_singleton()->camera_set_orthogonal(camera, 1.0, 0.01, 1000.0);
|
2015-06-01 00:13:24 +00:00
|
|
|
|
|
|
|
light = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL);
|
2017-03-18 23:36:26 +00:00
|
|
|
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)));
|
2015-06-01 00:13:24 +00:00
|
|
|
|
|
|
|
light2 = VS::get_singleton()->light_create(VS::LIGHT_DIRECTIONAL);
|
2017-03-18 23:36:26 +00:00
|
|
|
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);
|
2015-06-01 00:13:24 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
VS::get_singleton()->instance_set_transform(light_instance2, Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
|
2015-06-01 00:13:24 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
// sphere = VS::get_singleton()->mesh_create();
|
2015-06-01 00:13:24 +00:00
|
|
|
mesh_instance = VS::get_singleton()->instance_create();
|
2017-03-18 23:36:26 +00:00
|
|
|
VS::get_singleton()->instance_set_scenario(mesh_instance, scenario);
|
2015-06-01 00:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EditorMeshPreviewPlugin::~EditorMeshPreviewPlugin() {
|
|
|
|
|
|
|
|
//VS::get_singleton()->free(sphere);
|
|
|
|
VS::get_singleton()->free(mesh_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);
|
|
|
|
}
|