Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 37 additions & 18 deletions editor/inspector/editor_preview_plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "editor/file_system/editor_paths.h"
#include "editor/settings/editor_settings.h"
#include "editor/themes/editor_scale.h"
#include "scene/resources/3d/sky_material.h"
#include "scene/resources/atlas_texture.h"
#include "scene/resources/bit_map.h"
#include "scene/resources/font.h"
Expand Down Expand Up @@ -362,6 +363,7 @@ EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() {
RS::get_singleton()->viewport_set_scenario(viewport, scenario);
RS::get_singleton()->viewport_set_size(viewport, 128, 128);
RS::get_singleton()->viewport_set_transparent_background(viewport, true);
RS::get_singleton()->viewport_set_msaa_3d(viewport, RS::VIEWPORT_MSAA_4X);
RS::get_singleton()->viewport_set_active(viewport, true);
viewport_texture = RS::get_singleton()->viewport_get_texture(viewport);

Expand All @@ -370,10 +372,20 @@ EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() {
RS::get_singleton()->camera_set_transform(camera, Transform3D(Basis(), Vector3(0, 0, 3)));
RS::get_singleton()->camera_set_perspective(camera, 45, 0.1, 10);

sky = RS::get_singleton()->sky_create();
sky_material.instantiate();
RS::get_singleton()->sky_set_material(sky, sky_material->get_rid());

environment = RS::get_singleton()->environment_create();
RS::get_singleton()->environment_set_background(environment, RS::ENV_BG_SKY);
RS::get_singleton()->environment_set_sky(environment, sky);
RS::get_singleton()->camera_set_environment(camera, environment);

if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
camera_attributes = RS::get_singleton()->camera_attributes_create();
RS::get_singleton()->camera_attributes_set_exposure(camera_attributes, 1.0, 0.000032552); // Matches default CameraAttributesPhysical to work well with default DirectionalLight3Ds.
RS::get_singleton()->camera_set_camera_attributes(camera, camera_attributes);
RS::get_singleton()->environment_set_bg_energy(environment, 1.0f, 30000.0f);
}

light = RS::get_singleton()->directional_light_create();
Expand All @@ -391,9 +403,9 @@ EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() {
sphere = RS::get_singleton()->mesh_create();
sphere_instance = RS::get_singleton()->instance_create2(sphere, scenario);

int lats = 32;
int lats = 16;
int lons = 32;
const double lat_step = Math::TAU / lats;
const double lat_step = Math::PI / lats;
const double lon_step = Math::TAU / lons;
real_t radius = 1.0;

Expand Down Expand Up @@ -428,22 +440,27 @@ EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() {
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); \
#define ADD_POINT(m_idx) \
normals.push_back(v[m_idx]); \
vertices.push_back(v[m_idx] * radius); \
{ \
Vector2 uv; \
if (j >= lons / 2) { \
uv = Vector2(Math::atan2(-v[m_idx].x, -v[m_idx].z), Math::atan2(v[m_idx].y, -v[m_idx].z)); \
} else { \
uv = Vector2(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);
Expand Down Expand Up @@ -477,6 +494,8 @@ EditorMaterialPreviewPlugin::~EditorMaterialPreviewPlugin() {
RS::get_singleton()->free_rid(camera);
RS::get_singleton()->free_rid(camera_attributes);
RS::get_singleton()->free_rid(scenario);
RS::get_singleton()->free_rid(sky);
RS::get_singleton()->free_rid(environment);
}

///////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 4 additions & 0 deletions editor/inspector/editor_preview_plugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "editor/inspector/editor_resource_preview.h"

class ScriptLanguage;
class ProceduralSkyMaterial;

void post_process_preview(Ref<Image> p_image);

Expand Down Expand Up @@ -87,6 +88,9 @@ class EditorMaterialPreviewPlugin : public EditorResourcePreviewGenerator {
RID light_instance2;
RID camera;
RID camera_attributes;
RID environment;
RID sky;
Ref<ProceduralSkyMaterial> sky_material;
mutable DrawRequester draw_requester;

public:
Expand Down