Skip to content

Commit

Permalink
Add a material preview to visual shader editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaosus committed Jul 16, 2024
1 parent 97b8ad1 commit c44c48e
Show file tree
Hide file tree
Showing 7 changed files with 510 additions and 86 deletions.
23 changes: 23 additions & 0 deletions doc/classes/VisualShader.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,27 @@
Returns the position of the specified node within the shader graph.
</description>
</method>
<method name="get_preview_shader_parameter" qualifiers="const">
<return type="Variant" />
<param index="0" name="name" type="String" />
<description>
Returns the current value set for preview material of a parameter in the shader.
</description>
</method>
<method name="get_valid_node_id" qualifiers="const">
<return type="int" />
<param index="0" name="type" type="int" enum="VisualShader.Type" />
<description>
Returns next valid node ID that can be added to the shader graph.
</description>
</method>
<method name="has_preview_shader_parameter" qualifiers="const">
<return type="bool" />
<param index="0" name="name" type="String" />
<description>
Returns [code]true[/code] if preview parameter with a specified [param name] exists.
</description>
</method>
<method name="has_varying" qualifiers="const">
<return type="bool" />
<param index="0" name="name" type="String" />
Expand Down Expand Up @@ -185,6 +199,15 @@
Sets the position of the specified node.
</description>
</method>
<method name="set_preview_shader_parameter">
<return type="void" />
<param index="0" name="name" type="String" />
<param index="1" name="value" type="Variant" />
<description>
Changes the value set for preview material of a parameter in the shader.
If empty [Variant] is passed as [param value], removes the parameter.
</description>
</method>
</methods>
<members>
<member name="graph_offset" type="Vector2" setter="set_graph_offset" getter="get_graph_offset" default="Vector2(0, 0)">
Expand Down
33 changes: 31 additions & 2 deletions editor/plugins/material_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "core/config/project_settings.h"
#include "editor/editor_node.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
#include "editor/editor_undo_redo_manager.h"
#include "editor/themes/editor_scale.h"
#include "scene/3d/camera_3d.h"
Expand All @@ -41,6 +42,7 @@
#include "scene/gui/box_container.h"
#include "scene/gui/button.h"
#include "scene/gui/color_rect.h"
#include "scene/gui/label.h"
#include "scene/gui/subviewport_container.h"
#include "scene/main/viewport.h"
#include "scene/resources/3d/fog_material.h"
Expand Down Expand Up @@ -80,11 +82,15 @@ void MaterialEditor::_notification(int p_what) {

sphere_switch->set_icon(theme_cache.sphere_icon);
box_switch->set_icon(theme_cache.box_icon);

error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
} break;

case NOTIFICATION_DRAW: {
Size2 size = get_size();
draw_texture_rect(theme_cache.checkerboard, Rect2(Point2(), size), true);
if (!is_unsupported_shader_mode) {
Size2 size = get_size();
draw_texture_rect(theme_cache.checkerboard, Rect2(Point2(), size), true);
}
} break;
}
}
Expand All @@ -99,23 +105,32 @@ void MaterialEditor::_update_rotation() {
void MaterialEditor::edit(Ref<Material> p_material, const Ref<Environment> &p_env) {
material = p_material;
camera->set_environment(p_env);

is_unsupported_shader_mode = false;
if (!material.is_null()) {
Shader::Mode mode = p_material->get_shader_mode();
switch (mode) {
case Shader::MODE_CANVAS_ITEM:
layout_error->hide();
layout_3d->hide();
layout_2d->show();
vc->hide();
rect_instance->set_material(material);
break;
case Shader::MODE_SPATIAL:
layout_error->hide();
layout_2d->hide();
layout_3d->show();
vc->show();
sphere_instance->set_material_override(material);
box_instance->set_material_override(material);
break;
default:
layout_error->show();
layout_2d->hide();
layout_3d->hide();
vc->hide();
is_unsupported_shader_mode = true;
break;
}
} else {
Expand Down Expand Up @@ -175,6 +190,20 @@ MaterialEditor::MaterialEditor() {

layout_2d->set_visible(false);

layout_error = memnew(VBoxContainer);
layout_error->set_alignment(BoxContainer::ALIGNMENT_CENTER);
layout_error->set_anchors_and_offsets_preset(PRESET_FULL_RECT);

error_label = memnew(Label);
error_label->set_text(TTR("Preview is not available for this shader mode."));
error_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
error_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
error_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);

layout_error->add_child(error_label);
layout_error->hide();
add_child(layout_error);

// Spatial

vc = memnew(SubViewportContainer);
Expand Down
5 changes: 5 additions & 0 deletions editor/plugins/material_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class MeshInstance3D;
class SubViewport;
class SubViewportContainer;
class Button;
class Label;

class MaterialEditor : public Control {
GDCLASS(MaterialEditor, Control);
Expand All @@ -69,6 +70,10 @@ class MaterialEditor : public Control {
Ref<SphereMesh> sphere_mesh;
Ref<BoxMesh> box_mesh;

VBoxContainer *layout_error = nullptr;
Label *error_label = nullptr;
bool is_unsupported_shader_mode = false;

HBoxContainer *layout_3d = nullptr;

Ref<Material> material;
Expand Down
Loading

0 comments on commit c44c48e

Please sign in to comment.