material.cpp 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433
  1. /*************************************************************************/
  2. /* material.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "material.h"
  31. #include "core/engine.h"
  32. #ifdef TOOLS_ENABLED
  33. #include "editor/editor_settings.h"
  34. #endif
  35. #include "scene/scene_string_names.h"
  36. void Material::set_next_pass(const Ref<Material> &p_pass) {
  37. for (Ref<Material> pass_child = p_pass; pass_child != NULL; pass_child = pass_child->get_next_pass()) {
  38. ERR_FAIL_COND_MSG(pass_child == this, "Can't set as next_pass one of its parents to prevent crashes due to recursive loop.");
  39. }
  40. if (next_pass == p_pass)
  41. return;
  42. next_pass = p_pass;
  43. RID next_pass_rid;
  44. if (next_pass.is_valid())
  45. next_pass_rid = next_pass->get_rid();
  46. VS::get_singleton()->material_set_next_pass(material, next_pass_rid);
  47. }
  48. Ref<Material> Material::get_next_pass() const {
  49. return next_pass;
  50. }
  51. void Material::set_render_priority(int p_priority) {
  52. ERR_FAIL_COND(p_priority < RENDER_PRIORITY_MIN);
  53. ERR_FAIL_COND(p_priority > RENDER_PRIORITY_MAX);
  54. render_priority = p_priority;
  55. VS::get_singleton()->material_set_render_priority(material, p_priority);
  56. }
  57. int Material::get_render_priority() const {
  58. return render_priority;
  59. }
  60. RID Material::get_rid() const {
  61. return material;
  62. }
  63. void Material::_validate_property(PropertyInfo &property) const {
  64. if (!_can_do_next_pass() && property.name == "next_pass") {
  65. property.usage = 0;
  66. }
  67. }
  68. void Material::_bind_methods() {
  69. ClassDB::bind_method(D_METHOD("set_next_pass", "next_pass"), &Material::set_next_pass);
  70. ClassDB::bind_method(D_METHOD("get_next_pass"), &Material::get_next_pass);
  71. ClassDB::bind_method(D_METHOD("set_render_priority", "priority"), &Material::set_render_priority);
  72. ClassDB::bind_method(D_METHOD("get_render_priority"), &Material::get_render_priority);
  73. ADD_PROPERTY(PropertyInfo(Variant::INT, "render_priority", PROPERTY_HINT_RANGE, itos(RENDER_PRIORITY_MIN) + "," + itos(RENDER_PRIORITY_MAX) + ",1"), "set_render_priority", "get_render_priority");
  74. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "next_pass", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_next_pass", "get_next_pass");
  75. BIND_CONSTANT(RENDER_PRIORITY_MAX);
  76. BIND_CONSTANT(RENDER_PRIORITY_MIN);
  77. }
  78. Material::Material() {
  79. material = VisualServer::get_singleton()->material_create();
  80. render_priority = 0;
  81. }
  82. Material::~Material() {
  83. VisualServer::get_singleton()->free(material);
  84. }
  85. ///////////////////////////////////
  86. bool ShaderMaterial::_set(const StringName &p_name, const Variant &p_value) {
  87. if (shader.is_valid()) {
  88. StringName pr = shader->remap_param(p_name);
  89. if (!pr) {
  90. String n = p_name;
  91. if (n.find("param/") == 0) { //backwards compatibility
  92. pr = n.substr(6, n.length());
  93. }
  94. if (n.find("shader_param/") == 0) { //backwards compatibility
  95. pr = n.replace_first("shader_param/", "");
  96. }
  97. }
  98. if (pr) {
  99. VisualServer::get_singleton()->material_set_param(_get_material(), pr, p_value);
  100. return true;
  101. }
  102. }
  103. return false;
  104. }
  105. bool ShaderMaterial::_get(const StringName &p_name, Variant &r_ret) const {
  106. if (shader.is_valid()) {
  107. StringName pr = shader->remap_param(p_name);
  108. if (!pr) {
  109. String n = p_name;
  110. if (n.find("param/") == 0) { //backwards compatibility
  111. pr = n.substr(6, n.length());
  112. }
  113. if (n.find("shader_param/") == 0) { //backwards compatibility
  114. pr = n.replace_first("shader_param/", "");
  115. }
  116. }
  117. if (pr) {
  118. r_ret = VisualServer::get_singleton()->material_get_param(_get_material(), pr);
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. void ShaderMaterial::_get_property_list(List<PropertyInfo> *p_list) const {
  125. if (!shader.is_null()) {
  126. shader->get_param_list(p_list);
  127. }
  128. }
  129. bool ShaderMaterial::property_can_revert(const String &p_name) {
  130. if (shader.is_valid()) {
  131. StringName pr = shader->remap_param(p_name);
  132. if (pr) {
  133. Variant default_value = VisualServer::get_singleton()->material_get_param_default(_get_material(), pr);
  134. Variant current_value;
  135. _get(p_name, current_value);
  136. return default_value.get_type() != Variant::NIL && default_value != current_value;
  137. }
  138. }
  139. return false;
  140. }
  141. Variant ShaderMaterial::property_get_revert(const String &p_name) {
  142. Variant r_ret;
  143. if (shader.is_valid()) {
  144. StringName pr = shader->remap_param(p_name);
  145. if (pr) {
  146. r_ret = VisualServer::get_singleton()->material_get_param_default(_get_material(), pr);
  147. }
  148. }
  149. return r_ret;
  150. }
  151. void ShaderMaterial::set_shader(const Ref<Shader> &p_shader) {
  152. // Only connect/disconnect the signal when running in the editor.
  153. // This can be a slow operation, and `_change_notify()` (which is called by `_shader_changed()`)
  154. // does nothing in non-editor builds anyway. See GH-34741 for details.
  155. if (shader.is_valid() && Engine::get_singleton()->is_editor_hint()) {
  156. shader->disconnect("changed", this, "_shader_changed");
  157. }
  158. shader = p_shader;
  159. RID rid;
  160. if (shader.is_valid()) {
  161. rid = shader->get_rid();
  162. if (Engine::get_singleton()->is_editor_hint()) {
  163. shader->connect("changed", this, "_shader_changed");
  164. }
  165. }
  166. VS::get_singleton()->material_set_shader(_get_material(), rid);
  167. _change_notify(); //properties for shader exposed
  168. emit_changed();
  169. }
  170. Ref<Shader> ShaderMaterial::get_shader() const {
  171. return shader;
  172. }
  173. void ShaderMaterial::set_shader_param(const StringName &p_param, const Variant &p_value) {
  174. VS::get_singleton()->material_set_param(_get_material(), p_param, p_value);
  175. }
  176. Variant ShaderMaterial::get_shader_param(const StringName &p_param) const {
  177. return VS::get_singleton()->material_get_param(_get_material(), p_param);
  178. }
  179. void ShaderMaterial::_shader_changed() {
  180. _change_notify(); //update all properties
  181. }
  182. void ShaderMaterial::_bind_methods() {
  183. ClassDB::bind_method(D_METHOD("set_shader", "shader"), &ShaderMaterial::set_shader);
  184. ClassDB::bind_method(D_METHOD("get_shader"), &ShaderMaterial::get_shader);
  185. ClassDB::bind_method(D_METHOD("set_shader_param", "param", "value"), &ShaderMaterial::set_shader_param);
  186. ClassDB::bind_method(D_METHOD("get_shader_param", "param"), &ShaderMaterial::get_shader_param);
  187. ClassDB::bind_method(D_METHOD("_shader_changed"), &ShaderMaterial::_shader_changed);
  188. ClassDB::bind_method(D_METHOD("property_can_revert", "name"), &ShaderMaterial::property_can_revert);
  189. ClassDB::bind_method(D_METHOD("property_get_revert", "name"), &ShaderMaterial::property_get_revert);
  190. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shader", PROPERTY_HINT_RESOURCE_TYPE, "Shader"), "set_shader", "get_shader");
  191. }
  192. void ShaderMaterial::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  193. #ifdef TOOLS_ENABLED
  194. const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", 0) ? "'" : "\"";
  195. #else
  196. const String quote_style = "\"";
  197. #endif
  198. String f = p_function.operator String();
  199. if ((f == "get_shader_param" || f == "set_shader_param") && p_idx == 0) {
  200. if (shader.is_valid()) {
  201. List<PropertyInfo> pl;
  202. shader->get_param_list(&pl);
  203. for (List<PropertyInfo>::Element *E = pl.front(); E; E = E->next()) {
  204. r_options->push_back(quote_style + E->get().name.replace_first("shader_param/", "") + quote_style);
  205. }
  206. }
  207. }
  208. Resource::get_argument_options(p_function, p_idx, r_options);
  209. }
  210. bool ShaderMaterial::_can_do_next_pass() const {
  211. return shader.is_valid() && shader->get_mode() == Shader::MODE_SPATIAL;
  212. }
  213. Shader::Mode ShaderMaterial::get_shader_mode() const {
  214. if (shader.is_valid())
  215. return shader->get_mode();
  216. else
  217. return Shader::MODE_SPATIAL;
  218. }
  219. ShaderMaterial::ShaderMaterial() {
  220. }
  221. ShaderMaterial::~ShaderMaterial() {
  222. }
  223. /////////////////////////////////
  224. Mutex SpatialMaterial::material_mutex;
  225. SelfList<SpatialMaterial>::List *SpatialMaterial::dirty_materials = NULL;
  226. Map<SpatialMaterial::MaterialKey, SpatialMaterial::ShaderData> SpatialMaterial::shader_map;
  227. SpatialMaterial::ShaderNames *SpatialMaterial::shader_names = NULL;
  228. void SpatialMaterial::init_shaders() {
  229. dirty_materials = memnew(SelfList<SpatialMaterial>::List);
  230. shader_names = memnew(ShaderNames);
  231. shader_names->albedo = "albedo";
  232. shader_names->specular = "specular";
  233. shader_names->roughness = "roughness";
  234. shader_names->metallic = "metallic";
  235. shader_names->emission = "emission";
  236. shader_names->emission_energy = "emission_energy";
  237. shader_names->normal_scale = "normal_scale";
  238. shader_names->rim = "rim";
  239. shader_names->rim_tint = "rim_tint";
  240. shader_names->clearcoat = "clearcoat";
  241. shader_names->clearcoat_gloss = "clearcoat_gloss";
  242. shader_names->anisotropy = "anisotropy_ratio";
  243. shader_names->depth_scale = "depth_scale";
  244. shader_names->subsurface_scattering_strength = "subsurface_scattering_strength";
  245. shader_names->transmission = "transmission";
  246. shader_names->refraction = "refraction";
  247. shader_names->point_size = "point_size";
  248. shader_names->uv1_scale = "uv1_scale";
  249. shader_names->uv1_offset = "uv1_offset";
  250. shader_names->uv2_scale = "uv2_scale";
  251. shader_names->uv2_offset = "uv2_offset";
  252. shader_names->uv1_blend_sharpness = "uv1_blend_sharpness";
  253. shader_names->uv2_blend_sharpness = "uv2_blend_sharpness";
  254. shader_names->particles_anim_h_frames = "particles_anim_h_frames";
  255. shader_names->particles_anim_v_frames = "particles_anim_v_frames";
  256. shader_names->particles_anim_loop = "particles_anim_loop";
  257. shader_names->depth_min_layers = "depth_min_layers";
  258. shader_names->depth_max_layers = "depth_max_layers";
  259. shader_names->depth_flip = "depth_flip";
  260. shader_names->grow = "grow";
  261. shader_names->ao_light_affect = "ao_light_affect";
  262. shader_names->proximity_fade_distance = "proximity_fade_distance";
  263. shader_names->distance_fade_min = "distance_fade_min";
  264. shader_names->distance_fade_max = "distance_fade_max";
  265. shader_names->metallic_texture_channel = "metallic_texture_channel";
  266. shader_names->roughness_texture_channel = "roughness_texture_channel";
  267. shader_names->ao_texture_channel = "ao_texture_channel";
  268. shader_names->clearcoat_texture_channel = "clearcoat_texture_channel";
  269. shader_names->rim_texture_channel = "rim_texture_channel";
  270. shader_names->depth_texture_channel = "depth_texture_channel";
  271. shader_names->refraction_texture_channel = "refraction_texture_channel";
  272. shader_names->alpha_scissor_threshold = "alpha_scissor_threshold";
  273. shader_names->texture_names[TEXTURE_ALBEDO] = "texture_albedo";
  274. shader_names->texture_names[TEXTURE_METALLIC] = "texture_metallic";
  275. shader_names->texture_names[TEXTURE_ROUGHNESS] = "texture_roughness";
  276. shader_names->texture_names[TEXTURE_EMISSION] = "texture_emission";
  277. shader_names->texture_names[TEXTURE_NORMAL] = "texture_normal";
  278. shader_names->texture_names[TEXTURE_RIM] = "texture_rim";
  279. shader_names->texture_names[TEXTURE_CLEARCOAT] = "texture_clearcoat";
  280. shader_names->texture_names[TEXTURE_FLOWMAP] = "texture_flowmap";
  281. shader_names->texture_names[TEXTURE_AMBIENT_OCCLUSION] = "texture_ambient_occlusion";
  282. shader_names->texture_names[TEXTURE_DEPTH] = "texture_depth";
  283. shader_names->texture_names[TEXTURE_SUBSURFACE_SCATTERING] = "texture_subsurface_scattering";
  284. shader_names->texture_names[TEXTURE_TRANSMISSION] = "texture_transmission";
  285. shader_names->texture_names[TEXTURE_REFRACTION] = "texture_refraction";
  286. shader_names->texture_names[TEXTURE_DETAIL_MASK] = "texture_detail_mask";
  287. shader_names->texture_names[TEXTURE_DETAIL_ALBEDO] = "texture_detail_albedo";
  288. shader_names->texture_names[TEXTURE_DETAIL_NORMAL] = "texture_detail_normal";
  289. }
  290. Ref<SpatialMaterial> SpatialMaterial::materials_for_2d[SpatialMaterial::MAX_MATERIALS_FOR_2D];
  291. void SpatialMaterial::finish_shaders() {
  292. for (int i = 0; i < MAX_MATERIALS_FOR_2D; i++) {
  293. materials_for_2d[i].unref();
  294. }
  295. memdelete(dirty_materials);
  296. dirty_materials = NULL;
  297. memdelete(shader_names);
  298. }
  299. void SpatialMaterial::_update_shader() {
  300. dirty_materials->remove(&element);
  301. MaterialKey mk = _compute_key();
  302. if (mk.key == current_key.key)
  303. return; //no update required in the end
  304. if (shader_map.has(current_key)) {
  305. shader_map[current_key].users--;
  306. if (shader_map[current_key].users == 0) {
  307. //deallocate shader, as it's no longer in use
  308. VS::get_singleton()->free(shader_map[current_key].shader);
  309. shader_map.erase(current_key);
  310. }
  311. }
  312. current_key = mk;
  313. if (shader_map.has(mk)) {
  314. VS::get_singleton()->material_set_shader(_get_material(), shader_map[mk].shader);
  315. shader_map[mk].users++;
  316. return;
  317. }
  318. //must create a shader!
  319. String code = "shader_type spatial;\nrender_mode ";
  320. switch (blend_mode) {
  321. case BLEND_MODE_MIX: code += "blend_mix"; break;
  322. case BLEND_MODE_ADD: code += "blend_add"; break;
  323. case BLEND_MODE_SUB: code += "blend_sub"; break;
  324. case BLEND_MODE_MUL: code += "blend_mul"; break;
  325. }
  326. DepthDrawMode ddm = depth_draw_mode;
  327. if (features[FEATURE_REFRACTION]) {
  328. ddm = DEPTH_DRAW_ALWAYS;
  329. }
  330. switch (ddm) {
  331. case DEPTH_DRAW_OPAQUE_ONLY: code += ",depth_draw_opaque"; break;
  332. case DEPTH_DRAW_ALWAYS: code += ",depth_draw_always"; break;
  333. case DEPTH_DRAW_DISABLED: code += ",depth_draw_never"; break;
  334. case DEPTH_DRAW_ALPHA_OPAQUE_PREPASS: code += ",depth_draw_alpha_prepass"; break;
  335. }
  336. switch (cull_mode) {
  337. case CULL_BACK: code += ",cull_back"; break;
  338. case CULL_FRONT: code += ",cull_front"; break;
  339. case CULL_DISABLED: code += ",cull_disabled"; break;
  340. }
  341. switch (diffuse_mode) {
  342. case DIFFUSE_BURLEY: code += ",diffuse_burley"; break;
  343. case DIFFUSE_LAMBERT: code += ",diffuse_lambert"; break;
  344. case DIFFUSE_LAMBERT_WRAP: code += ",diffuse_lambert_wrap"; break;
  345. case DIFFUSE_OREN_NAYAR: code += ",diffuse_oren_nayar"; break;
  346. case DIFFUSE_TOON: code += ",diffuse_toon"; break;
  347. }
  348. switch (specular_mode) {
  349. case SPECULAR_SCHLICK_GGX: code += ",specular_schlick_ggx"; break;
  350. case SPECULAR_BLINN: code += ",specular_blinn"; break;
  351. case SPECULAR_PHONG: code += ",specular_phong"; break;
  352. case SPECULAR_TOON: code += ",specular_toon"; break;
  353. case SPECULAR_DISABLED: code += ",specular_disabled"; break;
  354. }
  355. if (flags[FLAG_UNSHADED]) {
  356. code += ",unshaded";
  357. }
  358. if (flags[FLAG_DISABLE_DEPTH_TEST]) {
  359. code += ",depth_test_disable";
  360. }
  361. if (flags[FLAG_USE_VERTEX_LIGHTING]) {
  362. code += ",vertex_lighting";
  363. }
  364. if (flags[FLAG_TRIPLANAR_USE_WORLD] && (flags[FLAG_UV1_USE_TRIPLANAR] || flags[FLAG_UV2_USE_TRIPLANAR])) {
  365. code += ",world_vertex_coords";
  366. }
  367. if (flags[FLAG_DONT_RECEIVE_SHADOWS]) {
  368. code += ",shadows_disabled";
  369. }
  370. if (flags[FLAG_DISABLE_AMBIENT_LIGHT]) {
  371. code += ",ambient_light_disabled";
  372. }
  373. if (flags[FLAG_ENSURE_CORRECT_NORMALS]) {
  374. code += ",ensure_correct_normals";
  375. }
  376. if (flags[FLAG_USE_SHADOW_TO_OPACITY]) {
  377. code += ",shadow_to_opacity";
  378. }
  379. code += ";\n";
  380. code += "uniform vec4 albedo : hint_color;\n";
  381. code += "uniform sampler2D texture_albedo : hint_albedo;\n";
  382. code += "uniform float specular;\n";
  383. code += "uniform float metallic;\n";
  384. if (grow_enabled) {
  385. code += "uniform float grow;\n";
  386. }
  387. if (proximity_fade_enabled) {
  388. code += "uniform float proximity_fade_distance;\n";
  389. }
  390. if (distance_fade != DISTANCE_FADE_DISABLED) {
  391. code += "uniform float distance_fade_min;\n";
  392. code += "uniform float distance_fade_max;\n";
  393. }
  394. if (flags[FLAG_USE_ALPHA_SCISSOR]) {
  395. code += "uniform float alpha_scissor_threshold;\n";
  396. }
  397. code += "uniform float roughness : hint_range(0,1);\n";
  398. code += "uniform float point_size : hint_range(0,128);\n";
  399. if (textures[TEXTURE_METALLIC] != NULL) {
  400. code += "uniform sampler2D texture_metallic : hint_white;\n";
  401. code += "uniform vec4 metallic_texture_channel;\n";
  402. }
  403. if (textures[TEXTURE_ROUGHNESS] != NULL) {
  404. code += "uniform sampler2D texture_roughness : hint_white;\n";
  405. code += "uniform vec4 roughness_texture_channel;\n";
  406. }
  407. if (billboard_mode == BILLBOARD_PARTICLES) {
  408. code += "uniform int particles_anim_h_frames;\n";
  409. code += "uniform int particles_anim_v_frames;\n";
  410. code += "uniform bool particles_anim_loop;\n";
  411. }
  412. if (features[FEATURE_EMISSION]) {
  413. code += "uniform sampler2D texture_emission : hint_black_albedo;\n";
  414. code += "uniform vec4 emission : hint_color;\n";
  415. code += "uniform float emission_energy;\n";
  416. }
  417. if (features[FEATURE_REFRACTION]) {
  418. code += "uniform sampler2D texture_refraction;\n";
  419. code += "uniform float refraction : hint_range(-16,16);\n";
  420. code += "uniform vec4 refraction_texture_channel;\n";
  421. }
  422. if (features[FEATURE_NORMAL_MAPPING]) {
  423. code += "uniform sampler2D texture_normal : hint_normal;\n";
  424. code += "uniform float normal_scale : hint_range(-16,16);\n";
  425. }
  426. if (features[FEATURE_RIM]) {
  427. code += "uniform float rim : hint_range(0,1);\n";
  428. code += "uniform float rim_tint : hint_range(0,1);\n";
  429. code += "uniform sampler2D texture_rim : hint_white;\n";
  430. }
  431. if (features[FEATURE_CLEARCOAT]) {
  432. code += "uniform float clearcoat : hint_range(0,1);\n";
  433. code += "uniform float clearcoat_gloss : hint_range(0,1);\n";
  434. code += "uniform sampler2D texture_clearcoat : hint_white;\n";
  435. }
  436. if (features[FEATURE_ANISOTROPY]) {
  437. code += "uniform float anisotropy_ratio : hint_range(0,256);\n";
  438. code += "uniform sampler2D texture_flowmap : hint_aniso;\n";
  439. }
  440. if (features[FEATURE_AMBIENT_OCCLUSION]) {
  441. code += "uniform sampler2D texture_ambient_occlusion : hint_white;\n";
  442. code += "uniform vec4 ao_texture_channel;\n";
  443. code += "uniform float ao_light_affect;\n";
  444. }
  445. if (features[FEATURE_DETAIL]) {
  446. code += "uniform sampler2D texture_detail_albedo : hint_albedo;\n";
  447. code += "uniform sampler2D texture_detail_normal : hint_normal;\n";
  448. code += "uniform sampler2D texture_detail_mask : hint_white;\n";
  449. }
  450. if (features[FEATURE_SUBSURACE_SCATTERING]) {
  451. code += "uniform float subsurface_scattering_strength : hint_range(0,1);\n";
  452. code += "uniform sampler2D texture_subsurface_scattering : hint_white;\n";
  453. }
  454. if (features[FEATURE_TRANSMISSION]) {
  455. code += "uniform vec4 transmission : hint_color;\n";
  456. code += "uniform sampler2D texture_transmission : hint_black;\n";
  457. }
  458. if (features[FEATURE_DEPTH_MAPPING]) {
  459. code += "uniform sampler2D texture_depth : hint_black;\n";
  460. code += "uniform float depth_scale;\n";
  461. code += "uniform int depth_min_layers;\n";
  462. code += "uniform int depth_max_layers;\n";
  463. code += "uniform vec2 depth_flip;\n";
  464. }
  465. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  466. code += "varying vec3 uv1_triplanar_pos;\n";
  467. }
  468. if (flags[FLAG_UV2_USE_TRIPLANAR]) {
  469. code += "varying vec3 uv2_triplanar_pos;\n";
  470. }
  471. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  472. code += "uniform float uv1_blend_sharpness;\n";
  473. code += "varying vec3 uv1_power_normal;\n";
  474. }
  475. if (flags[FLAG_UV2_USE_TRIPLANAR]) {
  476. code += "uniform float uv2_blend_sharpness;\n";
  477. code += "varying vec3 uv2_power_normal;\n";
  478. }
  479. code += "uniform vec3 uv1_scale;\n";
  480. code += "uniform vec3 uv1_offset;\n";
  481. code += "uniform vec3 uv2_scale;\n";
  482. code += "uniform vec3 uv2_offset;\n";
  483. code += "\n\n";
  484. code += "void vertex() {\n";
  485. if (flags[FLAG_SRGB_VERTEX_COLOR]) {
  486. code += "\tif (!OUTPUT_IS_SRGB) {\n";
  487. code += "\t\tCOLOR.rgb = mix(pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb * (1.0 / 12.92), lessThan(COLOR.rgb, vec3(0.04045)));\n";
  488. code += "\t}\n";
  489. }
  490. if (flags[FLAG_USE_POINT_SIZE]) {
  491. code += "\tPOINT_SIZE=point_size;\n";
  492. }
  493. if (flags[FLAG_USE_VERTEX_LIGHTING]) {
  494. code += "\tROUGHNESS=roughness;\n";
  495. }
  496. if (!flags[FLAG_UV1_USE_TRIPLANAR]) {
  497. code += "\tUV=UV*uv1_scale.xy+uv1_offset.xy;\n";
  498. }
  499. switch (billboard_mode) {
  500. case BILLBOARD_DISABLED: {
  501. } break;
  502. case BILLBOARD_ENABLED: {
  503. code += "\tMODELVIEW_MATRIX = INV_CAMERA_MATRIX * mat4(CAMERA_MATRIX[0],CAMERA_MATRIX[1],CAMERA_MATRIX[2],WORLD_MATRIX[3]);\n";
  504. if (flags[FLAG_BILLBOARD_KEEP_SCALE]) {
  505. code += "\tMODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4(vec4(length(WORLD_MATRIX[0].xyz), 0.0, 0.0, 0.0),vec4(0.0, length(WORLD_MATRIX[1].xyz), 0.0, 0.0),vec4(0.0, 0.0, length(WORLD_MATRIX[2].xyz), 0.0),vec4(0.0, 0.0, 0.0, 1.0));\n";
  506. }
  507. } break;
  508. case BILLBOARD_FIXED_Y: {
  509. code += "\tMODELVIEW_MATRIX = INV_CAMERA_MATRIX * mat4(CAMERA_MATRIX[0],WORLD_MATRIX[1],vec4(normalize(cross(CAMERA_MATRIX[0].xyz,WORLD_MATRIX[1].xyz)), 0.0),WORLD_MATRIX[3]);\n";
  510. if (flags[FLAG_BILLBOARD_KEEP_SCALE]) {
  511. code += "\tMODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4(vec4(length(WORLD_MATRIX[0].xyz), 0.0, 0.0, 0.0),vec4(0.0, 1.0, 0.0, 0.0),vec4(0.0, 0.0, length(WORLD_MATRIX[2].xyz), 0.0), vec4(0.0, 0.0, 0.0, 1.0));\n";
  512. } else {
  513. code += "\tMODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4(vec4(1.0, 0.0, 0.0, 0.0),vec4(0.0, 1.0/length(WORLD_MATRIX[1].xyz), 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0),vec4(0.0, 0.0, 0.0 ,1.0));\n";
  514. }
  515. } break;
  516. case BILLBOARD_PARTICLES: {
  517. //make billboard
  518. code += "\tmat4 mat_world = mat4(normalize(CAMERA_MATRIX[0])*length(WORLD_MATRIX[0]),normalize(CAMERA_MATRIX[1])*length(WORLD_MATRIX[0]),normalize(CAMERA_MATRIX[2])*length(WORLD_MATRIX[2]),WORLD_MATRIX[3]);\n";
  519. //rotate by rotation
  520. code += "\tmat_world = mat_world * mat4( vec4(cos(INSTANCE_CUSTOM.x),-sin(INSTANCE_CUSTOM.x), 0.0, 0.0), vec4(sin(INSTANCE_CUSTOM.x), cos(INSTANCE_CUSTOM.x), 0.0, 0.0),vec4(0.0, 0.0, 1.0, 0.0),vec4(0.0, 0.0, 0.0, 1.0));\n";
  521. //set modelview
  522. code += "\tMODELVIEW_MATRIX = INV_CAMERA_MATRIX * mat_world;\n";
  523. //handle animation
  524. code += "\tfloat h_frames = float(particles_anim_h_frames);\n";
  525. code += "\tfloat v_frames = float(particles_anim_v_frames);\n";
  526. code += "\tfloat particle_total_frames = float(particles_anim_h_frames * particles_anim_v_frames);\n";
  527. code += "\tfloat particle_frame = floor(INSTANCE_CUSTOM.z * float(particle_total_frames));\n";
  528. code += "\tif (!particles_anim_loop) {\n";
  529. code += "\t\tparticle_frame = clamp(particle_frame, 0.0, particle_total_frames - 1.0);\n";
  530. code += "\t} else {\n";
  531. code += "\t\tparticle_frame = mod(particle_frame, particle_total_frames);\n";
  532. code += "\t}";
  533. code += "\tUV /= vec2(h_frames, v_frames);\n";
  534. code += "\tUV += vec2(mod(particle_frame, h_frames) / h_frames, floor(particle_frame / h_frames) / v_frames);\n";
  535. } break;
  536. }
  537. if (flags[FLAG_FIXED_SIZE]) {
  538. code += "\tif (PROJECTION_MATRIX[3][3] != 0.0) {\n";
  539. //orthogonal matrix, try to do about the same
  540. //with viewport size
  541. code += "\t\tfloat h = abs(1.0 / (2.0 * PROJECTION_MATRIX[1][1]));\n";
  542. code += "\t\tfloat sc = (h * 2.0); //consistent with Y-fov\n";
  543. code += "\t\tMODELVIEW_MATRIX[0]*=sc;\n";
  544. code += "\t\tMODELVIEW_MATRIX[1]*=sc;\n";
  545. code += "\t\tMODELVIEW_MATRIX[2]*=sc;\n";
  546. code += "\t} else {\n";
  547. //just scale by depth
  548. code += "\t\tfloat sc = -(MODELVIEW_MATRIX)[3].z;\n";
  549. code += "\t\tMODELVIEW_MATRIX[0]*=sc;\n";
  550. code += "\t\tMODELVIEW_MATRIX[1]*=sc;\n";
  551. code += "\t\tMODELVIEW_MATRIX[2]*=sc;\n";
  552. code += "\t}\n";
  553. }
  554. if (detail_uv == DETAIL_UV_2 && !flags[FLAG_UV2_USE_TRIPLANAR]) {
  555. code += "\tUV2=UV2*uv2_scale.xy+uv2_offset.xy;\n";
  556. }
  557. if (flags[FLAG_UV1_USE_TRIPLANAR] || flags[FLAG_UV2_USE_TRIPLANAR]) {
  558. //generate tangent and binormal in world space
  559. code += "\tTANGENT = vec3(0.0,0.0,-1.0) * abs(NORMAL.x);\n";
  560. code += "\tTANGENT+= vec3(1.0,0.0,0.0) * abs(NORMAL.y);\n";
  561. code += "\tTANGENT+= vec3(1.0,0.0,0.0) * abs(NORMAL.z);\n";
  562. code += "\tTANGENT = normalize(TANGENT);\n";
  563. code += "\tBINORMAL = vec3(0.0,1.0,0.0) * abs(NORMAL.x);\n";
  564. code += "\tBINORMAL+= vec3(0.0,0.0,-1.0) * abs(NORMAL.y);\n";
  565. code += "\tBINORMAL+= vec3(0.0,1.0,0.0) * abs(NORMAL.z);\n";
  566. code += "\tBINORMAL = normalize(BINORMAL);\n";
  567. }
  568. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  569. code += "\tuv1_power_normal=pow(abs(NORMAL),vec3(uv1_blend_sharpness));\n";
  570. code += "\tuv1_power_normal/=dot(uv1_power_normal,vec3(1.0));\n";
  571. code += "\tuv1_triplanar_pos = VERTEX * uv1_scale + uv1_offset;\n";
  572. code += "\tuv1_triplanar_pos *= vec3(1.0,-1.0, 1.0);\n";
  573. }
  574. if (flags[FLAG_UV2_USE_TRIPLANAR]) {
  575. code += "\tuv2_power_normal=pow(abs(NORMAL), vec3(uv2_blend_sharpness));\n";
  576. code += "\tuv2_power_normal/=dot(uv2_power_normal,vec3(1.0));\n";
  577. code += "\tuv2_triplanar_pos = VERTEX * uv2_scale + uv2_offset;\n";
  578. code += "\tuv2_triplanar_pos *= vec3(1.0,-1.0, 1.0);\n";
  579. }
  580. if (grow_enabled) {
  581. code += "\tVERTEX+=NORMAL*grow;\n";
  582. }
  583. code += "}\n";
  584. code += "\n\n";
  585. if (flags[FLAG_UV1_USE_TRIPLANAR] || flags[FLAG_UV2_USE_TRIPLANAR]) {
  586. code += "vec4 triplanar_texture(sampler2D p_sampler,vec3 p_weights,vec3 p_triplanar_pos) {\n";
  587. code += "\tvec4 samp=vec4(0.0);\n";
  588. code += "\tsamp+= texture(p_sampler,p_triplanar_pos.xy) * p_weights.z;\n";
  589. code += "\tsamp+= texture(p_sampler,p_triplanar_pos.xz) * p_weights.y;\n";
  590. code += "\tsamp+= texture(p_sampler,p_triplanar_pos.zy * vec2(-1.0,1.0)) * p_weights.x;\n";
  591. code += "\treturn samp;\n";
  592. code += "}\n";
  593. }
  594. code += "\n\n";
  595. code += "void fragment() {\n";
  596. if (!flags[FLAG_UV1_USE_TRIPLANAR]) {
  597. code += "\tvec2 base_uv = UV;\n";
  598. }
  599. if ((features[FEATURE_DETAIL] && detail_uv == DETAIL_UV_2) || (features[FEATURE_AMBIENT_OCCLUSION] && flags[FLAG_AO_ON_UV2]) || (features[FEATURE_EMISSION] && flags[FLAG_EMISSION_ON_UV2])) {
  600. code += "\tvec2 base_uv2 = UV2;\n";
  601. }
  602. if (!VisualServer::get_singleton()->is_low_end() && features[FEATURE_DEPTH_MAPPING] && !flags[FLAG_UV1_USE_TRIPLANAR]) { //depthmap not supported with triplanar
  603. code += "\t{\n";
  604. code += "\t\tvec3 view_dir = normalize(normalize(-VERTEX)*mat3(TANGENT*depth_flip.x,-BINORMAL*depth_flip.y,NORMAL));\n"; // binormal is negative due to mikktspace, flip 'unflips' it ;-)
  605. if (deep_parallax) {
  606. code += "\t\tfloat num_layers = mix(float(depth_max_layers),float(depth_min_layers), abs(dot(vec3(0.0, 0.0, 1.0), view_dir)));\n";
  607. code += "\t\tfloat layer_depth = 1.0 / num_layers;\n";
  608. code += "\t\tfloat current_layer_depth = 0.0;\n";
  609. code += "\t\tvec2 P = view_dir.xy * depth_scale;\n";
  610. code += "\t\tvec2 delta = P / num_layers;\n";
  611. code += "\t\tvec2 ofs = base_uv;\n";
  612. code += "\t\tfloat depth = textureLod(texture_depth, ofs,0.0).r;\n";
  613. code += "\t\tfloat current_depth = 0.0;\n";
  614. code += "\t\twhile(current_depth < depth) {\n";
  615. code += "\t\t\tofs -= delta;\n";
  616. code += "\t\t\tdepth = textureLod(texture_depth, ofs,0.0).r;\n";
  617. code += "\t\t\tcurrent_depth += layer_depth;\n";
  618. code += "\t\t}\n";
  619. code += "\t\tvec2 prev_ofs = ofs + delta;\n";
  620. code += "\t\tfloat after_depth = depth - current_depth;\n";
  621. code += "\t\tfloat before_depth = textureLod(texture_depth, prev_ofs, 0.0).r - current_depth + layer_depth;\n";
  622. code += "\t\tfloat weight = after_depth / (after_depth - before_depth);\n";
  623. code += "\t\tofs = mix(ofs,prev_ofs,weight);\n";
  624. } else {
  625. code += "\t\tfloat depth = texture(texture_depth, base_uv).r;\n";
  626. code += "\t\tvec2 ofs = base_uv - view_dir.xy / view_dir.z * (depth * depth_scale);\n";
  627. }
  628. code += "\t\tbase_uv=ofs;\n";
  629. if (features[FEATURE_DETAIL] && detail_uv == DETAIL_UV_2) {
  630. code += "\t\tbase_uv2-=ofs;\n";
  631. }
  632. code += "\t}\n";
  633. }
  634. if (flags[FLAG_USE_POINT_SIZE]) {
  635. code += "\tvec4 albedo_tex = texture(texture_albedo,POINT_COORD);\n";
  636. } else {
  637. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  638. code += "\tvec4 albedo_tex = triplanar_texture(texture_albedo,uv1_power_normal,uv1_triplanar_pos);\n";
  639. } else {
  640. code += "\tvec4 albedo_tex = texture(texture_albedo,base_uv);\n";
  641. }
  642. }
  643. if (flags[FLAG_ALBEDO_TEXTURE_FORCE_SRGB]) {
  644. code += "\talbedo_tex.rgb = mix(pow((albedo_tex.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)),vec3(2.4)),albedo_tex.rgb.rgb * (1.0 / 12.92),lessThan(albedo_tex.rgb,vec3(0.04045)));\n";
  645. }
  646. if (flags[FLAG_ALBEDO_FROM_VERTEX_COLOR]) {
  647. code += "\talbedo_tex *= COLOR;\n";
  648. }
  649. code += "\tALBEDO = albedo.rgb * albedo_tex.rgb;\n";
  650. if (textures[TEXTURE_METALLIC] != NULL) {
  651. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  652. code += "\tfloat metallic_tex = dot(triplanar_texture(texture_metallic,uv1_power_normal,uv1_triplanar_pos),metallic_texture_channel);\n";
  653. } else {
  654. code += "\tfloat metallic_tex = dot(texture(texture_metallic,base_uv),metallic_texture_channel);\n";
  655. }
  656. code += "\tMETALLIC = metallic_tex * metallic;\n";
  657. } else {
  658. code += "\tMETALLIC = metallic;\n";
  659. }
  660. if (textures[TEXTURE_ROUGHNESS] != NULL) {
  661. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  662. code += "\tfloat roughness_tex = dot(triplanar_texture(texture_roughness,uv1_power_normal,uv1_triplanar_pos),roughness_texture_channel);\n";
  663. } else {
  664. code += "\tfloat roughness_tex = dot(texture(texture_roughness,base_uv),roughness_texture_channel);\n";
  665. }
  666. code += "\tROUGHNESS = roughness_tex * roughness;\n";
  667. } else {
  668. code += "\tROUGHNESS = roughness;\n";
  669. }
  670. code += "\tSPECULAR = specular;\n";
  671. if (features[FEATURE_NORMAL_MAPPING]) {
  672. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  673. code += "\tNORMALMAP = triplanar_texture(texture_normal,uv1_power_normal,uv1_triplanar_pos).rgb;\n";
  674. } else {
  675. code += "\tNORMALMAP = texture(texture_normal,base_uv).rgb;\n";
  676. }
  677. code += "\tNORMALMAP_DEPTH = normal_scale;\n";
  678. }
  679. if (features[FEATURE_EMISSION]) {
  680. if (flags[FLAG_EMISSION_ON_UV2]) {
  681. if (flags[FLAG_UV2_USE_TRIPLANAR]) {
  682. code += "\tvec3 emission_tex = triplanar_texture(texture_emission,uv2_power_normal,uv2_triplanar_pos).rgb;\n";
  683. } else {
  684. code += "\tvec3 emission_tex = texture(texture_emission,base_uv2).rgb;\n";
  685. }
  686. } else {
  687. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  688. code += "\tvec3 emission_tex = triplanar_texture(texture_emission,uv1_power_normal,uv1_triplanar_pos).rgb;\n";
  689. } else {
  690. code += "\tvec3 emission_tex = texture(texture_emission,base_uv).rgb;\n";
  691. }
  692. }
  693. if (emission_op == EMISSION_OP_ADD) {
  694. code += "\tEMISSION = (emission.rgb+emission_tex)*emission_energy;\n";
  695. } else {
  696. code += "\tEMISSION = (emission.rgb*emission_tex)*emission_energy;\n";
  697. }
  698. }
  699. if (features[FEATURE_REFRACTION]) {
  700. if (features[FEATURE_NORMAL_MAPPING]) {
  701. code += "\tvec3 unpacked_normal = NORMALMAP;\n";
  702. code += "\tunpacked_normal.xy = unpacked_normal.xy * 2.0 - 1.0;\n";
  703. code += "\tunpacked_normal.z = sqrt(max(0.0, 1.0 - dot(unpacked_normal.xy, unpacked_normal.xy)));\n";
  704. code += "\tvec3 ref_normal = normalize( mix(NORMAL,TANGENT * unpacked_normal.x + BINORMAL * unpacked_normal.y + NORMAL * unpacked_normal.z,NORMALMAP_DEPTH) );\n";
  705. } else {
  706. code += "\tvec3 ref_normal = NORMAL;\n";
  707. }
  708. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  709. code += "\tvec2 ref_ofs = SCREEN_UV - ref_normal.xy * dot(triplanar_texture(texture_refraction,uv1_power_normal,uv1_triplanar_pos),refraction_texture_channel) * refraction;\n";
  710. } else {
  711. code += "\tvec2 ref_ofs = SCREEN_UV - ref_normal.xy * dot(texture(texture_refraction,base_uv),refraction_texture_channel) * refraction;\n";
  712. }
  713. code += "\tfloat ref_amount = 1.0 - albedo.a * albedo_tex.a;\n";
  714. code += "\tEMISSION += textureLod(SCREEN_TEXTURE,ref_ofs,ROUGHNESS * 8.0).rgb * ref_amount;\n";
  715. code += "\tALBEDO *= 1.0 - ref_amount;\n";
  716. code += "\tALPHA = 1.0;\n";
  717. } else if (features[FEATURE_TRANSPARENT] || flags[FLAG_USE_ALPHA_SCISSOR] || flags[FLAG_USE_SHADOW_TO_OPACITY] || (distance_fade == DISTANCE_FADE_PIXEL_ALPHA) || proximity_fade_enabled) {
  718. code += "\tALPHA = albedo.a * albedo_tex.a;\n";
  719. }
  720. if (proximity_fade_enabled) {
  721. code += "\tfloat depth_tex = textureLod(DEPTH_TEXTURE,SCREEN_UV,0.0).r;\n";
  722. code += "\tvec4 world_pos = INV_PROJECTION_MATRIX * vec4(SCREEN_UV*2.0-1.0,depth_tex*2.0-1.0,1.0);\n";
  723. code += "\tworld_pos.xyz/=world_pos.w;\n";
  724. code += "\tALPHA*=clamp(1.0-smoothstep(world_pos.z+proximity_fade_distance,world_pos.z,VERTEX.z),0.0,1.0);\n";
  725. }
  726. if (distance_fade != DISTANCE_FADE_DISABLED) {
  727. if ((distance_fade == DISTANCE_FADE_OBJECT_DITHER || distance_fade == DISTANCE_FADE_PIXEL_DITHER)) {
  728. if (!VisualServer::get_singleton()->is_low_end()) {
  729. code += "\t{\n";
  730. if (distance_fade == DISTANCE_FADE_OBJECT_DITHER) {
  731. code += "\t\tfloat fade_distance = abs((INV_CAMERA_MATRIX * WORLD_MATRIX[3]).z);\n";
  732. } else {
  733. code += "\t\tfloat fade_distance=-VERTEX.z;\n";
  734. }
  735. code += "\t\tfloat fade=clamp(smoothstep(distance_fade_min,distance_fade_max,fade_distance),0.0,1.0);\n";
  736. code += "\t\tint x = int(FRAGCOORD.x) % 4;\n";
  737. code += "\t\tint y = int(FRAGCOORD.y) % 4;\n";
  738. code += "\t\tint index = x + y * 4;\n";
  739. code += "\t\tfloat limit = 0.0;\n\n";
  740. code += "\t\tif (x < 8) {\n";
  741. code += "\t\t\tif (index == 0) limit = 0.0625;\n";
  742. code += "\t\t\tif (index == 1) limit = 0.5625;\n";
  743. code += "\t\t\tif (index == 2) limit = 0.1875;\n";
  744. code += "\t\t\tif (index == 3) limit = 0.6875;\n";
  745. code += "\t\t\tif (index == 4) limit = 0.8125;\n";
  746. code += "\t\t\tif (index == 5) limit = 0.3125;\n";
  747. code += "\t\t\tif (index == 6) limit = 0.9375;\n";
  748. code += "\t\t\tif (index == 7) limit = 0.4375;\n";
  749. code += "\t\t\tif (index == 8) limit = 0.25;\n";
  750. code += "\t\t\tif (index == 9) limit = 0.75;\n";
  751. code += "\t\t\tif (index == 10) limit = 0.125;\n";
  752. code += "\t\t\tif (index == 11) limit = 0.625;\n";
  753. code += "\t\t\tif (index == 12) limit = 1.0;\n";
  754. code += "\t\t\tif (index == 13) limit = 0.5;\n";
  755. code += "\t\t\tif (index == 14) limit = 0.875;\n";
  756. code += "\t\t\tif (index == 15) limit = 0.375;\n";
  757. code += "\t\t}\n\n";
  758. code += "\tif (fade < limit)\n";
  759. code += "\t\tdiscard;\n";
  760. code += "\t}\n\n";
  761. }
  762. } else {
  763. code += "\tALPHA*=clamp(smoothstep(distance_fade_min,distance_fade_max,-VERTEX.z),0.0,1.0);\n";
  764. }
  765. }
  766. if (features[FEATURE_RIM]) {
  767. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  768. code += "\tvec2 rim_tex = triplanar_texture(texture_rim,uv1_power_normal,uv1_triplanar_pos).xy;\n";
  769. } else {
  770. code += "\tvec2 rim_tex = texture(texture_rim,base_uv).xy;\n";
  771. }
  772. code += "\tRIM = rim*rim_tex.x;";
  773. code += "\tRIM_TINT = rim_tint*rim_tex.y;\n";
  774. }
  775. if (features[FEATURE_CLEARCOAT]) {
  776. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  777. code += "\tvec2 clearcoat_tex = triplanar_texture(texture_clearcoat,uv1_power_normal,uv1_triplanar_pos).xy;\n";
  778. } else {
  779. code += "\tvec2 clearcoat_tex = texture(texture_clearcoat,base_uv).xy;\n";
  780. }
  781. code += "\tCLEARCOAT = clearcoat*clearcoat_tex.x;";
  782. code += "\tCLEARCOAT_GLOSS = clearcoat_gloss*clearcoat_tex.y;\n";
  783. }
  784. if (features[FEATURE_ANISOTROPY]) {
  785. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  786. code += "\tvec3 anisotropy_tex = triplanar_texture(texture_flowmap,uv1_power_normal,uv1_triplanar_pos).rga;\n";
  787. } else {
  788. code += "\tvec3 anisotropy_tex = texture(texture_flowmap,base_uv).rga;\n";
  789. }
  790. code += "\tANISOTROPY = anisotropy_ratio*anisotropy_tex.b;\n";
  791. code += "\tANISOTROPY_FLOW = anisotropy_tex.rg*2.0-1.0;\n";
  792. }
  793. if (features[FEATURE_AMBIENT_OCCLUSION]) {
  794. if (flags[FLAG_AO_ON_UV2]) {
  795. if (flags[FLAG_UV2_USE_TRIPLANAR]) {
  796. code += "\tAO = dot(triplanar_texture(texture_ambient_occlusion,uv2_power_normal,uv2_triplanar_pos),ao_texture_channel);\n";
  797. } else {
  798. code += "\tAO = dot(texture(texture_ambient_occlusion,base_uv2),ao_texture_channel);\n";
  799. }
  800. } else {
  801. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  802. code += "\tAO = dot(triplanar_texture(texture_ambient_occlusion,uv1_power_normal,uv1_triplanar_pos),ao_texture_channel);\n";
  803. } else {
  804. code += "\tAO = dot(texture(texture_ambient_occlusion,base_uv),ao_texture_channel);\n";
  805. }
  806. }
  807. code += "\tAO_LIGHT_AFFECT = ao_light_affect;\n";
  808. }
  809. if (features[FEATURE_SUBSURACE_SCATTERING]) {
  810. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  811. code += "\tfloat sss_tex = triplanar_texture(texture_subsurface_scattering,uv1_power_normal,uv1_triplanar_pos).r;\n";
  812. } else {
  813. code += "\tfloat sss_tex = texture(texture_subsurface_scattering,base_uv).r;\n";
  814. }
  815. code += "\tSSS_STRENGTH=subsurface_scattering_strength*sss_tex;\n";
  816. }
  817. if (features[FEATURE_TRANSMISSION]) {
  818. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  819. code += "\tvec3 transmission_tex = triplanar_texture(texture_transmission,uv1_power_normal,uv1_triplanar_pos).rgb;\n";
  820. } else {
  821. code += "\tvec3 transmission_tex = texture(texture_transmission,base_uv).rgb;\n";
  822. }
  823. code += "\tTRANSMISSION = (transmission.rgb+transmission_tex);\n";
  824. }
  825. if (features[FEATURE_DETAIL]) {
  826. bool triplanar = (flags[FLAG_UV1_USE_TRIPLANAR] && detail_uv == DETAIL_UV_1) || (flags[FLAG_UV2_USE_TRIPLANAR] && detail_uv == DETAIL_UV_2);
  827. if (triplanar) {
  828. String tp_uv = detail_uv == DETAIL_UV_1 ? "uv1" : "uv2";
  829. code += "\tvec4 detail_tex = triplanar_texture(texture_detail_albedo," + tp_uv + "_power_normal," + tp_uv + "_triplanar_pos);\n";
  830. code += "\tvec4 detail_norm_tex = triplanar_texture(texture_detail_normal," + tp_uv + "_power_normal," + tp_uv + "_triplanar_pos);\n";
  831. } else {
  832. String det_uv = detail_uv == DETAIL_UV_1 ? "base_uv" : "base_uv2";
  833. code += "\tvec4 detail_tex = texture(texture_detail_albedo," + det_uv + ");\n";
  834. code += "\tvec4 detail_norm_tex = texture(texture_detail_normal," + det_uv + ");\n";
  835. }
  836. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  837. code += "\tvec4 detail_mask_tex = triplanar_texture(texture_detail_mask,uv1_power_normal,uv1_triplanar_pos);\n";
  838. } else {
  839. code += "\tvec4 detail_mask_tex = texture(texture_detail_mask,base_uv);\n";
  840. }
  841. switch (detail_blend_mode) {
  842. case BLEND_MODE_MIX: {
  843. code += "\tvec3 detail = mix(ALBEDO.rgb,detail_tex.rgb,detail_tex.a);\n";
  844. } break;
  845. case BLEND_MODE_ADD: {
  846. code += "\tvec3 detail = mix(ALBEDO.rgb,ALBEDO.rgb+detail_tex.rgb,detail_tex.a);\n";
  847. } break;
  848. case BLEND_MODE_SUB: {
  849. code += "\tvec3 detail = mix(ALBEDO.rgb,ALBEDO.rgb-detail_tex.rgb,detail_tex.a);\n";
  850. } break;
  851. case BLEND_MODE_MUL: {
  852. code += "\tvec3 detail = mix(ALBEDO.rgb,ALBEDO.rgb*detail_tex.rgb,detail_tex.a);\n";
  853. } break;
  854. }
  855. code += "\tvec3 detail_norm = mix(NORMALMAP,detail_norm_tex.rgb,detail_tex.a);\n";
  856. code += "\tNORMALMAP = mix(NORMALMAP,detail_norm,detail_mask_tex.r);\n";
  857. code += "\tALBEDO.rgb = mix(ALBEDO.rgb,detail,detail_mask_tex.r);\n";
  858. }
  859. if (flags[FLAG_USE_ALPHA_SCISSOR]) {
  860. code += "\tALPHA_SCISSOR=alpha_scissor_threshold;\n";
  861. }
  862. code += "}\n";
  863. ShaderData shader_data;
  864. shader_data.shader = VS::get_singleton()->shader_create();
  865. shader_data.users = 1;
  866. VS::get_singleton()->shader_set_code(shader_data.shader, code);
  867. shader_map[mk] = shader_data;
  868. VS::get_singleton()->material_set_shader(_get_material(), shader_data.shader);
  869. }
  870. void SpatialMaterial::flush_changes() {
  871. material_mutex.lock();
  872. while (dirty_materials->first()) {
  873. dirty_materials->first()->self()->_update_shader();
  874. }
  875. material_mutex.unlock();
  876. }
  877. void SpatialMaterial::_queue_shader_change() {
  878. material_mutex.lock();
  879. if (is_initialized && !element.in_list()) {
  880. dirty_materials->add(&element);
  881. }
  882. material_mutex.unlock();
  883. }
  884. bool SpatialMaterial::_is_shader_dirty() const {
  885. bool dirty = false;
  886. material_mutex.lock();
  887. dirty = element.in_list();
  888. material_mutex.unlock();
  889. return dirty;
  890. }
  891. void SpatialMaterial::set_albedo(const Color &p_albedo) {
  892. albedo = p_albedo;
  893. VS::get_singleton()->material_set_param(_get_material(), shader_names->albedo, p_albedo);
  894. }
  895. Color SpatialMaterial::get_albedo() const {
  896. return albedo;
  897. }
  898. void SpatialMaterial::set_specular(float p_specular) {
  899. specular = p_specular;
  900. VS::get_singleton()->material_set_param(_get_material(), shader_names->specular, p_specular);
  901. }
  902. float SpatialMaterial::get_specular() const {
  903. return specular;
  904. }
  905. void SpatialMaterial::set_roughness(float p_roughness) {
  906. roughness = p_roughness;
  907. VS::get_singleton()->material_set_param(_get_material(), shader_names->roughness, p_roughness);
  908. }
  909. float SpatialMaterial::get_roughness() const {
  910. return roughness;
  911. }
  912. void SpatialMaterial::set_metallic(float p_metallic) {
  913. metallic = p_metallic;
  914. VS::get_singleton()->material_set_param(_get_material(), shader_names->metallic, p_metallic);
  915. }
  916. float SpatialMaterial::get_metallic() const {
  917. return metallic;
  918. }
  919. void SpatialMaterial::set_emission(const Color &p_emission) {
  920. emission = p_emission;
  921. VS::get_singleton()->material_set_param(_get_material(), shader_names->emission, p_emission);
  922. }
  923. Color SpatialMaterial::get_emission() const {
  924. return emission;
  925. }
  926. void SpatialMaterial::set_emission_energy(float p_emission_energy) {
  927. emission_energy = p_emission_energy;
  928. VS::get_singleton()->material_set_param(_get_material(), shader_names->emission_energy, p_emission_energy);
  929. }
  930. float SpatialMaterial::get_emission_energy() const {
  931. return emission_energy;
  932. }
  933. void SpatialMaterial::set_normal_scale(float p_normal_scale) {
  934. normal_scale = p_normal_scale;
  935. VS::get_singleton()->material_set_param(_get_material(), shader_names->normal_scale, p_normal_scale);
  936. }
  937. float SpatialMaterial::get_normal_scale() const {
  938. return normal_scale;
  939. }
  940. void SpatialMaterial::set_rim(float p_rim) {
  941. rim = p_rim;
  942. VS::get_singleton()->material_set_param(_get_material(), shader_names->rim, p_rim);
  943. }
  944. float SpatialMaterial::get_rim() const {
  945. return rim;
  946. }
  947. void SpatialMaterial::set_rim_tint(float p_rim_tint) {
  948. rim_tint = p_rim_tint;
  949. VS::get_singleton()->material_set_param(_get_material(), shader_names->rim_tint, p_rim_tint);
  950. }
  951. float SpatialMaterial::get_rim_tint() const {
  952. return rim_tint;
  953. }
  954. void SpatialMaterial::set_ao_light_affect(float p_ao_light_affect) {
  955. ao_light_affect = p_ao_light_affect;
  956. VS::get_singleton()->material_set_param(_get_material(), shader_names->ao_light_affect, p_ao_light_affect);
  957. }
  958. float SpatialMaterial::get_ao_light_affect() const {
  959. return ao_light_affect;
  960. }
  961. void SpatialMaterial::set_clearcoat(float p_clearcoat) {
  962. clearcoat = p_clearcoat;
  963. VS::get_singleton()->material_set_param(_get_material(), shader_names->clearcoat, p_clearcoat);
  964. }
  965. float SpatialMaterial::get_clearcoat() const {
  966. return clearcoat;
  967. }
  968. void SpatialMaterial::set_clearcoat_gloss(float p_clearcoat_gloss) {
  969. clearcoat_gloss = p_clearcoat_gloss;
  970. VS::get_singleton()->material_set_param(_get_material(), shader_names->clearcoat_gloss, p_clearcoat_gloss);
  971. }
  972. float SpatialMaterial::get_clearcoat_gloss() const {
  973. return clearcoat_gloss;
  974. }
  975. void SpatialMaterial::set_anisotropy(float p_anisotropy) {
  976. anisotropy = p_anisotropy;
  977. VS::get_singleton()->material_set_param(_get_material(), shader_names->anisotropy, p_anisotropy);
  978. }
  979. float SpatialMaterial::get_anisotropy() const {
  980. return anisotropy;
  981. }
  982. void SpatialMaterial::set_depth_scale(float p_depth_scale) {
  983. depth_scale = p_depth_scale;
  984. VS::get_singleton()->material_set_param(_get_material(), shader_names->depth_scale, p_depth_scale);
  985. }
  986. float SpatialMaterial::get_depth_scale() const {
  987. return depth_scale;
  988. }
  989. void SpatialMaterial::set_subsurface_scattering_strength(float p_subsurface_scattering_strength) {
  990. subsurface_scattering_strength = p_subsurface_scattering_strength;
  991. VS::get_singleton()->material_set_param(_get_material(), shader_names->subsurface_scattering_strength, subsurface_scattering_strength);
  992. }
  993. float SpatialMaterial::get_subsurface_scattering_strength() const {
  994. return subsurface_scattering_strength;
  995. }
  996. void SpatialMaterial::set_transmission(const Color &p_transmission) {
  997. transmission = p_transmission;
  998. VS::get_singleton()->material_set_param(_get_material(), shader_names->transmission, transmission);
  999. }
  1000. Color SpatialMaterial::get_transmission() const {
  1001. return transmission;
  1002. }
  1003. void SpatialMaterial::set_refraction(float p_refraction) {
  1004. refraction = p_refraction;
  1005. VS::get_singleton()->material_set_param(_get_material(), shader_names->refraction, refraction);
  1006. }
  1007. float SpatialMaterial::get_refraction() const {
  1008. return refraction;
  1009. }
  1010. void SpatialMaterial::set_detail_uv(DetailUV p_detail_uv) {
  1011. if (detail_uv == p_detail_uv)
  1012. return;
  1013. detail_uv = p_detail_uv;
  1014. _queue_shader_change();
  1015. }
  1016. SpatialMaterial::DetailUV SpatialMaterial::get_detail_uv() const {
  1017. return detail_uv;
  1018. }
  1019. void SpatialMaterial::set_blend_mode(BlendMode p_mode) {
  1020. if (blend_mode == p_mode)
  1021. return;
  1022. blend_mode = p_mode;
  1023. _queue_shader_change();
  1024. }
  1025. SpatialMaterial::BlendMode SpatialMaterial::get_blend_mode() const {
  1026. return blend_mode;
  1027. }
  1028. void SpatialMaterial::set_detail_blend_mode(BlendMode p_mode) {
  1029. detail_blend_mode = p_mode;
  1030. _queue_shader_change();
  1031. }
  1032. SpatialMaterial::BlendMode SpatialMaterial::get_detail_blend_mode() const {
  1033. return detail_blend_mode;
  1034. }
  1035. void SpatialMaterial::set_depth_draw_mode(DepthDrawMode p_mode) {
  1036. if (depth_draw_mode == p_mode)
  1037. return;
  1038. depth_draw_mode = p_mode;
  1039. _queue_shader_change();
  1040. }
  1041. SpatialMaterial::DepthDrawMode SpatialMaterial::get_depth_draw_mode() const {
  1042. return depth_draw_mode;
  1043. }
  1044. void SpatialMaterial::set_cull_mode(CullMode p_mode) {
  1045. if (cull_mode == p_mode)
  1046. return;
  1047. cull_mode = p_mode;
  1048. _queue_shader_change();
  1049. }
  1050. SpatialMaterial::CullMode SpatialMaterial::get_cull_mode() const {
  1051. return cull_mode;
  1052. }
  1053. void SpatialMaterial::set_diffuse_mode(DiffuseMode p_mode) {
  1054. if (diffuse_mode == p_mode)
  1055. return;
  1056. diffuse_mode = p_mode;
  1057. _queue_shader_change();
  1058. }
  1059. SpatialMaterial::DiffuseMode SpatialMaterial::get_diffuse_mode() const {
  1060. return diffuse_mode;
  1061. }
  1062. void SpatialMaterial::set_specular_mode(SpecularMode p_mode) {
  1063. if (specular_mode == p_mode)
  1064. return;
  1065. specular_mode = p_mode;
  1066. _queue_shader_change();
  1067. }
  1068. SpatialMaterial::SpecularMode SpatialMaterial::get_specular_mode() const {
  1069. return specular_mode;
  1070. }
  1071. void SpatialMaterial::set_flag(Flags p_flag, bool p_enabled) {
  1072. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  1073. if (flags[p_flag] == p_enabled)
  1074. return;
  1075. flags[p_flag] = p_enabled;
  1076. if ((p_flag == FLAG_USE_ALPHA_SCISSOR) || (p_flag == FLAG_UNSHADED) || (p_flag == FLAG_USE_SHADOW_TO_OPACITY)) {
  1077. _change_notify();
  1078. }
  1079. _queue_shader_change();
  1080. }
  1081. bool SpatialMaterial::get_flag(Flags p_flag) const {
  1082. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  1083. return flags[p_flag];
  1084. }
  1085. void SpatialMaterial::set_feature(Feature p_feature, bool p_enabled) {
  1086. ERR_FAIL_INDEX(p_feature, FEATURE_MAX);
  1087. if (features[p_feature] == p_enabled)
  1088. return;
  1089. features[p_feature] = p_enabled;
  1090. _change_notify();
  1091. _queue_shader_change();
  1092. }
  1093. bool SpatialMaterial::get_feature(Feature p_feature) const {
  1094. ERR_FAIL_INDEX_V(p_feature, FEATURE_MAX, false);
  1095. return features[p_feature];
  1096. }
  1097. void SpatialMaterial::set_texture(TextureParam p_param, const Ref<Texture> &p_texture) {
  1098. ERR_FAIL_INDEX(p_param, TEXTURE_MAX);
  1099. textures[p_param] = p_texture;
  1100. RID rid = p_texture.is_valid() ? p_texture->get_rid() : RID();
  1101. VS::get_singleton()->material_set_param(_get_material(), shader_names->texture_names[p_param], rid);
  1102. _change_notify();
  1103. _queue_shader_change();
  1104. }
  1105. Ref<Texture> SpatialMaterial::get_texture(TextureParam p_param) const {
  1106. ERR_FAIL_INDEX_V(p_param, TEXTURE_MAX, Ref<Texture>());
  1107. return textures[p_param];
  1108. }
  1109. Ref<Texture> SpatialMaterial::get_texture_by_name(StringName p_name) const {
  1110. for (int i = 0; i < (int)SpatialMaterial::TEXTURE_MAX; i++) {
  1111. TextureParam param = TextureParam(i);
  1112. if (p_name == shader_names->texture_names[param])
  1113. return textures[param];
  1114. }
  1115. return Ref<Texture>();
  1116. }
  1117. void SpatialMaterial::_validate_feature(const String &text, Feature feature, PropertyInfo &property) const {
  1118. if (property.name.begins_with(text) && property.name != text + "_enabled" && !features[feature]) {
  1119. property.usage = 0;
  1120. }
  1121. }
  1122. void SpatialMaterial::_validate_high_end(const String &text, PropertyInfo &property) const {
  1123. if (property.name.begins_with(text)) {
  1124. property.usage |= PROPERTY_USAGE_HIGH_END_GFX;
  1125. }
  1126. }
  1127. void SpatialMaterial::_validate_property(PropertyInfo &property) const {
  1128. _validate_feature("normal", FEATURE_NORMAL_MAPPING, property);
  1129. _validate_feature("emission", FEATURE_EMISSION, property);
  1130. _validate_feature("rim", FEATURE_RIM, property);
  1131. _validate_feature("clearcoat", FEATURE_CLEARCOAT, property);
  1132. _validate_feature("anisotropy", FEATURE_ANISOTROPY, property);
  1133. _validate_feature("ao", FEATURE_AMBIENT_OCCLUSION, property);
  1134. _validate_feature("depth", FEATURE_DEPTH_MAPPING, property);
  1135. _validate_feature("subsurf_scatter", FEATURE_SUBSURACE_SCATTERING, property);
  1136. _validate_feature("transmission", FEATURE_TRANSMISSION, property);
  1137. _validate_feature("refraction", FEATURE_REFRACTION, property);
  1138. _validate_feature("detail", FEATURE_DETAIL, property);
  1139. _validate_high_end("refraction", property);
  1140. _validate_high_end("subsurf_scatter", property);
  1141. _validate_high_end("anisotropy", property);
  1142. _validate_high_end("clearcoat", property);
  1143. _validate_high_end("depth", property);
  1144. if (property.name.begins_with("particles_anim_") && billboard_mode != BILLBOARD_PARTICLES) {
  1145. property.usage = 0;
  1146. }
  1147. if (property.name == "params_grow_amount" && !grow_enabled) {
  1148. property.usage = 0;
  1149. }
  1150. if (property.name == "proximity_fade_distance" && !proximity_fade_enabled) {
  1151. property.usage = 0;
  1152. }
  1153. if ((property.name == "distance_fade_max_distance" || property.name == "distance_fade_min_distance") && distance_fade == DISTANCE_FADE_DISABLED) {
  1154. property.usage = 0;
  1155. }
  1156. if (property.name == "params_alpha_scissor_threshold" && !flags[FLAG_USE_ALPHA_SCISSOR]) {
  1157. property.usage = 0;
  1158. }
  1159. if ((property.name == "depth_min_layers" || property.name == "depth_max_layers") && !deep_parallax) {
  1160. property.usage = 0;
  1161. }
  1162. if (flags[FLAG_UNSHADED]) {
  1163. if (property.name.begins_with("anisotropy")) {
  1164. property.usage = 0;
  1165. }
  1166. if (property.name.begins_with("ao")) {
  1167. property.usage = 0;
  1168. }
  1169. if (property.name.begins_with("clearcoat")) {
  1170. property.usage = 0;
  1171. }
  1172. if (property.name.begins_with("emission")) {
  1173. property.usage = 0;
  1174. }
  1175. if (property.name.begins_with("metallic")) {
  1176. property.usage = 0;
  1177. }
  1178. if (property.name.begins_with("normal")) {
  1179. property.usage = 0;
  1180. }
  1181. if (property.name.begins_with("rim")) {
  1182. property.usage = 0;
  1183. }
  1184. if (property.name.begins_with("roughness")) {
  1185. property.usage = 0;
  1186. }
  1187. if (property.name.begins_with("subsurf_scatter")) {
  1188. property.usage = 0;
  1189. }
  1190. if (property.name.begins_with("transmission")) {
  1191. property.usage = 0;
  1192. }
  1193. }
  1194. }
  1195. void SpatialMaterial::set_line_width(float p_line_width) {
  1196. line_width = p_line_width;
  1197. VS::get_singleton()->material_set_line_width(_get_material(), line_width);
  1198. }
  1199. float SpatialMaterial::get_line_width() const {
  1200. return line_width;
  1201. }
  1202. void SpatialMaterial::set_point_size(float p_point_size) {
  1203. point_size = p_point_size;
  1204. VS::get_singleton()->material_set_param(_get_material(), shader_names->point_size, p_point_size);
  1205. }
  1206. float SpatialMaterial::get_point_size() const {
  1207. return point_size;
  1208. }
  1209. void SpatialMaterial::set_uv1_scale(const Vector3 &p_scale) {
  1210. uv1_scale = p_scale;
  1211. VS::get_singleton()->material_set_param(_get_material(), shader_names->uv1_scale, p_scale);
  1212. }
  1213. Vector3 SpatialMaterial::get_uv1_scale() const {
  1214. return uv1_scale;
  1215. }
  1216. void SpatialMaterial::set_uv1_offset(const Vector3 &p_offset) {
  1217. uv1_offset = p_offset;
  1218. VS::get_singleton()->material_set_param(_get_material(), shader_names->uv1_offset, p_offset);
  1219. }
  1220. Vector3 SpatialMaterial::get_uv1_offset() const {
  1221. return uv1_offset;
  1222. }
  1223. void SpatialMaterial::set_uv1_triplanar_blend_sharpness(float p_sharpness) {
  1224. uv1_triplanar_sharpness = p_sharpness;
  1225. VS::get_singleton()->material_set_param(_get_material(), shader_names->uv1_blend_sharpness, p_sharpness);
  1226. }
  1227. float SpatialMaterial::get_uv1_triplanar_blend_sharpness() const {
  1228. return uv1_triplanar_sharpness;
  1229. }
  1230. void SpatialMaterial::set_uv2_scale(const Vector3 &p_scale) {
  1231. uv2_scale = p_scale;
  1232. VS::get_singleton()->material_set_param(_get_material(), shader_names->uv2_scale, p_scale);
  1233. }
  1234. Vector3 SpatialMaterial::get_uv2_scale() const {
  1235. return uv2_scale;
  1236. }
  1237. void SpatialMaterial::set_uv2_offset(const Vector3 &p_offset) {
  1238. uv2_offset = p_offset;
  1239. VS::get_singleton()->material_set_param(_get_material(), shader_names->uv2_offset, p_offset);
  1240. }
  1241. Vector3 SpatialMaterial::get_uv2_offset() const {
  1242. return uv2_offset;
  1243. }
  1244. void SpatialMaterial::set_uv2_triplanar_blend_sharpness(float p_sharpness) {
  1245. uv2_triplanar_sharpness = p_sharpness;
  1246. VS::get_singleton()->material_set_param(_get_material(), shader_names->uv2_blend_sharpness, p_sharpness);
  1247. }
  1248. float SpatialMaterial::get_uv2_triplanar_blend_sharpness() const {
  1249. return uv2_triplanar_sharpness;
  1250. }
  1251. void SpatialMaterial::set_billboard_mode(BillboardMode p_mode) {
  1252. billboard_mode = p_mode;
  1253. _queue_shader_change();
  1254. _change_notify();
  1255. }
  1256. SpatialMaterial::BillboardMode SpatialMaterial::get_billboard_mode() const {
  1257. return billboard_mode;
  1258. }
  1259. void SpatialMaterial::set_particles_anim_h_frames(int p_frames) {
  1260. particles_anim_h_frames = p_frames;
  1261. VS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_h_frames, p_frames);
  1262. }
  1263. int SpatialMaterial::get_particles_anim_h_frames() const {
  1264. return particles_anim_h_frames;
  1265. }
  1266. void SpatialMaterial::set_particles_anim_v_frames(int p_frames) {
  1267. particles_anim_v_frames = p_frames;
  1268. VS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_v_frames, p_frames);
  1269. }
  1270. int SpatialMaterial::get_particles_anim_v_frames() const {
  1271. return particles_anim_v_frames;
  1272. }
  1273. void SpatialMaterial::set_particles_anim_loop(bool p_loop) {
  1274. particles_anim_loop = p_loop;
  1275. VS::get_singleton()->material_set_param(_get_material(), shader_names->particles_anim_loop, particles_anim_loop);
  1276. }
  1277. bool SpatialMaterial::get_particles_anim_loop() const {
  1278. return particles_anim_loop;
  1279. }
  1280. void SpatialMaterial::set_depth_deep_parallax(bool p_enable) {
  1281. deep_parallax = p_enable;
  1282. _queue_shader_change();
  1283. _change_notify();
  1284. }
  1285. bool SpatialMaterial::is_depth_deep_parallax_enabled() const {
  1286. return deep_parallax;
  1287. }
  1288. void SpatialMaterial::set_depth_deep_parallax_min_layers(int p_layer) {
  1289. deep_parallax_min_layers = p_layer;
  1290. VS::get_singleton()->material_set_param(_get_material(), shader_names->depth_min_layers, p_layer);
  1291. }
  1292. int SpatialMaterial::get_depth_deep_parallax_min_layers() const {
  1293. return deep_parallax_min_layers;
  1294. }
  1295. void SpatialMaterial::set_depth_deep_parallax_max_layers(int p_layer) {
  1296. deep_parallax_max_layers = p_layer;
  1297. VS::get_singleton()->material_set_param(_get_material(), shader_names->depth_max_layers, p_layer);
  1298. }
  1299. int SpatialMaterial::get_depth_deep_parallax_max_layers() const {
  1300. return deep_parallax_max_layers;
  1301. }
  1302. void SpatialMaterial::set_depth_deep_parallax_flip_tangent(bool p_flip) {
  1303. depth_parallax_flip_tangent = p_flip;
  1304. VS::get_singleton()->material_set_param(_get_material(), shader_names->depth_flip, Vector2(depth_parallax_flip_tangent ? -1 : 1, depth_parallax_flip_binormal ? -1 : 1));
  1305. }
  1306. bool SpatialMaterial::get_depth_deep_parallax_flip_tangent() const {
  1307. return depth_parallax_flip_tangent;
  1308. }
  1309. void SpatialMaterial::set_depth_deep_parallax_flip_binormal(bool p_flip) {
  1310. depth_parallax_flip_binormal = p_flip;
  1311. VS::get_singleton()->material_set_param(_get_material(), shader_names->depth_flip, Vector2(depth_parallax_flip_tangent ? -1 : 1, depth_parallax_flip_binormal ? -1 : 1));
  1312. }
  1313. bool SpatialMaterial::get_depth_deep_parallax_flip_binormal() const {
  1314. return depth_parallax_flip_binormal;
  1315. }
  1316. void SpatialMaterial::set_grow_enabled(bool p_enable) {
  1317. grow_enabled = p_enable;
  1318. _queue_shader_change();
  1319. _change_notify();
  1320. }
  1321. bool SpatialMaterial::is_grow_enabled() const {
  1322. return grow_enabled;
  1323. }
  1324. void SpatialMaterial::set_alpha_scissor_threshold(float p_threshold) {
  1325. alpha_scissor_threshold = p_threshold;
  1326. VS::get_singleton()->material_set_param(_get_material(), shader_names->alpha_scissor_threshold, p_threshold);
  1327. }
  1328. float SpatialMaterial::get_alpha_scissor_threshold() const {
  1329. return alpha_scissor_threshold;
  1330. }
  1331. void SpatialMaterial::set_grow(float p_grow) {
  1332. grow = p_grow;
  1333. VS::get_singleton()->material_set_param(_get_material(), shader_names->grow, p_grow);
  1334. }
  1335. float SpatialMaterial::get_grow() const {
  1336. return grow;
  1337. }
  1338. static Plane _get_texture_mask(SpatialMaterial::TextureChannel p_channel) {
  1339. static const Plane masks[5] = {
  1340. Plane(1, 0, 0, 0),
  1341. Plane(0, 1, 0, 0),
  1342. Plane(0, 0, 1, 0),
  1343. Plane(0, 0, 0, 1),
  1344. Plane(0.3333333, 0.3333333, 0.3333333, 0),
  1345. };
  1346. return masks[p_channel];
  1347. }
  1348. void SpatialMaterial::set_metallic_texture_channel(TextureChannel p_channel) {
  1349. ERR_FAIL_INDEX(p_channel, 5);
  1350. metallic_texture_channel = p_channel;
  1351. VS::get_singleton()->material_set_param(_get_material(), shader_names->metallic_texture_channel, _get_texture_mask(p_channel));
  1352. }
  1353. SpatialMaterial::TextureChannel SpatialMaterial::get_metallic_texture_channel() const {
  1354. return metallic_texture_channel;
  1355. }
  1356. void SpatialMaterial::set_roughness_texture_channel(TextureChannel p_channel) {
  1357. ERR_FAIL_INDEX(p_channel, 5);
  1358. roughness_texture_channel = p_channel;
  1359. VS::get_singleton()->material_set_param(_get_material(), shader_names->roughness_texture_channel, _get_texture_mask(p_channel));
  1360. }
  1361. SpatialMaterial::TextureChannel SpatialMaterial::get_roughness_texture_channel() const {
  1362. return roughness_texture_channel;
  1363. }
  1364. void SpatialMaterial::set_ao_texture_channel(TextureChannel p_channel) {
  1365. ERR_FAIL_INDEX(p_channel, 5);
  1366. ao_texture_channel = p_channel;
  1367. VS::get_singleton()->material_set_param(_get_material(), shader_names->ao_texture_channel, _get_texture_mask(p_channel));
  1368. }
  1369. SpatialMaterial::TextureChannel SpatialMaterial::get_ao_texture_channel() const {
  1370. return ao_texture_channel;
  1371. }
  1372. void SpatialMaterial::set_refraction_texture_channel(TextureChannel p_channel) {
  1373. ERR_FAIL_INDEX(p_channel, 5);
  1374. refraction_texture_channel = p_channel;
  1375. VS::get_singleton()->material_set_param(_get_material(), shader_names->refraction_texture_channel, _get_texture_mask(p_channel));
  1376. }
  1377. SpatialMaterial::TextureChannel SpatialMaterial::get_refraction_texture_channel() const {
  1378. return refraction_texture_channel;
  1379. }
  1380. RID SpatialMaterial::get_material_rid_for_2d(bool p_shaded, bool p_transparent, bool p_double_sided, bool p_cut_alpha, bool p_opaque_prepass, bool p_billboard, bool p_billboard_y) {
  1381. int version = 0;
  1382. if (p_shaded)
  1383. version = 1;
  1384. if (p_transparent)
  1385. version |= 2;
  1386. if (p_cut_alpha)
  1387. version |= 4;
  1388. if (p_opaque_prepass)
  1389. version |= 8;
  1390. if (p_double_sided)
  1391. version |= 16;
  1392. if (p_billboard)
  1393. version |= 32;
  1394. if (p_billboard_y)
  1395. version |= 64;
  1396. if (materials_for_2d[version].is_valid()) {
  1397. return materials_for_2d[version]->get_rid();
  1398. }
  1399. Ref<SpatialMaterial> material;
  1400. material.instance();
  1401. material->set_flag(FLAG_UNSHADED, !p_shaded);
  1402. material->set_feature(FEATURE_TRANSPARENT, p_transparent);
  1403. material->set_cull_mode(p_double_sided ? CULL_DISABLED : CULL_BACK);
  1404. material->set_depth_draw_mode(p_opaque_prepass ? DEPTH_DRAW_ALPHA_OPAQUE_PREPASS : DEPTH_DRAW_OPAQUE_ONLY);
  1405. material->set_flag(FLAG_SRGB_VERTEX_COLOR, true);
  1406. material->set_flag(FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  1407. material->set_flag(FLAG_USE_ALPHA_SCISSOR, p_cut_alpha);
  1408. if (p_billboard || p_billboard_y) {
  1409. material->set_flag(FLAG_BILLBOARD_KEEP_SCALE, true);
  1410. material->set_billboard_mode(p_billboard_y ? BILLBOARD_FIXED_Y : BILLBOARD_ENABLED);
  1411. }
  1412. materials_for_2d[version] = material;
  1413. // flush before using so we can access the shader right away
  1414. flush_changes();
  1415. return materials_for_2d[version]->get_rid();
  1416. }
  1417. void SpatialMaterial::set_on_top_of_alpha() {
  1418. set_feature(FEATURE_TRANSPARENT, true);
  1419. set_render_priority(RENDER_PRIORITY_MAX);
  1420. set_flag(FLAG_DISABLE_DEPTH_TEST, true);
  1421. }
  1422. void SpatialMaterial::set_proximity_fade(bool p_enable) {
  1423. proximity_fade_enabled = p_enable;
  1424. _queue_shader_change();
  1425. _change_notify();
  1426. }
  1427. bool SpatialMaterial::is_proximity_fade_enabled() const {
  1428. return proximity_fade_enabled;
  1429. }
  1430. void SpatialMaterial::set_proximity_fade_distance(float p_distance) {
  1431. proximity_fade_distance = p_distance;
  1432. VS::get_singleton()->material_set_param(_get_material(), shader_names->proximity_fade_distance, p_distance);
  1433. }
  1434. float SpatialMaterial::get_proximity_fade_distance() const {
  1435. return proximity_fade_distance;
  1436. }
  1437. void SpatialMaterial::set_distance_fade(DistanceFadeMode p_mode) {
  1438. distance_fade = p_mode;
  1439. _queue_shader_change();
  1440. _change_notify();
  1441. }
  1442. SpatialMaterial::DistanceFadeMode SpatialMaterial::get_distance_fade() const {
  1443. return distance_fade;
  1444. }
  1445. void SpatialMaterial::set_distance_fade_max_distance(float p_distance) {
  1446. distance_fade_max_distance = p_distance;
  1447. VS::get_singleton()->material_set_param(_get_material(), shader_names->distance_fade_max, distance_fade_max_distance);
  1448. }
  1449. float SpatialMaterial::get_distance_fade_max_distance() const {
  1450. return distance_fade_max_distance;
  1451. }
  1452. void SpatialMaterial::set_distance_fade_min_distance(float p_distance) {
  1453. distance_fade_min_distance = p_distance;
  1454. VS::get_singleton()->material_set_param(_get_material(), shader_names->distance_fade_min, distance_fade_min_distance);
  1455. }
  1456. float SpatialMaterial::get_distance_fade_min_distance() const {
  1457. return distance_fade_min_distance;
  1458. }
  1459. void SpatialMaterial::set_emission_operator(EmissionOperator p_op) {
  1460. if (emission_op == p_op)
  1461. return;
  1462. emission_op = p_op;
  1463. _queue_shader_change();
  1464. }
  1465. SpatialMaterial::EmissionOperator SpatialMaterial::get_emission_operator() const {
  1466. return emission_op;
  1467. }
  1468. RID SpatialMaterial::get_shader_rid() const {
  1469. ERR_FAIL_COND_V(!shader_map.has(current_key), RID());
  1470. return shader_map[current_key].shader;
  1471. }
  1472. Shader::Mode SpatialMaterial::get_shader_mode() const {
  1473. return Shader::MODE_SPATIAL;
  1474. }
  1475. void SpatialMaterial::_bind_methods() {
  1476. ClassDB::bind_method(D_METHOD("set_albedo", "albedo"), &SpatialMaterial::set_albedo);
  1477. ClassDB::bind_method(D_METHOD("get_albedo"), &SpatialMaterial::get_albedo);
  1478. ClassDB::bind_method(D_METHOD("set_specular", "specular"), &SpatialMaterial::set_specular);
  1479. ClassDB::bind_method(D_METHOD("get_specular"), &SpatialMaterial::get_specular);
  1480. ClassDB::bind_method(D_METHOD("set_metallic", "metallic"), &SpatialMaterial::set_metallic);
  1481. ClassDB::bind_method(D_METHOD("get_metallic"), &SpatialMaterial::get_metallic);
  1482. ClassDB::bind_method(D_METHOD("set_roughness", "roughness"), &SpatialMaterial::set_roughness);
  1483. ClassDB::bind_method(D_METHOD("get_roughness"), &SpatialMaterial::get_roughness);
  1484. ClassDB::bind_method(D_METHOD("set_emission", "emission"), &SpatialMaterial::set_emission);
  1485. ClassDB::bind_method(D_METHOD("get_emission"), &SpatialMaterial::get_emission);
  1486. ClassDB::bind_method(D_METHOD("set_emission_energy", "emission_energy"), &SpatialMaterial::set_emission_energy);
  1487. ClassDB::bind_method(D_METHOD("get_emission_energy"), &SpatialMaterial::get_emission_energy);
  1488. ClassDB::bind_method(D_METHOD("set_normal_scale", "normal_scale"), &SpatialMaterial::set_normal_scale);
  1489. ClassDB::bind_method(D_METHOD("get_normal_scale"), &SpatialMaterial::get_normal_scale);
  1490. ClassDB::bind_method(D_METHOD("set_rim", "rim"), &SpatialMaterial::set_rim);
  1491. ClassDB::bind_method(D_METHOD("get_rim"), &SpatialMaterial::get_rim);
  1492. ClassDB::bind_method(D_METHOD("set_rim_tint", "rim_tint"), &SpatialMaterial::set_rim_tint);
  1493. ClassDB::bind_method(D_METHOD("get_rim_tint"), &SpatialMaterial::get_rim_tint);
  1494. ClassDB::bind_method(D_METHOD("set_clearcoat", "clearcoat"), &SpatialMaterial::set_clearcoat);
  1495. ClassDB::bind_method(D_METHOD("get_clearcoat"), &SpatialMaterial::get_clearcoat);
  1496. ClassDB::bind_method(D_METHOD("set_clearcoat_gloss", "clearcoat_gloss"), &SpatialMaterial::set_clearcoat_gloss);
  1497. ClassDB::bind_method(D_METHOD("get_clearcoat_gloss"), &SpatialMaterial::get_clearcoat_gloss);
  1498. ClassDB::bind_method(D_METHOD("set_anisotropy", "anisotropy"), &SpatialMaterial::set_anisotropy);
  1499. ClassDB::bind_method(D_METHOD("get_anisotropy"), &SpatialMaterial::get_anisotropy);
  1500. ClassDB::bind_method(D_METHOD("set_depth_scale", "depth_scale"), &SpatialMaterial::set_depth_scale);
  1501. ClassDB::bind_method(D_METHOD("get_depth_scale"), &SpatialMaterial::get_depth_scale);
  1502. ClassDB::bind_method(D_METHOD("set_subsurface_scattering_strength", "strength"), &SpatialMaterial::set_subsurface_scattering_strength);
  1503. ClassDB::bind_method(D_METHOD("get_subsurface_scattering_strength"), &SpatialMaterial::get_subsurface_scattering_strength);
  1504. ClassDB::bind_method(D_METHOD("set_transmission", "transmission"), &SpatialMaterial::set_transmission);
  1505. ClassDB::bind_method(D_METHOD("get_transmission"), &SpatialMaterial::get_transmission);
  1506. ClassDB::bind_method(D_METHOD("set_refraction", "refraction"), &SpatialMaterial::set_refraction);
  1507. ClassDB::bind_method(D_METHOD("get_refraction"), &SpatialMaterial::get_refraction);
  1508. ClassDB::bind_method(D_METHOD("set_line_width", "line_width"), &SpatialMaterial::set_line_width);
  1509. ClassDB::bind_method(D_METHOD("get_line_width"), &SpatialMaterial::get_line_width);
  1510. ClassDB::bind_method(D_METHOD("set_point_size", "point_size"), &SpatialMaterial::set_point_size);
  1511. ClassDB::bind_method(D_METHOD("get_point_size"), &SpatialMaterial::get_point_size);
  1512. ClassDB::bind_method(D_METHOD("set_detail_uv", "detail_uv"), &SpatialMaterial::set_detail_uv);
  1513. ClassDB::bind_method(D_METHOD("get_detail_uv"), &SpatialMaterial::get_detail_uv);
  1514. ClassDB::bind_method(D_METHOD("set_blend_mode", "blend_mode"), &SpatialMaterial::set_blend_mode);
  1515. ClassDB::bind_method(D_METHOD("get_blend_mode"), &SpatialMaterial::get_blend_mode);
  1516. ClassDB::bind_method(D_METHOD("set_depth_draw_mode", "depth_draw_mode"), &SpatialMaterial::set_depth_draw_mode);
  1517. ClassDB::bind_method(D_METHOD("get_depth_draw_mode"), &SpatialMaterial::get_depth_draw_mode);
  1518. ClassDB::bind_method(D_METHOD("set_cull_mode", "cull_mode"), &SpatialMaterial::set_cull_mode);
  1519. ClassDB::bind_method(D_METHOD("get_cull_mode"), &SpatialMaterial::get_cull_mode);
  1520. ClassDB::bind_method(D_METHOD("set_diffuse_mode", "diffuse_mode"), &SpatialMaterial::set_diffuse_mode);
  1521. ClassDB::bind_method(D_METHOD("get_diffuse_mode"), &SpatialMaterial::get_diffuse_mode);
  1522. ClassDB::bind_method(D_METHOD("set_specular_mode", "specular_mode"), &SpatialMaterial::set_specular_mode);
  1523. ClassDB::bind_method(D_METHOD("get_specular_mode"), &SpatialMaterial::get_specular_mode);
  1524. ClassDB::bind_method(D_METHOD("set_flag", "flag", "enable"), &SpatialMaterial::set_flag);
  1525. ClassDB::bind_method(D_METHOD("get_flag", "flag"), &SpatialMaterial::get_flag);
  1526. ClassDB::bind_method(D_METHOD("set_feature", "feature", "enable"), &SpatialMaterial::set_feature);
  1527. ClassDB::bind_method(D_METHOD("get_feature", "feature"), &SpatialMaterial::get_feature);
  1528. ClassDB::bind_method(D_METHOD("set_texture", "param", "texture"), &SpatialMaterial::set_texture);
  1529. ClassDB::bind_method(D_METHOD("get_texture", "param"), &SpatialMaterial::get_texture);
  1530. ClassDB::bind_method(D_METHOD("set_detail_blend_mode", "detail_blend_mode"), &SpatialMaterial::set_detail_blend_mode);
  1531. ClassDB::bind_method(D_METHOD("get_detail_blend_mode"), &SpatialMaterial::get_detail_blend_mode);
  1532. ClassDB::bind_method(D_METHOD("set_uv1_scale", "scale"), &SpatialMaterial::set_uv1_scale);
  1533. ClassDB::bind_method(D_METHOD("get_uv1_scale"), &SpatialMaterial::get_uv1_scale);
  1534. ClassDB::bind_method(D_METHOD("set_uv1_offset", "offset"), &SpatialMaterial::set_uv1_offset);
  1535. ClassDB::bind_method(D_METHOD("get_uv1_offset"), &SpatialMaterial::get_uv1_offset);
  1536. ClassDB::bind_method(D_METHOD("set_uv1_triplanar_blend_sharpness", "sharpness"), &SpatialMaterial::set_uv1_triplanar_blend_sharpness);
  1537. ClassDB::bind_method(D_METHOD("get_uv1_triplanar_blend_sharpness"), &SpatialMaterial::get_uv1_triplanar_blend_sharpness);
  1538. ClassDB::bind_method(D_METHOD("set_uv2_scale", "scale"), &SpatialMaterial::set_uv2_scale);
  1539. ClassDB::bind_method(D_METHOD("get_uv2_scale"), &SpatialMaterial::get_uv2_scale);
  1540. ClassDB::bind_method(D_METHOD("set_uv2_offset", "offset"), &SpatialMaterial::set_uv2_offset);
  1541. ClassDB::bind_method(D_METHOD("get_uv2_offset"), &SpatialMaterial::get_uv2_offset);
  1542. ClassDB::bind_method(D_METHOD("set_uv2_triplanar_blend_sharpness", "sharpness"), &SpatialMaterial::set_uv2_triplanar_blend_sharpness);
  1543. ClassDB::bind_method(D_METHOD("get_uv2_triplanar_blend_sharpness"), &SpatialMaterial::get_uv2_triplanar_blend_sharpness);
  1544. ClassDB::bind_method(D_METHOD("set_billboard_mode", "mode"), &SpatialMaterial::set_billboard_mode);
  1545. ClassDB::bind_method(D_METHOD("get_billboard_mode"), &SpatialMaterial::get_billboard_mode);
  1546. ClassDB::bind_method(D_METHOD("set_particles_anim_h_frames", "frames"), &SpatialMaterial::set_particles_anim_h_frames);
  1547. ClassDB::bind_method(D_METHOD("get_particles_anim_h_frames"), &SpatialMaterial::get_particles_anim_h_frames);
  1548. ClassDB::bind_method(D_METHOD("set_particles_anim_v_frames", "frames"), &SpatialMaterial::set_particles_anim_v_frames);
  1549. ClassDB::bind_method(D_METHOD("get_particles_anim_v_frames"), &SpatialMaterial::get_particles_anim_v_frames);
  1550. ClassDB::bind_method(D_METHOD("set_particles_anim_loop", "loop"), &SpatialMaterial::set_particles_anim_loop);
  1551. ClassDB::bind_method(D_METHOD("get_particles_anim_loop"), &SpatialMaterial::get_particles_anim_loop);
  1552. ClassDB::bind_method(D_METHOD("set_depth_deep_parallax", "enable"), &SpatialMaterial::set_depth_deep_parallax);
  1553. ClassDB::bind_method(D_METHOD("is_depth_deep_parallax_enabled"), &SpatialMaterial::is_depth_deep_parallax_enabled);
  1554. ClassDB::bind_method(D_METHOD("set_depth_deep_parallax_min_layers", "layer"), &SpatialMaterial::set_depth_deep_parallax_min_layers);
  1555. ClassDB::bind_method(D_METHOD("get_depth_deep_parallax_min_layers"), &SpatialMaterial::get_depth_deep_parallax_min_layers);
  1556. ClassDB::bind_method(D_METHOD("set_depth_deep_parallax_max_layers", "layer"), &SpatialMaterial::set_depth_deep_parallax_max_layers);
  1557. ClassDB::bind_method(D_METHOD("get_depth_deep_parallax_max_layers"), &SpatialMaterial::get_depth_deep_parallax_max_layers);
  1558. ClassDB::bind_method(D_METHOD("set_depth_deep_parallax_flip_tangent", "flip"), &SpatialMaterial::set_depth_deep_parallax_flip_tangent);
  1559. ClassDB::bind_method(D_METHOD("get_depth_deep_parallax_flip_tangent"), &SpatialMaterial::get_depth_deep_parallax_flip_tangent);
  1560. ClassDB::bind_method(D_METHOD("set_depth_deep_parallax_flip_binormal", "flip"), &SpatialMaterial::set_depth_deep_parallax_flip_binormal);
  1561. ClassDB::bind_method(D_METHOD("get_depth_deep_parallax_flip_binormal"), &SpatialMaterial::get_depth_deep_parallax_flip_binormal);
  1562. ClassDB::bind_method(D_METHOD("set_grow", "amount"), &SpatialMaterial::set_grow);
  1563. ClassDB::bind_method(D_METHOD("get_grow"), &SpatialMaterial::get_grow);
  1564. ClassDB::bind_method(D_METHOD("set_emission_operator", "operator"), &SpatialMaterial::set_emission_operator);
  1565. ClassDB::bind_method(D_METHOD("get_emission_operator"), &SpatialMaterial::get_emission_operator);
  1566. ClassDB::bind_method(D_METHOD("set_ao_light_affect", "amount"), &SpatialMaterial::set_ao_light_affect);
  1567. ClassDB::bind_method(D_METHOD("get_ao_light_affect"), &SpatialMaterial::get_ao_light_affect);
  1568. ClassDB::bind_method(D_METHOD("set_alpha_scissor_threshold", "threshold"), &SpatialMaterial::set_alpha_scissor_threshold);
  1569. ClassDB::bind_method(D_METHOD("get_alpha_scissor_threshold"), &SpatialMaterial::get_alpha_scissor_threshold);
  1570. ClassDB::bind_method(D_METHOD("set_grow_enabled", "enable"), &SpatialMaterial::set_grow_enabled);
  1571. ClassDB::bind_method(D_METHOD("is_grow_enabled"), &SpatialMaterial::is_grow_enabled);
  1572. ClassDB::bind_method(D_METHOD("set_metallic_texture_channel", "channel"), &SpatialMaterial::set_metallic_texture_channel);
  1573. ClassDB::bind_method(D_METHOD("get_metallic_texture_channel"), &SpatialMaterial::get_metallic_texture_channel);
  1574. ClassDB::bind_method(D_METHOD("set_roughness_texture_channel", "channel"), &SpatialMaterial::set_roughness_texture_channel);
  1575. ClassDB::bind_method(D_METHOD("get_roughness_texture_channel"), &SpatialMaterial::get_roughness_texture_channel);
  1576. ClassDB::bind_method(D_METHOD("set_ao_texture_channel", "channel"), &SpatialMaterial::set_ao_texture_channel);
  1577. ClassDB::bind_method(D_METHOD("get_ao_texture_channel"), &SpatialMaterial::get_ao_texture_channel);
  1578. ClassDB::bind_method(D_METHOD("set_refraction_texture_channel", "channel"), &SpatialMaterial::set_refraction_texture_channel);
  1579. ClassDB::bind_method(D_METHOD("get_refraction_texture_channel"), &SpatialMaterial::get_refraction_texture_channel);
  1580. ClassDB::bind_method(D_METHOD("set_proximity_fade", "enabled"), &SpatialMaterial::set_proximity_fade);
  1581. ClassDB::bind_method(D_METHOD("is_proximity_fade_enabled"), &SpatialMaterial::is_proximity_fade_enabled);
  1582. ClassDB::bind_method(D_METHOD("set_proximity_fade_distance", "distance"), &SpatialMaterial::set_proximity_fade_distance);
  1583. ClassDB::bind_method(D_METHOD("get_proximity_fade_distance"), &SpatialMaterial::get_proximity_fade_distance);
  1584. ClassDB::bind_method(D_METHOD("set_distance_fade", "mode"), &SpatialMaterial::set_distance_fade);
  1585. ClassDB::bind_method(D_METHOD("get_distance_fade"), &SpatialMaterial::get_distance_fade);
  1586. ClassDB::bind_method(D_METHOD("set_distance_fade_max_distance", "distance"), &SpatialMaterial::set_distance_fade_max_distance);
  1587. ClassDB::bind_method(D_METHOD("get_distance_fade_max_distance"), &SpatialMaterial::get_distance_fade_max_distance);
  1588. ClassDB::bind_method(D_METHOD("set_distance_fade_min_distance", "distance"), &SpatialMaterial::set_distance_fade_min_distance);
  1589. ClassDB::bind_method(D_METHOD("get_distance_fade_min_distance"), &SpatialMaterial::get_distance_fade_min_distance);
  1590. ADD_GROUP("Flags", "flags_");
  1591. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_transparent"), "set_feature", "get_feature", FEATURE_TRANSPARENT);
  1592. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_use_shadow_to_opacity"), "set_flag", "get_flag", FLAG_USE_SHADOW_TO_OPACITY);
  1593. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_unshaded"), "set_flag", "get_flag", FLAG_UNSHADED);
  1594. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_vertex_lighting"), "set_flag", "get_flag", FLAG_USE_VERTEX_LIGHTING);
  1595. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_no_depth_test"), "set_flag", "get_flag", FLAG_DISABLE_DEPTH_TEST);
  1596. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_use_point_size"), "set_flag", "get_flag", FLAG_USE_POINT_SIZE);
  1597. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_world_triplanar"), "set_flag", "get_flag", FLAG_TRIPLANAR_USE_WORLD);
  1598. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_fixed_size"), "set_flag", "get_flag", FLAG_FIXED_SIZE);
  1599. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_albedo_tex_force_srgb"), "set_flag", "get_flag", FLAG_ALBEDO_TEXTURE_FORCE_SRGB);
  1600. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_do_not_receive_shadows"), "set_flag", "get_flag", FLAG_DONT_RECEIVE_SHADOWS);
  1601. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_disable_ambient_light"), "set_flag", "get_flag", FLAG_DISABLE_AMBIENT_LIGHT);
  1602. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "flags_ensure_correct_normals"), "set_flag", "get_flag", FLAG_ENSURE_CORRECT_NORMALS);
  1603. ADD_GROUP("Vertex Color", "vertex_color");
  1604. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "vertex_color_use_as_albedo"), "set_flag", "get_flag", FLAG_ALBEDO_FROM_VERTEX_COLOR);
  1605. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "vertex_color_is_srgb"), "set_flag", "get_flag", FLAG_SRGB_VERTEX_COLOR);
  1606. ADD_GROUP("Parameters", "params_");
  1607. ADD_PROPERTY(PropertyInfo(Variant::INT, "params_diffuse_mode", PROPERTY_HINT_ENUM, "Burley,Lambert,Lambert Wrap,Oren Nayar,Toon"), "set_diffuse_mode", "get_diffuse_mode");
  1608. ADD_PROPERTY(PropertyInfo(Variant::INT, "params_specular_mode", PROPERTY_HINT_ENUM, "SchlickGGX,Blinn,Phong,Toon,Disabled"), "set_specular_mode", "get_specular_mode");
  1609. ADD_PROPERTY(PropertyInfo(Variant::INT, "params_blend_mode", PROPERTY_HINT_ENUM, "Mix,Add,Sub,Mul"), "set_blend_mode", "get_blend_mode");
  1610. ADD_PROPERTY(PropertyInfo(Variant::INT, "params_cull_mode", PROPERTY_HINT_ENUM, "Back,Front,Disabled"), "set_cull_mode", "get_cull_mode");
  1611. ADD_PROPERTY(PropertyInfo(Variant::INT, "params_depth_draw_mode", PROPERTY_HINT_ENUM, "Opaque Only,Always,Never,Opaque Pre-Pass"), "set_depth_draw_mode", "get_depth_draw_mode");
  1612. ADD_PROPERTY(PropertyInfo(Variant::REAL, "params_line_width", PROPERTY_HINT_RANGE, "0.1,128,0.1"), "set_line_width", "get_line_width");
  1613. ADD_PROPERTY(PropertyInfo(Variant::REAL, "params_point_size", PROPERTY_HINT_RANGE, "0.1,128,0.1"), "set_point_size", "get_point_size");
  1614. ADD_PROPERTY(PropertyInfo(Variant::INT, "params_billboard_mode", PROPERTY_HINT_ENUM, "Disabled,Enabled,Y-Billboard,Particle Billboard"), "set_billboard_mode", "get_billboard_mode");
  1615. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "params_billboard_keep_scale"), "set_flag", "get_flag", FLAG_BILLBOARD_KEEP_SCALE);
  1616. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "params_grow"), "set_grow_enabled", "is_grow_enabled");
  1617. ADD_PROPERTY(PropertyInfo(Variant::REAL, "params_grow_amount", PROPERTY_HINT_RANGE, "-16,16,0.001"), "set_grow", "get_grow");
  1618. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "params_use_alpha_scissor"), "set_flag", "get_flag", FLAG_USE_ALPHA_SCISSOR);
  1619. ADD_PROPERTY(PropertyInfo(Variant::REAL, "params_alpha_scissor_threshold", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_alpha_scissor_threshold", "get_alpha_scissor_threshold");
  1620. ADD_GROUP("Particles Anim", "particles_anim_");
  1621. ADD_PROPERTY(PropertyInfo(Variant::INT, "particles_anim_h_frames", PROPERTY_HINT_RANGE, "1,128,1"), "set_particles_anim_h_frames", "get_particles_anim_h_frames");
  1622. ADD_PROPERTY(PropertyInfo(Variant::INT, "particles_anim_v_frames", PROPERTY_HINT_RANGE, "1,128,1"), "set_particles_anim_v_frames", "get_particles_anim_v_frames");
  1623. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "particles_anim_loop"), "set_particles_anim_loop", "get_particles_anim_loop");
  1624. ADD_GROUP("Albedo", "albedo_");
  1625. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "albedo_color"), "set_albedo", "get_albedo");
  1626. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "albedo_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_ALBEDO);
  1627. ADD_GROUP("Metallic", "metallic_");
  1628. ADD_PROPERTY(PropertyInfo(Variant::REAL, "metallic", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_metallic", "get_metallic");
  1629. ADD_PROPERTY(PropertyInfo(Variant::REAL, "metallic_specular", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_specular", "get_specular");
  1630. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "metallic_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_METALLIC);
  1631. ADD_PROPERTY(PropertyInfo(Variant::INT, "metallic_texture_channel", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Gray"), "set_metallic_texture_channel", "get_metallic_texture_channel");
  1632. ADD_GROUP("Roughness", "roughness_");
  1633. ADD_PROPERTY(PropertyInfo(Variant::REAL, "roughness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_roughness", "get_roughness");
  1634. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "roughness_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_ROUGHNESS);
  1635. ADD_PROPERTY(PropertyInfo(Variant::INT, "roughness_texture_channel", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Gray"), "set_roughness_texture_channel", "get_roughness_texture_channel");
  1636. ADD_GROUP("Emission", "emission_");
  1637. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "emission_enabled"), "set_feature", "get_feature", FEATURE_EMISSION);
  1638. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "emission", PROPERTY_HINT_COLOR_NO_ALPHA), "set_emission", "get_emission");
  1639. ADD_PROPERTY(PropertyInfo(Variant::REAL, "emission_energy", PROPERTY_HINT_RANGE, "0,16,0.01,or_greater"), "set_emission_energy", "get_emission_energy");
  1640. ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_operator", PROPERTY_HINT_ENUM, "Add,Multiply"), "set_emission_operator", "get_emission_operator");
  1641. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "emission_on_uv2"), "set_flag", "get_flag", FLAG_EMISSION_ON_UV2);
  1642. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "emission_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_EMISSION);
  1643. ADD_GROUP("NormalMap", "normal_");
  1644. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "normal_enabled"), "set_feature", "get_feature", FEATURE_NORMAL_MAPPING);
  1645. ADD_PROPERTY(PropertyInfo(Variant::REAL, "normal_scale", PROPERTY_HINT_RANGE, "-16,16,0.01"), "set_normal_scale", "get_normal_scale");
  1646. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "normal_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_NORMAL);
  1647. ADD_GROUP("Rim", "rim_");
  1648. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "rim_enabled"), "set_feature", "get_feature", FEATURE_RIM);
  1649. ADD_PROPERTY(PropertyInfo(Variant::REAL, "rim", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_rim", "get_rim");
  1650. ADD_PROPERTY(PropertyInfo(Variant::REAL, "rim_tint", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_rim_tint", "get_rim_tint");
  1651. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "rim_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_RIM);
  1652. ADD_GROUP("Clearcoat", "clearcoat_");
  1653. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "clearcoat_enabled"), "set_feature", "get_feature", FEATURE_CLEARCOAT);
  1654. ADD_PROPERTY(PropertyInfo(Variant::REAL, "clearcoat", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_clearcoat", "get_clearcoat");
  1655. ADD_PROPERTY(PropertyInfo(Variant::REAL, "clearcoat_gloss", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_clearcoat_gloss", "get_clearcoat_gloss");
  1656. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "clearcoat_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_CLEARCOAT);
  1657. ADD_GROUP("Anisotropy", "anisotropy_");
  1658. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "anisotropy_enabled"), "set_feature", "get_feature", FEATURE_ANISOTROPY);
  1659. ADD_PROPERTY(PropertyInfo(Variant::REAL, "anisotropy", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_anisotropy", "get_anisotropy");
  1660. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anisotropy_flowmap", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_FLOWMAP);
  1661. ADD_GROUP("Ambient Occlusion", "ao_");
  1662. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "ao_enabled"), "set_feature", "get_feature", FEATURE_AMBIENT_OCCLUSION);
  1663. ADD_PROPERTY(PropertyInfo(Variant::REAL, "ao_light_affect", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_ao_light_affect", "get_ao_light_affect");
  1664. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "ao_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_AMBIENT_OCCLUSION);
  1665. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "ao_on_uv2"), "set_flag", "get_flag", FLAG_AO_ON_UV2);
  1666. ADD_PROPERTY(PropertyInfo(Variant::INT, "ao_texture_channel", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Gray"), "set_ao_texture_channel", "get_ao_texture_channel");
  1667. ADD_GROUP("Depth", "depth_");
  1668. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "depth_enabled"), "set_feature", "get_feature", FEATURE_DEPTH_MAPPING);
  1669. ADD_PROPERTY(PropertyInfo(Variant::REAL, "depth_scale", PROPERTY_HINT_RANGE, "-16,16,0.001"), "set_depth_scale", "get_depth_scale");
  1670. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "depth_deep_parallax"), "set_depth_deep_parallax", "is_depth_deep_parallax_enabled");
  1671. ADD_PROPERTY(PropertyInfo(Variant::INT, "depth_min_layers", PROPERTY_HINT_RANGE, "1,64,1"), "set_depth_deep_parallax_min_layers", "get_depth_deep_parallax_min_layers");
  1672. ADD_PROPERTY(PropertyInfo(Variant::INT, "depth_max_layers", PROPERTY_HINT_RANGE, "1,64,1"), "set_depth_deep_parallax_max_layers", "get_depth_deep_parallax_max_layers");
  1673. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "depth_flip_tangent"), "set_depth_deep_parallax_flip_tangent", "get_depth_deep_parallax_flip_tangent");
  1674. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "depth_flip_binormal"), "set_depth_deep_parallax_flip_binormal", "get_depth_deep_parallax_flip_binormal");
  1675. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "depth_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_DEPTH);
  1676. ADD_GROUP("Subsurf Scatter", "subsurf_scatter_");
  1677. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "subsurf_scatter_enabled"), "set_feature", "get_feature", FEATURE_SUBSURACE_SCATTERING);
  1678. ADD_PROPERTY(PropertyInfo(Variant::REAL, "subsurf_scatter_strength", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_subsurface_scattering_strength", "get_subsurface_scattering_strength");
  1679. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "subsurf_scatter_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_SUBSURFACE_SCATTERING);
  1680. ADD_GROUP("Transmission", "transmission_");
  1681. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "transmission_enabled"), "set_feature", "get_feature", FEATURE_TRANSMISSION);
  1682. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "transmission", PROPERTY_HINT_COLOR_NO_ALPHA), "set_transmission", "get_transmission");
  1683. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "transmission_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_TRANSMISSION);
  1684. ADD_GROUP("Refraction", "refraction_");
  1685. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "refraction_enabled"), "set_feature", "get_feature", FEATURE_REFRACTION);
  1686. ADD_PROPERTY(PropertyInfo(Variant::REAL, "refraction_scale", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_refraction", "get_refraction");
  1687. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "refraction_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_REFRACTION);
  1688. ADD_PROPERTY(PropertyInfo(Variant::INT, "refraction_texture_channel", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Gray"), "set_refraction_texture_channel", "get_refraction_texture_channel");
  1689. ADD_GROUP("Detail", "detail_");
  1690. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "detail_enabled"), "set_feature", "get_feature", FEATURE_DETAIL);
  1691. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "detail_mask", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_DETAIL_MASK);
  1692. ADD_PROPERTY(PropertyInfo(Variant::INT, "detail_blend_mode", PROPERTY_HINT_ENUM, "Mix,Add,Sub,Mul"), "set_detail_blend_mode", "get_detail_blend_mode");
  1693. ADD_PROPERTY(PropertyInfo(Variant::INT, "detail_uv_layer", PROPERTY_HINT_ENUM, "UV1,UV2"), "set_detail_uv", "get_detail_uv");
  1694. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "detail_albedo", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_DETAIL_ALBEDO);
  1695. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "detail_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture", TEXTURE_DETAIL_NORMAL);
  1696. ADD_GROUP("UV1", "uv1_");
  1697. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "uv1_scale"), "set_uv1_scale", "get_uv1_scale");
  1698. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "uv1_offset"), "set_uv1_offset", "get_uv1_offset");
  1699. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "uv1_triplanar"), "set_flag", "get_flag", FLAG_UV1_USE_TRIPLANAR);
  1700. ADD_PROPERTY(PropertyInfo(Variant::REAL, "uv1_triplanar_sharpness", PROPERTY_HINT_EXP_EASING), "set_uv1_triplanar_blend_sharpness", "get_uv1_triplanar_blend_sharpness");
  1701. ADD_GROUP("UV2", "uv2_");
  1702. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "uv2_scale"), "set_uv2_scale", "get_uv2_scale");
  1703. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "uv2_offset"), "set_uv2_offset", "get_uv2_offset");
  1704. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "uv2_triplanar"), "set_flag", "get_flag", FLAG_UV2_USE_TRIPLANAR);
  1705. ADD_PROPERTY(PropertyInfo(Variant::REAL, "uv2_triplanar_sharpness", PROPERTY_HINT_EXP_EASING), "set_uv2_triplanar_blend_sharpness", "get_uv2_triplanar_blend_sharpness");
  1706. ADD_GROUP("Proximity Fade", "proximity_fade_");
  1707. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "proximity_fade_enable"), "set_proximity_fade", "is_proximity_fade_enabled");
  1708. ADD_PROPERTY(PropertyInfo(Variant::REAL, "proximity_fade_distance", PROPERTY_HINT_RANGE, "0,4096,0.01"), "set_proximity_fade_distance", "get_proximity_fade_distance");
  1709. ADD_GROUP("Distance Fade", "distance_fade_");
  1710. ADD_PROPERTY(PropertyInfo(Variant::INT, "distance_fade_mode", PROPERTY_HINT_ENUM, "Disabled,PixelAlpha,PixelDither,ObjectDither"), "set_distance_fade", "get_distance_fade");
  1711. ADD_PROPERTY(PropertyInfo(Variant::REAL, "distance_fade_min_distance", PROPERTY_HINT_RANGE, "0,4096,0.01"), "set_distance_fade_min_distance", "get_distance_fade_min_distance");
  1712. ADD_PROPERTY(PropertyInfo(Variant::REAL, "distance_fade_max_distance", PROPERTY_HINT_RANGE, "0,4096,0.01"), "set_distance_fade_max_distance", "get_distance_fade_max_distance");
  1713. BIND_ENUM_CONSTANT(TEXTURE_ALBEDO);
  1714. BIND_ENUM_CONSTANT(TEXTURE_METALLIC);
  1715. BIND_ENUM_CONSTANT(TEXTURE_ROUGHNESS);
  1716. BIND_ENUM_CONSTANT(TEXTURE_EMISSION);
  1717. BIND_ENUM_CONSTANT(TEXTURE_NORMAL);
  1718. BIND_ENUM_CONSTANT(TEXTURE_RIM);
  1719. BIND_ENUM_CONSTANT(TEXTURE_CLEARCOAT);
  1720. BIND_ENUM_CONSTANT(TEXTURE_FLOWMAP);
  1721. BIND_ENUM_CONSTANT(TEXTURE_AMBIENT_OCCLUSION);
  1722. BIND_ENUM_CONSTANT(TEXTURE_DEPTH);
  1723. BIND_ENUM_CONSTANT(TEXTURE_SUBSURFACE_SCATTERING);
  1724. BIND_ENUM_CONSTANT(TEXTURE_TRANSMISSION);
  1725. BIND_ENUM_CONSTANT(TEXTURE_REFRACTION);
  1726. BIND_ENUM_CONSTANT(TEXTURE_DETAIL_MASK);
  1727. BIND_ENUM_CONSTANT(TEXTURE_DETAIL_ALBEDO);
  1728. BIND_ENUM_CONSTANT(TEXTURE_DETAIL_NORMAL);
  1729. BIND_ENUM_CONSTANT(TEXTURE_MAX);
  1730. BIND_ENUM_CONSTANT(DETAIL_UV_1);
  1731. BIND_ENUM_CONSTANT(DETAIL_UV_2);
  1732. BIND_ENUM_CONSTANT(FEATURE_TRANSPARENT);
  1733. BIND_ENUM_CONSTANT(FEATURE_EMISSION);
  1734. BIND_ENUM_CONSTANT(FEATURE_NORMAL_MAPPING);
  1735. BIND_ENUM_CONSTANT(FEATURE_RIM);
  1736. BIND_ENUM_CONSTANT(FEATURE_CLEARCOAT);
  1737. BIND_ENUM_CONSTANT(FEATURE_ANISOTROPY);
  1738. BIND_ENUM_CONSTANT(FEATURE_AMBIENT_OCCLUSION);
  1739. BIND_ENUM_CONSTANT(FEATURE_DEPTH_MAPPING);
  1740. BIND_ENUM_CONSTANT(FEATURE_SUBSURACE_SCATTERING);
  1741. BIND_ENUM_CONSTANT(FEATURE_TRANSMISSION);
  1742. BIND_ENUM_CONSTANT(FEATURE_REFRACTION);
  1743. BIND_ENUM_CONSTANT(FEATURE_DETAIL);
  1744. BIND_ENUM_CONSTANT(FEATURE_MAX);
  1745. BIND_ENUM_CONSTANT(BLEND_MODE_MIX);
  1746. BIND_ENUM_CONSTANT(BLEND_MODE_ADD);
  1747. BIND_ENUM_CONSTANT(BLEND_MODE_SUB);
  1748. BIND_ENUM_CONSTANT(BLEND_MODE_MUL);
  1749. BIND_ENUM_CONSTANT(DEPTH_DRAW_OPAQUE_ONLY);
  1750. BIND_ENUM_CONSTANT(DEPTH_DRAW_ALWAYS);
  1751. BIND_ENUM_CONSTANT(DEPTH_DRAW_DISABLED);
  1752. BIND_ENUM_CONSTANT(DEPTH_DRAW_ALPHA_OPAQUE_PREPASS);
  1753. BIND_ENUM_CONSTANT(CULL_BACK);
  1754. BIND_ENUM_CONSTANT(CULL_FRONT);
  1755. BIND_ENUM_CONSTANT(CULL_DISABLED);
  1756. BIND_ENUM_CONSTANT(FLAG_UNSHADED);
  1757. BIND_ENUM_CONSTANT(FLAG_USE_VERTEX_LIGHTING);
  1758. BIND_ENUM_CONSTANT(FLAG_DISABLE_DEPTH_TEST);
  1759. BIND_ENUM_CONSTANT(FLAG_ALBEDO_FROM_VERTEX_COLOR);
  1760. BIND_ENUM_CONSTANT(FLAG_SRGB_VERTEX_COLOR);
  1761. BIND_ENUM_CONSTANT(FLAG_USE_POINT_SIZE);
  1762. BIND_ENUM_CONSTANT(FLAG_FIXED_SIZE);
  1763. BIND_ENUM_CONSTANT(FLAG_BILLBOARD_KEEP_SCALE);
  1764. BIND_ENUM_CONSTANT(FLAG_UV1_USE_TRIPLANAR);
  1765. BIND_ENUM_CONSTANT(FLAG_UV2_USE_TRIPLANAR);
  1766. BIND_ENUM_CONSTANT(FLAG_AO_ON_UV2);
  1767. BIND_ENUM_CONSTANT(FLAG_EMISSION_ON_UV2);
  1768. BIND_ENUM_CONSTANT(FLAG_USE_ALPHA_SCISSOR);
  1769. BIND_ENUM_CONSTANT(FLAG_TRIPLANAR_USE_WORLD);
  1770. BIND_ENUM_CONSTANT(FLAG_ALBEDO_TEXTURE_FORCE_SRGB);
  1771. BIND_ENUM_CONSTANT(FLAG_DONT_RECEIVE_SHADOWS);
  1772. BIND_ENUM_CONSTANT(FLAG_DISABLE_AMBIENT_LIGHT);
  1773. BIND_ENUM_CONSTANT(FLAG_ENSURE_CORRECT_NORMALS);
  1774. BIND_ENUM_CONSTANT(FLAG_USE_SHADOW_TO_OPACITY);
  1775. BIND_ENUM_CONSTANT(FLAG_MAX);
  1776. BIND_ENUM_CONSTANT(DIFFUSE_BURLEY);
  1777. BIND_ENUM_CONSTANT(DIFFUSE_LAMBERT);
  1778. BIND_ENUM_CONSTANT(DIFFUSE_LAMBERT_WRAP);
  1779. BIND_ENUM_CONSTANT(DIFFUSE_OREN_NAYAR);
  1780. BIND_ENUM_CONSTANT(DIFFUSE_TOON);
  1781. BIND_ENUM_CONSTANT(SPECULAR_SCHLICK_GGX);
  1782. BIND_ENUM_CONSTANT(SPECULAR_BLINN);
  1783. BIND_ENUM_CONSTANT(SPECULAR_PHONG);
  1784. BIND_ENUM_CONSTANT(SPECULAR_TOON);
  1785. BIND_ENUM_CONSTANT(SPECULAR_DISABLED);
  1786. BIND_ENUM_CONSTANT(BILLBOARD_DISABLED);
  1787. BIND_ENUM_CONSTANT(BILLBOARD_ENABLED);
  1788. BIND_ENUM_CONSTANT(BILLBOARD_FIXED_Y);
  1789. BIND_ENUM_CONSTANT(BILLBOARD_PARTICLES);
  1790. BIND_ENUM_CONSTANT(TEXTURE_CHANNEL_RED);
  1791. BIND_ENUM_CONSTANT(TEXTURE_CHANNEL_GREEN);
  1792. BIND_ENUM_CONSTANT(TEXTURE_CHANNEL_BLUE);
  1793. BIND_ENUM_CONSTANT(TEXTURE_CHANNEL_ALPHA);
  1794. BIND_ENUM_CONSTANT(TEXTURE_CHANNEL_GRAYSCALE);
  1795. BIND_ENUM_CONSTANT(EMISSION_OP_ADD);
  1796. BIND_ENUM_CONSTANT(EMISSION_OP_MULTIPLY);
  1797. BIND_ENUM_CONSTANT(DISTANCE_FADE_DISABLED);
  1798. BIND_ENUM_CONSTANT(DISTANCE_FADE_PIXEL_ALPHA);
  1799. BIND_ENUM_CONSTANT(DISTANCE_FADE_PIXEL_DITHER);
  1800. BIND_ENUM_CONSTANT(DISTANCE_FADE_OBJECT_DITHER);
  1801. }
  1802. SpatialMaterial::SpatialMaterial() :
  1803. element(this) {
  1804. // Initialize to the same values as the shader
  1805. set_albedo(Color(1.0, 1.0, 1.0, 1.0));
  1806. set_specular(0.5);
  1807. set_roughness(1.0);
  1808. set_metallic(0.0);
  1809. set_emission(Color(0, 0, 0));
  1810. set_emission_energy(1.0);
  1811. set_normal_scale(1);
  1812. set_rim(1.0);
  1813. set_rim_tint(0.5);
  1814. set_clearcoat(1);
  1815. set_clearcoat_gloss(0.5);
  1816. set_anisotropy(0);
  1817. set_depth_scale(0.05);
  1818. set_subsurface_scattering_strength(0);
  1819. set_transmission(Color(0, 0, 0));
  1820. set_refraction(0.05);
  1821. set_line_width(1);
  1822. set_point_size(1);
  1823. set_uv1_offset(Vector3(0, 0, 0));
  1824. set_uv1_scale(Vector3(1, 1, 1));
  1825. set_uv1_triplanar_blend_sharpness(1);
  1826. set_uv2_offset(Vector3(0, 0, 0));
  1827. set_uv2_scale(Vector3(1, 1, 1));
  1828. set_uv2_triplanar_blend_sharpness(1);
  1829. set_billboard_mode(BILLBOARD_DISABLED);
  1830. set_particles_anim_h_frames(1);
  1831. set_particles_anim_v_frames(1);
  1832. set_particles_anim_loop(false);
  1833. set_alpha_scissor_threshold(0.98);
  1834. emission_op = EMISSION_OP_ADD;
  1835. proximity_fade_enabled = false;
  1836. distance_fade = DISTANCE_FADE_DISABLED;
  1837. set_proximity_fade_distance(1);
  1838. set_distance_fade_min_distance(0);
  1839. set_distance_fade_max_distance(10);
  1840. set_ao_light_affect(0.0);
  1841. set_metallic_texture_channel(TEXTURE_CHANNEL_RED);
  1842. set_roughness_texture_channel(TEXTURE_CHANNEL_RED);
  1843. set_ao_texture_channel(TEXTURE_CHANNEL_RED);
  1844. set_refraction_texture_channel(TEXTURE_CHANNEL_RED);
  1845. grow_enabled = false;
  1846. set_grow(0.0);
  1847. deep_parallax = false;
  1848. depth_parallax_flip_tangent = false;
  1849. depth_parallax_flip_binormal = false;
  1850. set_depth_deep_parallax_min_layers(8);
  1851. set_depth_deep_parallax_max_layers(32);
  1852. set_depth_deep_parallax_flip_tangent(false); //also sets binormal
  1853. detail_uv = DETAIL_UV_1;
  1854. blend_mode = BLEND_MODE_MIX;
  1855. detail_blend_mode = BLEND_MODE_MIX;
  1856. depth_draw_mode = DEPTH_DRAW_OPAQUE_ONLY;
  1857. cull_mode = CULL_BACK;
  1858. for (int i = 0; i < FLAG_MAX; i++) {
  1859. flags[i] = 0;
  1860. }
  1861. diffuse_mode = DIFFUSE_BURLEY;
  1862. specular_mode = SPECULAR_SCHLICK_GGX;
  1863. for (int i = 0; i < FEATURE_MAX; i++) {
  1864. features[i] = false;
  1865. }
  1866. current_key.key = 0;
  1867. current_key.invalid_key = 1;
  1868. is_initialized = true;
  1869. _queue_shader_change();
  1870. }
  1871. SpatialMaterial::~SpatialMaterial() {
  1872. material_mutex.lock();
  1873. if (shader_map.has(current_key)) {
  1874. shader_map[current_key].users--;
  1875. if (shader_map[current_key].users == 0) {
  1876. //deallocate shader, as it's no longer in use
  1877. VS::get_singleton()->free(shader_map[current_key].shader);
  1878. shader_map.erase(current_key);
  1879. }
  1880. VS::get_singleton()->material_set_shader(_get_material(), RID());
  1881. }
  1882. material_mutex.unlock();
  1883. }