Godot Engine – Multi-platform 2D and 3D game engine
/**************************************************************************/
/* visual_shader_group.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "visual_shader.h"
#include "scene/property_list_helper.h"
class VisualShaderGroup : public Resource {
GDCLASS(VisualShaderGroup, Resource);
public:
struct Port {
VisualShaderNode::PortType type = VisualShaderNode::PortType::PORT_TYPE_MAX;
String name;
};
private:
String group_name;
Vector<Port> input_ports;
Vector<Port> output_ports;
Ref<ShaderGraph> graph;
mutable SafeFlag dirty;
static inline PropertyListHelper input_port_base_property_helper;
static inline PropertyListHelper output_port_base_property_helper;
PropertyListHelper input_port_property_helper;
PropertyListHelper output_port_property_helper;
String _validate_port_name(const String &p_port_name, int p_port_id, bool p_output) const;
String _validate_group_name(const String &p_name) const;
String _build_function_signature() const;
void _write_body(
Shader::Mode p_mode,
StringBuilder &r_body,
StringBuilder *r_global_code,
StringBuilder *r_global_code_per_node,
HashMap<ShaderGraph::Type, StringBuilder> *r_global_code_per_func,
Vector<ShaderGraph::DefaultTextureParam> &r_def_tex_params,
HashSet<StringName> &r_classes) const;
protected:
static void _bind_methods();
// Necessary for the debugger to forward changes to a running project.
virtual void _resource_path_changed() override;
void _queue_update();
void _emit_pending_change();
public:
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
Ref<ShaderGraph> get_graph() const;
String get_unique_func_name() const;
// Writes this group's global code and its function definition into the given builders.
// The r_classes set makes sure each group is only written once.
// The function body does not depend on the shader func/stage, so all stages can share it.
void write_definition(
Shader::Mode p_mode,
StringBuilder *r_global_code,
StringBuilder *r_global_code_per_node,
HashMap<ShaderGraph::Type, StringBuilder> *r_global_code_per_func,
Vector<ShaderGraph::DefaultTextureParam> &r_def_tex_params,
HashSet<StringName> &r_classes) const;
void set_group_name(const String &p_name);
String get_group_name() const;
String insert_input_port(int p_id, VisualShaderNode::PortType p_type, const String &p_name);
void set_input_port_name(int p_id, const String &p_name);
void set_input_port_type(int p_id, VisualShaderNode::PortType p_type);
void set_input_port_count(int p_count);
int get_input_port_count() const;
String get_input_port_name(int p_id) const;
VisualShaderNode::PortType get_input_port_type(int p_id) const;
void remove_input_port(int p_id);
void move_input_port(int p_from, int p_to);
String insert_output_port(int p_id, VisualShaderNode::PortType p_type, const String &p_name);
void set_output_port_name(int p_id, const String &p_name);
void set_output_port_type(int p_id, VisualShaderNode::PortType p_type);
void set_output_port_count(int p_count);
int get_output_port_count() const;
String get_output_port_name(int p_id) const;
VisualShaderNode::PortType get_output_port_type(int p_id) const;
void remove_output_port(int p_id);
void move_output_port(int p_from, int p_to);
void add_node(const Ref<VisualShaderNode> &p_node, const Vector2 &p_position, int p_id);
void set_node_position(int p_id, const Vector2 &p_position);
Vector2 get_node_position(int p_id) const;
Ref<VisualShaderNode> get_node(int p_id) const;
Vector<int> get_node_ids() const;
int get_valid_node_id() const;
int find_node_id(const Ref<VisualShaderNode> &p_node) const;
void remove_node(int p_id);
void replace_node(int p_id, const StringName &p_new_class);
bool are_nodes_connected(int p_from_node, int p_from_port, int p_to_node, int p_to_port) const;
bool is_node_reachable(int p_from, int p_target) const;
bool can_connect_nodes(int p_from_node, int p_from_port, int p_to_node, int p_to_port) const;
Error connect_nodes(int p_from_node, int p_from_port, int p_to_node, int p_to_port);
void disconnect_nodes(int p_from_node, int p_from_port, int p_to_node, int p_to_port);
void connect_nodes_forced(int p_from_node, int p_from_port, int p_to_node, int p_to_port);
bool is_port_types_compatible(int p_a, int p_b) const;
void attach_node_to_frame(int p_node, int p_frame);
void detach_node_from_frame(int p_node);
String get_reroute_parameter_name(int p_reroute_node) const;
TypedArray<Dictionary> _get_node_connections() const;
void get_node_connections(List<ShaderGraph::Connection> *r_connections) const;
String generate_preview_shader(int p_node, int p_port, Vector<ShaderGraph::DefaultTextureParam> &r_default_tex_params) const;
void create_default_nodes();
VisualShaderGroup();
};
class VisualShaderNodeGroup : public VisualShaderNode {
GDCLASS(VisualShaderNodeGroup, VisualShaderNode);
Ref<VisualShaderGroup> group;
void _update_default_input_values();
void _on_group_changed();
bool _has_incompatible_nodes(Shader::Mode p_mode, VisualShader::Type p_type) const;
protected:
static void _bind_methods();
public:
virtual String get_caption() const override;
virtual int get_input_port_count() const override;
virtual PortType get_input_port_type(int p_port) const override;
virtual String get_input_port_name(int p_port) const override;
virtual int get_output_port_count() const override;
virtual PortType get_output_port_type(int p_port) const override;
virtual String get_output_port_name(int p_port) const override;
virtual bool is_show_prop_names() const override;
virtual Vector<StringName> get_editable_properties() const override;
virtual bool is_use_prop_slots() const override;
void set_group(const Ref<VisualShaderGroup> &p_group);
Ref<VisualShaderGroup> get_group() const;
virtual String generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview = false) const override;
virtual bool is_output_port_expandable(int p_port) const override;
virtual String get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const override;
virtual Category get_category() const override { return CATEGORY_SPECIAL; }
virtual bool is_available(Shader::Mode p_mode, VisualShader::Type p_type) const override;
VisualShaderNodeGroup();
};
class VisualShaderNodeGroupInput : public VisualShaderNode {
GDCLASS(VisualShaderNodeGroupInput, VisualShaderNode);
// Raw pointer to avoid a Ref<> cycle (group owns graph which owns this node).
VisualShaderGroup *group = nullptr;
public:
void set_group(VisualShaderGroup *p_group);
VisualShaderGroup *get_group() const;
virtual int get_input_port_count() const override;
virtual PortType get_input_port_type(int p_port) const override;
virtual String get_input_port_name(int p_port) const override;
virtual int get_output_port_count() const override;
virtual PortType get_output_port_type(int p_port) const override;
virtual String get_output_port_name(int p_port) const override;
virtual bool is_output_port_expandable(int p_port) const override;
virtual String get_caption() const override;
virtual String generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview = false) const override;
void set_input_name(String p_name);
String get_input_name() const;
String get_input_real_name() const;
int get_input_index_count() const;
PortType get_input_index_type(int p_index) const;
String get_input_index_name(int p_index) const;
PortType get_input_type_by_name(String p_name) const;
virtual Vector<StringName> get_editable_properties() const override;
virtual Category get_category() const override { return CATEGORY_INPUT; }
};
class VisualShaderNodeGroupOutput : public VisualShaderNode {
GDCLASS(VisualShaderNodeGroupOutput, VisualShaderNode);
// Raw pointer to avoid a Ref cycle (group owns graph which owns this node).
VisualShaderGroup *group = nullptr;
void _group_changed();
public:
void set_group(VisualShaderGroup *p_group);
VisualShaderGroup *get_group() const;
virtual int get_input_port_count() const override;
virtual PortType get_input_port_type(int p_port) const override;
virtual String get_input_port_name(int p_port) const override;
virtual int get_output_port_count() const override;
virtual PortType get_output_port_type(int p_port) const override;
virtual String get_output_port_name(int p_port) const override;
virtual String get_caption() const override;
virtual String generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview = false) const override;
virtual Category get_category() const override { return CATEGORY_OUTPUT; }
};