visual_shader_particle_nodes.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /**************************************************************************/
  2. /* visual_shader_particle_nodes.h */
  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. #ifndef VISUAL_SHADER_PARTICLE_NODES_H
  31. #define VISUAL_SHADER_PARTICLE_NODES_H
  32. #include "scene/resources/visual_shader.h"
  33. class ImageTexture;
  34. // Emit nodes
  35. class VisualShaderNodeParticleEmitter : public VisualShaderNode {
  36. GDCLASS(VisualShaderNodeParticleEmitter, VisualShaderNode);
  37. protected:
  38. bool mode_2d = false;
  39. static void _bind_methods();
  40. public:
  41. virtual int get_output_port_count() const override;
  42. virtual PortType get_output_port_type(int p_port) const override;
  43. virtual String get_output_port_name(int p_port) const override;
  44. virtual bool has_output_port_preview(int p_port) const override;
  45. virtual void set_mode_2d(bool p_enabled);
  46. bool is_mode_2d() const;
  47. Vector<StringName> get_editable_properties() const override;
  48. virtual HashMap<StringName, String> get_editable_properties_names() const override;
  49. bool is_show_prop_names() const override;
  50. virtual Category get_category() const override { return CATEGORY_PARTICLE; }
  51. VisualShaderNodeParticleEmitter();
  52. };
  53. class VisualShaderNodeParticleSphereEmitter : public VisualShaderNodeParticleEmitter {
  54. GDCLASS(VisualShaderNodeParticleSphereEmitter, VisualShaderNodeParticleEmitter);
  55. public:
  56. virtual String get_caption() const override;
  57. virtual int get_input_port_count() const override;
  58. virtual PortType get_input_port_type(int p_port) const override;
  59. virtual String get_input_port_name(int p_port) const override;
  60. virtual String generate_global_per_node(Shader::Mode p_mode, int p_id) const override;
  61. 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;
  62. VisualShaderNodeParticleSphereEmitter();
  63. };
  64. class VisualShaderNodeParticleBoxEmitter : public VisualShaderNodeParticleEmitter {
  65. GDCLASS(VisualShaderNodeParticleBoxEmitter, VisualShaderNodeParticleEmitter);
  66. public:
  67. virtual String get_caption() const override;
  68. virtual int get_input_port_count() const override;
  69. virtual PortType get_input_port_type(int p_port) const override;
  70. virtual String get_input_port_name(int p_port) const override;
  71. virtual void set_mode_2d(bool p_enabled) override;
  72. virtual String generate_global_per_node(Shader::Mode p_mode, int p_id) const override;
  73. 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;
  74. VisualShaderNodeParticleBoxEmitter();
  75. };
  76. class VisualShaderNodeParticleRingEmitter : public VisualShaderNodeParticleEmitter {
  77. GDCLASS(VisualShaderNodeParticleRingEmitter, VisualShaderNodeParticleEmitter);
  78. public:
  79. virtual String get_caption() const override;
  80. virtual int get_input_port_count() const override;
  81. virtual PortType get_input_port_type(int p_port) const override;
  82. virtual String get_input_port_name(int p_port) const override;
  83. virtual String generate_global_per_node(Shader::Mode p_mode, int p_id) const override;
  84. 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;
  85. VisualShaderNodeParticleRingEmitter();
  86. };
  87. class VisualShaderNodeParticleMeshEmitter : public VisualShaderNodeParticleEmitter {
  88. GDCLASS(VisualShaderNodeParticleMeshEmitter, VisualShaderNodeParticleEmitter);
  89. Ref<Mesh> mesh;
  90. bool use_all_surfaces = true;
  91. int surface_index = 0;
  92. Ref<ImageTexture> position_texture;
  93. Ref<ImageTexture> normal_texture;
  94. Ref<ImageTexture> color_texture;
  95. Ref<ImageTexture> uv_texture;
  96. Ref<ImageTexture> uv2_texture;
  97. String _generate_code(VisualShader::Type p_type, int p_id, const String *p_output_vars, int p_index, const String &p_texture_name, PortType p_port_type) const;
  98. void _update_texture(const Vector<Vector2> &p_array, Ref<ImageTexture> &r_texture);
  99. void _update_texture(const Vector<Vector3> &p_array, Ref<ImageTexture> &r_texture);
  100. void _update_texture(const Vector<Color> &p_array, Ref<ImageTexture> &r_texture);
  101. void _update_textures();
  102. protected:
  103. static void _bind_methods();
  104. public:
  105. virtual String get_caption() const override;
  106. virtual int get_output_port_count() const override;
  107. virtual PortType get_output_port_type(int p_port) const override;
  108. virtual String get_output_port_name(int p_port) const override;
  109. virtual int get_input_port_count() const override;
  110. virtual PortType get_input_port_type(int p_port) const override;
  111. virtual String get_input_port_name(int p_port) const override;
  112. virtual String generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const override;
  113. 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;
  114. void set_mesh(Ref<Mesh> p_mesh);
  115. Ref<Mesh> get_mesh() const;
  116. void set_use_all_surfaces(bool p_enabled);
  117. bool is_use_all_surfaces() const;
  118. void set_surface_index(int p_surface_index);
  119. int get_surface_index() const;
  120. Vector<StringName> get_editable_properties() const override;
  121. HashMap<StringName, String> get_editable_properties_names() const override;
  122. Vector<VisualShader::DefaultTextureParam> get_default_texture_parameters(VisualShader::Type p_type, int p_id) const override;
  123. VisualShaderNodeParticleMeshEmitter();
  124. };
  125. class VisualShaderNodeParticleMultiplyByAxisAngle : public VisualShaderNode {
  126. GDCLASS(VisualShaderNodeParticleMultiplyByAxisAngle, VisualShaderNode);
  127. bool degrees_mode = true;
  128. protected:
  129. static void _bind_methods();
  130. public:
  131. virtual String get_caption() const override;
  132. virtual int get_input_port_count() const override;
  133. virtual PortType get_input_port_type(int p_port) const override;
  134. virtual String get_input_port_name(int p_port) const override;
  135. virtual bool is_show_prop_names() const override;
  136. virtual int get_output_port_count() const override;
  137. virtual PortType get_output_port_type(int p_port) const override;
  138. virtual String get_output_port_name(int p_port) const override;
  139. virtual bool has_output_port_preview(int p_port) const override;
  140. 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;
  141. void set_degrees_mode(bool p_enabled);
  142. bool is_degrees_mode() const;
  143. Vector<StringName> get_editable_properties() const override;
  144. virtual Category get_category() const override { return CATEGORY_PARTICLE; }
  145. VisualShaderNodeParticleMultiplyByAxisAngle();
  146. };
  147. class VisualShaderNodeParticleConeVelocity : public VisualShaderNode {
  148. GDCLASS(VisualShaderNodeParticleConeVelocity, VisualShaderNode);
  149. public:
  150. virtual String get_caption() const override;
  151. virtual int get_input_port_count() const override;
  152. virtual PortType get_input_port_type(int p_port) const override;
  153. virtual String get_input_port_name(int p_port) const override;
  154. virtual int get_output_port_count() const override;
  155. virtual PortType get_output_port_type(int p_port) const override;
  156. virtual String get_output_port_name(int p_port) const override;
  157. virtual bool has_output_port_preview(int p_port) const override;
  158. 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;
  159. virtual Category get_category() const override { return CATEGORY_PARTICLE; }
  160. VisualShaderNodeParticleConeVelocity();
  161. };
  162. class VisualShaderNodeParticleRandomness : public VisualShaderNode {
  163. GDCLASS(VisualShaderNodeParticleRandomness, VisualShaderNode);
  164. public:
  165. enum OpType {
  166. OP_TYPE_SCALAR,
  167. OP_TYPE_VECTOR_2D,
  168. OP_TYPE_VECTOR_3D,
  169. OP_TYPE_VECTOR_4D,
  170. OP_TYPE_MAX,
  171. };
  172. private:
  173. OpType op_type = OP_TYPE_SCALAR;
  174. protected:
  175. static void _bind_methods();
  176. public:
  177. Vector<StringName> get_editable_properties() const override;
  178. virtual String get_caption() const override;
  179. virtual int get_input_port_count() const override;
  180. virtual PortType get_input_port_type(int p_port) const override;
  181. virtual String get_input_port_name(int p_port) const override;
  182. virtual bool is_input_port_default(int p_port, Shader::Mode p_mode) const override;
  183. virtual int get_output_port_count() const override;
  184. virtual PortType get_output_port_type(int p_port) const override;
  185. virtual String get_output_port_name(int p_port) const override;
  186. virtual bool has_output_port_preview(int p_port) const override;
  187. virtual String generate_global_per_node(Shader::Mode p_mode, int p_id) const override;
  188. 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;
  189. void set_op_type(OpType p_type);
  190. OpType get_op_type() const;
  191. virtual Category get_category() const override { return CATEGORY_PARTICLE; }
  192. VisualShaderNodeParticleRandomness();
  193. };
  194. VARIANT_ENUM_CAST(VisualShaderNodeParticleRandomness::OpType)
  195. // Process nodes
  196. class VisualShaderNodeParticleAccelerator : public VisualShaderNode {
  197. GDCLASS(VisualShaderNodeParticleAccelerator, VisualShaderNode);
  198. public:
  199. enum Mode {
  200. MODE_LINEAR,
  201. MODE_RADIAL,
  202. MODE_TANGENTIAL,
  203. MODE_MAX,
  204. };
  205. private:
  206. Mode mode = MODE_LINEAR;
  207. protected:
  208. static void _bind_methods();
  209. public:
  210. Vector<StringName> get_editable_properties() const override;
  211. virtual String get_caption() const override;
  212. virtual int get_input_port_count() const override;
  213. virtual PortType get_input_port_type(int p_port) const override;
  214. virtual String get_input_port_name(int p_port) const override;
  215. virtual int get_output_port_count() const override;
  216. virtual PortType get_output_port_type(int p_port) const override;
  217. virtual String get_output_port_name(int p_port) const override;
  218. virtual bool has_output_port_preview(int p_port) const override;
  219. 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;
  220. void set_mode(Mode p_mode);
  221. Mode get_mode() const;
  222. virtual Category get_category() const override { return CATEGORY_PARTICLE; }
  223. VisualShaderNodeParticleAccelerator();
  224. };
  225. VARIANT_ENUM_CAST(VisualShaderNodeParticleAccelerator::Mode)
  226. // Common nodes
  227. class VisualShaderNodeParticleOutput : public VisualShaderNodeOutput {
  228. GDCLASS(VisualShaderNodeParticleOutput, VisualShaderNodeOutput);
  229. public:
  230. virtual String get_caption() const override;
  231. virtual int get_input_port_count() const override;
  232. virtual PortType get_input_port_type(int p_port) const override;
  233. virtual String get_input_port_name(int p_port) const override;
  234. virtual bool is_port_separator(int p_index) const override;
  235. 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;
  236. VisualShaderNodeParticleOutput();
  237. };
  238. class VisualShaderNodeParticleEmit : public VisualShaderNode {
  239. GDCLASS(VisualShaderNodeParticleEmit, VisualShaderNode);
  240. public:
  241. enum EmitFlags {
  242. EMIT_FLAG_POSITION = 1,
  243. EMIT_FLAG_ROT_SCALE = 2,
  244. EMIT_FLAG_VELOCITY = 4,
  245. EMIT_FLAG_COLOR = 8,
  246. EMIT_FLAG_CUSTOM = 16,
  247. };
  248. protected:
  249. int flags = EMIT_FLAG_POSITION | EMIT_FLAG_ROT_SCALE | EMIT_FLAG_VELOCITY | EMIT_FLAG_COLOR | EMIT_FLAG_CUSTOM;
  250. static void _bind_methods();
  251. public:
  252. Vector<StringName> get_editable_properties() const override;
  253. virtual String get_caption() const override;
  254. virtual int get_input_port_count() const override;
  255. virtual PortType get_input_port_type(int p_port) const override;
  256. virtual String get_input_port_name(int p_port) const override;
  257. virtual int get_output_port_count() const override;
  258. virtual PortType get_output_port_type(int p_port) const override;
  259. virtual String get_output_port_name(int p_port) const override;
  260. void add_flag(EmitFlags p_flag);
  261. bool has_flag(EmitFlags p_flag) const;
  262. void set_flags(EmitFlags p_flags);
  263. EmitFlags get_flags() const;
  264. virtual bool is_show_prop_names() const override;
  265. virtual bool is_generate_input_var(int p_port) const override;
  266. virtual bool is_input_port_default(int p_port, Shader::Mode p_mode) const override;
  267. 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;
  268. virtual Category get_category() const override { return CATEGORY_PARTICLE; }
  269. VisualShaderNodeParticleEmit();
  270. };
  271. VARIANT_ENUM_CAST(VisualShaderNodeParticleEmit::EmitFlags)
  272. #endif // VISUAL_SHADER_PARTICLE_NODES_H