Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/material_editor_plugin.cpp
9896 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/3d/camera_3d.h"
40
#include "scene/3d/light_3d.h"
41
#include "scene/3d/mesh_instance_3d.h"
42
#include "scene/gui/box_container.h"
43
#include "scene/gui/button.h"
44
#include "scene/gui/color_rect.h"
45
#include "scene/gui/label.h"
46
#include "scene/gui/subviewport_container.h"
47
#include "scene/main/viewport.h"
48
#include "scene/resources/3d/fog_material.h"
49
#include "scene/resources/3d/sky_material.h"
50
#include "scene/resources/canvas_item_material.h"
51
#include "scene/resources/particle_process_material.h"
52
53
void MaterialEditor::gui_input(const Ref<InputEvent> &p_event) {
54
ERR_FAIL_COND(p_event.is_null());
55
56
Ref<InputEventMouseMotion> mm = p_event;
57
if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
58
rot.x -= mm->get_relative().y * 0.01;
59
rot.y -= mm->get_relative().x * 0.01;
60
if (quad_instance->is_visible()) {
61
// Clamp rotation so the quad is always visible.
62
const real_t limit = Math::deg_to_rad(80.0);
63
rot = rot.clampf(-limit, limit);
64
} else {
65
rot.x = CLAMP(rot.x, -Math::PI / 2, Math::PI / 2);
66
}
67
_update_rotation();
68
_store_rotation_metadata();
69
}
70
}
71
72
void MaterialEditor::_update_theme_item_cache() {
73
Control::_update_theme_item_cache();
74
75
theme_cache.light_1_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight1"));
76
theme_cache.light_2_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight2"));
77
78
theme_cache.sphere_icon = get_editor_theme_icon(SNAME("MaterialPreviewSphere"));
79
theme_cache.box_icon = get_editor_theme_icon(SNAME("MaterialPreviewCube"));
80
theme_cache.quad_icon = get_editor_theme_icon(SNAME("MaterialPreviewQuad"));
81
82
theme_cache.checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));
83
}
84
85
void MaterialEditor::_notification(int p_what) {
86
switch (p_what) {
87
case NOTIFICATION_THEME_CHANGED: {
88
light_1_switch->set_button_icon(theme_cache.light_1_icon);
89
light_2_switch->set_button_icon(theme_cache.light_2_icon);
90
91
sphere_switch->set_button_icon(theme_cache.sphere_icon);
92
box_switch->set_button_icon(theme_cache.box_icon);
93
quad_switch->set_button_icon(theme_cache.quad_icon);
94
95
error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
96
} break;
97
98
case NOTIFICATION_DRAW: {
99
if (!is_unsupported_shader_mode) {
100
Size2 size = get_size();
101
draw_texture_rect(theme_cache.checkerboard, Rect2(Point2(), size), true);
102
}
103
} break;
104
}
105
}
106
107
void MaterialEditor::_set_rotation(real_t p_x_degrees, real_t p_y_degrees) {
108
rot.x = Math::deg_to_rad(p_x_degrees);
109
rot.y = Math::deg_to_rad(p_y_degrees);
110
_update_rotation();
111
}
112
113
// Store the rotation so it can persist when switching between materials.
114
void MaterialEditor::_store_rotation_metadata() {
115
Vector2 rotation_degrees = Vector2(Math::rad_to_deg(rot.x), Math::rad_to_deg(rot.y));
116
EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_rotation", rotation_degrees);
117
}
118
119
void MaterialEditor::_update_rotation() {
120
Transform3D t;
121
t.basis.rotate(Vector3(0, 1, 0), -rot.y);
122
t.basis.rotate(Vector3(1, 0, 0), -rot.x);
123
rotation->set_transform(t);
124
}
125
126
void MaterialEditor::edit(Ref<Material> p_material, const Ref<Environment> &p_env) {
127
material = p_material;
128
camera->set_environment(p_env);
129
130
is_unsupported_shader_mode = false;
131
if (material.is_valid()) {
132
Shader::Mode mode = p_material->get_shader_mode();
133
switch (mode) {
134
case Shader::MODE_CANVAS_ITEM:
135
layout_error->hide();
136
layout_3d->hide();
137
layout_2d->show();
138
vc->hide();
139
rect_instance->set_material(material);
140
break;
141
case Shader::MODE_SPATIAL:
142
layout_error->hide();
143
layout_2d->hide();
144
layout_3d->show();
145
vc->show();
146
sphere_instance->set_material_override(material);
147
box_instance->set_material_override(material);
148
quad_instance->set_material_override(material);
149
break;
150
default:
151
layout_error->show();
152
layout_2d->hide();
153
layout_3d->hide();
154
vc->hide();
155
is_unsupported_shader_mode = true;
156
break;
157
}
158
} else {
159
hide();
160
}
161
}
162
163
void MaterialEditor::_on_light_1_switch_pressed() {
164
light1->set_visible(light_1_switch->is_pressed());
165
}
166
167
void MaterialEditor::_on_light_2_switch_pressed() {
168
light2->set_visible(light_2_switch->is_pressed());
169
}
170
171
void MaterialEditor::_on_sphere_switch_pressed() {
172
sphere_instance->show();
173
box_instance->hide();
174
quad_instance->hide();
175
box_switch->set_pressed(false);
176
quad_switch->set_pressed(false);
177
_set_rotation(-15.0, 30.0);
178
_store_rotation_metadata();
179
EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_mesh", "sphere");
180
}
181
182
void MaterialEditor::_on_box_switch_pressed() {
183
sphere_instance->hide();
184
box_instance->show();
185
quad_instance->hide();
186
sphere_switch->set_pressed(false);
187
quad_switch->set_pressed(false);
188
_set_rotation(-15.0, 30.0);
189
_store_rotation_metadata();
190
EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_mesh", "box");
191
}
192
193
void MaterialEditor::_on_quad_switch_pressed() {
194
sphere_instance->hide();
195
box_instance->hide();
196
quad_instance->show();
197
sphere_switch->set_pressed(false);
198
box_switch->set_pressed(false);
199
_set_rotation(0.0, 0.0);
200
_store_rotation_metadata();
201
EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_mesh", "quad");
202
}
203
204
MaterialEditor::MaterialEditor() {
205
// Canvas item
206
207
vc_2d = memnew(SubViewportContainer);
208
vc_2d->set_stretch(true);
209
add_child(vc_2d);
210
vc_2d->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
211
212
viewport_2d = memnew(SubViewport);
213
vc_2d->add_child(viewport_2d);
214
viewport_2d->set_disable_input(true);
215
viewport_2d->set_transparent_background(true);
216
217
layout_2d = memnew(HBoxContainer);
218
layout_2d->set_alignment(BoxContainer::ALIGNMENT_CENTER);
219
viewport_2d->add_child(layout_2d);
220
layout_2d->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
221
222
rect_instance = memnew(ColorRect);
223
layout_2d->add_child(rect_instance);
224
rect_instance->set_custom_minimum_size(Size2(150, 150) * EDSCALE);
225
226
layout_2d->set_visible(false);
227
228
layout_error = memnew(VBoxContainer);
229
layout_error->set_alignment(BoxContainer::ALIGNMENT_CENTER);
230
layout_error->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
231
232
error_label = memnew(Label);
233
error_label->set_focus_mode(FOCUS_ACCESSIBILITY);
234
error_label->set_text(TTR("Preview is not available for this shader mode."));
235
error_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
236
error_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
237
error_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
238
239
layout_error->add_child(error_label);
240
layout_error->hide();
241
add_child(layout_error);
242
243
// Spatial
244
245
vc = memnew(SubViewportContainer);
246
vc->set_stretch(true);
247
add_child(vc);
248
vc->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
249
viewport = memnew(SubViewport);
250
Ref<World3D> world_3d;
251
world_3d.instantiate();
252
viewport->set_world_3d(world_3d); // Use own world.
253
vc->add_child(viewport);
254
viewport->set_disable_input(true);
255
viewport->set_transparent_background(true);
256
viewport->set_msaa_3d(Viewport::MSAA_4X);
257
258
camera = memnew(Camera3D);
259
camera->set_transform(Transform3D(Basis(), Vector3(0, 0, 1.1)));
260
// Use low field of view so the sphere/box/quad is fully encompassed within the preview,
261
// without much distortion.
262
camera->set_perspective(20, 0.1, 10);
263
camera->make_current();
264
if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
265
camera_attributes.instantiate();
266
camera->set_attributes(camera_attributes);
267
}
268
viewport->add_child(camera);
269
270
light1 = memnew(DirectionalLight3D);
271
light1->set_transform(Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
272
viewport->add_child(light1);
273
274
light2 = memnew(DirectionalLight3D);
275
light2->set_transform(Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
276
light2->set_color(Color(0.7, 0.7, 0.7));
277
viewport->add_child(light2);
278
279
rotation = memnew(Node3D);
280
viewport->add_child(rotation);
281
282
sphere_instance = memnew(MeshInstance3D);
283
rotation->add_child(sphere_instance);
284
285
box_instance = memnew(MeshInstance3D);
286
rotation->add_child(box_instance);
287
288
quad_instance = memnew(MeshInstance3D);
289
rotation->add_child(quad_instance);
290
291
sphere_instance->set_transform(Transform3D() * 0.375);
292
box_instance->set_transform(Transform3D() * 0.25);
293
quad_instance->set_transform(Transform3D() * 0.375);
294
295
sphere_mesh.instantiate();
296
sphere_instance->set_mesh(sphere_mesh);
297
box_mesh.instantiate();
298
box_instance->set_mesh(box_mesh);
299
quad_mesh.instantiate();
300
quad_instance->set_mesh(quad_mesh);
301
302
set_custom_minimum_size(Size2(1, 150) * EDSCALE);
303
304
layout_3d = memnew(HBoxContainer);
305
add_child(layout_3d);
306
layout_3d->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 2);
307
308
VBoxContainer *vb_shape = memnew(VBoxContainer);
309
layout_3d->add_child(vb_shape);
310
311
sphere_switch = memnew(Button);
312
sphere_switch->set_theme_type_variation("PreviewLightButton");
313
sphere_switch->set_toggle_mode(true);
314
sphere_switch->set_accessibility_name(TTRC("Sphere"));
315
vb_shape->add_child(sphere_switch);
316
sphere_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_sphere_switch_pressed));
317
318
box_switch = memnew(Button);
319
box_switch->set_theme_type_variation("PreviewLightButton");
320
box_switch->set_toggle_mode(true);
321
box_switch->set_accessibility_name(TTRC("Box"));
322
vb_shape->add_child(box_switch);
323
box_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_box_switch_pressed));
324
325
quad_switch = memnew(Button);
326
quad_switch->set_theme_type_variation("PreviewLightButton");
327
quad_switch->set_toggle_mode(true);
328
quad_switch->set_accessibility_name(TTRC("Quad"));
329
vb_shape->add_child(quad_switch);
330
quad_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_quad_switch_pressed));
331
332
layout_3d->add_spacer();
333
334
VBoxContainer *vb_light = memnew(VBoxContainer);
335
layout_3d->add_child(vb_light);
336
337
light_1_switch = memnew(Button);
338
light_1_switch->set_theme_type_variation("PreviewLightButton");
339
light_1_switch->set_toggle_mode(true);
340
light_1_switch->set_pressed(true);
341
light_1_switch->set_accessibility_name(TTRC("First Light"));
342
vb_light->add_child(light_1_switch);
343
light_1_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_light_1_switch_pressed));
344
345
light_2_switch = memnew(Button);
346
light_2_switch->set_theme_type_variation("PreviewLightButton");
347
light_2_switch->set_toggle_mode(true);
348
light_2_switch->set_pressed(true);
349
light_2_switch->set_accessibility_name(TTRC("Second Light"));
350
vb_light->add_child(light_2_switch);
351
light_2_switch->connect(SceneStringName(pressed), callable_mp(this, &MaterialEditor::_on_light_2_switch_pressed));
352
353
String shape = EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_mesh", "sphere");
354
if (shape == "sphere") {
355
box_instance->hide();
356
quad_instance->hide();
357
sphere_switch->set_pressed_no_signal(true);
358
} else if (shape == "box") {
359
sphere_instance->hide();
360
quad_instance->hide();
361
box_switch->set_pressed_no_signal(true);
362
} else {
363
sphere_instance->hide();
364
box_instance->hide();
365
quad_switch->set_pressed_no_signal(true);
366
}
367
368
Vector2 stored_rot = EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_rotation", Vector2());
369
_set_rotation(stored_rot.x, stored_rot.y);
370
}
371
372
///////////////////////
373
374
bool EditorInspectorPluginMaterial::can_handle(Object *p_object) {
375
Material *material = Object::cast_to<Material>(p_object);
376
if (!material) {
377
return false;
378
}
379
Shader::Mode mode = material->get_shader_mode();
380
return mode == Shader::MODE_SPATIAL || mode == Shader::MODE_CANVAS_ITEM;
381
}
382
383
void EditorInspectorPluginMaterial::parse_begin(Object *p_object) {
384
Material *material = Object::cast_to<Material>(p_object);
385
if (!material) {
386
return;
387
}
388
Ref<Material> m(material);
389
390
MaterialEditor *editor = memnew(MaterialEditor);
391
editor->edit(m, env);
392
add_custom_control(editor);
393
}
394
395
void EditorInspectorPluginMaterial::_undo_redo_inspector_callback(Object *p_undo_redo, Object *p_edited, const String &p_property, const Variant &p_new_value) {
396
EditorUndoRedoManager *undo_redo = Object::cast_to<EditorUndoRedoManager>(p_undo_redo);
397
ERR_FAIL_NULL(undo_redo);
398
399
// For BaseMaterial3D, if a roughness or metallic textures is being assigned to an empty slot,
400
// set the respective metallic or roughness factor to 1.0 as a convenience feature
401
BaseMaterial3D *base_material = Object::cast_to<StandardMaterial3D>(p_edited);
402
if (base_material) {
403
Texture2D *texture = Object::cast_to<Texture2D>(p_new_value);
404
if (texture) {
405
if (p_property == "roughness_texture") {
406
if (base_material->get_texture(StandardMaterial3D::TEXTURE_ROUGHNESS).is_null()) {
407
undo_redo->add_do_property(p_edited, "roughness", 1.0);
408
409
bool valid = false;
410
Variant value = p_edited->get("roughness", &valid);
411
if (valid) {
412
undo_redo->add_undo_property(p_edited, "roughness", value);
413
}
414
}
415
} else if (p_property == "metallic_texture") {
416
if (base_material->get_texture(StandardMaterial3D::TEXTURE_METALLIC).is_null()) {
417
undo_redo->add_do_property(p_edited, "metallic", 1.0);
418
419
bool valid = false;
420
Variant value = p_edited->get("metallic", &valid);
421
if (valid) {
422
undo_redo->add_undo_property(p_edited, "metallic", value);
423
}
424
}
425
}
426
}
427
}
428
}
429
430
EditorInspectorPluginMaterial::EditorInspectorPluginMaterial() {
431
env.instantiate();
432
Ref<Sky> sky = memnew(Sky());
433
env->set_sky(sky);
434
env->set_background(Environment::BG_COLOR);
435
env->set_ambient_source(Environment::AMBIENT_SOURCE_SKY);
436
env->set_reflection_source(Environment::REFLECTION_SOURCE_SKY);
437
438
EditorNode::get_editor_data().add_undo_redo_inspector_hook_callback(callable_mp(this, &EditorInspectorPluginMaterial::_undo_redo_inspector_callback));
439
}
440
441
MaterialEditorPlugin::MaterialEditorPlugin() {
442
Ref<EditorInspectorPluginMaterial> plugin;
443
plugin.instantiate();
444
add_inspector_plugin(plugin);
445
}
446
447
String StandardMaterial3DConversionPlugin::converts_to() const {
448
return "ShaderMaterial";
449
}
450
451
bool StandardMaterial3DConversionPlugin::handles(const Ref<Resource> &p_resource) const {
452
Ref<StandardMaterial3D> mat = p_resource;
453
return mat.is_valid();
454
}
455
456
Ref<Resource> StandardMaterial3DConversionPlugin::convert(const Ref<Resource> &p_resource) const {
457
Ref<StandardMaterial3D> mat = p_resource;
458
ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
459
460
Ref<ShaderMaterial> smat;
461
smat.instantiate();
462
463
Ref<Shader> shader;
464
shader.instantiate();
465
466
String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
467
468
shader->set_code(code);
469
470
smat->set_shader(shader);
471
472
List<PropertyInfo> params;
473
RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
474
475
for (const PropertyInfo &E : params) {
476
// Texture parameter has to be treated specially since StandardMaterial3D saved it
477
// as RID but ShaderMaterial needs Texture itself
478
Ref<Texture2D> texture = mat->get_texture_by_name(E.name);
479
if (texture.is_valid()) {
480
smat->set_shader_parameter(E.name, texture);
481
} else {
482
Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
483
smat->set_shader_parameter(E.name, value);
484
}
485
}
486
487
smat->set_render_priority(mat->get_render_priority());
488
smat->set_local_to_scene(mat->is_local_to_scene());
489
smat->set_name(mat->get_name());
490
return smat;
491
}
492
493
String ORMMaterial3DConversionPlugin::converts_to() const {
494
return "ShaderMaterial";
495
}
496
497
bool ORMMaterial3DConversionPlugin::handles(const Ref<Resource> &p_resource) const {
498
Ref<ORMMaterial3D> mat = p_resource;
499
return mat.is_valid();
500
}
501
502
Ref<Resource> ORMMaterial3DConversionPlugin::convert(const Ref<Resource> &p_resource) const {
503
Ref<ORMMaterial3D> mat = p_resource;
504
ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
505
506
Ref<ShaderMaterial> smat;
507
smat.instantiate();
508
509
Ref<Shader> shader;
510
shader.instantiate();
511
512
String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
513
514
shader->set_code(code);
515
516
smat->set_shader(shader);
517
518
List<PropertyInfo> params;
519
RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
520
521
for (const PropertyInfo &E : params) {
522
// Texture parameter has to be treated specially since ORMMaterial3D saved it
523
// as RID but ShaderMaterial needs Texture itself
524
Ref<Texture2D> texture = mat->get_texture_by_name(E.name);
525
if (texture.is_valid()) {
526
smat->set_shader_parameter(E.name, texture);
527
} else {
528
Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
529
smat->set_shader_parameter(E.name, value);
530
}
531
}
532
533
smat->set_render_priority(mat->get_render_priority());
534
smat->set_local_to_scene(mat->is_local_to_scene());
535
smat->set_name(mat->get_name());
536
return smat;
537
}
538
539
String ParticleProcessMaterialConversionPlugin::converts_to() const {
540
return "ShaderMaterial";
541
}
542
543
bool ParticleProcessMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
544
Ref<ParticleProcessMaterial> mat = p_resource;
545
return mat.is_valid();
546
}
547
548
Ref<Resource> ParticleProcessMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
549
Ref<ParticleProcessMaterial> mat = p_resource;
550
ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
551
552
Ref<ShaderMaterial> smat;
553
smat.instantiate();
554
555
Ref<Shader> shader;
556
shader.instantiate();
557
558
String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
559
560
shader->set_code(code);
561
562
smat->set_shader(shader);
563
564
List<PropertyInfo> params;
565
RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
566
567
for (const PropertyInfo &E : params) {
568
Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
569
smat->set_shader_parameter(E.name, value);
570
}
571
572
smat->set_render_priority(mat->get_render_priority());
573
smat->set_local_to_scene(mat->is_local_to_scene());
574
smat->set_name(mat->get_name());
575
return smat;
576
}
577
578
String CanvasItemMaterialConversionPlugin::converts_to() const {
579
return "ShaderMaterial";
580
}
581
582
bool CanvasItemMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
583
Ref<CanvasItemMaterial> mat = p_resource;
584
return mat.is_valid();
585
}
586
587
Ref<Resource> CanvasItemMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
588
Ref<CanvasItemMaterial> mat = p_resource;
589
ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
590
591
Ref<ShaderMaterial> smat;
592
smat.instantiate();
593
594
Ref<Shader> shader;
595
shader.instantiate();
596
597
String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
598
599
shader->set_code(code);
600
601
smat->set_shader(shader);
602
603
List<PropertyInfo> params;
604
RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
605
606
for (const PropertyInfo &E : params) {
607
Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
608
smat->set_shader_parameter(E.name, value);
609
}
610
611
smat->set_render_priority(mat->get_render_priority());
612
smat->set_local_to_scene(mat->is_local_to_scene());
613
smat->set_name(mat->get_name());
614
return smat;
615
}
616
617
String ProceduralSkyMaterialConversionPlugin::converts_to() const {
618
return "ShaderMaterial";
619
}
620
621
bool ProceduralSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
622
Ref<ProceduralSkyMaterial> mat = p_resource;
623
return mat.is_valid();
624
}
625
626
Ref<Resource> ProceduralSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
627
Ref<ProceduralSkyMaterial> mat = p_resource;
628
ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
629
630
Ref<ShaderMaterial> smat;
631
smat.instantiate();
632
633
Ref<Shader> shader;
634
shader.instantiate();
635
636
String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
637
638
shader->set_code(code);
639
640
smat->set_shader(shader);
641
642
List<PropertyInfo> params;
643
RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
644
645
for (const PropertyInfo &E : params) {
646
Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
647
smat->set_shader_parameter(E.name, value);
648
}
649
650
smat->set_render_priority(mat->get_render_priority());
651
smat->set_local_to_scene(mat->is_local_to_scene());
652
smat->set_name(mat->get_name());
653
return smat;
654
}
655
656
String PanoramaSkyMaterialConversionPlugin::converts_to() const {
657
return "ShaderMaterial";
658
}
659
660
bool PanoramaSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
661
Ref<PanoramaSkyMaterial> mat = p_resource;
662
return mat.is_valid();
663
}
664
665
Ref<Resource> PanoramaSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
666
Ref<PanoramaSkyMaterial> mat = p_resource;
667
ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
668
669
Ref<ShaderMaterial> smat;
670
smat.instantiate();
671
672
Ref<Shader> shader;
673
shader.instantiate();
674
675
String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
676
677
shader->set_code(code);
678
679
smat->set_shader(shader);
680
681
List<PropertyInfo> params;
682
RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
683
684
for (const PropertyInfo &E : params) {
685
Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
686
smat->set_shader_parameter(E.name, value);
687
}
688
689
smat->set_render_priority(mat->get_render_priority());
690
smat->set_local_to_scene(mat->is_local_to_scene());
691
smat->set_name(mat->get_name());
692
return smat;
693
}
694
695
String PhysicalSkyMaterialConversionPlugin::converts_to() const {
696
return "ShaderMaterial";
697
}
698
699
bool PhysicalSkyMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
700
Ref<PhysicalSkyMaterial> mat = p_resource;
701
return mat.is_valid();
702
}
703
704
Ref<Resource> PhysicalSkyMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
705
Ref<PhysicalSkyMaterial> mat = p_resource;
706
ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
707
708
Ref<ShaderMaterial> smat;
709
smat.instantiate();
710
711
Ref<Shader> shader;
712
shader.instantiate();
713
714
String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
715
716
shader->set_code(code);
717
718
smat->set_shader(shader);
719
720
List<PropertyInfo> params;
721
RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
722
723
for (const PropertyInfo &E : params) {
724
Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
725
smat->set_shader_parameter(E.name, value);
726
}
727
728
smat->set_render_priority(mat->get_render_priority());
729
smat->set_local_to_scene(mat->is_local_to_scene());
730
smat->set_name(mat->get_name());
731
return smat;
732
}
733
734
String FogMaterialConversionPlugin::converts_to() const {
735
return "ShaderMaterial";
736
}
737
738
bool FogMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
739
Ref<FogMaterial> mat = p_resource;
740
return mat.is_valid();
741
}
742
743
Ref<Resource> FogMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
744
Ref<FogMaterial> mat = p_resource;
745
ERR_FAIL_COND_V(mat.is_null(), Ref<Resource>());
746
747
Ref<ShaderMaterial> smat;
748
smat.instantiate();
749
750
Ref<Shader> shader;
751
shader.instantiate();
752
753
String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
754
755
shader->set_code(code);
756
757
smat->set_shader(shader);
758
759
List<PropertyInfo> params;
760
RS::get_singleton()->get_shader_parameter_list(mat->get_shader_rid(), &params);
761
762
for (const PropertyInfo &E : params) {
763
Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
764
smat->set_shader_parameter(E.name, value);
765
}
766
767
smat->set_render_priority(mat->get_render_priority());
768
return smat;
769
}
770
771