shader_graph.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*************************************************************************/
  2. /* shader_graph.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef SHADER_GRAPH_H
  30. #define SHADER_GRAPH_H
  31. #include "map.h"
  32. #include "scene/resources/shader.h"
  33. class ShaderGraph : public Shader {
  34. OBJ_TYPE( ShaderGraph, Shader );
  35. RES_BASE_EXTENSION("sgp");
  36. public:
  37. enum NodeType {
  38. NODE_INPUT, // all inputs (shader type dependent)
  39. NODE_SCALAR_CONST, //scalar constant
  40. NODE_VEC_CONST, //vec3 constant
  41. NODE_RGB_CONST, //rgb constant (shows a color picker instead)
  42. NODE_XFORM_CONST, // 4x4 matrix constant
  43. NODE_TIME, // time in seconds
  44. NODE_SCREEN_TEX, // screen texture sampler (takes UV) (only usable in fragment shader)
  45. NODE_SCALAR_OP, // scalar vs scalar op (mul, add, div, etc)
  46. NODE_VEC_OP, // vec3 vs vec3 op (mul,ad,div,crossprod,etc)
  47. NODE_VEC_SCALAR_OP, // vec3 vs scalar op (mul, add, div, etc)
  48. NODE_RGB_OP, // vec3 vs vec3 rgb op (with scalar amount), like brighten, darken, burn, dodge, multiply, etc.
  49. NODE_XFORM_MULT, // mat4 x mat4
  50. NODE_XFORM_VEC_MULT, // mat4 x vec3 mult (with no-translation option)
  51. NODE_XFORM_VEC_INV_MULT, // mat4 x vec3 inverse mult (with no-translation option)
  52. NODE_SCALAR_FUNC, // scalar function (sin, cos, etc)
  53. NODE_VEC_FUNC, // vector function (normalize, negate, reciprocal, rgb2hsv, hsv2rgb, etc, etc)
  54. NODE_VEC_LEN, // vec3 length
  55. NODE_DOT_PROD, // vec3 . vec3 (dot product -> scalar output)
  56. NODE_VEC_TO_SCALAR, // 1 vec3 input, 3 scalar outputs
  57. NODE_SCALAR_TO_VEC, // 3 scalar input, 1 vec3 output
  58. NODE_XFORM_TO_VEC, // 3 vec input, 1 xform output
  59. NODE_VEC_TO_XFORM, // 3 vec input, 1 xform output
  60. NODE_SCALAR_INTERP, // scalar interpolation (with optional curve)
  61. NODE_VEC_INTERP, // vec3 interpolation (with optional curve)
  62. /*NODE_SCALAR_CURVE_REMAP,
  63. NODE_VEC_CURVE_REMAP,*/
  64. NODE_SCALAR_INPUT, // scalar uniform (assignable in material)
  65. NODE_VEC_INPUT, // vec3 uniform (assignable in material)
  66. NODE_RGB_INPUT, // color uniform (assignable in material)
  67. NODE_XFORM_INPUT, // mat4 uniform (assignable in material)
  68. NODE_TEXTURE_INPUT, // texture input (assignable in material)
  69. NODE_CUBEMAP_INPUT, // cubemap input (assignable in material)
  70. NODE_OUTPUT, // output (shader type dependent)
  71. NODE_COMMENT, // comment
  72. NODE_TYPE_MAX
  73. };
  74. struct Connection {
  75. int src_id;
  76. int src_slot;
  77. int dst_id;
  78. int dst_slot;
  79. };
  80. enum SlotType {
  81. SLOT_TYPE_SCALAR,
  82. SLOT_TYPE_VEC,
  83. SLOT_TYPE_XFORM,
  84. SLOT_TYPE_TEXTURE,
  85. SLOT_MAX
  86. };
  87. enum ShaderType {
  88. SHADER_TYPE_VERTEX,
  89. SHADER_TYPE_FRAGMENT,
  90. SHADER_TYPE_LIGHT,
  91. SHADER_TYPE_MAX
  92. };
  93. enum SlotDir {
  94. SLOT_IN,
  95. SLOT_OUT
  96. };
  97. enum GraphError {
  98. GRAPH_OK,
  99. GRAPH_ERROR_CYCLIC,
  100. GRAPH_ERROR_MISSING_CONNECTIONS
  101. };
  102. private:
  103. String _find_unique_name(const String& p_base);
  104. struct SourceSlot {
  105. int id;
  106. int slot;
  107. bool operator==(const SourceSlot& p_slot) const {
  108. return id==p_slot.id && slot==p_slot.slot;
  109. }
  110. };
  111. struct Node {
  112. Vector2 pos;
  113. NodeType type;
  114. Variant param1;
  115. Variant param2;
  116. int id;
  117. mutable int order; // used for sorting
  118. int sort_order;
  119. Map<int,SourceSlot> connections;
  120. };
  121. struct ShaderData {
  122. Map<int,Node> node_map;
  123. GraphError error;
  124. } shader[3];
  125. struct InOutParamInfo {
  126. Mode shader_mode;
  127. ShaderType shader_type;
  128. const char *name;
  129. const char *variable;
  130. const char *postfix;
  131. SlotType slot_type;
  132. SlotDir dir;
  133. };
  134. static const InOutParamInfo inout_param_info[];
  135. struct NodeSlotInfo {
  136. enum { MAX_INS=3, MAX_OUTS=3 };
  137. NodeType type;
  138. const SlotType ins[MAX_INS];
  139. const SlotType outs[MAX_OUTS];
  140. };
  141. static const NodeSlotInfo node_slot_info[];
  142. bool _pending_update_shader;
  143. void _update_shader();
  144. void _request_update();
  145. void _add_node_code(ShaderType p_type,Node *p_node,const Vector<String>& p_inputs,String& code);
  146. Array _get_node_list(ShaderType p_type) const;
  147. Array _get_connections(ShaderType p_type) const;
  148. void _set_data(const Dictionary& p_data);
  149. Dictionary _get_data() const;
  150. protected:
  151. static void _bind_methods();
  152. public:
  153. void node_add(ShaderType p_type, NodeType p_node_type, int p_id);
  154. void node_remove(ShaderType p_which,int p_id);
  155. void node_set_pos(ShaderType p_which,int p_id,const Point2& p_pos);
  156. Point2 node_get_pos(ShaderType p_which,int p_id) const;
  157. void get_node_list(ShaderType p_which,List<int> *p_node_list) const;
  158. NodeType node_get_type(ShaderType p_which,int p_id) const;
  159. void scalar_const_node_set_value(ShaderType p_which,int p_id,float p_value);
  160. float scalar_const_node_get_value(ShaderType p_which,int p_id) const;
  161. void vec_const_node_set_value(ShaderType p_which,int p_id,const Vector3& p_value);
  162. Vector3 vec_const_node_get_value(ShaderType p_which,int p_id) const;
  163. void rgb_const_node_set_value(ShaderType p_which,int p_id,const Color& p_value);
  164. Color rgb_const_node_get_value(ShaderType p_which,int p_id) const;
  165. void xform_const_node_set_value(ShaderType p_which,int p_id,const Transform& p_value);
  166. Transform xform_const_node_get_value(ShaderType p_which,int p_id) const;
  167. void texture_node_set_filter_size(ShaderType p_which,int p_id,int p_size);
  168. int texture_node_get_filter_size(ShaderType p_which,int p_id) const;
  169. void texture_node_set_filter_strength(ShaderType p_which,float p_id,float p_strength);
  170. float texture_node_get_filter_strength(ShaderType p_which,float p_id) const;
  171. enum ScalarOp {
  172. SCALAR_OP_ADD,
  173. SCALAR_OP_SUB,
  174. SCALAR_OP_MUL,
  175. SCALAR_OP_DIV,
  176. SCALAR_OP_MOD,
  177. SCALAR_OP_POW,
  178. SCALAR_OP_MAX,
  179. SCALAR_OP_MIN,
  180. SCALAR_OP_ATAN2,
  181. SCALAR_MAX_OP
  182. };
  183. void scalar_op_node_set_op(ShaderType p_which,float p_id,ScalarOp p_op);
  184. ScalarOp scalar_op_node_get_op(ShaderType p_which,float p_id) const;
  185. enum VecOp {
  186. VEC_OP_ADD,
  187. VEC_OP_SUB,
  188. VEC_OP_MUL,
  189. VEC_OP_DIV,
  190. VEC_OP_MOD,
  191. VEC_OP_POW,
  192. VEC_OP_MAX,
  193. VEC_OP_MIN,
  194. VEC_OP_CROSS,
  195. VEC_MAX_OP
  196. };
  197. void vec_op_node_set_op(ShaderType p_which,float p_id,VecOp p_op);
  198. VecOp vec_op_node_get_op(ShaderType p_which,float p_id) const;
  199. enum VecScalarOp {
  200. VEC_SCALAR_OP_MUL,
  201. VEC_SCALAR_OP_DIV,
  202. VEC_SCALAR_OP_POW,
  203. VEC_SCALAR_MAX_OP
  204. };
  205. void vec_scalar_op_node_set_op(ShaderType p_which,float p_id,VecScalarOp p_op);
  206. VecScalarOp vec_scalar_op_node_get_op(ShaderType p_which,float p_id) const;
  207. enum RGBOp {
  208. RGB_OP_SCREEN,
  209. RGB_OP_DIFFERENCE,
  210. RGB_OP_DARKEN,
  211. RGB_OP_LIGHTEN,
  212. RGB_OP_OVERLAY,
  213. RGB_OP_DODGE,
  214. RGB_OP_BURN,
  215. RGB_OP_SOFT_LIGHT,
  216. RGB_OP_HARD_LIGHT,
  217. RGB_MAX_OP
  218. };
  219. void rgb_op_node_set_op(ShaderType p_which,float p_id,RGBOp p_op);
  220. RGBOp rgb_op_node_get_op(ShaderType p_which,float p_id) const;
  221. void xform_vec_mult_node_set_no_translation(ShaderType p_which,int p_id,bool p_no_translation);
  222. bool xform_vec_mult_node_get_no_translation(ShaderType p_which,int p_id) const;
  223. enum ScalarFunc {
  224. SCALAR_FUNC_SIN,
  225. SCALAR_FUNC_COS,
  226. SCALAR_FUNC_TAN,
  227. SCALAR_FUNC_ASIN,
  228. SCALAR_FUNC_ACOS,
  229. SCALAR_FUNC_ATAN,
  230. SCALAR_FUNC_SINH,
  231. SCALAR_FUNC_COSH,
  232. SCALAR_FUNC_TANH,
  233. SCALAR_FUNC_LOG,
  234. SCALAR_FUNC_EXP,
  235. SCALAR_FUNC_SQRT,
  236. SCALAR_FUNC_ABS,
  237. SCALAR_FUNC_SIGN,
  238. SCALAR_FUNC_FLOOR,
  239. SCALAR_FUNC_ROUND,
  240. SCALAR_FUNC_CEIL,
  241. SCALAR_FUNC_FRAC,
  242. SCALAR_FUNC_SATURATE,
  243. SCALAR_FUNC_NEGATE,
  244. SCALAR_MAX_FUNC
  245. };
  246. void scalar_func_node_set_function(ShaderType p_which,int p_id,ScalarFunc p_func);
  247. ScalarFunc scalar_func_node_get_function(ShaderType p_which,int p_id) const;
  248. enum VecFunc {
  249. VEC_FUNC_NORMALIZE,
  250. VEC_FUNC_SATURATE,
  251. VEC_FUNC_NEGATE,
  252. VEC_FUNC_RECIPROCAL,
  253. VEC_FUNC_RGB2HSV,
  254. VEC_FUNC_HSV2RGB,
  255. VEC_MAX_FUNC
  256. };
  257. void vec_func_node_set_function(ShaderType p_which,int p_id,VecFunc p_func);
  258. VecFunc vec_func_node_get_function(ShaderType p_which,int p_id) const;
  259. void input_node_set_name(ShaderType p_which,int p_id,const String& p_name);
  260. String input_node_get_name(ShaderType p_which,int p_id);
  261. void scalar_input_node_set_value(ShaderType p_which,int p_id,float p_value);
  262. float scalar_input_node_get_value(ShaderType p_which,int p_id) const;
  263. void vec_input_node_set_value(ShaderType p_which,int p_id,const Vector3& p_value);
  264. Vector3 vec_input_node_get_value(ShaderType p_which,int p_id) const;
  265. void rgb_input_node_set_value(ShaderType p_which,int p_id,const Color& p_value);
  266. Color rgb_input_node_get_value(ShaderType p_which,int p_id) const;
  267. void xform_input_node_set_value(ShaderType p_which,int p_id,const Transform& p_value);
  268. Transform xform_input_node_get_value(ShaderType p_which,int p_id) const;
  269. void texture_input_node_set_value(ShaderType p_which,int p_id,const Ref<Texture>& p_texture);
  270. Ref<Texture> texture_input_node_get_value(ShaderType p_which,int p_id) const;
  271. void cubemap_input_node_set_value(ShaderType p_which,int p_id,const Ref<CubeMap>& p_cubemap);
  272. Ref<CubeMap> cubemap_input_node_get_value(ShaderType p_which,int p_id) const;
  273. void comment_node_set_text(ShaderType p_which,int p_id,const String& p_comment);
  274. String comment_node_get_text(ShaderType p_which,int p_id) const;
  275. Error connect_node(ShaderType p_which,int p_src_id,int p_src_slot, int p_dst_id,int p_dst_slot);
  276. bool is_node_connected(ShaderType p_which,int p_src_id,int p_src_slot, int p_dst_id,int p_dst_slot) const;
  277. void disconnect_node(ShaderType p_which,int p_src_id,int p_src_slot, int p_dst_id,int p_dst_slot);
  278. void get_node_connections(ShaderType p_which,List<Connection> *p_connections) const;
  279. void clear(ShaderType p_which);
  280. Variant node_get_state(ShaderType p_type, int p_node) const;
  281. void node_set_state(ShaderType p_type, int p_id, const Variant& p_state);
  282. GraphError get_graph_error(ShaderType p_type) const;
  283. static int get_type_input_count(NodeType p_type);
  284. static int get_type_output_count(NodeType p_type);
  285. static SlotType get_type_input_type(NodeType p_type,int p_idx);
  286. static SlotType get_type_output_type(NodeType p_type,int p_idx);
  287. static bool is_type_valid(Mode p_mode,ShaderType p_type);
  288. struct SlotInfo {
  289. String name;
  290. SlotType type;
  291. SlotDir dir;
  292. };
  293. static void get_input_output_node_slot_info(Mode p_mode, ShaderType p_type, List<SlotInfo> *r_slots);
  294. static int get_node_input_slot_count(Mode p_mode, ShaderType p_shader_type,NodeType p_type);
  295. static int get_node_output_slot_count(Mode p_mode, ShaderType p_shader_type,NodeType p_type);
  296. static SlotType get_node_input_slot_type(Mode p_mode, ShaderType p_shader_type,NodeType p_type,int p_idx);
  297. static SlotType get_node_output_slot_type(Mode p_mode, ShaderType p_shader_type,NodeType p_type,int p_idx);
  298. ShaderGraph(Mode p_mode);
  299. ~ShaderGraph();
  300. };
  301. //helper functions
  302. VARIANT_ENUM_CAST( ShaderGraph::NodeType );
  303. VARIANT_ENUM_CAST( ShaderGraph::ShaderType );
  304. VARIANT_ENUM_CAST( ShaderGraph::SlotType );
  305. VARIANT_ENUM_CAST( ShaderGraph::ScalarOp );
  306. VARIANT_ENUM_CAST( ShaderGraph::VecOp );
  307. VARIANT_ENUM_CAST( ShaderGraph::VecScalarOp );
  308. VARIANT_ENUM_CAST( ShaderGraph::RGBOp );
  309. VARIANT_ENUM_CAST( ShaderGraph::ScalarFunc );
  310. VARIANT_ENUM_CAST( ShaderGraph::VecFunc );
  311. VARIANT_ENUM_CAST( ShaderGraph::GraphError );
  312. class MaterialShaderGraph : public ShaderGraph {
  313. OBJ_TYPE( MaterialShaderGraph, ShaderGraph );
  314. RES_BASE_EXTENSION("sgp");
  315. public:
  316. MaterialShaderGraph() : ShaderGraph(MODE_MATERIAL) {
  317. }
  318. };
  319. #endif // SHADER_GRAPH_H