visual_shader_nodes.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927
  1. /*************************************************************************/
  2. /* visual_shader_nodes.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. #include "visual_shader_nodes.h"
  31. ////////////// Scalar
  32. String VisualShaderNodeScalarConstant::get_caption() const {
  33. return "Scalar";
  34. }
  35. int VisualShaderNodeScalarConstant::get_input_port_count() const {
  36. return 0;
  37. }
  38. VisualShaderNodeScalarConstant::PortType VisualShaderNodeScalarConstant::get_input_port_type(int p_port) const {
  39. return PORT_TYPE_SCALAR;
  40. }
  41. String VisualShaderNodeScalarConstant::get_input_port_name(int p_port) const {
  42. return String();
  43. }
  44. int VisualShaderNodeScalarConstant::get_output_port_count() const {
  45. return 1;
  46. }
  47. VisualShaderNodeScalarConstant::PortType VisualShaderNodeScalarConstant::get_output_port_type(int p_port) const {
  48. return PORT_TYPE_SCALAR;
  49. }
  50. String VisualShaderNodeScalarConstant::get_output_port_name(int p_port) const {
  51. return ""; //no output port means the editor will be used as port
  52. }
  53. String VisualShaderNodeScalarConstant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  54. return "\t" + p_output_vars[0] + " = " + vformat("%.6f", constant) + ";\n";
  55. }
  56. void VisualShaderNodeScalarConstant::set_constant(float p_value) {
  57. constant = p_value;
  58. emit_changed();
  59. }
  60. float VisualShaderNodeScalarConstant::get_constant() const {
  61. return constant;
  62. }
  63. Vector<StringName> VisualShaderNodeScalarConstant::get_editable_properties() const {
  64. Vector<StringName> props;
  65. props.push_back("constant");
  66. return props;
  67. }
  68. void VisualShaderNodeScalarConstant::_bind_methods() {
  69. ClassDB::bind_method(D_METHOD("set_constant", "value"), &VisualShaderNodeScalarConstant::set_constant);
  70. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeScalarConstant::get_constant);
  71. ADD_PROPERTY(PropertyInfo(Variant::REAL, "constant"), "set_constant", "get_constant");
  72. }
  73. VisualShaderNodeScalarConstant::VisualShaderNodeScalarConstant() {
  74. constant = 0;
  75. }
  76. ////////////// Color
  77. String VisualShaderNodeColorConstant::get_caption() const {
  78. return "Color";
  79. }
  80. int VisualShaderNodeColorConstant::get_input_port_count() const {
  81. return 0;
  82. }
  83. VisualShaderNodeColorConstant::PortType VisualShaderNodeColorConstant::get_input_port_type(int p_port) const {
  84. return PORT_TYPE_VECTOR;
  85. }
  86. String VisualShaderNodeColorConstant::get_input_port_name(int p_port) const {
  87. return String();
  88. }
  89. int VisualShaderNodeColorConstant::get_output_port_count() const {
  90. return 2;
  91. }
  92. VisualShaderNodeColorConstant::PortType VisualShaderNodeColorConstant::get_output_port_type(int p_port) const {
  93. return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR;
  94. }
  95. String VisualShaderNodeColorConstant::get_output_port_name(int p_port) const {
  96. return p_port == 0 ? "" : "alpha"; //no output port means the editor will be used as port
  97. }
  98. String VisualShaderNodeColorConstant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  99. String code;
  100. code += "\t" + p_output_vars[0] + " = " + vformat("vec3(%.6f,%.6f,%.6f)", constant.r, constant.g, constant.b) + ";\n";
  101. code += "\t" + p_output_vars[1] + " = " + vformat("%.6f", constant.a) + ";\n";
  102. return code;
  103. }
  104. void VisualShaderNodeColorConstant::set_constant(Color p_value) {
  105. constant = p_value;
  106. emit_changed();
  107. }
  108. Color VisualShaderNodeColorConstant::get_constant() const {
  109. return constant;
  110. }
  111. Vector<StringName> VisualShaderNodeColorConstant::get_editable_properties() const {
  112. Vector<StringName> props;
  113. props.push_back("constant");
  114. return props;
  115. }
  116. void VisualShaderNodeColorConstant::_bind_methods() {
  117. ClassDB::bind_method(D_METHOD("set_constant", "value"), &VisualShaderNodeColorConstant::set_constant);
  118. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeColorConstant::get_constant);
  119. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "constant"), "set_constant", "get_constant");
  120. }
  121. VisualShaderNodeColorConstant::VisualShaderNodeColorConstant() {
  122. constant = Color(1, 1, 1, 1);
  123. }
  124. ////////////// Vector
  125. String VisualShaderNodeVec3Constant::get_caption() const {
  126. return "Vector";
  127. }
  128. int VisualShaderNodeVec3Constant::get_input_port_count() const {
  129. return 0;
  130. }
  131. VisualShaderNodeVec3Constant::PortType VisualShaderNodeVec3Constant::get_input_port_type(int p_port) const {
  132. return PORT_TYPE_VECTOR;
  133. }
  134. String VisualShaderNodeVec3Constant::get_input_port_name(int p_port) const {
  135. return String();
  136. }
  137. int VisualShaderNodeVec3Constant::get_output_port_count() const {
  138. return 1;
  139. }
  140. VisualShaderNodeVec3Constant::PortType VisualShaderNodeVec3Constant::get_output_port_type(int p_port) const {
  141. return PORT_TYPE_VECTOR;
  142. }
  143. String VisualShaderNodeVec3Constant::get_output_port_name(int p_port) const {
  144. return ""; //no output port means the editor will be used as port
  145. }
  146. String VisualShaderNodeVec3Constant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  147. return "\t" + p_output_vars[0] + " = " + vformat("vec3(%.6f,%.6f,%.6f)", constant.x, constant.y, constant.z) + ";\n";
  148. }
  149. void VisualShaderNodeVec3Constant::set_constant(Vector3 p_value) {
  150. constant = p_value;
  151. emit_changed();
  152. }
  153. Vector3 VisualShaderNodeVec3Constant::get_constant() const {
  154. return constant;
  155. }
  156. Vector<StringName> VisualShaderNodeVec3Constant::get_editable_properties() const {
  157. Vector<StringName> props;
  158. props.push_back("constant");
  159. return props;
  160. }
  161. void VisualShaderNodeVec3Constant::_bind_methods() {
  162. ClassDB::bind_method(D_METHOD("set_constant", "value"), &VisualShaderNodeVec3Constant::set_constant);
  163. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeVec3Constant::get_constant);
  164. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant"), "set_constant", "get_constant");
  165. }
  166. VisualShaderNodeVec3Constant::VisualShaderNodeVec3Constant() {
  167. }
  168. ////////////// Transform
  169. String VisualShaderNodeTransformConstant::get_caption() const {
  170. return "Transform";
  171. }
  172. int VisualShaderNodeTransformConstant::get_input_port_count() const {
  173. return 0;
  174. }
  175. VisualShaderNodeTransformConstant::PortType VisualShaderNodeTransformConstant::get_input_port_type(int p_port) const {
  176. return PORT_TYPE_VECTOR;
  177. }
  178. String VisualShaderNodeTransformConstant::get_input_port_name(int p_port) const {
  179. return String();
  180. }
  181. int VisualShaderNodeTransformConstant::get_output_port_count() const {
  182. return 1;
  183. }
  184. VisualShaderNodeTransformConstant::PortType VisualShaderNodeTransformConstant::get_output_port_type(int p_port) const {
  185. return PORT_TYPE_TRANSFORM;
  186. }
  187. String VisualShaderNodeTransformConstant::get_output_port_name(int p_port) const {
  188. return ""; //no output port means the editor will be used as port
  189. }
  190. String VisualShaderNodeTransformConstant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  191. Transform t = constant;
  192. t.basis.transpose();
  193. String code = "\t" + p_output_vars[0] + " = mat4(";
  194. code += vformat("vec4(%.6f,%.6f,%.6f,0.0),", t.basis[0].x, t.basis[0].y, t.basis[0].z);
  195. code += vformat("vec4(%.6f,%.6f,%.6f,0.0),", t.basis[1].x, t.basis[1].y, t.basis[1].z);
  196. code += vformat("vec4(%.6f,%.6f,%.6f,0.0),", t.basis[2].x, t.basis[2].y, t.basis[2].z);
  197. code += vformat("vec4(%.6f,%.6f,%.6f,1.0) );\n", t.origin.x, t.origin.y, t.origin.z);
  198. return code;
  199. }
  200. void VisualShaderNodeTransformConstant::set_constant(Transform p_value) {
  201. constant = p_value;
  202. emit_changed();
  203. }
  204. Transform VisualShaderNodeTransformConstant::get_constant() const {
  205. return constant;
  206. }
  207. Vector<StringName> VisualShaderNodeTransformConstant::get_editable_properties() const {
  208. Vector<StringName> props;
  209. props.push_back("constant");
  210. return props;
  211. }
  212. void VisualShaderNodeTransformConstant::_bind_methods() {
  213. ClassDB::bind_method(D_METHOD("set_constant", "value"), &VisualShaderNodeTransformConstant::set_constant);
  214. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeTransformConstant::get_constant);
  215. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "constant"), "set_constant", "get_constant");
  216. }
  217. VisualShaderNodeTransformConstant::VisualShaderNodeTransformConstant() {
  218. }
  219. ////////////// Texture
  220. String VisualShaderNodeTexture::get_caption() const {
  221. return "Texture";
  222. }
  223. int VisualShaderNodeTexture::get_input_port_count() const {
  224. return 2;
  225. }
  226. VisualShaderNodeTexture::PortType VisualShaderNodeTexture::get_input_port_type(int p_port) const {
  227. return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR;
  228. }
  229. String VisualShaderNodeTexture::get_input_port_name(int p_port) const {
  230. return p_port == 0 ? "uv" : "lod";
  231. }
  232. int VisualShaderNodeTexture::get_output_port_count() const {
  233. return 2;
  234. }
  235. VisualShaderNodeTexture::PortType VisualShaderNodeTexture::get_output_port_type(int p_port) const {
  236. return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR;
  237. }
  238. String VisualShaderNodeTexture::get_output_port_name(int p_port) const {
  239. return p_port == 0 ? "rgb" : "alpha";
  240. }
  241. static String make_unique_id(VisualShader::Type p_type, int p_id, const String &p_name) {
  242. static const char *typepf[VisualShader::TYPE_MAX] = { "vtx", "frg", "lgt" };
  243. return p_name + "_" + String(typepf[p_type]) + "_" + itos(p_id);
  244. }
  245. Vector<VisualShader::DefaultTextureParam> VisualShaderNodeTexture::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
  246. VisualShader::DefaultTextureParam dtp;
  247. dtp.name = make_unique_id(p_type, p_id, "tex");
  248. dtp.param = texture;
  249. Vector<VisualShader::DefaultTextureParam> ret;
  250. ret.push_back(dtp);
  251. return ret;
  252. }
  253. String VisualShaderNodeTexture::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  254. if (source == SOURCE_TEXTURE) {
  255. String u = "uniform sampler2D " + make_unique_id(p_type, p_id, "tex");
  256. switch (texture_type) {
  257. case TYPE_DATA: break;
  258. case TYPE_COLOR: u += " : hint_color"; break;
  259. case TYPE_NORMALMAP: u += " : hint_normal"; break;
  260. }
  261. return u + ";";
  262. }
  263. return String();
  264. }
  265. String VisualShaderNodeTexture::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  266. if (source == SOURCE_TEXTURE) {
  267. String id = make_unique_id(p_type, p_id, "tex");
  268. String code;
  269. if (p_input_vars[0] == String()) { //none bound, do nothing
  270. code += "\tvec4 " + id + "_read = vec4(0.0);\n";
  271. } else if (p_input_vars[1] == String()) {
  272. //no lod
  273. code += "\tvec4 " + id + "_read = texture( " + id + " , " + p_input_vars[0] + ".xy );\n";
  274. } else {
  275. code += "\tvec4 " + id + "_read = textureLod( " + id + " , " + p_input_vars[0] + ".xy , " + p_input_vars[1] + " );\n";
  276. }
  277. code += "\t" + p_output_vars[0] + " = " + id + "_read.rgb;\n";
  278. code += "\t" + p_output_vars[1] + " = " + id + "_read.a;\n";
  279. return code;
  280. }
  281. if (source == SOURCE_SCREEN && (p_mode == Shader::MODE_SPATIAL || p_mode == Shader::MODE_CANVAS_ITEM) && p_type == VisualShader::TYPE_FRAGMENT) {
  282. String code = "\t{\n";
  283. if (p_input_vars[0] == String()) { //none bound, do nothing
  284. code += "\t\tvec4 _tex_read = vec4(0.0);\n";
  285. } else if (p_input_vars[1] == String()) {
  286. //no lod
  287. code += "\t\tvec4 _tex_read = textureLod( SCREEN_TEXTURE , " + p_input_vars[0] + ".xy, 0.0 );\n";
  288. } else {
  289. code += "\t\tvec4 _tex_read = textureLod( SCREEN_TEXTURE , " + p_input_vars[0] + ".xy , " + p_input_vars[1] + " );\n";
  290. }
  291. code += "\t\t" + p_output_vars[0] + " = _tex_read.rgb;\n";
  292. code += "\t\t" + p_output_vars[1] + " = _tex_read.a;\n";
  293. code += "\t}\n";
  294. return code;
  295. }
  296. if (source == SOURCE_2D_TEXTURE && p_mode == Shader::MODE_CANVAS_ITEM && p_type == VisualShader::TYPE_FRAGMENT) {
  297. String code = "\t{\n";
  298. if (p_input_vars[0] == String()) { //none bound, do nothing
  299. code += "\t\tvec4 _tex_read = vec4(0.0);\n";
  300. } else if (p_input_vars[1] == String()) {
  301. //no lod
  302. code += "\t\tvec4 _tex_read = texture( TEXTURE , " + p_input_vars[0] + ".xy );\n";
  303. } else {
  304. code += "\t\tvec4 _tex_read = textureLod( TEXTURE , " + p_input_vars[0] + ".xy , " + p_input_vars[1] + " );\n";
  305. }
  306. code += "\t\t" + p_output_vars[0] + " = _tex_read.rgb;\n";
  307. code += "\t\t" + p_output_vars[1] + " = _tex_read.a;\n";
  308. code += "\t}\n";
  309. return code;
  310. }
  311. if (source == SOURCE_2D_NORMAL && p_mode == Shader::MODE_CANVAS_ITEM && p_type == VisualShader::TYPE_FRAGMENT) {
  312. String code = "\t{\n";
  313. if (p_input_vars[0] == String()) { //none bound, do nothing
  314. code += "\t\tvec4 _tex_read = vec4(0.0);\n";
  315. } else if (p_input_vars[1] == String()) {
  316. //no lod
  317. code += "\t\tvec4 _tex_read = texture( NORMAL_TEXTURE , " + p_input_vars[0] + ".xy );\n";
  318. } else {
  319. code += "\t\tvec4 _tex_read = textureLod( NORMAL_TEXTURE , " + p_input_vars[0] + ".xy , " + p_input_vars[1] + " );\n";
  320. }
  321. code += "\t\t" + p_output_vars[0] + " = _tex_read.rgb;\n";
  322. code += "\t\t" + p_output_vars[1] + " = _tex_read.a;\n";
  323. code += "\t}\n";
  324. return code;
  325. }
  326. //none
  327. String code;
  328. code += "\t" + p_output_vars[0] + " = vec3(0.0);\n";
  329. code += "\t" + p_output_vars[1] + " = 1.0;\n";
  330. return code;
  331. }
  332. void VisualShaderNodeTexture::set_source(Source p_source) {
  333. source = p_source;
  334. emit_changed();
  335. emit_signal("editor_refresh_request");
  336. }
  337. VisualShaderNodeTexture::Source VisualShaderNodeTexture::get_source() const {
  338. return source;
  339. }
  340. void VisualShaderNodeTexture::set_texture(Ref<Texture> p_value) {
  341. texture = p_value;
  342. emit_changed();
  343. }
  344. Ref<Texture> VisualShaderNodeTexture::get_texture() const {
  345. return texture;
  346. }
  347. void VisualShaderNodeTexture::set_texture_type(TextureType p_type) {
  348. texture_type = p_type;
  349. emit_changed();
  350. }
  351. VisualShaderNodeTexture::TextureType VisualShaderNodeTexture::get_texture_type() const {
  352. return texture_type;
  353. }
  354. Vector<StringName> VisualShaderNodeTexture::get_editable_properties() const {
  355. Vector<StringName> props;
  356. props.push_back("source");
  357. if (source == SOURCE_TEXTURE) {
  358. props.push_back("texture");
  359. props.push_back("texture_type");
  360. }
  361. return props;
  362. }
  363. String VisualShaderNodeTexture::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  364. if (source == SOURCE_TEXTURE) {
  365. return String(); // all good
  366. }
  367. if (source == SOURCE_SCREEN && (p_mode == Shader::MODE_SPATIAL || p_mode == Shader::MODE_CANVAS_ITEM) && p_type == VisualShader::TYPE_FRAGMENT) {
  368. return String(); // all good
  369. }
  370. if (source == SOURCE_2D_TEXTURE && p_mode == Shader::MODE_CANVAS_ITEM && p_type == VisualShader::TYPE_FRAGMENT) {
  371. return String(); // all good
  372. }
  373. if (source == SOURCE_2D_NORMAL && p_mode == Shader::MODE_CANVAS_ITEM) {
  374. return String(); // all good
  375. }
  376. return TTR("Invalid source for shader.");
  377. }
  378. void VisualShaderNodeTexture::_bind_methods() {
  379. ClassDB::bind_method(D_METHOD("set_source", "value"), &VisualShaderNodeTexture::set_source);
  380. ClassDB::bind_method(D_METHOD("get_source"), &VisualShaderNodeTexture::get_source);
  381. ClassDB::bind_method(D_METHOD("set_texture", "value"), &VisualShaderNodeTexture::set_texture);
  382. ClassDB::bind_method(D_METHOD("get_texture"), &VisualShaderNodeTexture::get_texture);
  383. ClassDB::bind_method(D_METHOD("set_texture_type", "value"), &VisualShaderNodeTexture::set_texture_type);
  384. ClassDB::bind_method(D_METHOD("get_texture_type"), &VisualShaderNodeTexture::get_texture_type);
  385. ADD_PROPERTY(PropertyInfo(Variant::INT, "source", PROPERTY_HINT_ENUM, "Texture,Screen,Texture2D,NormalMap2D"), "set_source", "get_source");
  386. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  387. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_type", PROPERTY_HINT_ENUM, "Data,Color,Normalmap"), "set_texture_type", "get_texture_type");
  388. BIND_ENUM_CONSTANT(SOURCE_TEXTURE);
  389. BIND_ENUM_CONSTANT(SOURCE_SCREEN);
  390. BIND_ENUM_CONSTANT(SOURCE_2D_TEXTURE);
  391. BIND_ENUM_CONSTANT(SOURCE_2D_NORMAL);
  392. BIND_ENUM_CONSTANT(TYPE_DATA);
  393. BIND_ENUM_CONSTANT(TYPE_COLOR);
  394. BIND_ENUM_CONSTANT(TYPE_NORMALMAP);
  395. }
  396. VisualShaderNodeTexture::VisualShaderNodeTexture() {
  397. texture_type = TYPE_DATA;
  398. source = SOURCE_TEXTURE;
  399. }
  400. ////////////// CubeMap
  401. String VisualShaderNodeCubeMap::get_caption() const {
  402. return "CubeMap";
  403. }
  404. int VisualShaderNodeCubeMap::get_input_port_count() const {
  405. return 2;
  406. }
  407. VisualShaderNodeCubeMap::PortType VisualShaderNodeCubeMap::get_input_port_type(int p_port) const {
  408. return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR;
  409. }
  410. String VisualShaderNodeCubeMap::get_input_port_name(int p_port) const {
  411. return p_port == 0 ? "uv" : "lod";
  412. }
  413. int VisualShaderNodeCubeMap::get_output_port_count() const {
  414. return 2;
  415. }
  416. VisualShaderNodeCubeMap::PortType VisualShaderNodeCubeMap::get_output_port_type(int p_port) const {
  417. return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR;
  418. }
  419. String VisualShaderNodeCubeMap::get_output_port_name(int p_port) const {
  420. return p_port == 0 ? "rgb" : "alpha";
  421. }
  422. Vector<VisualShader::DefaultTextureParam> VisualShaderNodeCubeMap::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
  423. VisualShader::DefaultTextureParam dtp;
  424. dtp.name = make_unique_id(p_type, p_id, "cube");
  425. dtp.param = cube_map;
  426. Vector<VisualShader::DefaultTextureParam> ret;
  427. ret.push_back(dtp);
  428. return ret;
  429. }
  430. String VisualShaderNodeCubeMap::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  431. String u = "uniform sampler2DCube " + make_unique_id(p_type, p_id, "cube");
  432. switch (texture_type) {
  433. case TYPE_DATA: break;
  434. case TYPE_COLOR: u += " : hint_color"; break;
  435. case TYPE_NORMALMAP: u += " : hint_normal"; break;
  436. }
  437. return u + ";";
  438. }
  439. String VisualShaderNodeCubeMap::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  440. String id = make_unique_id(p_type, p_id, "cube");
  441. String code;
  442. if (p_input_vars[0] == String()) { //none bound, do nothing
  443. code += "\tvec4 " + id + "_read = vec4(0.0);\n";
  444. } else if (p_input_vars[1] == String()) {
  445. //no lod
  446. code += "\tvec4 " + id + "_read = texture( " + id + " , " + p_input_vars[0] + " );\n";
  447. } else {
  448. code += "\tvec4 " + id + "_read = textureLod( " + id + " , " + p_input_vars[0] + " , " + p_input_vars[1] + " );\n";
  449. }
  450. code += "\t" + p_output_vars[0] + " = " + id + "_read.rgb;\n";
  451. code += "\t" + p_output_vars[1] + " = " + id + "_read.a;\n";
  452. return code;
  453. }
  454. void VisualShaderNodeCubeMap::set_cube_map(Ref<CubeMap> p_value) {
  455. cube_map = p_value;
  456. emit_changed();
  457. }
  458. Ref<CubeMap> VisualShaderNodeCubeMap::get_cube_map() const {
  459. return cube_map;
  460. }
  461. void VisualShaderNodeCubeMap::set_texture_type(TextureType p_type) {
  462. texture_type = p_type;
  463. emit_changed();
  464. }
  465. VisualShaderNodeCubeMap::TextureType VisualShaderNodeCubeMap::get_texture_type() const {
  466. return texture_type;
  467. }
  468. Vector<StringName> VisualShaderNodeCubeMap::get_editable_properties() const {
  469. Vector<StringName> props;
  470. props.push_back("cube_map");
  471. props.push_back("texture_type");
  472. return props;
  473. }
  474. void VisualShaderNodeCubeMap::_bind_methods() {
  475. ClassDB::bind_method(D_METHOD("set_cube_map", "value"), &VisualShaderNodeCubeMap::set_cube_map);
  476. ClassDB::bind_method(D_METHOD("get_cube_map"), &VisualShaderNodeCubeMap::get_cube_map);
  477. ClassDB::bind_method(D_METHOD("set_texture_type", "value"), &VisualShaderNodeCubeMap::set_texture_type);
  478. ClassDB::bind_method(D_METHOD("get_texture_type"), &VisualShaderNodeCubeMap::get_texture_type);
  479. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "cube_map", PROPERTY_HINT_RESOURCE_TYPE, "CubeMap"), "set_cube_map", "get_cube_map");
  480. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_type", PROPERTY_HINT_ENUM, "Data,Color,Normalmap"), "set_texture_type", "get_texture_type");
  481. BIND_ENUM_CONSTANT(TYPE_DATA);
  482. BIND_ENUM_CONSTANT(TYPE_COLOR);
  483. BIND_ENUM_CONSTANT(TYPE_NORMALMAP);
  484. }
  485. VisualShaderNodeCubeMap::VisualShaderNodeCubeMap() {
  486. texture_type = TYPE_DATA;
  487. }
  488. ////////////// Scalar Op
  489. String VisualShaderNodeScalarOp::get_caption() const {
  490. return "ScalarOp";
  491. }
  492. int VisualShaderNodeScalarOp::get_input_port_count() const {
  493. return 2;
  494. }
  495. VisualShaderNodeScalarOp::PortType VisualShaderNodeScalarOp::get_input_port_type(int p_port) const {
  496. return PORT_TYPE_SCALAR;
  497. }
  498. String VisualShaderNodeScalarOp::get_input_port_name(int p_port) const {
  499. return p_port == 0 ? "a" : "b";
  500. }
  501. int VisualShaderNodeScalarOp::get_output_port_count() const {
  502. return 1;
  503. }
  504. VisualShaderNodeScalarOp::PortType VisualShaderNodeScalarOp::get_output_port_type(int p_port) const {
  505. return PORT_TYPE_SCALAR;
  506. }
  507. String VisualShaderNodeScalarOp::get_output_port_name(int p_port) const {
  508. return "op"; //no output port means the editor will be used as port
  509. }
  510. String VisualShaderNodeScalarOp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  511. String code = "\t" + p_output_vars[0] + " = ";
  512. switch (op) {
  513. case OP_ADD: code += p_input_vars[0] + " + " + p_input_vars[1] + ";\n"; break;
  514. case OP_SUB: code += p_input_vars[0] + " - " + p_input_vars[1] + ";\n"; break;
  515. case OP_MUL: code += p_input_vars[0] + " * " + p_input_vars[1] + ";\n"; break;
  516. case OP_DIV: code += p_input_vars[0] + " / " + p_input_vars[1] + ";\n"; break;
  517. case OP_MOD: code += "mod( " + p_input_vars[0] + " , " + p_input_vars[1] + " );\n"; break;
  518. case OP_POW: code += "pow( " + p_input_vars[0] + " , " + p_input_vars[1] + " );\n"; break;
  519. case OP_MAX: code += "max( " + p_input_vars[0] + " , " + p_input_vars[1] + " );\n"; break;
  520. case OP_MIN: code += "min( " + p_input_vars[0] + " , " + p_input_vars[1] + " );\n"; break;
  521. case OP_ATAN2: code += "atan( " + p_input_vars[0] + " , " + p_input_vars[1] + " );\n"; break;
  522. }
  523. return code;
  524. }
  525. void VisualShaderNodeScalarOp::set_operator(Operator p_op) {
  526. op = p_op;
  527. emit_changed();
  528. }
  529. VisualShaderNodeScalarOp::Operator VisualShaderNodeScalarOp::get_operator() const {
  530. return op;
  531. }
  532. Vector<StringName> VisualShaderNodeScalarOp::get_editable_properties() const {
  533. Vector<StringName> props;
  534. props.push_back("operator");
  535. return props;
  536. }
  537. void VisualShaderNodeScalarOp::_bind_methods() {
  538. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeScalarOp::set_operator);
  539. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeScalarOp::get_operator);
  540. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "Add,Sub,Multiply,Divide,Remainder,Power,Max,Min,Atan2"), "set_operator", "get_operator");
  541. BIND_ENUM_CONSTANT(OP_ADD);
  542. BIND_ENUM_CONSTANT(OP_SUB);
  543. BIND_ENUM_CONSTANT(OP_MUL);
  544. BIND_ENUM_CONSTANT(OP_DIV);
  545. BIND_ENUM_CONSTANT(OP_MOD);
  546. BIND_ENUM_CONSTANT(OP_POW);
  547. BIND_ENUM_CONSTANT(OP_MAX);
  548. BIND_ENUM_CONSTANT(OP_MIN);
  549. BIND_ENUM_CONSTANT(OP_ATAN2);
  550. }
  551. VisualShaderNodeScalarOp::VisualShaderNodeScalarOp() {
  552. op = OP_ADD;
  553. set_input_port_default_value(0, 0.0);
  554. set_input_port_default_value(1, 0.0);
  555. }
  556. ////////////// Vector Op
  557. String VisualShaderNodeVectorOp::get_caption() const {
  558. return "VectorOp";
  559. }
  560. int VisualShaderNodeVectorOp::get_input_port_count() const {
  561. return 2;
  562. }
  563. VisualShaderNodeVectorOp::PortType VisualShaderNodeVectorOp::get_input_port_type(int p_port) const {
  564. return PORT_TYPE_VECTOR;
  565. }
  566. String VisualShaderNodeVectorOp::get_input_port_name(int p_port) const {
  567. return p_port == 0 ? "a" : "b";
  568. }
  569. int VisualShaderNodeVectorOp::get_output_port_count() const {
  570. return 1;
  571. }
  572. VisualShaderNodeVectorOp::PortType VisualShaderNodeVectorOp::get_output_port_type(int p_port) const {
  573. return PORT_TYPE_VECTOR;
  574. }
  575. String VisualShaderNodeVectorOp::get_output_port_name(int p_port) const {
  576. return "op"; //no output port means the editor will be used as port
  577. }
  578. String VisualShaderNodeVectorOp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  579. String code = "\t" + p_output_vars[0] + " = ";
  580. switch (op) {
  581. case OP_ADD: code += p_input_vars[0] + " + " + p_input_vars[1] + ";\n"; break;
  582. case OP_SUB: code += p_input_vars[0] + " - " + p_input_vars[1] + ";\n"; break;
  583. case OP_MUL: code += p_input_vars[0] + " * " + p_input_vars[1] + ";\n"; break;
  584. case OP_DIV: code += p_input_vars[0] + " / " + p_input_vars[1] + ";\n"; break;
  585. case OP_MOD: code += "mod( " + p_input_vars[0] + " , " + p_input_vars[1] + " );\n"; break;
  586. case OP_POW: code += "pow( " + p_input_vars[0] + " , " + p_input_vars[1] + " );\n"; break;
  587. case OP_MAX: code += "max( " + p_input_vars[0] + " , " + p_input_vars[1] + " );\n"; break;
  588. case OP_MIN: code += "min( " + p_input_vars[0] + " , " + p_input_vars[1] + " );\n"; break;
  589. case OP_CROSS: code += "cross( " + p_input_vars[0] + " , " + p_input_vars[1] + " );\n"; break;
  590. }
  591. return code;
  592. }
  593. void VisualShaderNodeVectorOp::set_operator(Operator p_op) {
  594. op = p_op;
  595. emit_changed();
  596. }
  597. VisualShaderNodeVectorOp::Operator VisualShaderNodeVectorOp::get_operator() const {
  598. return op;
  599. }
  600. Vector<StringName> VisualShaderNodeVectorOp::get_editable_properties() const {
  601. Vector<StringName> props;
  602. props.push_back("operator");
  603. return props;
  604. }
  605. void VisualShaderNodeVectorOp::_bind_methods() {
  606. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeVectorOp::set_operator);
  607. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeVectorOp::get_operator);
  608. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "Add,Sub,Multiply,Divide,Remainder,Power,Max,Min,Cross"), "set_operator", "get_operator");
  609. BIND_ENUM_CONSTANT(OP_ADD);
  610. BIND_ENUM_CONSTANT(OP_SUB);
  611. BIND_ENUM_CONSTANT(OP_MUL);
  612. BIND_ENUM_CONSTANT(OP_DIV);
  613. BIND_ENUM_CONSTANT(OP_MOD);
  614. BIND_ENUM_CONSTANT(OP_POW);
  615. BIND_ENUM_CONSTANT(OP_MAX);
  616. BIND_ENUM_CONSTANT(OP_MIN);
  617. BIND_ENUM_CONSTANT(OP_CROSS);
  618. }
  619. VisualShaderNodeVectorOp::VisualShaderNodeVectorOp() {
  620. op = OP_ADD;
  621. set_input_port_default_value(0, Vector3());
  622. set_input_port_default_value(1, Vector3());
  623. }
  624. ////////////// Color Op
  625. String VisualShaderNodeColorOp::get_caption() const {
  626. return "ColorOp";
  627. }
  628. int VisualShaderNodeColorOp::get_input_port_count() const {
  629. return 2;
  630. }
  631. VisualShaderNodeColorOp::PortType VisualShaderNodeColorOp::get_input_port_type(int p_port) const {
  632. return PORT_TYPE_VECTOR;
  633. }
  634. String VisualShaderNodeColorOp::get_input_port_name(int p_port) const {
  635. return p_port == 0 ? "a" : "b";
  636. }
  637. int VisualShaderNodeColorOp::get_output_port_count() const {
  638. return 1;
  639. }
  640. VisualShaderNodeColorOp::PortType VisualShaderNodeColorOp::get_output_port_type(int p_port) const {
  641. return PORT_TYPE_VECTOR;
  642. }
  643. String VisualShaderNodeColorOp::get_output_port_name(int p_port) const {
  644. return "op"; //no output port means the editor will be used as port
  645. }
  646. String VisualShaderNodeColorOp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  647. String code;
  648. static const char *axisn[3] = { "x", "y", "z" };
  649. switch (op) {
  650. case OP_SCREEN: {
  651. code += "\t" + p_output_vars[0] + "=vec3(1.0)-(vec3(1.0)-" + p_input_vars[0] + ")*(vec3(1.0)-" + p_input_vars[1] + ");\n";
  652. } break;
  653. case OP_DIFFERENCE: {
  654. code += "\t" + p_output_vars[0] + "=abs(" + p_input_vars[0] + "-" + p_input_vars[1] + ");\n";
  655. } break;
  656. case OP_DARKEN: {
  657. code += "\t" + p_output_vars[0] + "=min(" + p_input_vars[0] + "," + p_input_vars[1] + ");\n";
  658. } break;
  659. case OP_LIGHTEN: {
  660. code += "\t" + p_output_vars[0] + "=max(" + p_input_vars[0] + "," + p_input_vars[1] + ");\n";
  661. } break;
  662. case OP_OVERLAY: {
  663. for (int i = 0; i < 3; i++) {
  664. code += "\t{\n";
  665. code += "\t\tfloat base=" + p_input_vars[0] + "." + axisn[i] + ";\n";
  666. code += "\t\tfloat blend=" + p_input_vars[1] + "." + axisn[i] + ";\n";
  667. code += "\t\tif (base < 0.5) {\n";
  668. code += "\t\t\t" + p_output_vars[0] + "." + axisn[i] + " = 2.0 * base * blend;\n";
  669. code += "\t\t} else {\n";
  670. code += "\t\t\t" + p_output_vars[0] + "." + axisn[i] + " = 1.0 - 2.0 * (1.0 - blend) * (1.0 - base);\n";
  671. code += "\t\t}\n";
  672. code += "\t}\n";
  673. }
  674. } break;
  675. case OP_DODGE: {
  676. code += "\t" + p_output_vars[0] + "=(" + p_input_vars[0] + ")/(vec3(1.0)-" + p_input_vars[1] + ");\n";
  677. } break;
  678. case OP_BURN: {
  679. code += "\t" + p_output_vars[0] + "=vec3(1.0)-(vec3(1.0)-" + p_input_vars[0] + ")/(" + p_input_vars[1] + ");\n";
  680. } break;
  681. case OP_SOFT_LIGHT: {
  682. for (int i = 0; i < 3; i++) {
  683. code += "\t{\n";
  684. code += "\t\tfloat base=" + p_input_vars[0] + "." + axisn[i] + ";\n";
  685. code += "\t\tfloat blend=" + p_input_vars[1] + "." + axisn[i] + ";\n";
  686. code += "\t\tif (base < 0.5) {\n";
  687. code += "\t\t\t" + p_output_vars[0] + "." + axisn[i] + " = (base * (blend+0.5));\n";
  688. code += "\t\t} else {\n";
  689. code += "\t\t\t" + p_output_vars[0] + "." + axisn[i] + " = (1.0 - (1.0-base) * (1.0-(blend-0.5)));\n";
  690. code += "\t\t}\n";
  691. code += "\t}\n";
  692. }
  693. } break;
  694. case OP_HARD_LIGHT: {
  695. for (int i = 0; i < 3; i++) {
  696. code += "\t{\n";
  697. code += "\t\tfloat base=" + p_input_vars[0] + "." + axisn[i] + ";\n";
  698. code += "\t\tfloat blend=" + p_input_vars[1] + "." + axisn[i] + ";\n";
  699. code += "\t\tif (base < 0.5) {\n";
  700. code += "\t\t\t" + p_output_vars[0] + "." + axisn[i] + " = (base * (2.0*blend));\n";
  701. code += "\t\t} else {\n";
  702. code += "\t\t\t" + p_output_vars[0] + "." + axisn[i] + " = (1.0 - (1.0-base) * (1.0-2.0*(blend-0.5)));\n";
  703. code += "\t\t}\n";
  704. code += "\t}\n";
  705. }
  706. } break;
  707. }
  708. return code;
  709. }
  710. void VisualShaderNodeColorOp::set_operator(Operator p_op) {
  711. op = p_op;
  712. emit_changed();
  713. }
  714. VisualShaderNodeColorOp::Operator VisualShaderNodeColorOp::get_operator() const {
  715. return op;
  716. }
  717. Vector<StringName> VisualShaderNodeColorOp::get_editable_properties() const {
  718. Vector<StringName> props;
  719. props.push_back("operator");
  720. return props;
  721. }
  722. void VisualShaderNodeColorOp::_bind_methods() {
  723. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeColorOp::set_operator);
  724. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeColorOp::get_operator);
  725. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "Screen,Difference,Darken,Lighten,Overlay,Dodge,Burn,SoftLight,HardLight"), "set_operator", "get_operator");
  726. BIND_ENUM_CONSTANT(OP_SCREEN);
  727. BIND_ENUM_CONSTANT(OP_DIFFERENCE);
  728. BIND_ENUM_CONSTANT(OP_DARKEN);
  729. BIND_ENUM_CONSTANT(OP_LIGHTEN);
  730. BIND_ENUM_CONSTANT(OP_OVERLAY);
  731. BIND_ENUM_CONSTANT(OP_DODGE);
  732. BIND_ENUM_CONSTANT(OP_BURN);
  733. BIND_ENUM_CONSTANT(OP_SOFT_LIGHT);
  734. BIND_ENUM_CONSTANT(OP_HARD_LIGHT);
  735. }
  736. VisualShaderNodeColorOp::VisualShaderNodeColorOp() {
  737. op = OP_SCREEN;
  738. set_input_port_default_value(0, Vector3());
  739. set_input_port_default_value(1, Vector3());
  740. }
  741. ////////////// Transform Mult
  742. String VisualShaderNodeTransformMult::get_caption() const {
  743. return "TransformMult";
  744. }
  745. int VisualShaderNodeTransformMult::get_input_port_count() const {
  746. return 2;
  747. }
  748. VisualShaderNodeTransformMult::PortType VisualShaderNodeTransformMult::get_input_port_type(int p_port) const {
  749. return PORT_TYPE_TRANSFORM;
  750. }
  751. String VisualShaderNodeTransformMult::get_input_port_name(int p_port) const {
  752. return p_port == 0 ? "a" : "b";
  753. }
  754. int VisualShaderNodeTransformMult::get_output_port_count() const {
  755. return 1;
  756. }
  757. VisualShaderNodeTransformMult::PortType VisualShaderNodeTransformMult::get_output_port_type(int p_port) const {
  758. return PORT_TYPE_TRANSFORM;
  759. }
  760. String VisualShaderNodeTransformMult::get_output_port_name(int p_port) const {
  761. return "mult"; //no output port means the editor will be used as port
  762. }
  763. String VisualShaderNodeTransformMult::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  764. if (op == OP_AxB) {
  765. return "\t" + p_output_vars[0] + " = " + p_input_vars[0] + " * " + p_input_vars[1] + ";\n";
  766. } else {
  767. return "\t" + p_output_vars[0] + " = " + p_input_vars[1] + " * " + p_input_vars[0] + ";\n";
  768. }
  769. }
  770. void VisualShaderNodeTransformMult::set_operator(Operator p_op) {
  771. op = p_op;
  772. emit_changed();
  773. }
  774. VisualShaderNodeTransformMult::Operator VisualShaderNodeTransformMult::get_operator() const {
  775. return op;
  776. }
  777. Vector<StringName> VisualShaderNodeTransformMult::get_editable_properties() const {
  778. Vector<StringName> props;
  779. props.push_back("operator");
  780. return props;
  781. }
  782. void VisualShaderNodeTransformMult::_bind_methods() {
  783. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeTransformMult::set_operator);
  784. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeTransformMult::get_operator);
  785. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "A x B,B x A"), "set_operator", "get_operator");
  786. BIND_ENUM_CONSTANT(OP_AxB);
  787. BIND_ENUM_CONSTANT(OP_BxA);
  788. }
  789. VisualShaderNodeTransformMult::VisualShaderNodeTransformMult() {
  790. op = OP_AxB;
  791. set_input_port_default_value(0, Transform());
  792. set_input_port_default_value(1, Transform());
  793. }
  794. ////////////// TransformVec Mult
  795. String VisualShaderNodeTransformVecMult::get_caption() const {
  796. return "TransformVectorMult";
  797. }
  798. int VisualShaderNodeTransformVecMult::get_input_port_count() const {
  799. return 2;
  800. }
  801. VisualShaderNodeTransformVecMult::PortType VisualShaderNodeTransformVecMult::get_input_port_type(int p_port) const {
  802. return p_port == 0 ? PORT_TYPE_TRANSFORM : PORT_TYPE_VECTOR;
  803. }
  804. String VisualShaderNodeTransformVecMult::get_input_port_name(int p_port) const {
  805. return p_port == 0 ? "a" : "b";
  806. }
  807. int VisualShaderNodeTransformVecMult::get_output_port_count() const {
  808. return 1;
  809. }
  810. VisualShaderNodeTransformVecMult::PortType VisualShaderNodeTransformVecMult::get_output_port_type(int p_port) const {
  811. return PORT_TYPE_VECTOR;
  812. }
  813. String VisualShaderNodeTransformVecMult::get_output_port_name(int p_port) const {
  814. return ""; //no output port means the editor will be used as port
  815. }
  816. String VisualShaderNodeTransformVecMult::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  817. if (op == OP_AxB) {
  818. return "\t" + p_output_vars[0] + " = ( " + p_input_vars[0] + " * vec4(" + p_input_vars[1] + ", 1.0) ).xyz;\n";
  819. } else if (op == OP_BxA) {
  820. return "\t" + p_output_vars[0] + " = ( vec4(" + p_input_vars[1] + ", 1.0) * " + p_input_vars[0] + " ).xyz;\n";
  821. } else if (op == OP_3x3_AxB) {
  822. return "\t" + p_output_vars[0] + " = ( " + p_input_vars[0] + " * vec4(" + p_input_vars[1] + ", 0.0) ).xyz;\n";
  823. } else {
  824. return "\t" + p_output_vars[0] + " = ( vec4(" + p_input_vars[1] + ", 0.0) * " + p_input_vars[0] + " ).xyz;\n";
  825. }
  826. }
  827. void VisualShaderNodeTransformVecMult::set_operator(Operator p_op) {
  828. op = p_op;
  829. emit_changed();
  830. }
  831. VisualShaderNodeTransformVecMult::Operator VisualShaderNodeTransformVecMult::get_operator() const {
  832. return op;
  833. }
  834. Vector<StringName> VisualShaderNodeTransformVecMult::get_editable_properties() const {
  835. Vector<StringName> props;
  836. props.push_back("operator");
  837. return props;
  838. }
  839. void VisualShaderNodeTransformVecMult::_bind_methods() {
  840. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeTransformVecMult::set_operator);
  841. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeTransformVecMult::get_operator);
  842. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "A x B,B x A,A x B (3x3),B x A (3x3)"), "set_operator", "get_operator");
  843. BIND_ENUM_CONSTANT(OP_AxB);
  844. BIND_ENUM_CONSTANT(OP_BxA);
  845. BIND_ENUM_CONSTANT(OP_3x3_AxB);
  846. BIND_ENUM_CONSTANT(OP_3x3_BxA);
  847. }
  848. VisualShaderNodeTransformVecMult::VisualShaderNodeTransformVecMult() {
  849. op = OP_AxB;
  850. set_input_port_default_value(0, Transform());
  851. set_input_port_default_value(1, Vector3());
  852. }
  853. ////////////// Scalar Func
  854. String VisualShaderNodeScalarFunc::get_caption() const {
  855. return "ScalarFunc";
  856. }
  857. int VisualShaderNodeScalarFunc::get_input_port_count() const {
  858. return 1;
  859. }
  860. VisualShaderNodeScalarFunc::PortType VisualShaderNodeScalarFunc::get_input_port_type(int p_port) const {
  861. return PORT_TYPE_SCALAR;
  862. }
  863. String VisualShaderNodeScalarFunc::get_input_port_name(int p_port) const {
  864. return "";
  865. }
  866. int VisualShaderNodeScalarFunc::get_output_port_count() const {
  867. return 1;
  868. }
  869. VisualShaderNodeScalarFunc::PortType VisualShaderNodeScalarFunc::get_output_port_type(int p_port) const {
  870. return PORT_TYPE_SCALAR;
  871. }
  872. String VisualShaderNodeScalarFunc::get_output_port_name(int p_port) const {
  873. return ""; //no output port means the editor will be used as port
  874. }
  875. String VisualShaderNodeScalarFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  876. static const char *scalar_func_id[FUNC_NEGATE + 1] = {
  877. "sin($)",
  878. "cos($)",
  879. "tan($)",
  880. "asin($)",
  881. "acos($)",
  882. "atan($)",
  883. "sinh($)",
  884. "cosh($)",
  885. "tanh($)",
  886. "log($)",
  887. "exp($)",
  888. "sqrt($)",
  889. "abs($)",
  890. "sign($)",
  891. "floor($)",
  892. "round($)",
  893. "ceil($)",
  894. "fract($)",
  895. "min(max($,0),1)",
  896. "-($)",
  897. };
  898. return "\t" + p_output_vars[0] + " = " + String(scalar_func_id[func]).replace("$", p_input_vars[0]) + ";\n";
  899. }
  900. void VisualShaderNodeScalarFunc::set_function(Function p_func) {
  901. func = p_func;
  902. emit_changed();
  903. }
  904. VisualShaderNodeScalarFunc::Function VisualShaderNodeScalarFunc::get_function() const {
  905. return func;
  906. }
  907. Vector<StringName> VisualShaderNodeScalarFunc::get_editable_properties() const {
  908. Vector<StringName> props;
  909. props.push_back("function");
  910. return props;
  911. }
  912. void VisualShaderNodeScalarFunc::_bind_methods() {
  913. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeScalarFunc::set_function);
  914. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeScalarFunc::get_function);
  915. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Sin,Cos,Tan,ASin,ACos,ATan,SinH,CosH,TanH,Log,Exp,Sqrt,Abs,Sign,Floor,Round,Ceil,Frac,Saturate,Negate"), "set_function", "get_function");
  916. BIND_ENUM_CONSTANT(FUNC_SIN);
  917. BIND_ENUM_CONSTANT(FUNC_COS);
  918. BIND_ENUM_CONSTANT(FUNC_TAN);
  919. BIND_ENUM_CONSTANT(FUNC_ASIN);
  920. BIND_ENUM_CONSTANT(FUNC_ACOS);
  921. BIND_ENUM_CONSTANT(FUNC_ATAN);
  922. BIND_ENUM_CONSTANT(FUNC_SINH);
  923. BIND_ENUM_CONSTANT(FUNC_COSH);
  924. BIND_ENUM_CONSTANT(FUNC_TANH);
  925. BIND_ENUM_CONSTANT(FUNC_LOG);
  926. BIND_ENUM_CONSTANT(FUNC_EXP);
  927. BIND_ENUM_CONSTANT(FUNC_SQRT);
  928. BIND_ENUM_CONSTANT(FUNC_ABS);
  929. BIND_ENUM_CONSTANT(FUNC_SIGN);
  930. BIND_ENUM_CONSTANT(FUNC_FLOOR);
  931. BIND_ENUM_CONSTANT(FUNC_ROUND);
  932. BIND_ENUM_CONSTANT(FUNC_CEIL);
  933. BIND_ENUM_CONSTANT(FUNC_FRAC);
  934. BIND_ENUM_CONSTANT(FUNC_SATURATE);
  935. BIND_ENUM_CONSTANT(FUNC_NEGATE);
  936. }
  937. VisualShaderNodeScalarFunc::VisualShaderNodeScalarFunc() {
  938. func = FUNC_SIGN;
  939. set_input_port_default_value(0, 0.0);
  940. }
  941. ////////////// Vector Func
  942. String VisualShaderNodeVectorFunc::get_caption() const {
  943. return "VectorFunc";
  944. }
  945. int VisualShaderNodeVectorFunc::get_input_port_count() const {
  946. return 1;
  947. }
  948. VisualShaderNodeVectorFunc::PortType VisualShaderNodeVectorFunc::get_input_port_type(int p_port) const {
  949. return PORT_TYPE_VECTOR;
  950. }
  951. String VisualShaderNodeVectorFunc::get_input_port_name(int p_port) const {
  952. return "";
  953. }
  954. int VisualShaderNodeVectorFunc::get_output_port_count() const {
  955. return 1;
  956. }
  957. VisualShaderNodeVectorFunc::PortType VisualShaderNodeVectorFunc::get_output_port_type(int p_port) const {
  958. return PORT_TYPE_VECTOR;
  959. }
  960. String VisualShaderNodeVectorFunc::get_output_port_name(int p_port) const {
  961. return ""; //no output port means the editor will be used as port
  962. }
  963. String VisualShaderNodeVectorFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  964. static const char *vec_func_id[FUNC_HSV2RGB + 1] = {
  965. "normalize($)",
  966. "max(min($,vec3(1.0)),vec3(0.0))",
  967. "-($)",
  968. "1.0/($)",
  969. "",
  970. "",
  971. };
  972. String code;
  973. if (func == FUNC_RGB2HSV) {
  974. code += "\t{\n";
  975. code += "\t\tvec3 c = " + p_input_vars[0] + ";\n";
  976. code += "\t\tvec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n";
  977. code += "\t\tvec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n";
  978. code += "\t\tvec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n";
  979. code += "\t\tfloat d = q.x - min(q.w, q.y);\n";
  980. code += "\t\tfloat e = 1.0e-10;\n";
  981. code += "\t\t" + p_output_vars[0] + "=vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n";
  982. code += "\t}\n";
  983. } else if (func == FUNC_HSV2RGB) {
  984. code += "\t{\n";
  985. code += "\t\tvec3 c = " + p_input_vars[0] + ";\n";
  986. code += "\t\tvec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n";
  987. code += "\t\tvec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n";
  988. code += "\t\t" + p_output_vars[0] + "=c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n";
  989. code += "\t}\n";
  990. } else {
  991. code += "\t" + p_output_vars[0] + "=" + String(vec_func_id[func]).replace("$", p_input_vars[0]) + ";\n";
  992. }
  993. return code;
  994. }
  995. void VisualShaderNodeVectorFunc::set_function(Function p_func) {
  996. func = p_func;
  997. emit_changed();
  998. }
  999. VisualShaderNodeVectorFunc::Function VisualShaderNodeVectorFunc::get_function() const {
  1000. return func;
  1001. }
  1002. Vector<StringName> VisualShaderNodeVectorFunc::get_editable_properties() const {
  1003. Vector<StringName> props;
  1004. props.push_back("function");
  1005. return props;
  1006. }
  1007. void VisualShaderNodeVectorFunc::_bind_methods() {
  1008. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeVectorFunc::set_function);
  1009. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeVectorFunc::get_function);
  1010. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Normalize,Saturate,Negate,Reciprocal,RGB2HSV,HSV2RGB"), "set_function", "get_function");
  1011. BIND_ENUM_CONSTANT(FUNC_NORMALIZE);
  1012. BIND_ENUM_CONSTANT(FUNC_SATURATE);
  1013. BIND_ENUM_CONSTANT(FUNC_NEGATE);
  1014. BIND_ENUM_CONSTANT(FUNC_RECIPROCAL);
  1015. BIND_ENUM_CONSTANT(FUNC_RGB2HSV);
  1016. BIND_ENUM_CONSTANT(FUNC_HSV2RGB);
  1017. }
  1018. VisualShaderNodeVectorFunc::VisualShaderNodeVectorFunc() {
  1019. func = FUNC_NORMALIZE;
  1020. set_input_port_default_value(0, Vector3());
  1021. }
  1022. ////////////// Dot Product
  1023. String VisualShaderNodeDotProduct::get_caption() const {
  1024. return "DotProduct";
  1025. }
  1026. int VisualShaderNodeDotProduct::get_input_port_count() const {
  1027. return 2;
  1028. }
  1029. VisualShaderNodeDotProduct::PortType VisualShaderNodeDotProduct::get_input_port_type(int p_port) const {
  1030. return PORT_TYPE_VECTOR;
  1031. }
  1032. String VisualShaderNodeDotProduct::get_input_port_name(int p_port) const {
  1033. return p_port == 0 ? "a" : "b";
  1034. }
  1035. int VisualShaderNodeDotProduct::get_output_port_count() const {
  1036. return 1;
  1037. }
  1038. VisualShaderNodeDotProduct::PortType VisualShaderNodeDotProduct::get_output_port_type(int p_port) const {
  1039. return PORT_TYPE_SCALAR;
  1040. }
  1041. String VisualShaderNodeDotProduct::get_output_port_name(int p_port) const {
  1042. return "dot";
  1043. }
  1044. String VisualShaderNodeDotProduct::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  1045. return "\t" + p_output_vars[0] + " = dot( " + p_input_vars[0] + " , " + p_input_vars[1] + " );\n";
  1046. }
  1047. VisualShaderNodeDotProduct::VisualShaderNodeDotProduct() {
  1048. set_input_port_default_value(0, Vector3());
  1049. set_input_port_default_value(1, Vector3());
  1050. }
  1051. ////////////// Vector Len
  1052. String VisualShaderNodeVectorLen::get_caption() const {
  1053. return "VectorLen";
  1054. }
  1055. int VisualShaderNodeVectorLen::get_input_port_count() const {
  1056. return 1;
  1057. }
  1058. VisualShaderNodeVectorLen::PortType VisualShaderNodeVectorLen::get_input_port_type(int p_port) const {
  1059. return PORT_TYPE_VECTOR;
  1060. }
  1061. String VisualShaderNodeVectorLen::get_input_port_name(int p_port) const {
  1062. return "";
  1063. }
  1064. int VisualShaderNodeVectorLen::get_output_port_count() const {
  1065. return 1;
  1066. }
  1067. VisualShaderNodeVectorLen::PortType VisualShaderNodeVectorLen::get_output_port_type(int p_port) const {
  1068. return PORT_TYPE_SCALAR;
  1069. }
  1070. String VisualShaderNodeVectorLen::get_output_port_name(int p_port) const {
  1071. return "length";
  1072. }
  1073. String VisualShaderNodeVectorLen::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  1074. return "\t" + p_output_vars[0] + " = length( " + p_input_vars[0] + " );\n";
  1075. }
  1076. VisualShaderNodeVectorLen::VisualShaderNodeVectorLen() {
  1077. set_input_port_default_value(0, Vector3());
  1078. }
  1079. ////////////// Scalar Interp
  1080. String VisualShaderNodeScalarInterp::get_caption() const {
  1081. return "ScalarInterp";
  1082. }
  1083. int VisualShaderNodeScalarInterp::get_input_port_count() const {
  1084. return 3;
  1085. }
  1086. VisualShaderNodeScalarInterp::PortType VisualShaderNodeScalarInterp::get_input_port_type(int p_port) const {
  1087. return PORT_TYPE_SCALAR;
  1088. }
  1089. String VisualShaderNodeScalarInterp::get_input_port_name(int p_port) const {
  1090. if (p_port == 0) {
  1091. return "a";
  1092. } else if (p_port == 1) {
  1093. return "b";
  1094. } else {
  1095. return "c";
  1096. }
  1097. }
  1098. int VisualShaderNodeScalarInterp::get_output_port_count() const {
  1099. return 1;
  1100. }
  1101. VisualShaderNodeScalarInterp::PortType VisualShaderNodeScalarInterp::get_output_port_type(int p_port) const {
  1102. return PORT_TYPE_SCALAR;
  1103. }
  1104. String VisualShaderNodeScalarInterp::get_output_port_name(int p_port) const {
  1105. return "mix";
  1106. }
  1107. String VisualShaderNodeScalarInterp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  1108. return "\t" + p_output_vars[0] + " = mix( " + p_input_vars[0] + " , " + p_input_vars[1] + " , " + p_input_vars[2] + " );\n";
  1109. }
  1110. VisualShaderNodeScalarInterp::VisualShaderNodeScalarInterp() {
  1111. set_input_port_default_value(0, 0.0);
  1112. set_input_port_default_value(1, 0.0);
  1113. set_input_port_default_value(2, 0.0);
  1114. }
  1115. ////////////// Vector Interp
  1116. String VisualShaderNodeVectorInterp::get_caption() const {
  1117. return "VectorInterp";
  1118. }
  1119. int VisualShaderNodeVectorInterp::get_input_port_count() const {
  1120. return 3;
  1121. }
  1122. VisualShaderNodeVectorInterp::PortType VisualShaderNodeVectorInterp::get_input_port_type(int p_port) const {
  1123. return PORT_TYPE_VECTOR;
  1124. }
  1125. String VisualShaderNodeVectorInterp::get_input_port_name(int p_port) const {
  1126. if (p_port == 0) {
  1127. return "a";
  1128. } else if (p_port == 1) {
  1129. return "b";
  1130. } else {
  1131. return "c";
  1132. }
  1133. }
  1134. int VisualShaderNodeVectorInterp::get_output_port_count() const {
  1135. return 1;
  1136. }
  1137. VisualShaderNodeVectorInterp::PortType VisualShaderNodeVectorInterp::get_output_port_type(int p_port) const {
  1138. return PORT_TYPE_VECTOR;
  1139. }
  1140. String VisualShaderNodeVectorInterp::get_output_port_name(int p_port) const {
  1141. return "mix";
  1142. }
  1143. String VisualShaderNodeVectorInterp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  1144. return "\t" + p_output_vars[0] + " = mix( " + p_input_vars[0] + " , " + p_input_vars[1] + " , " + p_input_vars[2] + " );\n";
  1145. }
  1146. VisualShaderNodeVectorInterp::VisualShaderNodeVectorInterp() {
  1147. set_input_port_default_value(0, Vector3());
  1148. set_input_port_default_value(1, Vector3());
  1149. set_input_port_default_value(2, Vector3());
  1150. }
  1151. ////////////// Vector Compose
  1152. String VisualShaderNodeVectorCompose::get_caption() const {
  1153. return "VectorCompose";
  1154. }
  1155. int VisualShaderNodeVectorCompose::get_input_port_count() const {
  1156. return 3;
  1157. }
  1158. VisualShaderNodeVectorCompose::PortType VisualShaderNodeVectorCompose::get_input_port_type(int p_port) const {
  1159. return PORT_TYPE_SCALAR;
  1160. }
  1161. String VisualShaderNodeVectorCompose::get_input_port_name(int p_port) const {
  1162. if (p_port == 0) {
  1163. return "x";
  1164. } else if (p_port == 1) {
  1165. return "y";
  1166. } else {
  1167. return "z";
  1168. }
  1169. }
  1170. int VisualShaderNodeVectorCompose::get_output_port_count() const {
  1171. return 1;
  1172. }
  1173. VisualShaderNodeVectorCompose::PortType VisualShaderNodeVectorCompose::get_output_port_type(int p_port) const {
  1174. return PORT_TYPE_VECTOR;
  1175. }
  1176. String VisualShaderNodeVectorCompose::get_output_port_name(int p_port) const {
  1177. return "vec";
  1178. }
  1179. String VisualShaderNodeVectorCompose::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  1180. return "\t" + p_output_vars[0] + " = vec3( " + p_input_vars[0] + " , " + p_input_vars[1] + " , " + p_input_vars[2] + " );\n";
  1181. }
  1182. VisualShaderNodeVectorCompose::VisualShaderNodeVectorCompose() {
  1183. set_input_port_default_value(0, 0.0);
  1184. set_input_port_default_value(1, 0.0);
  1185. set_input_port_default_value(2, 0.0);
  1186. }
  1187. ////////////// Transform Compose
  1188. String VisualShaderNodeTransformCompose::get_caption() const {
  1189. return "TransformCompose";
  1190. }
  1191. int VisualShaderNodeTransformCompose::get_input_port_count() const {
  1192. return 4;
  1193. }
  1194. VisualShaderNodeTransformCompose::PortType VisualShaderNodeTransformCompose::get_input_port_type(int p_port) const {
  1195. return PORT_TYPE_VECTOR;
  1196. }
  1197. String VisualShaderNodeTransformCompose::get_input_port_name(int p_port) const {
  1198. if (p_port == 0) {
  1199. return "x";
  1200. } else if (p_port == 1) {
  1201. return "y";
  1202. } else if (p_port == 2) {
  1203. return "z";
  1204. } else {
  1205. return "origin";
  1206. }
  1207. }
  1208. int VisualShaderNodeTransformCompose::get_output_port_count() const {
  1209. return 1;
  1210. }
  1211. VisualShaderNodeTransformCompose::PortType VisualShaderNodeTransformCompose::get_output_port_type(int p_port) const {
  1212. return PORT_TYPE_TRANSFORM;
  1213. }
  1214. String VisualShaderNodeTransformCompose::get_output_port_name(int p_port) const {
  1215. return "xform";
  1216. }
  1217. String VisualShaderNodeTransformCompose::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  1218. return "\t" + p_output_vars[0] + " = mat4( vec4(" + p_input_vars[0] + ", 0.0) , vec4(" + p_input_vars[1] + ", 0.0) , vec4(" + p_input_vars[2] + ",0.0), vec4(" + p_input_vars[3] + ",1.0) );\n";
  1219. }
  1220. VisualShaderNodeTransformCompose::VisualShaderNodeTransformCompose() {
  1221. set_input_port_default_value(0, Vector3());
  1222. set_input_port_default_value(1, Vector3());
  1223. set_input_port_default_value(2, Vector3());
  1224. set_input_port_default_value(3, Vector3());
  1225. }
  1226. ////////////// Vector Decompose
  1227. String VisualShaderNodeVectorDecompose::get_caption() const {
  1228. return "VectorDecompose";
  1229. }
  1230. int VisualShaderNodeVectorDecompose::get_input_port_count() const {
  1231. return 1;
  1232. }
  1233. VisualShaderNodeVectorDecompose::PortType VisualShaderNodeVectorDecompose::get_input_port_type(int p_port) const {
  1234. return PORT_TYPE_VECTOR;
  1235. }
  1236. String VisualShaderNodeVectorDecompose::get_input_port_name(int p_port) const {
  1237. return "vec";
  1238. }
  1239. int VisualShaderNodeVectorDecompose::get_output_port_count() const {
  1240. return 3;
  1241. }
  1242. VisualShaderNodeVectorDecompose::PortType VisualShaderNodeVectorDecompose::get_output_port_type(int p_port) const {
  1243. return PORT_TYPE_SCALAR;
  1244. }
  1245. String VisualShaderNodeVectorDecompose::get_output_port_name(int p_port) const {
  1246. if (p_port == 0) {
  1247. return "x";
  1248. } else if (p_port == 1) {
  1249. return "y";
  1250. } else {
  1251. return "z";
  1252. }
  1253. }
  1254. String VisualShaderNodeVectorDecompose::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  1255. String code;
  1256. code += "\t" + p_output_vars[0] + " = " + p_input_vars[0] + ".x;\n";
  1257. code += "\t" + p_output_vars[1] + " = " + p_input_vars[0] + ".y;\n";
  1258. code += "\t" + p_output_vars[2] + " = " + p_input_vars[0] + ".z;\n";
  1259. return code;
  1260. }
  1261. VisualShaderNodeVectorDecompose::VisualShaderNodeVectorDecompose() {
  1262. set_input_port_default_value(0, Vector3());
  1263. }
  1264. ////////////// Transform Decompose
  1265. String VisualShaderNodeTransformDecompose::get_caption() const {
  1266. return "TransformDecompose";
  1267. }
  1268. int VisualShaderNodeTransformDecompose::get_input_port_count() const {
  1269. return 1;
  1270. }
  1271. VisualShaderNodeTransformDecompose::PortType VisualShaderNodeTransformDecompose::get_input_port_type(int p_port) const {
  1272. return PORT_TYPE_TRANSFORM;
  1273. }
  1274. String VisualShaderNodeTransformDecompose::get_input_port_name(int p_port) const {
  1275. return "xform";
  1276. }
  1277. int VisualShaderNodeTransformDecompose::get_output_port_count() const {
  1278. return 4;
  1279. }
  1280. VisualShaderNodeTransformDecompose::PortType VisualShaderNodeTransformDecompose::get_output_port_type(int p_port) const {
  1281. return PORT_TYPE_VECTOR;
  1282. }
  1283. String VisualShaderNodeTransformDecompose::get_output_port_name(int p_port) const {
  1284. if (p_port == 0) {
  1285. return "x";
  1286. } else if (p_port == 1) {
  1287. return "y";
  1288. } else if (p_port == 2) {
  1289. return "z";
  1290. } else {
  1291. return "origin";
  1292. }
  1293. }
  1294. String VisualShaderNodeTransformDecompose::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  1295. String code;
  1296. code += "\t" + p_output_vars[0] + " = " + p_input_vars[0] + "[0].xyz;\n";
  1297. code += "\t" + p_output_vars[1] + " = " + p_input_vars[0] + "[1].xyz;\n";
  1298. code += "\t" + p_output_vars[2] + " = " + p_input_vars[0] + "[2].xyz;\n";
  1299. code += "\t" + p_output_vars[3] + " = " + p_input_vars[0] + "[3].xyz;\n";
  1300. return code;
  1301. }
  1302. VisualShaderNodeTransformDecompose::VisualShaderNodeTransformDecompose() {
  1303. set_input_port_default_value(0, Transform());
  1304. }
  1305. ////////////// Scalar Uniform
  1306. String VisualShaderNodeScalarUniform::get_caption() const {
  1307. return "ScalarUniform";
  1308. }
  1309. int VisualShaderNodeScalarUniform::get_input_port_count() const {
  1310. return 0;
  1311. }
  1312. VisualShaderNodeScalarUniform::PortType VisualShaderNodeScalarUniform::get_input_port_type(int p_port) const {
  1313. return PORT_TYPE_SCALAR;
  1314. }
  1315. String VisualShaderNodeScalarUniform::get_input_port_name(int p_port) const {
  1316. return String();
  1317. }
  1318. int VisualShaderNodeScalarUniform::get_output_port_count() const {
  1319. return 1;
  1320. }
  1321. VisualShaderNodeScalarUniform::PortType VisualShaderNodeScalarUniform::get_output_port_type(int p_port) const {
  1322. return PORT_TYPE_SCALAR;
  1323. }
  1324. String VisualShaderNodeScalarUniform::get_output_port_name(int p_port) const {
  1325. return ""; //no output port means the editor will be used as port
  1326. }
  1327. String VisualShaderNodeScalarUniform::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  1328. return "uniform float " + get_uniform_name() + ";\n";
  1329. }
  1330. String VisualShaderNodeScalarUniform::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  1331. return "\t" + p_output_vars[0] + " = " + get_uniform_name() + ";\n";
  1332. }
  1333. VisualShaderNodeScalarUniform::VisualShaderNodeScalarUniform() {
  1334. }
  1335. ////////////// Color Uniform
  1336. String VisualShaderNodeColorUniform::get_caption() const {
  1337. return "ColorUniform";
  1338. }
  1339. int VisualShaderNodeColorUniform::get_input_port_count() const {
  1340. return 0;
  1341. }
  1342. VisualShaderNodeColorUniform::PortType VisualShaderNodeColorUniform::get_input_port_type(int p_port) const {
  1343. return PORT_TYPE_VECTOR;
  1344. }
  1345. String VisualShaderNodeColorUniform::get_input_port_name(int p_port) const {
  1346. return String();
  1347. }
  1348. int VisualShaderNodeColorUniform::get_output_port_count() const {
  1349. return 2;
  1350. }
  1351. VisualShaderNodeColorUniform::PortType VisualShaderNodeColorUniform::get_output_port_type(int p_port) const {
  1352. return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR;
  1353. }
  1354. String VisualShaderNodeColorUniform::get_output_port_name(int p_port) const {
  1355. return p_port == 0 ? "color" : "alpha"; //no output port means the editor will be used as port
  1356. }
  1357. String VisualShaderNodeColorUniform::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  1358. return "uniform vec4 " + get_uniform_name() + " : hint_color;\n";
  1359. }
  1360. String VisualShaderNodeColorUniform::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  1361. String code = "\t" + p_output_vars[0] + " = " + get_uniform_name() + ".rgb;\n";
  1362. code += "\t" + p_output_vars[1] + " = " + get_uniform_name() + ".a;\n";
  1363. return code;
  1364. }
  1365. VisualShaderNodeColorUniform::VisualShaderNodeColorUniform() {
  1366. }
  1367. ////////////// Vector Uniform
  1368. String VisualShaderNodeVec3Uniform::get_caption() const {
  1369. return "VectorUniform";
  1370. }
  1371. int VisualShaderNodeVec3Uniform::get_input_port_count() const {
  1372. return 0;
  1373. }
  1374. VisualShaderNodeVec3Uniform::PortType VisualShaderNodeVec3Uniform::get_input_port_type(int p_port) const {
  1375. return PORT_TYPE_VECTOR;
  1376. }
  1377. String VisualShaderNodeVec3Uniform::get_input_port_name(int p_port) const {
  1378. return String();
  1379. }
  1380. int VisualShaderNodeVec3Uniform::get_output_port_count() const {
  1381. return 1;
  1382. }
  1383. VisualShaderNodeVec3Uniform::PortType VisualShaderNodeVec3Uniform::get_output_port_type(int p_port) const {
  1384. return PORT_TYPE_VECTOR;
  1385. }
  1386. String VisualShaderNodeVec3Uniform::get_output_port_name(int p_port) const {
  1387. return ""; //no output port means the editor will be used as port
  1388. }
  1389. String VisualShaderNodeVec3Uniform::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  1390. return "uniform vec3 " + get_uniform_name() + ";\n";
  1391. }
  1392. String VisualShaderNodeVec3Uniform::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  1393. return "\t" + p_output_vars[0] + " = " + get_uniform_name() + ";\n";
  1394. }
  1395. VisualShaderNodeVec3Uniform::VisualShaderNodeVec3Uniform() {
  1396. }
  1397. ////////////// Transform Uniform
  1398. String VisualShaderNodeTransformUniform::get_caption() const {
  1399. return "TransformUniform";
  1400. }
  1401. int VisualShaderNodeTransformUniform::get_input_port_count() const {
  1402. return 0;
  1403. }
  1404. VisualShaderNodeTransformUniform::PortType VisualShaderNodeTransformUniform::get_input_port_type(int p_port) const {
  1405. return PORT_TYPE_VECTOR;
  1406. }
  1407. String VisualShaderNodeTransformUniform::get_input_port_name(int p_port) const {
  1408. return String();
  1409. }
  1410. int VisualShaderNodeTransformUniform::get_output_port_count() const {
  1411. return 1;
  1412. }
  1413. VisualShaderNodeTransformUniform::PortType VisualShaderNodeTransformUniform::get_output_port_type(int p_port) const {
  1414. return PORT_TYPE_TRANSFORM;
  1415. }
  1416. String VisualShaderNodeTransformUniform::get_output_port_name(int p_port) const {
  1417. return ""; //no output port means the editor will be used as port
  1418. }
  1419. String VisualShaderNodeTransformUniform::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  1420. return "uniform mat4 " + get_uniform_name() + ";\n";
  1421. }
  1422. String VisualShaderNodeTransformUniform::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  1423. return "\t" + p_output_vars[0] + " = " + get_uniform_name() + ";\n";
  1424. }
  1425. VisualShaderNodeTransformUniform::VisualShaderNodeTransformUniform() {
  1426. }
  1427. ////////////// Texture Uniform
  1428. String VisualShaderNodeTextureUniform::get_caption() const {
  1429. return "TextureUniform";
  1430. }
  1431. int VisualShaderNodeTextureUniform::get_input_port_count() const {
  1432. return 2;
  1433. }
  1434. VisualShaderNodeTextureUniform::PortType VisualShaderNodeTextureUniform::get_input_port_type(int p_port) const {
  1435. return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR;
  1436. }
  1437. String VisualShaderNodeTextureUniform::get_input_port_name(int p_port) const {
  1438. return p_port == 0 ? "uv" : "lod";
  1439. }
  1440. int VisualShaderNodeTextureUniform::get_output_port_count() const {
  1441. return 2;
  1442. }
  1443. VisualShaderNodeTextureUniform::PortType VisualShaderNodeTextureUniform::get_output_port_type(int p_port) const {
  1444. return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR;
  1445. }
  1446. String VisualShaderNodeTextureUniform::get_output_port_name(int p_port) const {
  1447. return p_port == 0 ? "rgb" : "alpha";
  1448. }
  1449. String VisualShaderNodeTextureUniform::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  1450. String code = "uniform sampler2D " + get_uniform_name();
  1451. switch (texture_type) {
  1452. case TYPE_DATA:
  1453. if (color_default == COLOR_DEFAULT_BLACK)
  1454. code += " : hint_black;\n";
  1455. else
  1456. code += ";\n";
  1457. break;
  1458. case TYPE_COLOR:
  1459. if (color_default == COLOR_DEFAULT_BLACK)
  1460. code += " : hint_black_albedo;\n";
  1461. else
  1462. code += " : hint_albedo;\n";
  1463. break;
  1464. case TYPE_NORMALMAP: code += " : hint_normal;\n"; break;
  1465. case TYPE_ANISO: code += " : hint_aniso;\n"; break;
  1466. }
  1467. return code;
  1468. }
  1469. String VisualShaderNodeTextureUniform::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  1470. String id = get_uniform_name();
  1471. String code = "\t{\n";
  1472. if (p_input_vars[0] == String()) { //none bound, do nothing
  1473. code += "\t\tvec4 n_tex_read = vec4(0.0);\n";
  1474. } else if (p_input_vars[1] == String()) {
  1475. //no lod
  1476. code += "\t\tvec4 n_tex_read = texture( " + id + " , " + p_input_vars[0] + ".xy );\n";
  1477. } else {
  1478. code += "\t\tvec4 n_tex_read = textureLod( " + id + " , " + p_input_vars[0] + ".xy , " + p_input_vars[1] + " );\n";
  1479. }
  1480. code += "\t\t" + p_output_vars[0] + " = n_tex_read.rgb;\n";
  1481. code += "\t\t" + p_output_vars[1] + " = n_tex_read.a;\n";
  1482. code += "\t}\n";
  1483. return code;
  1484. }
  1485. void VisualShaderNodeTextureUniform::set_texture_type(TextureType p_type) {
  1486. texture_type = p_type;
  1487. emit_changed();
  1488. }
  1489. VisualShaderNodeTextureUniform::TextureType VisualShaderNodeTextureUniform::get_texture_type() const {
  1490. return texture_type;
  1491. }
  1492. void VisualShaderNodeTextureUniform::set_color_default(ColorDefault p_default) {
  1493. color_default = p_default;
  1494. emit_changed();
  1495. }
  1496. VisualShaderNodeTextureUniform::ColorDefault VisualShaderNodeTextureUniform::get_color_default() const {
  1497. return color_default;
  1498. }
  1499. Vector<StringName> VisualShaderNodeTextureUniform::get_editable_properties() const {
  1500. Vector<StringName> props;
  1501. props.push_back("texture_type");
  1502. props.push_back("color_default");
  1503. return props;
  1504. }
  1505. void VisualShaderNodeTextureUniform::_bind_methods() {
  1506. ClassDB::bind_method(D_METHOD("set_texture_type", "type"), &VisualShaderNodeTextureUniform::set_texture_type);
  1507. ClassDB::bind_method(D_METHOD("get_texture_type"), &VisualShaderNodeTextureUniform::get_texture_type);
  1508. ClassDB::bind_method(D_METHOD("set_color_default", "type"), &VisualShaderNodeTextureUniform::set_color_default);
  1509. ClassDB::bind_method(D_METHOD("get_color_default"), &VisualShaderNodeTextureUniform::get_color_default);
  1510. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_type", PROPERTY_HINT_ENUM, "Data,Color,Normalmap,Aniso"), "set_texture_type", "get_texture_type");
  1511. ADD_PROPERTY(PropertyInfo(Variant::INT, "color_default", PROPERTY_HINT_ENUM, "White Default,Black Default"), "set_color_default", "get_color_default");
  1512. BIND_ENUM_CONSTANT(TYPE_DATA);
  1513. BIND_ENUM_CONSTANT(TYPE_COLOR);
  1514. BIND_ENUM_CONSTANT(TYPE_NORMALMAP);
  1515. BIND_ENUM_CONSTANT(TYPE_ANISO);
  1516. BIND_ENUM_CONSTANT(COLOR_DEFAULT_WHITE);
  1517. BIND_ENUM_CONSTANT(COLOR_DEFAULT_BLACK);
  1518. }
  1519. VisualShaderNodeTextureUniform::VisualShaderNodeTextureUniform() {
  1520. texture_type = TYPE_DATA;
  1521. color_default = COLOR_DEFAULT_WHITE;
  1522. }
  1523. ////////////// CubeMap Uniform
  1524. String VisualShaderNodeCubeMapUniform::get_caption() const {
  1525. return "CubeMapUniform";
  1526. }
  1527. int VisualShaderNodeCubeMapUniform::get_input_port_count() const {
  1528. return 2;
  1529. }
  1530. VisualShaderNodeCubeMapUniform::PortType VisualShaderNodeCubeMapUniform::get_input_port_type(int p_port) const {
  1531. return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR;
  1532. }
  1533. String VisualShaderNodeCubeMapUniform::get_input_port_name(int p_port) const {
  1534. return p_port == 0 ? "normal" : "lod";
  1535. }
  1536. int VisualShaderNodeCubeMapUniform::get_output_port_count() const {
  1537. return 2;
  1538. }
  1539. VisualShaderNodeCubeMapUniform::PortType VisualShaderNodeCubeMapUniform::get_output_port_type(int p_port) const {
  1540. return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR;
  1541. }
  1542. String VisualShaderNodeCubeMapUniform::get_output_port_name(int p_port) const {
  1543. return p_port == 0 ? "rgb" : "alpha";
  1544. }
  1545. String VisualShaderNodeCubeMapUniform::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  1546. return String();
  1547. }
  1548. VisualShaderNodeCubeMapUniform::VisualShaderNodeCubeMapUniform() {
  1549. }