Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/material_editor_plugin.cpp
20837 views
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/blit_material.h"
46
#include "scene/resources/canvas_item_material.h"
47
#include "scene/resources/particle_process_material.h"
48
#include "scene/resources/sky.h"
49
50
// 3D.
51
#include "scene/3d/camera_3d.h"
52
#include "scene/3d/light_3d.h"
53
#include "scene/3d/mesh_instance_3d.h"
54
55
Ref<ShaderMaterial> MaterialEditor::make_shader_material(const Ref<Material> &p_from, bool p_copy_params) {
56
ERR_FAIL_COND_V(p_from.is_null(), Ref<ShaderMaterial>());
57
58
Ref<ShaderMaterial> smat;
59
smat.instantiate();
60
61
Ref<Shader> shader;
62
shader.instantiate();
63
64
String code = RS::get_singleton()->shader_get_code(p_from->get_shader_rid());
65
shader->set_code(code);
66
smat->set_shader(shader);
67
68
if (p_copy_params) {
69
List<PropertyInfo> params;
70
RS::get_singleton()->get_shader_parameter_list(p_from->get_shader_rid(), &params);
71
72
for (const PropertyInfo &E : params) {
73
Variant value = RS::get_singleton()->material_get_param(p_from->get_rid(), E.name);
74
smat->set_shader_parameter(E.name, value);
75
}
76
}
77
78
smat->set_render_priority(p_from->get_render_priority());
79
smat->set_local_to_scene(p_from->is_local_to_scene());
80
smat->set_name(p_from->get_name());
81
return smat;
82
}
83
84
void MaterialEditor::gui_input(const Ref<InputEvent> &p_event) {
85
ERR_FAIL_COND(p_event.is_null());
86
87
Ref<InputEventMouseMotion> mm = p_event;
88
if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
89
rot.x -= mm->get_relative().y * 0.01;
90
rot.y -= mm->get_relative().x * 0.01;
91
if (quad_instance->is_visible()) {
92
// Clamp rotation so the quad is always visible.
93
const real_t limit = Math::deg_to_rad(80.0);
94
rot = rot.clampf(-limit, limit);
95
} else {
96
rot.x = CLAMP(rot.x, -Math::PI / 2, Math::PI / 2);
97
}
98
_update_rotation();
99
_store_rotation_metadata();
100
}
101
}
102
103
void MaterialEditor::_update_theme_item_cache() {
104
Control::_update_theme_item_cache();
105
106
theme_cache.light_1_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight1"));
107
theme_cache.light_2_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight2"));
108
109
theme_cache.sphere_icon = get_editor_theme_icon(SNAME("MaterialPreviewSphere"));
110
theme_cache.box_icon = get_editor_theme_icon(SNAME("MaterialPreviewCube"));
111
theme_cache.quad_icon = get_editor_theme_icon(SNAME("MaterialPreviewQuad"));
112
113
theme_cache.checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));
114
}
115
116
void MaterialEditor::_notification(int p_what) {
117
switch (p_what) {
118
case NOTIFICATION_THEME_CHANGED: {
119
light_1_switch->set_button_icon(theme_cache.light_1_icon);
120
light_2_switch->set_button_icon(theme_cache.light_2_icon);
121
122
sphere_switch->set_button_icon(theme_cache.sphere_icon);
123
box_switch->set_button_icon(theme_cache.box_icon);
124
quad_switch->set_button_icon(theme_cache.quad_icon);
125
126
error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
127
} break;
128
129
case NOTIFICATION_DRAW: {
130
if (!is_unsupported_shader_mode) {
131
Size2 size = get_size();
132
draw_texture_rect(theme_cache.checkerboard, Rect2(Point2(), size), true);
133
}
134
} break;
135
}
136
}
137
138
void MaterialEditor::_set_rotation(real_t p_x_degrees, real_t p_y_degrees) {
139
rot.x = Math::deg_to_rad(p_x_degrees);
140
rot.y = Math::deg_to_rad(p_y_degrees);
141
_update_rotation();
142
}
143
144
// Store the rotation so it can persist when switching between materials.
145
void MaterialEditor::_store_rotation_metadata() {
146
Vector2 rotation_degrees = Vector2(Math::rad_to_deg(rot.x), Math::rad_to_deg(rot.y));
147
EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_rotation", rotation_degrees);
148
}
149
150
void MaterialEditor::_update_rotation() {
151
Transform3D t;
152
t.basis.rotate(Vector3(0, 1, 0), -rot.y);
153
t.basis.rotate(Vector3(1, 0, 0), -rot.x);
154
rotation->set_transform(t);
155
}
156
157
void MaterialEditor::edit(Ref<Material> p_material, const Ref<Environment> &p_env) {
158
material = p_material;
159
camera->set_environment(p_env);
160
161
is_unsupported_shader_mode = false;
162
if (material.is_valid()) {
163
Shader::Mode mode = p_material->get_shader_mode();
164
switch (mode) {
165
case Shader::MODE_CANVAS_ITEM:
166
layout_error->hide();
167
layout_3d->hide();
168
layout_2d->show();
169
rect_instance->set_material(material);
170
vc->hide();
171
break;
172
case Shader::MODE_SPATIAL:
173
layout_error->hide();
174
layout_2d->hide();
175
layout_3d->show();
176
sphere_instance->set_material_override(material);
177
box_instance->set_material_override(material);
178
quad_instance->set_material_override(material);
179
vc->show();
180
break;
181
default:
182
layout_error->show();
183
layout_2d->hide();
184
layout_3d->hide();
185
is_unsupported_shader_mode = true;
186
vc->hide();
187
break;
188
}
189
} else {
190
hide();
191
}
192
}
193
194
void MaterialEditor::_on_light_1_switch_pressed() {
195
light1->set_visible(light_1_switch->is_pressed());
196
}
197
198
void MaterialEditor::_on_light_2_switch_pressed() {
199
light2->set_visible(light_2_switch->is_pressed());
200
}
201
202
void MaterialEditor::_on_sphere_switch_pressed() {
203
sphere_instance->show();
204
box_instance->hide();
205
quad_instance->hide();
206
box_switch->set_pressed(false);
207
quad_switch->set_pressed(false);
208
_set_rotation(-15.0, 30.0);
209
_store_rotation_metadata();
210
EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_mesh", "sphere");
211
}
212
213
void MaterialEditor::_on_box_switch_pressed() {
214
sphere_instance->hide();
215
box_instance->show();
216
quad_instance->hide();
217
sphere_switch->set_pressed(false);
218
quad_switch->set_pressed(false);
219
_set_rotation(-15.0, 30.0);
220
_store_rotation_metadata();
221
EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_mesh", "box");
222
}
223
224
void MaterialEditor::_on_quad_switch_pressed() {
225
sphere_instance->hide();
226
box_instance->hide();
227
quad_instance->show();
228
sphere_switch->set_pressed(false);
229
box_switch->set_pressed(false);
230
_set_rotation(0.0, 0.0);
231
_store_rotation_metadata();
232
EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_mesh", "quad");
233
}
234
235
MaterialEditor::MaterialEditor() {
236
set_custom_minimum_size(Size2(1, 150) * EDSCALE);
237
238
// Canvas item
239
240
vc_2d = memnew(SubViewportContainer);
241
vc_2d->set_stretch(true);
242
add_child(vc_2d);
243
vc_2d->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
244
245
viewport_2d = memnew(SubViewport);
246
vc_2d->add_child(viewport_2d);
247
viewport_2d->set_disable_input(true);
248
viewport_2d->set_transparent_background(true);
249
250
layout_2d = memnew(HBoxContainer);
251
layout_2d->set_alignment(BoxContainer::ALIGNMENT_CENTER);
252
viewport_2d->add_child(layout_2d);
253
layout_2d->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
254
255
rect_instance = memnew(ColorRect);
256
layout_2d->add_child(rect_instance);
257
rect_instance->set_custom_minimum_size(Size2(150, 150) * EDSCALE);
258
259
layout_2d->set_visible(false);
260
261
layout_error = memnew(VBoxContainer);
262
layout_error->set_alignment(BoxContainer::ALIGNMENT_CENTER);
263
layout_error->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
264
265
error_label = memnew(Label);
266
error_label->set_focus_mode(FOCUS_ACCESSIBILITY);
267
error_label->set_text(TTR("Preview is not available for this shader mode."));
268
error_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
269
error_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
270
error_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
271
272
layout_error->add_child(error_label);
273
layout_error->hide();
274
add_child(layout_error);
275
276
// Spatial
277
278
vc = memnew(SubViewportContainer);
279
vc->set_stretch(true);
280
add_child(vc);
281
vc->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
282
viewport = memnew(SubViewport);
283
Ref<World3D> world_3d;
284
world_3d.instantiate();
285
viewport->set_world_3d(world_3d); // Use own world.
286
vc->add_child(viewport);
287
viewport->set_disable_input(true);
288
viewport->set_transparent_background(true);
289
viewport->set_msaa_3d(Viewport::MSAA_4X);
290
291
camera = memnew(Camera3D);
292
camera->set_transform(Transform3D(Basis(), Vector3(0, 0, 1.1)));
293
// Use low field of view so the sphere/box/quad is fully encompassed within the preview,
294
// without much distortion.
295
camera->set_perspective(20, 0.1, 10);
296
camera->make_current();
297
if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
298
camera_attributes.instantiate();
299
camera->set_attributes(camera_attributes);
300
}
301
viewport->add_child(camera);
302
303
light1 = memnew(DirectionalLight3D);
304
light1->set_transform(Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
305
viewport->add_child(light1);
306
307
light2 = memnew(DirectionalLight3D);
308
light2->set_transform(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
309
light2->set_color(Color(0.7, 0.7, 0.7));
310
viewport->add_child(light2);
311
312
rotation = memnew(Node3D);
313
viewport->add_child(rotation);
314
315
sphere_instance = memnew(MeshInstance3D);
316
rotation->add_child(sphere_instance);
317
318
box_instance = memnew(MeshInstance3D);
319
rotation->add_child(box_instance);
320
321
quad_instance = memnew(MeshInstance3D);
322
rotation->add_child(quad_instance);
323
324
sphere_instance->set_transform(Transform3D() * 0.375);
325
box_instance->set_transform(Transform3D() * 0.25);
326
quad_instance->set_transform(Transform3D() * 0.375);
327
328
sphere_mesh.instantiate();
329
sphere_instance->set_mesh(sphere_mesh);
330
box_mesh.instantiate();
331
box_instance->set_mesh(box_mesh);
332
quad_mesh.instantiate();
333
quad_instance->set_mesh(quad_mesh);
334
335
layout_3d = memnew(HBoxContainer);
336
add_child(layout_3d);
337
layout_3d->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 2);
338
339
VBoxContainer *vb_shape = memnew(VBoxContainer);
340
layout_3d->add_child(vb_shape);
341
342
sphere_switch = memnew(Button);
343
sphere_switch->set_theme_type_variation("PreviewLightButton");
344
sphere_switch->set_toggle_mode(true);
345
sphere_switch->set_accessibility_name(TTRC("Sphere"));
346
vb_shape->add_child(sphere_switch);
347
sphere_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_sphere_switch_pressed));
348
349
box_switch = memnew(Button);
350
box_switch->set_theme_type_variation("PreviewLightButton");
351
box_switch->set_toggle_mode(true);
352
box_switch->set_accessibility_name(TTRC("Box"));
353
vb_shape->add_child(box_switch);
354
box_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_box_switch_pressed));
355
356
quad_switch = memnew(Button);
357
quad_switch->set_theme_type_variation("PreviewLightButton");
358
quad_switch->set_toggle_mode(true);
359
quad_switch->set_accessibility_name(TTRC("Quad"));
360
vb_shape->add_child(quad_switch);
361
quad_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_quad_switch_pressed));
362
363
layout_3d->add_spacer();
364
365
VBoxContainer *vb_light = memnew(VBoxContainer);
366
layout_3d->add_child(vb_light);
367
368
light_1_switch = memnew(Button);
369
light_1_switch->set_theme_type_variation("PreviewLightButton");
370
light_1_switch->set_toggle_mode(true);
371
light_1_switch->set_pressed(true);
372
light_1_switch->set_accessibility_name(TTRC("First Light"));
373
vb_light->add_child(light_1_switch);
374
light_1_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_light_1_switch_pressed));
375
376
light_2_switch = memnew(Button);
377
light_2_switch->set_theme_type_variation("PreviewLightButton");
378
light_2_switch->set_toggle_mode(true);
379
light_2_switch->set_pressed(true);
380
light_2_switch->set_accessibility_name(TTRC("Second Light"));
381
vb_light->add_child(light_2_switch);
382
light_2_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_light_2_switch_pressed));
383
384
String shape = EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_mesh", "sphere");
385
if (shape == "sphere") {
386
box_instance->hide();
387
quad_instance->hide();
388
sphere_switch->set_pressed_no_signal(true);
389
} else if (shape == "box") {
390
sphere_instance->hide();
391
quad_instance->hide();
392
box_switch->set_pressed_no_signal(true);
393
} else {
394
sphere_instance->hide();
395
box_instance->hide();
396
quad_switch->set_pressed_no_signal(true);
397
}
398
399
Vector2 stored_rot = EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_rotation", Vector2());
400
_set_rotation(stored_rot.x, stored_rot.y);
401
}
402
403
///////////////////////
404
405
bool EditorInspectorPluginMaterial::can_handle(Object *p_object) {
406
Material *material = Object::cast_to<Material>(p_object);
407
if (!material) {
408
return false;
409
}
410
Shader::Mode mode = material->get_shader_mode();
411
return mode == Shader::MODE_SPATIAL || mode == Shader::MODE_CANVAS_ITEM;
412
}
413
414
void EditorInspectorPluginMaterial::parse_begin(Object *p_object) {
415
Material *material = Object::cast_to<Material>(p_object);
416
if (!material) {
417
return;
418
}
419
Ref<Material> m(material);
420
421
MaterialEditor *editor = memnew(MaterialEditor);
422
editor->edit(m, env);
423
add_custom_control(editor);
424
}
425
426
void EditorInspectorPluginMaterial::_undo_redo_inspector_callback(Object *p_undo_redo, Object *p_edited, const String &p_property, const Variant &p_new_value) {
427
EditorUndoRedoManager *undo_redo = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
428
ERR_FAIL_NULL(undo_redo);
429
430
// For BaseMaterial3D, if a roughness or metallic textures is being assigned to an empty slot,
431
// set the respective metallic or roughness factor to 1.0 as a convenience feature
432
BaseMaterial3D *base_material = Object::cast_to<StandardMaterial3D>(p_edited);
433
if (base_material) {
434
Texture2D *texture = Object::cast_to<Texture2D>(p_new_value);
435
if (texture) {
436
if (p_property == "roughness_texture") {
437
if (base_material->get_texture(StandardMaterial3D::TEXTURE_ROUGHNESS).is_null()) {
438
undo_redo->add_do_property(p_edited, "roughness", 1.0);
439
440
bool valid = false;
441
Variant value = p_edited->get("roughness", &valid);
442
if (valid) {
443
undo_redo->add_undo_property(p_edited, "roughness", value);
444
}
445
}
446
} else if (p_property == "metallic_texture") {
447
if (base_material->get_texture(StandardMaterial3D::TEXTURE_METALLIC).is_null()) {
448
undo_redo->add_do_property(p_edited, "metallic", 1.0);
449
450
bool valid = false;
451
Variant value = p_edited->get("metallic", &valid);
452
if (valid) {
453
undo_redo->add_undo_property(p_edited, "metallic", value);
454
}
455
}
456
}
457
}
458
}
459
}
460
461
EditorInspectorPluginMaterial::EditorInspectorPluginMaterial() {
462
env.instantiate();
463
Ref<Sky> sky = memnew(Sky());
464
env->set_sky(sky);
465
env->set_background(Environment::BG_COLOR);
466
env->set_ambient_source(Environment::AMBIENT_SOURCE_SKY);
467
env->set_reflection_source(Environment::REFLECTION_SOURCE_SKY);
468
469
EditorNode::get_editor_data().add_undo_redo_inspector_hook_callback(callable_mp(this, &EditorInspectorPluginMaterial::_undo_redo_inspector_callback));
470
}
471
472
MaterialEditorPlugin::MaterialEditorPlugin() {
473
Ref<EditorInspectorPluginMaterial> plugin;
474
plugin.instantiate();
475
add_inspector_plugin(plugin);
476
}
477
478
String ParticleProcessMaterialConversionPlugin::converts_to() const {
479
return "ShaderMaterial";
480
}
481
482
bool ParticleProcessMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
483
Ref<ParticleProcessMaterial> mat = p_resource;
484
return mat.is_valid();
485
}
486
487
Ref<Resource> ParticleProcessMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
488
return MaterialEditor::make_shader_material(p_resource);
489
}
490
491
String CanvasItemMaterialConversionPlugin::converts_to() const {
492
return "ShaderMaterial";
493
}
494
495
bool CanvasItemMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
496
Ref<CanvasItemMaterial> mat = p_resource;
497
return mat.is_valid();
498
}
499
500
Ref<Resource> CanvasItemMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
501
ERR_FAIL_COND_V(!Object::cast_to<CanvasItemMaterial>(*p_resource) || !Object::cast_to<CanvasItemMaterial>(*p_resource)->_is_initialized(), Ref<CanvasItemMaterial>());
502
return MaterialEditor::make_shader_material(p_resource);
503
}
504
505
String BlitMaterialConversionPlugin::converts_to() const {
506
return "ShaderMaterial";
507
}
508
509
bool BlitMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
510
Ref<BlitMaterial> mat = p_resource;
511
return mat.is_valid();
512
}
513
514
Ref<Resource> BlitMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
515
Ref<BlitMaterial> mat = p_resource;
516
ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
517
518
Ref<ShaderMaterial> smat;
519
smat.instantiate();
520
521
Ref<Shader> shader;
522
shader.instantiate();
523
524
String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
525
526
shader->set_code(code);
527
528
smat->set_shader(shader);
529
530
List<PropertyInfo> params;
531
RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
532
533
for (const PropertyInfo &E : params) {
534
Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
535
smat->set_shader_parameter(E.name, value);
536
}
537
538
smat->set_render_priority(mat->get_render_priority());
539
smat->set_local_to_scene(mat->is_local_to_scene());
540
smat->set_name(mat->get_name());
541
return smat;
542
}
543
544