Debian Codegraph @

editor/scene/material_editor_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* material_editor_plugin.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "material_editor_plugin.h"
32
33 #include "core/config/project_settings.h"
34 #include "editor/editor_node.h"
35 #include "editor/editor_string_names.h"
36 #include "editor/editor_undo_redo_manager.h"
37 #include "editor/settings/editor_settings.h"
38 #include "editor/themes/editor_scale.h"
39 #include "scene/gui/box_container.h"
40 #include "scene/gui/button.h"
41 #include "scene/gui/color_rect.h"
42 #include "scene/gui/label.h"
43 #include "scene/gui/subviewport_container.h"
44 #include "scene/main/viewport.h"
45 #include "scene/resources/canvas_item_material.h"
46 #include "scene/resources/particle_process_material.h"
47
48 // 3D.
49 #include "scene/3d/camera_3d.h"
50 #include "scene/3d/light_3d.h"
51 #include "scene/3d/mesh_instance_3d.h"
52
53 Ref<ShaderMaterial> MaterialEditor::make_shader_material(const Ref<Material> &p_from, bool p_copy_params) {
54 ERR_FAIL_COND_V(p_from.is_null(), Ref<ShaderMaterial>());
55
56 Ref<ShaderMaterial> smat;
57 smat.instantiate();
58
59 Ref<Shader> shader;
60 shader.instantiate();
61
62 String code = RS::get_singleton()->shader_get_code(p_from->get_shader_rid());
63 shader->set_code(code);
64 smat->set_shader(shader);
65
66 if (p_copy_params) {
67 List<PropertyInfo> params;
68 RS::get_singleton()->get_shader_parameter_list(p_from->get_shader_rid(), &params);
69
70 for (const PropertyInfo &E : params) {
71 Variant value = RS::get_singleton()->material_get_param(p_from->get_rid(), E.name);
72 smat->set_shader_parameter(E.name, value);
73 }
74 }
75
76 smat->set_render_priority(p_from->get_render_priority());
77 smat->set_local_to_scene(p_from->is_local_to_scene());
78 smat->set_name(p_from->get_name());
79 return smat;
80 }
81
82 void MaterialEditor::gui_input(const Ref<InputEvent> &p_event) {
83 ERR_FAIL_COND(p_event.is_null());
84
85 Ref<InputEventMouseMotion> mm = p_event;
86 if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
87 rot.x -= mm->get_relative().y * 0.01;
88 rot.y -= mm->get_relative().x * 0.01;
89 if (quad_instance->is_visible()) {
90 // Clamp rotation so the quad is always visible.
91 const real_t limit = Math::deg_to_rad(80.0);
92 rot = rot.clampf(-limit, limit);
93 } else {
94 rot.x = CLAMP(rot.x, -Math::PI / 2, Math::PI / 2);
95 }
96 _update_rotation();
97 _store_rotation_metadata();
98 }
99 }
100
101 void MaterialEditor::_update_theme_item_cache() {
102 Control::_update_theme_item_cache();
103
104 theme_cache.light_1_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight1"));
105 theme_cache.light_2_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight2"));
106
107 theme_cache.sphere_icon = get_editor_theme_icon(SNAME("MaterialPreviewSphere"));
108 theme_cache.box_icon = get_editor_theme_icon(SNAME("MaterialPreviewCube"));
109 theme_cache.quad_icon = get_editor_theme_icon(SNAME("MaterialPreviewQuad"));
110
111 theme_cache.checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));
112 }
113
114 void MaterialEditor::_notification(int p_what) {
115 switch (p_what) {
116 case NOTIFICATION_THEME_CHANGED: {
117 light_1_switch->set_button_icon(theme_cache.light_1_icon);
118 light_2_switch->set_button_icon(theme_cache.light_2_icon);
119
120 sphere_switch->set_button_icon(theme_cache.sphere_icon);
121 box_switch->set_button_icon(theme_cache.box_icon);
122 quad_switch->set_button_icon(theme_cache.quad_icon);
123
124 error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
125 } break;
126
127 case NOTIFICATION_DRAW: {
128 if (!is_unsupported_shader_mode) {
129 Size2 size = get_size();
130 draw_texture_rect(theme_cache.checkerboard, Rect2(Point2(), size), true);
131 }
132 } break;
133 }
134 }
135
136 void MaterialEditor::_set_rotation(real_t p_x_degrees, real_t p_y_degrees) {
137 rot.x = Math::deg_to_rad(p_x_degrees);
138 rot.y = Math::deg_to_rad(p_y_degrees);
139 _update_rotation();
140 }
141
142 // Store the rotation so it can persist when switching between materials.
143 void MaterialEditor::_store_rotation_metadata() {
144 Vector2 rotation_degrees = Vector2(Math::rad_to_deg(rot.x), Math::rad_to_deg(rot.y));
145 EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_rotation", rotation_degrees);
146 }
147
148 void MaterialEditor::_update_rotation() {
149 Transform3D t;
150 t.basis.rotate(Vector3(0, 1, 0), -rot.y);
151 t.basis.rotate(Vector3(1, 0, 0), -rot.x);
152 rotation->set_transform(t);
153 }
154
155 void MaterialEditor::edit(Ref<Material> p_material, const Ref<Environment> &p_env) {
156 material = p_material;
157 camera->set_environment(p_env);
158
159 is_unsupported_shader_mode = false;
160 if (material.is_valid()) {
161 Shader::Mode mode = p_material->get_shader_mode();
162 switch (mode) {
163 case Shader::MODE_CANVAS_ITEM:
164 layout_error->hide();
165 layout_3d->hide();
166 layout_2d->show();
167 rect_instance->set_material(material);
168 vc->hide();
169 break;
170 case Shader::MODE_SPATIAL:
171 layout_error->hide();
172 layout_2d->hide();
173 layout_3d->show();
174 sphere_instance->set_material_override(material);
175 box_instance->set_material_override(material);
176 quad_instance->set_material_override(material);
177 vc->show();
178 break;
179 default:
180 layout_error->show();
181 layout_2d->hide();
182 layout_3d->hide();
183 is_unsupported_shader_mode = true;
184 vc->hide();
185 break;
186 }
187 } else {
188 hide();
189 }
190 }
191
192 void MaterialEditor::_on_light_1_switch_pressed() {
193 light1->set_visible(light_1_switch->is_pressed());
194 }
195
196 void MaterialEditor::_on_light_2_switch_pressed() {
197 light2->set_visible(light_2_switch->is_pressed());
198 }
199
200 void MaterialEditor::_on_sphere_switch_pressed() {
201 sphere_instance->show();
202 box_instance->hide();
203 quad_instance->hide();
204 box_switch->set_pressed(false);
205 quad_switch->set_pressed(false);
206 _set_rotation(-15.0, 30.0);
207 _store_rotation_metadata();
208 EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_mesh", "sphere");
209 }
210
211 void MaterialEditor::_on_box_switch_pressed() {
212 sphere_instance->hide();
213 box_instance->show();
214 quad_instance->hide();
215 sphere_switch->set_pressed(false);
216 quad_switch->set_pressed(false);
217 _set_rotation(-15.0, 30.0);
218 _store_rotation_metadata();
219 EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_mesh", "box");
220 }
221
222 void MaterialEditor::_on_quad_switch_pressed() {
223 sphere_instance->hide();
224 box_instance->hide();
225 quad_instance->show();
226 sphere_switch->set_pressed(false);
227 box_switch->set_pressed(false);
228 _set_rotation(0.0, 0.0);
229 _store_rotation_metadata();
230 EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_mesh", "quad");
231 }
232
233 MaterialEditor::MaterialEditor() {
234 set_custom_minimum_size(Size2(1, 150) * EDSCALE);
235
236 // Canvas item
237
238 vc_2d = memnew(SubViewportContainer);
239 vc_2d->set_stretch(true);
240 add_child(vc_2d);
241 vc_2d->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
242
243 viewport_2d = memnew(SubViewport);
244 vc_2d->add_child(viewport_2d);
245 viewport_2d->set_disable_input(true);
246 viewport_2d->set_transparent_background(true);
247
248 layout_2d = memnew(HBoxContainer);
249 layout_2d->set_alignment(BoxContainer::ALIGNMENT_CENTER);
250 viewport_2d->add_child(layout_2d);
251 layout_2d->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
252
253 rect_instance = memnew(ColorRect);
254 layout_2d->add_child(rect_instance);
255 rect_instance->set_custom_minimum_size(Size2(150, 150) * EDSCALE);
256
257 layout_2d->set_visible(false);
258
259 layout_error = memnew(VBoxContainer);
260 layout_error->set_alignment(BoxContainer::ALIGNMENT_CENTER);
261 layout_error->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
262
263 error_label = memnew(Label);
264 error_label->set_focus_mode(FOCUS_ACCESSIBILITY);
265 error_label->set_text(TTR("Preview is not available for this shader mode."));
266 error_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
267 error_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
268 error_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
269
270 layout_error->add_child(error_label);
271 layout_error->hide();
272 add_child(layout_error);
273
274 // Spatial
275
276 vc = memnew(SubViewportContainer);
277 vc->set_stretch(true);
278 add_child(vc);
279 vc->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
280 viewport = memnew(SubViewport);
281 Ref<World3D> world_3d;
282 world_3d.instantiate();
283 viewport->set_world_3d(world_3d); // Use own world.
284 vc->add_child(viewport);
285 viewport->set_disable_input(true);
286 viewport->set_transparent_background(true);
287 viewport->set_msaa_3d(Viewport::MSAA_4X);
288
289 camera = memnew(Camera3D);
290 camera->set_transform(Transform3D(Basis(), Vector3(0, 0, 1.1)));
291 // Use low field of view so the sphere/box/quad is fully encompassed within the preview,
292 // without much distortion.
293 camera->set_perspective(20, 0.1, 10);
294 camera->make_current();
295 if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
296 camera_attributes.instantiate();
297 camera->set_attributes(camera_attributes);
298 }
299 viewport->add_child(camera);
300
301 light1 = memnew(DirectionalLight3D);
302 light1->set_transform(Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
303 viewport->add_child(light1);
304
305 light2 = memnew(DirectionalLight3D);
306 light2->set_transform(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
307 light2->set_color(Color(0.7, 0.7, 0.7));
308 viewport->add_child(light2);
309
310 rotation = memnew(Node3D);
311 viewport->add_child(rotation);
312
313 sphere_instance = memnew(MeshInstance3D);
314 rotation->add_child(sphere_instance);
315
316 box_instance = memnew(MeshInstance3D);
317 rotation->add_child(box_instance);
318
319 quad_instance = memnew(MeshInstance3D);
320 rotation->add_child(quad_instance);
321
322 sphere_instance->set_transform(Transform3D() * 0.375);
323 box_instance->set_transform(Transform3D() * 0.25);
324 quad_instance->set_transform(Transform3D() * 0.375);
325
326 sphere_mesh.instantiate();
327 sphere_instance->set_mesh(sphere_mesh);
328 box_mesh.instantiate();
329 box_instance->set_mesh(box_mesh);
330 quad_mesh.instantiate();
331 quad_instance->set_mesh(quad_mesh);
332
333 layout_3d = memnew(HBoxContainer);
334 add_child(layout_3d);
335 layout_3d->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 2);
336
337 VBoxContainer *vb_shape = memnew(VBoxContainer);
338 layout_3d->add_child(vb_shape);
339
340 sphere_switch = memnew(Button);
341 sphere_switch->set_theme_type_variation("PreviewLightButton");
342 sphere_switch->set_toggle_mode(true);
343 sphere_switch->set_accessibility_name(TTRC("Sphere"));
344 vb_shape->add_child(sphere_switch);
345 sphere_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_sphere_switch_pressed));
346
347 box_switch = memnew(Button);
348 box_switch->set_theme_type_variation("PreviewLightButton");
349 box_switch->set_toggle_mode(true);
350 box_switch->set_accessibility_name(TTRC("Box"));
351 vb_shape->add_child(box_switch);
352 box_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_box_switch_pressed));
353
354 quad_switch = memnew(Button);
355 quad_switch->set_theme_type_variation("PreviewLightButton");
356 quad_switch->set_toggle_mode(true);
357 quad_switch->set_accessibility_name(TTRC("Quad"));
358 vb_shape->add_child(quad_switch);
359 quad_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_quad_switch_pressed));
360
361 layout_3d->add_spacer();
362
363 VBoxContainer *vb_light = memnew(VBoxContainer);
364 layout_3d->add_child(vb_light);
365
366 light_1_switch = memnew(Button);
367 light_1_switch->set_theme_type_variation("PreviewLightButton");
368 light_1_switch->set_toggle_mode(true);
369 light_1_switch->set_pressed(true);
370 light_1_switch->set_accessibility_name(TTRC("First Light"));
371 vb_light->add_child(light_1_switch);
372 light_1_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_light_1_switch_pressed));
373
374 light_2_switch = memnew(Button);
375 light_2_switch->set_theme_type_variation("PreviewLightButton");
376 light_2_switch->set_toggle_mode(true);
377 light_2_switch->set_pressed(true);
378 light_2_switch->set_accessibility_name(TTRC("Second Light"));
379 vb_light->add_child(light_2_switch);
380 light_2_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_light_2_switch_pressed));
381
382 String shape = EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_mesh", "sphere");
383 if (shape == "sphere") {
384 box_instance->hide();
385 quad_instance->hide();
386 sphere_switch->set_pressed_no_signal(true);
387 } else if (shape == "box") {
388 sphere_instance->hide();
389 quad_instance->hide();
390 box_switch->set_pressed_no_signal(true);
391 } else {
392 sphere_instance->hide();
393 box_instance->hide();
394 quad_switch->set_pressed_no_signal(true);
395 }
396
397 Vector2 stored_rot = EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_rotation", Vector2());
398 _set_rotation(stored_rot.x, stored_rot.y);
399 }
400
401 ///////////////////////
402
403 bool EditorInspectorPluginMaterial::can_handle(Object *p_object) {
404 Material *material = Object::cast_to<Material>(p_object);
405 if (!material) {
406 return false;
407 }
408 Shader::Mode mode = material->get_shader_mode();
409 return mode == Shader::MODE_SPATIAL || mode == Shader::MODE_CANVAS_ITEM;
410 }
411
412 void EditorInspectorPluginMaterial::parse_begin(Object *p_object) {
413 Material *material = Object::cast_to<Material>(p_object);
414 if (!material) {
415 return;
416 }
417 Ref<Material> m(material);
418
419 MaterialEditor *editor = memnew(MaterialEditor);
420 editor->edit(m, env);
421 add_custom_control(editor);
422 }
423
424 void EditorInspectorPluginMaterial::_undo_redo_inspector_callback(Object *p_undo_redo, Object *p_edited, const String &p_property, const Variant &p_new_value) {
425 EditorUndoRedoManager *undo_redo = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
426 ERR_FAIL_NULL(undo_redo);
427
428 // For BaseMaterial3D, if a roughness or metallic textures is being assigned to an empty slot,
429 // set the respective metallic or roughness factor to 1.0 as a convenience feature
430 BaseMaterial3D *base_material = Object::cast_to<StandardMaterial3D>(p_edited);
431 if (base_material) {
432 Texture2D *texture = Object::cast_to<Texture2D>(p_new_value);
433 if (texture) {
434 if (p_property == "roughness_texture") {
435 if (base_material->get_texture(StandardMaterial3D::TEXTURE_ROUGHNESS).is_null()) {
436 undo_redo->add_do_property(p_edited, "roughness", 1.0);
437
438 bool valid = false;
439 Variant value = p_edited->get("roughness", &valid);
440 if (valid) {
441 undo_redo->add_undo_property(p_edited, "roughness", value);
442 }
443 }
444 } else if (p_property == "metallic_texture") {
445 if (base_material->get_texture(StandardMaterial3D::TEXTURE_METALLIC).is_null()) {
446 undo_redo->add_do_property(p_edited, "metallic", 1.0);
447
448 bool valid = false;
449 Variant value = p_edited->get("metallic", &valid);
450 if (valid) {
451 undo_redo->add_undo_property(p_edited, "metallic", value);
452 }
453 }
454 }
455 }
456 }
457 }
458
459 EditorInspectorPluginMaterial::EditorInspectorPluginMaterial() {
460 env.instantiate();
461 Ref<Sky> sky = memnew(Sky());
462 env->set_sky(sky);
463 env->set_background(Environment::BG_COLOR);
464 env->set_ambient_source(Environment::AMBIENT_SOURCE_SKY);
465 env->set_reflection_source(Environment::REFLECTION_SOURCE_SKY);
466
467 EditorNode::get_editor_data().add_undo_redo_inspector_hook_callback(callable_mp(this, &EditorInspectorPluginMaterial::_undo_redo_inspector_callback));
468 }
469
470 MaterialEditorPlugin::MaterialEditorPlugin() {
471 Ref<EditorInspectorPluginMaterial> plugin;
472 plugin.instantiate();
473 add_inspector_plugin(plugin);
474 }
475
476 String ParticleProcessMaterialConversionPlugin::converts_to() const {
477 return "ShaderMaterial";
478 }
479
480 bool ParticleProcessMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
481 Ref<ParticleProcessMaterial> mat = p_resource;
482 return mat.is_valid();
483 }
484
485 Ref<Resource> ParticleProcessMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
486 return MaterialEditor::make_shader_material(p_resource);
487 }
488
489 String CanvasItemMaterialConversionPlugin::converts_to() const {
490 return "ShaderMaterial";
491 }
492
493 bool CanvasItemMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
494 Ref<CanvasItemMaterial> mat = p_resource;
495 return mat.is_valid();
496 }
497
498 Ref<Resource> CanvasItemMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
499 ERR_FAIL_COND_V(!Object::cast_to<CanvasItemMaterial>(*p_resource) || !Object::cast_to<CanvasItemMaterial>(*p_resource)->_is_initialized(), Ref<CanvasItemMaterial>());
500 return MaterialEditor::make_shader_material(p_resource);
501 }