material.cpp 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638
  1. /**************************************************************************/
  2. /* material.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "material.h"
  31. #include "core/config/engine.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/error/error_macros.h"
  34. #include "core/version.h"
  35. #include "scene/main/scene_tree.h"
  36. void Material::set_next_pass(const Ref<Material> &p_pass) {
  37. for (Ref<Material> pass_child = p_pass; pass_child.is_valid(); 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. }
  43. next_pass = p_pass;
  44. if (material.is_valid()) {
  45. RID next_pass_rid;
  46. if (next_pass.is_valid()) {
  47. next_pass_rid = next_pass->get_rid();
  48. }
  49. RS::get_singleton()->material_set_next_pass(material, next_pass_rid);
  50. }
  51. }
  52. Ref<Material> Material::get_next_pass() const {
  53. return next_pass;
  54. }
  55. void Material::set_render_priority(int p_priority) {
  56. ERR_FAIL_COND(p_priority < RENDER_PRIORITY_MIN);
  57. ERR_FAIL_COND(p_priority > RENDER_PRIORITY_MAX);
  58. render_priority = p_priority;
  59. if (material.is_valid()) {
  60. RS::get_singleton()->material_set_render_priority(material, p_priority);
  61. }
  62. }
  63. int Material::get_render_priority() const {
  64. return render_priority;
  65. }
  66. RID Material::get_rid() const {
  67. return material;
  68. }
  69. void Material::_validate_property(PropertyInfo &p_property) const {
  70. if (!_can_do_next_pass() && p_property.name == "next_pass") {
  71. p_property.usage = PROPERTY_USAGE_NONE;
  72. }
  73. if (!_can_use_render_priority() && p_property.name == "render_priority") {
  74. p_property.usage = PROPERTY_USAGE_NONE;
  75. }
  76. }
  77. void Material::_mark_ready() {
  78. init_state = INIT_STATE_READY;
  79. }
  80. void Material::_mark_initialized(const Callable &p_add_to_dirty_list, const Callable &p_update_shader) {
  81. // If this is happening as part of resource loading, it is not safe to queue the update
  82. // as an addition to the dirty list. It would be if the load is happening on the main thread,
  83. // but even so we'd rather perform the update directly instead of using the dirty list.
  84. if (ResourceLoader::is_within_load()) {
  85. DEV_ASSERT(init_state != INIT_STATE_READY);
  86. if (init_state == INIT_STATE_UNINITIALIZED) { // Prevent queueing twice.
  87. init_state = INIT_STATE_INITIALIZING;
  88. callable_mp(this, &Material::_mark_ready).call_deferred();
  89. p_update_shader.call_deferred();
  90. }
  91. } else {
  92. // Straightforward conditions.
  93. init_state = INIT_STATE_READY;
  94. p_add_to_dirty_list.call();
  95. }
  96. }
  97. void Material::inspect_native_shader_code() {
  98. SceneTree *st = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop());
  99. RID shader = get_shader_rid();
  100. if (st && shader.is_valid()) {
  101. st->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, "_native_shader_source_visualizer", "_inspect_shader", shader);
  102. }
  103. }
  104. RID Material::get_shader_rid() const {
  105. RID ret;
  106. GDVIRTUAL_CALL(_get_shader_rid, ret);
  107. return ret;
  108. }
  109. Shader::Mode Material::get_shader_mode() const {
  110. Shader::Mode ret = Shader::MODE_MAX;
  111. GDVIRTUAL_CALL(_get_shader_mode, ret);
  112. return ret;
  113. }
  114. bool Material::_can_do_next_pass() const {
  115. bool ret = false;
  116. GDVIRTUAL_CALL(_can_do_next_pass, ret);
  117. return ret;
  118. }
  119. bool Material::_can_use_render_priority() const {
  120. bool ret = false;
  121. GDVIRTUAL_CALL(_can_use_render_priority, ret);
  122. return ret;
  123. }
  124. Ref<Resource> Material::create_placeholder() const {
  125. Ref<PlaceholderMaterial> placeholder;
  126. placeholder.instantiate();
  127. return placeholder;
  128. }
  129. void Material::_bind_methods() {
  130. ClassDB::bind_method(D_METHOD("set_next_pass", "next_pass"), &Material::set_next_pass);
  131. ClassDB::bind_method(D_METHOD("get_next_pass"), &Material::get_next_pass);
  132. ClassDB::bind_method(D_METHOD("set_render_priority", "priority"), &Material::set_render_priority);
  133. ClassDB::bind_method(D_METHOD("get_render_priority"), &Material::get_render_priority);
  134. ClassDB::bind_method(D_METHOD("inspect_native_shader_code"), &Material::inspect_native_shader_code);
  135. ClassDB::set_method_flags(get_class_static(), _scs_create("inspect_native_shader_code"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  136. ClassDB::bind_method(D_METHOD("create_placeholder"), &Material::create_placeholder);
  137. 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");
  138. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "next_pass", PROPERTY_HINT_RESOURCE_TYPE, "Material"), "set_next_pass", "get_next_pass");
  139. BIND_CONSTANT(RENDER_PRIORITY_MAX);
  140. BIND_CONSTANT(RENDER_PRIORITY_MIN);
  141. GDVIRTUAL_BIND(_get_shader_rid)
  142. GDVIRTUAL_BIND(_get_shader_mode)
  143. GDVIRTUAL_BIND(_can_do_next_pass)
  144. GDVIRTUAL_BIND(_can_use_render_priority)
  145. }
  146. Material::Material() {
  147. render_priority = 0;
  148. }
  149. Material::~Material() {
  150. if (material.is_valid()) {
  151. ERR_FAIL_NULL(RenderingServer::get_singleton());
  152. RenderingServer::get_singleton()->free(material);
  153. }
  154. }
  155. ///////////////////////////////////
  156. bool ShaderMaterial::_set(const StringName &p_name, const Variant &p_value) {
  157. if (shader.is_valid()) {
  158. const StringName *sn = remap_cache.getptr(p_name);
  159. if (sn) {
  160. set_shader_parameter(*sn, p_value);
  161. return true;
  162. }
  163. String s = p_name;
  164. if (s.begins_with("shader_parameter/")) {
  165. String param = s.replace_first("shader_parameter/", "");
  166. remap_cache[s] = param;
  167. set_shader_parameter(param, p_value);
  168. return true;
  169. }
  170. #ifndef DISABLE_DEPRECATED
  171. // Compatibility remaps are only needed here.
  172. if (s.begins_with("param/")) {
  173. s = s.replace_first("param/", "shader_parameter/");
  174. } else if (s.begins_with("shader_param/")) {
  175. s = s.replace_first("shader_param/", "shader_parameter/");
  176. } else if (s.begins_with("shader_uniform/")) {
  177. s = s.replace_first("shader_uniform/", "shader_parameter/");
  178. } else {
  179. return false; // Not a shader parameter.
  180. }
  181. WARN_PRINT("This material (containing shader with path: '" + shader->get_path() + "') uses an old deprecated parameter names. Consider re-saving this resource (or scene which contains it) in order for it to continue working in future versions.");
  182. String param = s.replace_first("shader_parameter/", "");
  183. remap_cache[s] = param;
  184. set_shader_parameter(param, p_value);
  185. return true;
  186. #endif
  187. }
  188. return false;
  189. }
  190. bool ShaderMaterial::_get(const StringName &p_name, Variant &r_ret) const {
  191. if (shader.is_valid()) {
  192. const StringName *sn = remap_cache.getptr(p_name);
  193. if (sn) {
  194. // Only return a parameter if it was previously set.
  195. r_ret = get_shader_parameter(*sn);
  196. return true;
  197. }
  198. }
  199. return false;
  200. }
  201. void ShaderMaterial::_get_property_list(List<PropertyInfo> *p_list) const {
  202. if (!shader.is_null()) {
  203. List<PropertyInfo> list;
  204. shader->get_shader_uniform_list(&list, true);
  205. HashMap<String, HashMap<String, List<PropertyInfo>>> groups;
  206. LocalVector<Pair<String, LocalVector<String>>> vgroups;
  207. {
  208. HashMap<String, List<PropertyInfo>> none_subgroup;
  209. none_subgroup.insert("<None>", List<PropertyInfo>());
  210. groups.insert("<None>", none_subgroup);
  211. }
  212. String last_group = "<None>";
  213. String last_subgroup = "<None>";
  214. bool is_none_group_undefined = true;
  215. bool is_none_group = true;
  216. for (List<PropertyInfo>::Element *E = list.front(); E; E = E->next()) {
  217. if (E->get().usage == PROPERTY_USAGE_GROUP) {
  218. if (!E->get().name.is_empty()) {
  219. Vector<String> vgroup = E->get().name.split("::");
  220. last_group = vgroup[0];
  221. if (vgroup.size() > 1) {
  222. last_subgroup = vgroup[1];
  223. } else {
  224. last_subgroup = "<None>";
  225. }
  226. is_none_group = false;
  227. if (!groups.has(last_group)) {
  228. PropertyInfo info;
  229. info.usage = PROPERTY_USAGE_GROUP;
  230. info.name = last_group.capitalize();
  231. info.hint_string = "shader_parameter/";
  232. List<PropertyInfo> none_subgroup;
  233. none_subgroup.push_back(info);
  234. HashMap<String, List<PropertyInfo>> subgroup_map;
  235. subgroup_map.insert("<None>", none_subgroup);
  236. groups.insert(last_group, subgroup_map);
  237. vgroups.push_back(Pair<String, LocalVector<String>>(last_group, { "<None>" }));
  238. }
  239. if (!groups[last_group].has(last_subgroup)) {
  240. PropertyInfo info;
  241. info.usage = PROPERTY_USAGE_SUBGROUP;
  242. info.name = last_subgroup.capitalize();
  243. info.hint_string = "shader_parameter/";
  244. List<PropertyInfo> subgroup;
  245. subgroup.push_back(info);
  246. groups[last_group].insert(last_subgroup, subgroup);
  247. for (Pair<String, LocalVector<String>> &group : vgroups) {
  248. if (group.first == last_group) {
  249. group.second.push_back(last_subgroup);
  250. break;
  251. }
  252. }
  253. }
  254. } else {
  255. last_group = "<None>";
  256. last_subgroup = "<None>";
  257. is_none_group = true;
  258. }
  259. continue; // Pass group.
  260. }
  261. if (is_none_group_undefined && is_none_group) {
  262. is_none_group_undefined = false;
  263. PropertyInfo info;
  264. info.usage = PROPERTY_USAGE_GROUP;
  265. info.name = "Shader Parameters";
  266. info.hint_string = "shader_parameter/";
  267. groups["<None>"]["<None>"].push_back(info);
  268. vgroups.push_back(Pair<String, LocalVector<String>>("<None>", { "<None>" }));
  269. }
  270. const bool is_uniform_cached = param_cache.has(E->get().name);
  271. bool is_uniform_type_compatible = true;
  272. if (is_uniform_cached) {
  273. // Check if the uniform Variant type changed, for example vec3 to vec4.
  274. const Variant &cached = param_cache.get(E->get().name);
  275. if (cached.is_array()) {
  276. // Allow some array conversions for backwards compatibility.
  277. is_uniform_type_compatible = Variant::can_convert(E->get().type, cached.get_type());
  278. } else {
  279. is_uniform_type_compatible = E->get().type == cached.get_type();
  280. }
  281. #ifndef DISABLE_DEPRECATED
  282. // PackedFloat32Array -> PackedVector4Array conversion.
  283. if (!is_uniform_type_compatible && E->get().type == Variant::PACKED_VECTOR4_ARRAY && cached.get_type() == Variant::PACKED_FLOAT32_ARRAY) {
  284. PackedVector4Array varray;
  285. PackedFloat32Array array = (PackedFloat32Array)cached;
  286. for (int i = 0; i + 3 < array.size(); i += 4) {
  287. varray.push_back(Vector4(array[i], array[i + 1], array[i + 2], array[i + 3]));
  288. }
  289. param_cache.insert(E->get().name, varray);
  290. is_uniform_type_compatible = true;
  291. }
  292. #endif
  293. if (is_uniform_type_compatible && E->get().type == Variant::OBJECT && cached.get_type() == Variant::OBJECT) {
  294. // Check if the Object class (hint string) changed, for example Texture2D sampler to Texture3D.
  295. // Allow inheritance, Texture2D type sampler should also accept CompressedTexture2D.
  296. Object *cached_obj = cached;
  297. if (!cached_obj->is_class(E->get().hint_string)) {
  298. is_uniform_type_compatible = false;
  299. }
  300. }
  301. }
  302. PropertyInfo info = E->get();
  303. info.name = "shader_parameter/" + info.name;
  304. if (!is_uniform_cached || !is_uniform_type_compatible) {
  305. // Property has never been edited or its type changed, retrieve with default value.
  306. Variant default_value = RenderingServer::get_singleton()->shader_get_parameter_default(shader->get_rid(), E->get().name);
  307. param_cache.insert(E->get().name, default_value);
  308. remap_cache.insert(info.name, E->get().name);
  309. }
  310. groups[last_group][last_subgroup].push_back(info);
  311. }
  312. for (const Pair<String, LocalVector<String>> &group_pair : vgroups) {
  313. String group = group_pair.first;
  314. for (const String &subgroup : group_pair.second) {
  315. List<PropertyInfo> &prop_infos = groups[group][subgroup];
  316. for (List<PropertyInfo>::Element *item = prop_infos.front(); item; item = item->next()) {
  317. p_list->push_back(item->get());
  318. }
  319. }
  320. }
  321. }
  322. }
  323. bool ShaderMaterial::_property_can_revert(const StringName &p_name) const {
  324. if (shader.is_valid()) {
  325. if (remap_cache.has(p_name)) {
  326. return true;
  327. }
  328. const String sname = p_name;
  329. return sname == "render_priority" || sname == "next_pass";
  330. }
  331. return false;
  332. }
  333. bool ShaderMaterial::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  334. if (shader.is_valid()) {
  335. const StringName *pr = remap_cache.getptr(p_name);
  336. if (pr) {
  337. r_property = RenderingServer::get_singleton()->shader_get_parameter_default(shader->get_rid(), *pr);
  338. return true;
  339. } else if (p_name == "render_priority") {
  340. r_property = 0;
  341. return true;
  342. } else if (p_name == "next_pass") {
  343. r_property = Variant();
  344. return true;
  345. }
  346. }
  347. return false;
  348. }
  349. void ShaderMaterial::set_shader(const Ref<Shader> &p_shader) {
  350. // Only connect/disconnect the signal when running in the editor.
  351. // This can be a slow operation, and `notify_property_list_changed()` (which is called by `_shader_changed()`)
  352. // does nothing in non-editor builds anyway. See GH-34741 for details.
  353. if (shader.is_valid() && Engine::get_singleton()->is_editor_hint()) {
  354. shader->disconnect_changed(callable_mp(this, &ShaderMaterial::_shader_changed));
  355. }
  356. shader = p_shader;
  357. RID rid;
  358. if (shader.is_valid()) {
  359. rid = shader->get_rid();
  360. if (Engine::get_singleton()->is_editor_hint()) {
  361. shader->connect_changed(callable_mp(this, &ShaderMaterial::_shader_changed));
  362. }
  363. }
  364. RID material_rid = _get_material();
  365. if (material_rid.is_valid()) {
  366. RS::get_singleton()->material_set_shader(material_rid, rid);
  367. }
  368. notify_property_list_changed(); //properties for shader exposed
  369. emit_changed();
  370. }
  371. Ref<Shader> ShaderMaterial::get_shader() const {
  372. return shader;
  373. }
  374. void ShaderMaterial::set_shader_parameter(const StringName &p_param, const Variant &p_value) {
  375. RID material_rid = _get_material();
  376. if (p_value.get_type() == Variant::NIL) {
  377. param_cache.erase(p_param);
  378. if (material_rid.is_valid()) {
  379. RS::get_singleton()->material_set_param(material_rid, p_param, Variant());
  380. }
  381. } else {
  382. Variant *v = param_cache.getptr(p_param);
  383. if (!v) {
  384. // Never assigned, also update the remap cache.
  385. remap_cache["shader_parameter/" + p_param.operator String()] = p_param;
  386. param_cache.insert(p_param, p_value);
  387. } else {
  388. *v = p_value;
  389. }
  390. if (p_value.get_type() == Variant::OBJECT) {
  391. RID tex_rid = p_value;
  392. if (tex_rid == RID()) {
  393. param_cache.erase(p_param);
  394. if (material_rid.is_valid()) {
  395. RS::get_singleton()->material_set_param(material_rid, p_param, Variant());
  396. }
  397. } else if (material_rid.is_valid()) {
  398. RS::get_singleton()->material_set_param(material_rid, p_param, tex_rid);
  399. }
  400. } else if (material_rid.is_valid()) {
  401. RS::get_singleton()->material_set_param(material_rid, p_param, p_value);
  402. }
  403. }
  404. }
  405. Variant ShaderMaterial::get_shader_parameter(const StringName &p_param) const {
  406. if (param_cache.has(p_param)) {
  407. return param_cache[p_param];
  408. } else {
  409. return Variant();
  410. }
  411. }
  412. void ShaderMaterial::_shader_changed() {
  413. notify_property_list_changed(); //update all properties
  414. }
  415. void ShaderMaterial::_check_material_rid() const {
  416. MutexLock lock(material_rid_mutex);
  417. if (_get_material().is_null()) {
  418. RID shader_rid = shader.is_valid() ? shader->get_rid() : RID();
  419. RID next_pass_rid;
  420. if (get_next_pass().is_valid()) {
  421. next_pass_rid = get_next_pass()->get_rid();
  422. }
  423. _set_material(RS::get_singleton()->material_create_from_shader(next_pass_rid, get_render_priority(), shader_rid));
  424. for (KeyValue<StringName, Variant> param : param_cache) {
  425. if (param.value.get_type() == Variant::OBJECT) {
  426. RID tex_rid = param.value;
  427. if (tex_rid.is_valid()) {
  428. RS::get_singleton()->material_set_param(_get_material(), param.key, tex_rid);
  429. } else {
  430. RS::get_singleton()->material_set_param(_get_material(), param.key, Variant());
  431. }
  432. } else {
  433. RS::get_singleton()->material_set_param(_get_material(), param.key, param.value);
  434. }
  435. }
  436. }
  437. }
  438. void ShaderMaterial::_bind_methods() {
  439. ClassDB::bind_method(D_METHOD("set_shader", "shader"), &ShaderMaterial::set_shader);
  440. ClassDB::bind_method(D_METHOD("get_shader"), &ShaderMaterial::get_shader);
  441. ClassDB::bind_method(D_METHOD("set_shader_parameter", "param", "value"), &ShaderMaterial::set_shader_parameter);
  442. ClassDB::bind_method(D_METHOD("get_shader_parameter", "param"), &ShaderMaterial::get_shader_parameter);
  443. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shader", PROPERTY_HINT_RESOURCE_TYPE, "Shader"), "set_shader", "get_shader");
  444. }
  445. #ifdef TOOLS_ENABLED
  446. void ShaderMaterial::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  447. const String pf = p_function;
  448. if (p_idx == 0 && (pf == "get_shader_parameter" || pf == "set_shader_parameter")) {
  449. if (shader.is_valid()) {
  450. List<PropertyInfo> pl;
  451. shader->get_shader_uniform_list(&pl);
  452. for (const PropertyInfo &E : pl) {
  453. r_options->push_back(E.name.replace_first("shader_parameter/", "").quote());
  454. }
  455. }
  456. }
  457. Material::get_argument_options(p_function, p_idx, r_options);
  458. }
  459. #endif
  460. bool ShaderMaterial::_can_do_next_pass() const {
  461. return shader.is_valid() && shader->get_mode() == Shader::MODE_SPATIAL;
  462. }
  463. bool ShaderMaterial::_can_use_render_priority() const {
  464. return shader.is_valid() && shader->get_mode() == Shader::MODE_SPATIAL;
  465. }
  466. Shader::Mode ShaderMaterial::get_shader_mode() const {
  467. if (shader.is_valid()) {
  468. return shader->get_mode();
  469. } else {
  470. return Shader::MODE_SPATIAL;
  471. }
  472. }
  473. RID ShaderMaterial::get_rid() const {
  474. _check_material_rid();
  475. return Material::get_rid();
  476. }
  477. RID ShaderMaterial::get_shader_rid() const {
  478. if (shader.is_valid()) {
  479. return shader->get_rid();
  480. } else {
  481. return RID();
  482. }
  483. }
  484. ShaderMaterial::ShaderMaterial() {
  485. // Material RID will be empty until it is required.
  486. }
  487. ShaderMaterial::~ShaderMaterial() {
  488. }
  489. /////////////////////////////////
  490. HashMap<BaseMaterial3D::MaterialKey, BaseMaterial3D::ShaderData, BaseMaterial3D::MaterialKey> BaseMaterial3D::shader_map;
  491. Mutex BaseMaterial3D::shader_map_mutex;
  492. BaseMaterial3D::ShaderNames *BaseMaterial3D::shader_names = nullptr;
  493. void BaseMaterial3D::init_shaders() {
  494. shader_names = memnew(ShaderNames);
  495. shader_names->albedo = "albedo";
  496. shader_names->specular = "specular";
  497. shader_names->roughness = "roughness";
  498. shader_names->metallic = "metallic";
  499. shader_names->emission = "emission";
  500. shader_names->emission_energy = "emission_energy";
  501. shader_names->normal_scale = "normal_scale";
  502. shader_names->rim = "rim";
  503. shader_names->rim_tint = "rim_tint";
  504. shader_names->clearcoat = "clearcoat";
  505. shader_names->clearcoat_roughness = "clearcoat_roughness";
  506. shader_names->anisotropy = "anisotropy_ratio";
  507. shader_names->heightmap_scale = "heightmap_scale";
  508. shader_names->subsurface_scattering_strength = "subsurface_scattering_strength";
  509. shader_names->backlight = "backlight";
  510. shader_names->refraction = "refraction";
  511. shader_names->point_size = "point_size";
  512. shader_names->uv1_scale = "uv1_scale";
  513. shader_names->uv1_offset = "uv1_offset";
  514. shader_names->uv2_scale = "uv2_scale";
  515. shader_names->uv2_offset = "uv2_offset";
  516. shader_names->uv1_blend_sharpness = "uv1_blend_sharpness";
  517. shader_names->uv2_blend_sharpness = "uv2_blend_sharpness";
  518. shader_names->particles_anim_h_frames = "particles_anim_h_frames";
  519. shader_names->particles_anim_v_frames = "particles_anim_v_frames";
  520. shader_names->particles_anim_loop = "particles_anim_loop";
  521. shader_names->heightmap_min_layers = "heightmap_min_layers";
  522. shader_names->heightmap_max_layers = "heightmap_max_layers";
  523. shader_names->heightmap_flip = "heightmap_flip";
  524. shader_names->grow = "grow";
  525. shader_names->ao_light_affect = "ao_light_affect";
  526. shader_names->proximity_fade_distance = "proximity_fade_distance";
  527. shader_names->distance_fade_min = "distance_fade_min";
  528. shader_names->distance_fade_max = "distance_fade_max";
  529. shader_names->msdf_pixel_range = "msdf_pixel_range";
  530. shader_names->msdf_outline_size = "msdf_outline_size";
  531. shader_names->metallic_texture_channel = "metallic_texture_channel";
  532. shader_names->ao_texture_channel = "ao_texture_channel";
  533. shader_names->clearcoat_texture_channel = "clearcoat_texture_channel";
  534. shader_names->rim_texture_channel = "rim_texture_channel";
  535. shader_names->heightmap_texture_channel = "heightmap_texture_channel";
  536. shader_names->refraction_texture_channel = "refraction_texture_channel";
  537. shader_names->transmittance_color = "transmittance_color";
  538. shader_names->transmittance_depth = "transmittance_depth";
  539. shader_names->transmittance_boost = "transmittance_boost";
  540. shader_names->texture_names[TEXTURE_ALBEDO] = "texture_albedo";
  541. shader_names->texture_names[TEXTURE_METALLIC] = "texture_metallic";
  542. shader_names->texture_names[TEXTURE_ROUGHNESS] = "texture_roughness";
  543. shader_names->texture_names[TEXTURE_EMISSION] = "texture_emission";
  544. shader_names->texture_names[TEXTURE_NORMAL] = "texture_normal";
  545. shader_names->texture_names[TEXTURE_RIM] = "texture_rim";
  546. shader_names->texture_names[TEXTURE_CLEARCOAT] = "texture_clearcoat";
  547. shader_names->texture_names[TEXTURE_FLOWMAP] = "texture_flowmap";
  548. shader_names->texture_names[TEXTURE_AMBIENT_OCCLUSION] = "texture_ambient_occlusion";
  549. shader_names->texture_names[TEXTURE_HEIGHTMAP] = "texture_heightmap";
  550. shader_names->texture_names[TEXTURE_SUBSURFACE_SCATTERING] = "texture_subsurface_scattering";
  551. shader_names->texture_names[TEXTURE_SUBSURFACE_TRANSMITTANCE] = "texture_subsurface_transmittance";
  552. shader_names->texture_names[TEXTURE_BACKLIGHT] = "texture_backlight";
  553. shader_names->texture_names[TEXTURE_REFRACTION] = "texture_refraction";
  554. shader_names->texture_names[TEXTURE_DETAIL_MASK] = "texture_detail_mask";
  555. shader_names->texture_names[TEXTURE_DETAIL_ALBEDO] = "texture_detail_albedo";
  556. shader_names->texture_names[TEXTURE_DETAIL_NORMAL] = "texture_detail_normal";
  557. shader_names->texture_names[TEXTURE_ORM] = "texture_orm";
  558. shader_names->alpha_scissor_threshold = "alpha_scissor_threshold";
  559. shader_names->alpha_hash_scale = "alpha_hash_scale";
  560. shader_names->alpha_antialiasing_edge = "alpha_antialiasing_edge";
  561. shader_names->albedo_texture_size = "albedo_texture_size";
  562. }
  563. HashMap<uint64_t, Ref<StandardMaterial3D>> BaseMaterial3D::materials_for_2d;
  564. void BaseMaterial3D::finish_shaders() {
  565. materials_for_2d.clear();
  566. memdelete(shader_names);
  567. shader_names = nullptr;
  568. }
  569. void BaseMaterial3D::_mark_dirty() {
  570. dirty = true;
  571. }
  572. void BaseMaterial3D::_update_shader() {
  573. if (!dirty) {
  574. return;
  575. }
  576. dirty = false;
  577. MaterialKey mk = _compute_key();
  578. if (mk == current_key) {
  579. return; //no update required in the end
  580. }
  581. MutexLock lock(shader_map_mutex);
  582. if (shader_map.has(current_key)) {
  583. shader_map[current_key].users--;
  584. if (shader_map[current_key].users == 0) {
  585. // Deallocate shader which is no longer in use.
  586. RS::get_singleton()->free(shader_map[current_key].shader);
  587. shader_map.erase(current_key);
  588. }
  589. }
  590. current_key = mk;
  591. if (shader_map.has(mk)) {
  592. shader_rid = shader_map[mk].shader;
  593. shader_map[mk].users++;
  594. if (_get_material().is_valid()) {
  595. RS::get_singleton()->material_set_shader(_get_material(), shader_rid);
  596. }
  597. return;
  598. }
  599. String texfilter_str;
  600. // Force linear filtering for the heightmap texture, as the heightmap effect
  601. // looks broken with nearest-neighbor filtering (with and without Deep Parallax).
  602. String texfilter_height_str;
  603. switch (texture_filter) {
  604. case TEXTURE_FILTER_NEAREST:
  605. texfilter_str = "filter_nearest";
  606. texfilter_height_str = "filter_linear";
  607. break;
  608. case TEXTURE_FILTER_LINEAR:
  609. texfilter_str = "filter_linear";
  610. texfilter_height_str = "filter_linear";
  611. break;
  612. case TEXTURE_FILTER_NEAREST_WITH_MIPMAPS:
  613. texfilter_str = "filter_nearest_mipmap";
  614. texfilter_height_str = "filter_linear_mipmap";
  615. break;
  616. case TEXTURE_FILTER_LINEAR_WITH_MIPMAPS:
  617. texfilter_str = "filter_linear_mipmap";
  618. texfilter_height_str = "filter_linear_mipmap";
  619. break;
  620. case TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC:
  621. texfilter_str = "filter_nearest_mipmap_anisotropic";
  622. texfilter_height_str = "filter_linear_mipmap_anisotropic";
  623. break;
  624. case TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC:
  625. texfilter_str = "filter_linear_mipmap_anisotropic";
  626. texfilter_height_str = "filter_linear_mipmap_anisotropic";
  627. break;
  628. case TEXTURE_FILTER_MAX:
  629. break; // Internal value, skip.
  630. }
  631. if (flags[FLAG_USE_TEXTURE_REPEAT]) {
  632. texfilter_str += ", repeat_enable";
  633. texfilter_height_str += ", repeat_enable";
  634. } else {
  635. texfilter_str += ", repeat_disable";
  636. texfilter_height_str += ", repeat_disable";
  637. }
  638. // Add a comment to describe the shader origin (useful when converting to ShaderMaterial).
  639. String code = vformat(
  640. "// NOTE: Shader automatically converted from " VERSION_NAME " " VERSION_FULL_CONFIG "'s %s.\n\n",
  641. orm ? "ORMMaterial3D" : "StandardMaterial3D");
  642. // Define shader type and render mode based on property values.
  643. code += "shader_type spatial;\nrender_mode ";
  644. switch (blend_mode) {
  645. case BLEND_MODE_MIX:
  646. code += "blend_mix";
  647. break;
  648. case BLEND_MODE_ADD:
  649. code += "blend_add";
  650. break;
  651. case BLEND_MODE_SUB:
  652. code += "blend_sub";
  653. break;
  654. case BLEND_MODE_MUL:
  655. code += "blend_mul";
  656. break;
  657. case BLEND_MODE_PREMULT_ALPHA:
  658. code += "blend_premul_alpha";
  659. break;
  660. case BLEND_MODE_MAX:
  661. break; // Internal value, skip.
  662. }
  663. DepthDrawMode ddm = depth_draw_mode;
  664. if (features[FEATURE_REFRACTION]) {
  665. ddm = DEPTH_DRAW_ALWAYS;
  666. }
  667. switch (ddm) {
  668. case DEPTH_DRAW_OPAQUE_ONLY:
  669. code += ", depth_draw_opaque";
  670. break;
  671. case DEPTH_DRAW_ALWAYS:
  672. code += ", depth_draw_always";
  673. break;
  674. case DEPTH_DRAW_DISABLED:
  675. code += ", depth_draw_never";
  676. break;
  677. case DEPTH_DRAW_MAX:
  678. break; // Internal value, skip.
  679. }
  680. switch (cull_mode) {
  681. case CULL_BACK:
  682. code += ", cull_back";
  683. break;
  684. case CULL_FRONT:
  685. code += ", cull_front";
  686. break;
  687. case CULL_DISABLED:
  688. code += ", cull_disabled";
  689. break;
  690. case CULL_MAX:
  691. break; // Internal value, skip.
  692. }
  693. switch (diffuse_mode) {
  694. case DIFFUSE_BURLEY:
  695. code += ", diffuse_burley";
  696. break;
  697. case DIFFUSE_LAMBERT:
  698. code += ", diffuse_lambert";
  699. break;
  700. case DIFFUSE_LAMBERT_WRAP:
  701. code += ", diffuse_lambert_wrap";
  702. break;
  703. case DIFFUSE_TOON:
  704. code += ", diffuse_toon";
  705. break;
  706. case DIFFUSE_MAX:
  707. break; // Internal value, skip.
  708. }
  709. switch (specular_mode) {
  710. case SPECULAR_SCHLICK_GGX:
  711. code += ", specular_schlick_ggx";
  712. break;
  713. case SPECULAR_TOON:
  714. code += ", specular_toon";
  715. break;
  716. case SPECULAR_DISABLED:
  717. code += ", specular_disabled";
  718. break;
  719. case SPECULAR_MAX:
  720. break; // Internal value, skip.
  721. }
  722. if (features[FEATURE_SUBSURFACE_SCATTERING] && flags[FLAG_SUBSURFACE_MODE_SKIN]) {
  723. code += ", sss_mode_skin";
  724. }
  725. if (shading_mode == SHADING_MODE_UNSHADED) {
  726. code += ", unshaded";
  727. }
  728. if (flags[FLAG_DISABLE_DEPTH_TEST]) {
  729. code += ", depth_test_disabled";
  730. }
  731. if (flags[FLAG_PARTICLE_TRAILS_MODE]) {
  732. code += ", particle_trails";
  733. }
  734. if (shading_mode == SHADING_MODE_PER_VERTEX) {
  735. code += ", vertex_lighting";
  736. }
  737. if (flags[FLAG_DONT_RECEIVE_SHADOWS]) {
  738. code += ", shadows_disabled";
  739. }
  740. if (flags[FLAG_DISABLE_AMBIENT_LIGHT]) {
  741. code += ", ambient_light_disabled";
  742. }
  743. if (flags[FLAG_USE_SHADOW_TO_OPACITY]) {
  744. code += ", shadow_to_opacity";
  745. }
  746. if (flags[FLAG_DISABLE_FOG]) {
  747. code += ", fog_disabled";
  748. }
  749. if (transparency == TRANSPARENCY_ALPHA_DEPTH_PRE_PASS) {
  750. code += ", depth_prepass_alpha";
  751. }
  752. // Although it's technically possible to do alpha antialiasing without using alpha hash or alpha scissor,
  753. // it is restricted in the base material because it has no use, and abusing it with regular Alpha blending can
  754. // saturate the MSAA mask.
  755. if (transparency == TRANSPARENCY_ALPHA_HASH || transparency == TRANSPARENCY_ALPHA_SCISSOR) {
  756. // Alpha antialiasing is only useful with ALPHA_HASH or ALPHA_SCISSOR.
  757. if (alpha_antialiasing_mode == ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE) {
  758. code += ", alpha_to_coverage";
  759. } else if (alpha_antialiasing_mode == ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE_AND_TO_ONE) {
  760. code += ", alpha_to_coverage_and_one";
  761. }
  762. }
  763. code += ";\n";
  764. // Generate list of uniforms.
  765. code += vformat(R"(
  766. uniform vec4 albedo : source_color;
  767. uniform sampler2D texture_albedo : source_color, %s;
  768. )",
  769. texfilter_str);
  770. if (grow_enabled) {
  771. code += "uniform float grow : hint_range(-16.0, 16.0, 0.001);\n";
  772. }
  773. if (proximity_fade_enabled) {
  774. code += "uniform float proximity_fade_distance : hint_range(0.0, 4096.0, 0.01);\n";
  775. }
  776. if (distance_fade != DISTANCE_FADE_DISABLED) {
  777. code += R"(
  778. uniform float distance_fade_min : hint_range(0.0, 4096.0, 0.01);
  779. uniform float distance_fade_max : hint_range(0.0, 4096.0, 0.01);
  780. )";
  781. }
  782. if (flags[FLAG_ALBEDO_TEXTURE_MSDF] && flags[FLAG_UV1_USE_TRIPLANAR]) {
  783. String msg = "MSDF is not supported on triplanar materials. Ignoring MSDF in favor of triplanar mapping.";
  784. if (textures[TEXTURE_ALBEDO].is_valid()) {
  785. WARN_PRINT(vformat("%s (albedo %s): " + msg, get_path(), textures[TEXTURE_ALBEDO]->get_path()));
  786. } else if (!get_path().is_empty()) {
  787. WARN_PRINT(vformat("%s: " + msg, get_path()));
  788. } else {
  789. WARN_PRINT(msg);
  790. }
  791. }
  792. if (flags[FLAG_ALBEDO_TEXTURE_MSDF] && !flags[FLAG_UV1_USE_TRIPLANAR]) {
  793. code += R"(
  794. uniform float msdf_pixel_range : hint_range(1.0, 100.0, 1.0);
  795. uniform float msdf_outline_size : hint_range(0.0, 250.0, 1.0);
  796. )";
  797. }
  798. // Alpha scissor is only valid if there is no antialiasing edge.
  799. // Alpha hash is valid whenever, but not with alpha scissor.
  800. if (transparency == TRANSPARENCY_ALPHA_SCISSOR) {
  801. code += "uniform float alpha_scissor_threshold : hint_range(0.0, 1.0, 0.001);\n";
  802. } else if (transparency == TRANSPARENCY_ALPHA_HASH) {
  803. code += "uniform float alpha_hash_scale : hint_range(0.0, 2.0, 0.01);\n";
  804. }
  805. // If alpha antialiasing isn't off, add in the edge variable.
  806. if (alpha_antialiasing_mode != ALPHA_ANTIALIASING_OFF &&
  807. (transparency == TRANSPARENCY_ALPHA_SCISSOR || transparency == TRANSPARENCY_ALPHA_HASH)) {
  808. code += R"(
  809. uniform float alpha_antialiasing_edge : hint_range(0.0, 1.0, 0.01);
  810. uniform ivec2 albedo_texture_size;
  811. )";
  812. }
  813. code += "uniform float point_size : hint_range(0.1, 128.0, 0.1);\n";
  814. if (!orm) {
  815. code += vformat(R"(
  816. uniform float roughness : hint_range(0.0, 1.0);
  817. uniform sampler2D texture_metallic : hint_default_white, %s;
  818. uniform vec4 metallic_texture_channel;
  819. )",
  820. texfilter_str);
  821. switch (roughness_texture_channel) {
  822. case TEXTURE_CHANNEL_RED: {
  823. code += vformat("uniform sampler2D texture_roughness : hint_roughness_r, %s;\n", texfilter_str);
  824. } break;
  825. case TEXTURE_CHANNEL_GREEN: {
  826. code += vformat("uniform sampler2D texture_roughness : hint_roughness_g, %s;\n", texfilter_str);
  827. } break;
  828. case TEXTURE_CHANNEL_BLUE: {
  829. code += vformat("uniform sampler2D texture_roughness : hint_roughness_b, %s;\n", texfilter_str);
  830. } break;
  831. case TEXTURE_CHANNEL_ALPHA: {
  832. code += vformat("uniform sampler2D texture_roughness : hint_roughness_a, %s;\n", texfilter_str);
  833. } break;
  834. case TEXTURE_CHANNEL_GRAYSCALE: {
  835. code += vformat("uniform sampler2D texture_roughness : hint_roughness_gray, %s;\n", texfilter_str);
  836. } break;
  837. case TEXTURE_CHANNEL_MAX:
  838. break; // Internal value, skip.
  839. }
  840. code += R"(
  841. uniform float specular : hint_range(0.0, 1.0, 0.01);
  842. uniform float metallic : hint_range(0.0, 1.0, 0.01);
  843. )";
  844. } else {
  845. code += "uniform sampler2D texture_orm : hint_roughness_g, " + texfilter_str + ";\n";
  846. }
  847. if (billboard_mode == BILLBOARD_PARTICLES) {
  848. code += R"(
  849. uniform int particles_anim_h_frames : hint_range(1, 128);
  850. uniform int particles_anim_v_frames : hint_range(1, 128);
  851. uniform bool particles_anim_loop;
  852. )";
  853. }
  854. if (features[FEATURE_EMISSION]) {
  855. code += vformat(R"(
  856. uniform sampler2D texture_emission : source_color, hint_default_black, %s;
  857. uniform vec4 emission : source_color;
  858. uniform float emission_energy : hint_range(0.0, 100.0, 0.01);
  859. )",
  860. texfilter_str);
  861. }
  862. if (features[FEATURE_REFRACTION]) {
  863. code += vformat(R"(
  864. uniform sampler2D texture_refraction : %s;
  865. uniform float refraction : hint_range(-1.0, 1.0, 0.001);
  866. uniform vec4 refraction_texture_channel;
  867. )",
  868. texfilter_str);
  869. }
  870. if (features[FEATURE_REFRACTION]) {
  871. code += "uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_linear_mipmap;\n";
  872. }
  873. if (features[FEATURE_REFRACTION] || proximity_fade_enabled) {
  874. code += "uniform sampler2D depth_texture : hint_depth_texture, repeat_disable, filter_nearest;\n";
  875. }
  876. if (features[FEATURE_NORMAL_MAPPING]) {
  877. code += vformat(R"(
  878. uniform sampler2D texture_normal : hint_roughness_normal, %s;
  879. uniform float normal_scale : hint_range(-16.0, 16.0);
  880. )",
  881. texfilter_str);
  882. }
  883. if (features[FEATURE_RIM]) {
  884. code += vformat(R"(
  885. uniform float rim : hint_range(0.0, 1.0, 0.01);
  886. uniform float rim_tint : hint_range(0.0, 1.0, 0.01);
  887. uniform sampler2D texture_rim : hint_default_white, %s;
  888. )",
  889. texfilter_str);
  890. }
  891. if (features[FEATURE_CLEARCOAT]) {
  892. code += vformat(R"(
  893. uniform float clearcoat : hint_range(0.0, 1.0, 0.01);
  894. uniform float clearcoat_roughness : hint_range(0.0, 1.0, 0.01);
  895. uniform sampler2D texture_clearcoat : hint_default_white, %s;
  896. )",
  897. texfilter_str);
  898. }
  899. if (features[FEATURE_ANISOTROPY]) {
  900. code += vformat(R"(
  901. uniform float anisotropy_ratio : hint_range(0.0, 1.0, 0.01);
  902. uniform sampler2D texture_flowmap : hint_anisotropy, %s;
  903. )",
  904. texfilter_str);
  905. }
  906. if (features[FEATURE_AMBIENT_OCCLUSION]) {
  907. code += vformat(R"(
  908. uniform sampler2D texture_ambient_occlusion : hint_default_white, %s;
  909. uniform vec4 ao_texture_channel;
  910. uniform float ao_light_affect : hint_range(0.0, 1.0, 0.01);
  911. )",
  912. texfilter_str);
  913. }
  914. if (features[FEATURE_DETAIL]) {
  915. code += vformat(R"(
  916. uniform sampler2D texture_detail_albedo : source_color, %s;
  917. uniform sampler2D texture_detail_normal : hint_normal, %s;
  918. uniform sampler2D texture_detail_mask : hint_default_white, %s;
  919. )",
  920. texfilter_str, texfilter_str, texfilter_str);
  921. }
  922. if (features[FEATURE_SUBSURFACE_SCATTERING]) {
  923. code += vformat(R"(
  924. uniform float subsurface_scattering_strength : hint_range(0.0, 1.0, 0.01);
  925. uniform sampler2D texture_subsurface_scattering : hint_default_white, %s;
  926. )",
  927. texfilter_str);
  928. }
  929. if (features[FEATURE_SUBSURFACE_TRANSMITTANCE]) {
  930. code += vformat(R"(
  931. uniform vec4 transmittance_color : source_color;
  932. uniform float transmittance_depth : hint_range(0.001, 8.0, 0.001);
  933. uniform sampler2D texture_subsurface_transmittance : hint_default_white, %s;
  934. uniform float transmittance_boost : hint_range(0.0, 1.0, 0.01);
  935. )",
  936. texfilter_str);
  937. }
  938. if (features[FEATURE_BACKLIGHT]) {
  939. code += vformat(R"(
  940. uniform vec4 backlight : source_color;
  941. uniform sampler2D texture_backlight : hint_default_black, %s;
  942. )",
  943. texfilter_str);
  944. }
  945. if (features[FEATURE_HEIGHT_MAPPING]) {
  946. code += vformat(R"(
  947. uniform sampler2D texture_heightmap : hint_default_black, %s;
  948. uniform float heightmap_scale : hint_range(-16.0, 16.0, 0.001);
  949. uniform int heightmap_min_layers : hint_range(1, 64);
  950. uniform int heightmap_max_layers : hint_range(1, 64);
  951. uniform vec2 heightmap_flip;
  952. )",
  953. texfilter_height_str);
  954. }
  955. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  956. code += "varying vec3 uv1_triplanar_pos;\n";
  957. }
  958. if (flags[FLAG_UV2_USE_TRIPLANAR]) {
  959. code += "varying vec3 uv2_triplanar_pos;\n";
  960. }
  961. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  962. code += R"(
  963. uniform float uv1_blend_sharpness : hint_range(0.0, 150.0, 0.001);
  964. varying vec3 uv1_power_normal;
  965. )";
  966. }
  967. if (flags[FLAG_UV2_USE_TRIPLANAR]) {
  968. code += R"(uniform float uv2_blend_sharpness : hint_range(0.0, 150.0, 0.001);
  969. varying vec3 uv2_power_normal;
  970. )";
  971. }
  972. code += R"(
  973. uniform vec3 uv1_scale;
  974. uniform vec3 uv1_offset;
  975. uniform vec3 uv2_scale;
  976. uniform vec3 uv2_offset;
  977. )";
  978. // Generate vertex shader.
  979. code += R"(
  980. void vertex() {)";
  981. if (flags[FLAG_SRGB_VERTEX_COLOR]) {
  982. code += R"(
  983. // Vertex Color is sRGB: Enabled
  984. if (!OUTPUT_IS_SRGB) {
  985. COLOR.rgb = mix(
  986. pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)),
  987. COLOR.rgb * (1.0 / 12.92),
  988. lessThan(COLOR.rgb, vec3(0.04045)));
  989. }
  990. )";
  991. }
  992. if (flags[FLAG_USE_POINT_SIZE]) {
  993. code += R"(
  994. // Use Point Size: Enabled
  995. POINT_SIZE = point_size;
  996. )";
  997. }
  998. if (shading_mode == SHADING_MODE_PER_VERTEX) {
  999. code += R"(
  1000. // Shading Mode: Per Vertex
  1001. ROUGHNESS = roughness;
  1002. )";
  1003. }
  1004. if (!flags[FLAG_UV1_USE_TRIPLANAR]) {
  1005. code += R"(
  1006. UV = UV * uv1_scale.xy + uv1_offset.xy;
  1007. )";
  1008. }
  1009. if (detail_uv == DETAIL_UV_2 && !flags[FLAG_UV2_USE_TRIPLANAR]) {
  1010. // Don't add a newline if the UV assignment above is already performed,
  1011. // so that UV1 and UV2 are closer to each other.
  1012. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1013. code += "\n";
  1014. }
  1015. code += R"( // Detail UV Layer: UV2
  1016. UV2 = UV2 * uv2_scale.xy + uv2_offset.xy;
  1017. )";
  1018. }
  1019. switch (billboard_mode) {
  1020. case BILLBOARD_DISABLED: {
  1021. } break;
  1022. case BILLBOARD_ENABLED: {
  1023. // `MAIN_CAM_INV_VIEW_MATRIX` is inverse of the camera, even on shadow passes.
  1024. // This ensures the billboard faces the camera when casting shadows.
  1025. code += R"(
  1026. // Billboard Mode: Enabled
  1027. MODELVIEW_MATRIX = VIEW_MATRIX * mat4(
  1028. MAIN_CAM_INV_VIEW_MATRIX[0],
  1029. MAIN_CAM_INV_VIEW_MATRIX[1],
  1030. MAIN_CAM_INV_VIEW_MATRIX[2],
  1031. MODEL_MATRIX[3]);
  1032. )";
  1033. if (flags[FLAG_BILLBOARD_KEEP_SCALE]) {
  1034. code += R"(
  1035. // Billboard Keep Scale: Enabled
  1036. MODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4(
  1037. vec4(length(MODEL_MATRIX[0].xyz), 0.0, 0.0, 0.0),
  1038. vec4(0.0, length(MODEL_MATRIX[1].xyz), 0.0, 0.0),
  1039. vec4(0.0, 0.0, length(MODEL_MATRIX[2].xyz), 0.0),
  1040. vec4(0.0, 0.0, 0.0, 1.0));
  1041. )";
  1042. }
  1043. code += " MODELVIEW_NORMAL_MATRIX = mat3(MODELVIEW_MATRIX);\n";
  1044. } break;
  1045. case BILLBOARD_FIXED_Y: {
  1046. // `MAIN_CAM_INV_VIEW_MATRIX` is inverse of the camera, even on shadow passes.
  1047. // This ensures the billboard faces the camera when casting shadows.
  1048. code += R"(
  1049. // Billboard Mode: Y-Billboard
  1050. MODELVIEW_MATRIX = VIEW_MATRIX * mat4(
  1051. vec4(normalize(cross(vec3(0.0, 1.0, 0.0), MAIN_CAM_INV_VIEW_MATRIX[2].xyz)), 0.0),
  1052. vec4(0.0, 1.0, 0.0, 0.0),
  1053. vec4(normalize(cross(MAIN_CAM_INV_VIEW_MATRIX[0].xyz, vec3(0.0, 1.0, 0.0))), 0.0),
  1054. MODEL_MATRIX[3]);
  1055. )";
  1056. if (flags[FLAG_BILLBOARD_KEEP_SCALE]) {
  1057. code += R"(
  1058. // Billboard Keep Scale: Enabled
  1059. MODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4(
  1060. vec4(length(MODEL_MATRIX[0].xyz), 0.0, 0.0, 0.0),
  1061. vec4(0.0, length(MODEL_MATRIX[1].xyz), 0.0, 0.0),
  1062. vec4(0.0, 0.0, length(MODEL_MATRIX[2].xyz), 0.0),
  1063. vec4(0.0, 0.0, 0.0, 1.0));
  1064. )";
  1065. }
  1066. code += " MODELVIEW_NORMAL_MATRIX = mat3(MODELVIEW_MATRIX);\n";
  1067. } break;
  1068. case BILLBOARD_PARTICLES: {
  1069. // Make billboard and rotated by rotation.
  1070. code += R"(
  1071. // Billboard Mode: Particles
  1072. mat4 mat_world = mat4(
  1073. normalize(INV_VIEW_MATRIX[0]),
  1074. normalize(INV_VIEW_MATRIX[1]),
  1075. normalize(INV_VIEW_MATRIX[2]),
  1076. MODEL_MATRIX[3]);
  1077. mat_world = mat_world * mat4(
  1078. vec4(cos(INSTANCE_CUSTOM.x), -sin(INSTANCE_CUSTOM.x), 0.0, 0.0),
  1079. vec4(sin(INSTANCE_CUSTOM.x), cos(INSTANCE_CUSTOM.x), 0.0, 0.0),
  1080. vec4(0.0, 0.0, 1.0, 0.0),
  1081. vec4(0.0, 0.0, 0.0, 1.0));
  1082. )";
  1083. // Set modelview.
  1084. code += " MODELVIEW_MATRIX = VIEW_MATRIX * mat_world;\n";
  1085. if (flags[FLAG_BILLBOARD_KEEP_SCALE]) {
  1086. code += R"(
  1087. // Billboard Keep Scale: Enabled
  1088. MODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4(
  1089. vec4(length(MODEL_MATRIX[0].xyz), 0.0, 0.0, 0.0),
  1090. vec4(0.0, length(MODEL_MATRIX[1].xyz), 0.0, 0.0),
  1091. vec4(0.0, 0.0, length(MODEL_MATRIX[2].xyz), 0.0),
  1092. vec4(0.0, 0.0, 0.0, 1.0));
  1093. )";
  1094. }
  1095. // Set modelview normal and handle animation.
  1096. code += R"(
  1097. MODELVIEW_NORMAL_MATRIX = mat3(MODELVIEW_MATRIX);
  1098. float h_frames = float(particles_anim_h_frames);
  1099. float v_frames = float(particles_anim_v_frames);
  1100. float particle_total_frames = float(particles_anim_h_frames * particles_anim_v_frames);
  1101. float particle_frame = floor(INSTANCE_CUSTOM.z * float(particle_total_frames));
  1102. if (!particles_anim_loop) {
  1103. particle_frame = clamp(particle_frame, 0.0, particle_total_frames - 1.0);
  1104. } else {
  1105. particle_frame = mod(particle_frame, particle_total_frames);
  1106. }
  1107. UV /= vec2(h_frames, v_frames);
  1108. UV += vec2(mod(particle_frame, h_frames) / h_frames, floor((particle_frame + 0.5) / h_frames) / v_frames);
  1109. )";
  1110. } break;
  1111. case BILLBOARD_MAX:
  1112. break; // Internal value, skip.
  1113. }
  1114. if (flags[FLAG_FIXED_SIZE]) {
  1115. code += R"(
  1116. // Fixed Size: Enabled
  1117. if (PROJECTION_MATRIX[3][3] != 0.0) {
  1118. // Orthogonal matrix; try to do about the same with viewport size.
  1119. float h = abs(1.0 / (2.0 * PROJECTION_MATRIX[1][1]));
  1120. // Consistent with vertical FOV (Keep Height).
  1121. float sc = (h * 2.0);
  1122. MODELVIEW_MATRIX[0] *= sc;
  1123. MODELVIEW_MATRIX[1] *= sc;
  1124. MODELVIEW_MATRIX[2] *= sc;
  1125. } else {
  1126. // Scale by depth.
  1127. float sc = -(MODELVIEW_MATRIX)[3].z;
  1128. MODELVIEW_MATRIX[0] *= sc;
  1129. MODELVIEW_MATRIX[1] *= sc;
  1130. MODELVIEW_MATRIX[2] *= sc;
  1131. }
  1132. )";
  1133. }
  1134. if (flags[FLAG_UV1_USE_TRIPLANAR] || flags[FLAG_UV2_USE_TRIPLANAR]) {
  1135. // Generate tangent and binormal in world space.
  1136. if (flags[FLAG_UV1_USE_WORLD_TRIPLANAR]) {
  1137. code += R"(
  1138. vec3 normal = MODEL_NORMAL_MATRIX * NORMAL;
  1139. )";
  1140. } else {
  1141. code += R"(
  1142. vec3 normal = NORMAL;
  1143. )";
  1144. }
  1145. code += R"(
  1146. TANGENT = vec3(0.0, 0.0, -1.0) * abs(normal.x);
  1147. TANGENT += vec3(1.0, 0.0, 0.0) * abs(normal.y);
  1148. TANGENT += vec3(1.0, 0.0, 0.0) * abs(normal.z);
  1149. )";
  1150. if (flags[FLAG_UV1_USE_WORLD_TRIPLANAR]) {
  1151. code += " TANGENT = inverse(MODEL_NORMAL_MATRIX) * normalize(TANGENT);\n";
  1152. } else {
  1153. code += " TANGENT = normalize(TANGENT);\n";
  1154. }
  1155. code += R"(
  1156. BINORMAL = vec3(0.0, 1.0, 0.0) * abs(normal.x);
  1157. BINORMAL += vec3(0.0, 0.0, -1.0) * abs(normal.y);
  1158. BINORMAL += vec3(0.0, 1.0, 0.0) * abs(normal.z);
  1159. )";
  1160. if (flags[FLAG_UV1_USE_WORLD_TRIPLANAR]) {
  1161. code += " BINORMAL = inverse(MODEL_NORMAL_MATRIX) * normalize(BINORMAL);\n";
  1162. } else {
  1163. code += " BINORMAL = normalize(BINORMAL);\n";
  1164. }
  1165. }
  1166. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1167. if (flags[FLAG_UV1_USE_WORLD_TRIPLANAR]) {
  1168. code += R"(
  1169. // UV1 Triplanar: Enabled (with World Triplanar)
  1170. uv1_power_normal = pow(abs(normal), vec3(uv1_blend_sharpness));
  1171. uv1_triplanar_pos = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz * uv1_scale + uv1_offset;
  1172. )";
  1173. } else {
  1174. code += R"(
  1175. // UV1 Triplanar: Enabled
  1176. uv1_power_normal = pow(abs(NORMAL), vec3(uv1_blend_sharpness));
  1177. uv1_triplanar_pos = VERTEX * uv1_scale + uv1_offset;
  1178. )";
  1179. }
  1180. code += R"( uv1_power_normal /= dot(uv1_power_normal, vec3(1.0));
  1181. uv1_triplanar_pos *= vec3(1.0, -1.0, 1.0);
  1182. )";
  1183. }
  1184. if (flags[FLAG_UV2_USE_TRIPLANAR]) {
  1185. if (flags[FLAG_UV2_USE_WORLD_TRIPLANAR]) {
  1186. code += R"(
  1187. // UV2 Triplanar: Enabled (with World Triplanar)
  1188. uv2_power_normal = pow(abs(mat3(MODEL_MATRIX) * NORMAL), vec3(uv2_blend_sharpness));
  1189. uv2_triplanar_pos = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz * uv2_scale + uv2_offset;
  1190. )";
  1191. } else {
  1192. code += R"(
  1193. // UV2 Triplanar: Enabled
  1194. uv2_power_normal = pow(abs(NORMAL), vec3(uv2_blend_sharpness));
  1195. uv2_triplanar_pos = VERTEX * uv2_scale + uv2_offset;
  1196. )";
  1197. }
  1198. code += R"( uv2_power_normal /= dot(uv2_power_normal, vec3(1.0));
  1199. uv2_triplanar_pos *= vec3(1.0, -1.0, 1.0);
  1200. )";
  1201. }
  1202. if (grow_enabled) {
  1203. code += R"(
  1204. // Grow: Enabled
  1205. VERTEX += NORMAL * grow;
  1206. )";
  1207. }
  1208. code += "}\n";
  1209. if (flags[FLAG_ALBEDO_TEXTURE_MSDF] && !flags[FLAG_UV1_USE_TRIPLANAR]) {
  1210. code += R"(
  1211. float msdf_median(float r, float g, float b, float a) {
  1212. return min(max(min(r, g), min(max(r, g), b)), a);
  1213. }
  1214. )";
  1215. }
  1216. if (flags[FLAG_UV1_USE_TRIPLANAR] || flags[FLAG_UV2_USE_TRIPLANAR]) {
  1217. code += R"(
  1218. vec4 triplanar_texture(sampler2D p_sampler, vec3 p_weights, vec3 p_triplanar_pos) {
  1219. vec4 samp = vec4(0.0);
  1220. samp += texture(p_sampler, p_triplanar_pos.xy) * p_weights.z;
  1221. samp += texture(p_sampler, p_triplanar_pos.xz) * p_weights.y;
  1222. samp += texture(p_sampler, p_triplanar_pos.zy * vec2(-1.0, 1.0)) * p_weights.x;
  1223. return samp;
  1224. }
  1225. )";
  1226. }
  1227. // Generate fragment shader.
  1228. code += R"(
  1229. void fragment() {)";
  1230. if (!flags[FLAG_UV1_USE_TRIPLANAR]) {
  1231. code += R"(
  1232. vec2 base_uv = UV;
  1233. )";
  1234. }
  1235. 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])) {
  1236. // Don't add a newline if the UV assignment above is already performed,
  1237. // so that UV1 and UV2 are closer to each other.
  1238. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1239. code += "\n";
  1240. }
  1241. code += R"( vec2 base_uv2 = UV2;
  1242. )";
  1243. }
  1244. if (features[FEATURE_HEIGHT_MAPPING] && flags[FLAG_UV1_USE_TRIPLANAR]) {
  1245. // Display both resource name and albedo texture name.
  1246. // Materials are often built-in to scenes, so displaying the resource name alone may not be meaningful.
  1247. // On the other hand, albedo textures are almost always external to the scene.
  1248. if (textures[TEXTURE_ALBEDO].is_valid()) {
  1249. WARN_PRINT(vformat("%s (albedo %s): Height mapping is not supported on triplanar materials. Ignoring height mapping in favor of triplanar mapping.", get_path(), textures[TEXTURE_ALBEDO]->get_path()));
  1250. } else if (!get_path().is_empty()) {
  1251. WARN_PRINT(vformat("%s: Height mapping is not supported on triplanar materials. Ignoring height mapping in favor of triplanar mapping.", get_path()));
  1252. } else {
  1253. // Resource wasn't saved yet.
  1254. WARN_PRINT("Height mapping is not supported on triplanar materials. Ignoring height mapping in favor of triplanar mapping.");
  1255. }
  1256. }
  1257. // Heightmapping isn't supported at the same time as triplanar mapping.
  1258. if (features[FEATURE_HEIGHT_MAPPING] && !flags[FLAG_UV1_USE_TRIPLANAR]) {
  1259. // Binormal is negative due to mikktspace. Flipping it "unflips" it.
  1260. code += R"(
  1261. {
  1262. // Height: Enabled
  1263. vec3 view_dir = normalize(normalize(-VERTEX + EYE_OFFSET) * mat3(TANGENT * heightmap_flip.x, -BINORMAL * heightmap_flip.y, NORMAL));
  1264. )";
  1265. if (deep_parallax) {
  1266. // Multiply the heightmap scale by 0.01 to improve heightmap scale usability.
  1267. code += R"(
  1268. // Height Deep Parallax: Enabled
  1269. float num_layers = mix(float(heightmap_max_layers), float(heightmap_min_layers), abs(dot(vec3(0.0, 0.0, 1.0), view_dir)));
  1270. float layer_depth = 1.0 / num_layers;
  1271. float current_layer_depth = 0.0;
  1272. vec2 p = view_dir.xy * heightmap_scale * 0.01;
  1273. vec2 delta = p / num_layers;
  1274. vec2 ofs = base_uv;
  1275. )";
  1276. if (flags[FLAG_INVERT_HEIGHTMAP]) {
  1277. code += " float depth = texture(texture_heightmap, ofs).r;\n";
  1278. } else {
  1279. code += " float depth = 1.0 - texture(texture_heightmap, ofs).r;\n";
  1280. }
  1281. code += R"(
  1282. float current_depth = 0.0;
  1283. while (current_depth < depth) {
  1284. ofs -= delta;
  1285. )";
  1286. if (flags[FLAG_INVERT_HEIGHTMAP]) {
  1287. code += " depth = texture(texture_heightmap, ofs).r;\n";
  1288. } else {
  1289. code += " depth = 1.0 - texture(texture_heightmap, ofs).r;\n";
  1290. }
  1291. code += R"(
  1292. current_depth += layer_depth;
  1293. }
  1294. vec2 prev_ofs = ofs + delta;
  1295. float after_depth = depth - current_depth;
  1296. )";
  1297. if (flags[FLAG_INVERT_HEIGHTMAP]) {
  1298. code += " float before_depth = texture(texture_heightmap, prev_ofs).r - current_depth + layer_depth;\n";
  1299. } else {
  1300. code += " float before_depth = (1.0 - texture(texture_heightmap, prev_ofs).r) - current_depth + layer_depth;\n";
  1301. }
  1302. code += R"(
  1303. float weight = after_depth / (after_depth - before_depth);
  1304. ofs = mix(ofs, prev_ofs, weight);
  1305. )";
  1306. } else {
  1307. if (flags[FLAG_INVERT_HEIGHTMAP]) {
  1308. code += " float depth = texture(texture_heightmap, base_uv).r;\n";
  1309. } else {
  1310. code += " float depth = 1.0 - texture(texture_heightmap, base_uv).r;\n";
  1311. }
  1312. // Use offset limiting to improve the appearance of non-deep parallax.
  1313. // This reduces the impression of depth, but avoids visible warping in the distance.
  1314. // Multiply the heightmap scale by 0.01 to improve heightmap scale usability.
  1315. code += " vec2 ofs = base_uv - view_dir.xy * depth * heightmap_scale * 0.01;\n";
  1316. }
  1317. code += " base_uv = ofs;\n";
  1318. if (features[FEATURE_DETAIL] && detail_uv == DETAIL_UV_2) {
  1319. code += " base_uv2 -= ofs;\n";
  1320. }
  1321. code += " }\n";
  1322. }
  1323. if (flags[FLAG_USE_POINT_SIZE]) {
  1324. code += R"(
  1325. // Use Point Size: Enabled
  1326. vec4 albedo_tex = texture(texture_albedo, POINT_COORD);
  1327. )";
  1328. } else {
  1329. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1330. code += R"(
  1331. vec4 albedo_tex = triplanar_texture(texture_albedo, uv1_power_normal, uv1_triplanar_pos);
  1332. )";
  1333. } else {
  1334. code += R"(
  1335. vec4 albedo_tex = texture(texture_albedo, base_uv);
  1336. )";
  1337. }
  1338. }
  1339. if (flags[FLAG_ALBEDO_TEXTURE_MSDF] && !flags[FLAG_UV1_USE_TRIPLANAR]) {
  1340. code += R"(
  1341. {
  1342. // Albedo Texture MSDF: Enabled
  1343. albedo_tex.rgb = mix(
  1344. vec3(1.0 + 0.055) * pow(albedo_tex.rgb, vec3(1.0 / 2.4)) - vec3(0.055),
  1345. vec3(12.92) * albedo_tex.rgb,
  1346. lessThan(albedo_tex.rgb, vec3(0.0031308)));
  1347. vec2 msdf_size = vec2(msdf_pixel_range) / vec2(textureSize(texture_albedo, 0));
  1348. )";
  1349. if (flags[FLAG_USE_POINT_SIZE]) {
  1350. code += " vec2 dest_size = vec2(1.0) / fwidth(POINT_COORD);\n";
  1351. } else {
  1352. code += " vec2 dest_size = vec2(1.0) / fwidth(base_uv);\n";
  1353. }
  1354. code += R"(
  1355. float px_size = max(0.5 * dot(msdf_size, dest_size), 1.0);
  1356. float d = msdf_median(albedo_tex.r, albedo_tex.g, albedo_tex.b, albedo_tex.a) - 0.5;
  1357. if (msdf_outline_size > 0.0) {
  1358. float cr = clamp(msdf_outline_size, 0.0, msdf_pixel_range / 2.0) / msdf_pixel_range;
  1359. albedo_tex.a = clamp((d + cr) * px_size, 0.0, 1.0);
  1360. } else {
  1361. albedo_tex.a = clamp(d * px_size + 0.5, 0.0, 1.0);
  1362. }
  1363. albedo_tex.rgb = vec3(1.0);
  1364. }
  1365. )";
  1366. } else if (flags[FLAG_ALBEDO_TEXTURE_FORCE_SRGB]) {
  1367. code += R"(
  1368. // Albedo Texture Force sRGB: Enabled
  1369. albedo_tex.rgb = mix(
  1370. pow((albedo_tex.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)),
  1371. albedo_tex.rgb.rgb * (1.0 / 12.92),
  1372. lessThan(albedo_tex.rgb, vec3(0.04045)));
  1373. )";
  1374. }
  1375. if (flags[FLAG_ALBEDO_FROM_VERTEX_COLOR]) {
  1376. code += R"(
  1377. // Vertex Color Use as Albedo: Enabled
  1378. albedo_tex *= COLOR;
  1379. )";
  1380. }
  1381. code += " ALBEDO = albedo.rgb * albedo_tex.rgb;\n";
  1382. if (!orm) {
  1383. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1384. code += R"(
  1385. float metallic_tex = dot(triplanar_texture(texture_metallic, uv1_power_normal, uv1_triplanar_pos), metallic_texture_channel);
  1386. )";
  1387. } else {
  1388. code += R"(
  1389. float metallic_tex = dot(texture(texture_metallic, base_uv), metallic_texture_channel);
  1390. )";
  1391. }
  1392. code += R"( METALLIC = metallic_tex * metallic;
  1393. SPECULAR = specular;
  1394. )";
  1395. switch (roughness_texture_channel) {
  1396. case TEXTURE_CHANNEL_RED: {
  1397. code += R"(
  1398. vec4 roughness_texture_channel = vec4(1.0, 0.0, 0.0, 0.0);
  1399. )";
  1400. } break;
  1401. case TEXTURE_CHANNEL_GREEN: {
  1402. code += R"(
  1403. vec4 roughness_texture_channel = vec4(0.0, 1.0, 0.0, 0.0);
  1404. )";
  1405. } break;
  1406. case TEXTURE_CHANNEL_BLUE: {
  1407. code += R"(
  1408. vec4 roughness_texture_channel = vec4(0.0, 0.0, 1.0, 0.0);
  1409. )";
  1410. } break;
  1411. case TEXTURE_CHANNEL_ALPHA: {
  1412. code += R"(
  1413. vec4 roughness_texture_channel = vec4(0.0, 0.0, 0.0, 1.0);
  1414. )";
  1415. } break;
  1416. case TEXTURE_CHANNEL_GRAYSCALE: {
  1417. code += R"(
  1418. vec4 roughness_texture_channel = vec4(0.333333, 0.333333, 0.333333, 0.0);
  1419. )";
  1420. } break;
  1421. case TEXTURE_CHANNEL_MAX:
  1422. break; // Internal value, skip.
  1423. }
  1424. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1425. code += " float roughness_tex = dot(triplanar_texture(texture_roughness, uv1_power_normal, uv1_triplanar_pos), roughness_texture_channel);\n";
  1426. } else {
  1427. code += " float roughness_tex = dot(texture(texture_roughness, base_uv), roughness_texture_channel);\n";
  1428. }
  1429. code += R"( ROUGHNESS = roughness_tex * roughness;
  1430. )";
  1431. } else {
  1432. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1433. code += R"(
  1434. vec4 orm_tex = triplanar_texture(texture_orm, uv1_power_normal, uv1_triplanar_pos);
  1435. )";
  1436. } else {
  1437. code += R"(
  1438. vec4 orm_tex = texture(texture_orm, base_uv);
  1439. )";
  1440. }
  1441. code += R"( ROUGHNESS = orm_tex.g;
  1442. METALLIC = orm_tex.b;
  1443. )";
  1444. }
  1445. if (features[FEATURE_NORMAL_MAPPING]) {
  1446. code += R"(
  1447. // Normal Map: Enabled
  1448. )";
  1449. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1450. code += " NORMAL_MAP = triplanar_texture(texture_normal, uv1_power_normal, uv1_triplanar_pos).rgb;\n";
  1451. } else {
  1452. code += " NORMAL_MAP = texture(texture_normal, base_uv).rgb;\n";
  1453. }
  1454. code += " NORMAL_MAP_DEPTH = normal_scale;\n";
  1455. }
  1456. if (features[FEATURE_EMISSION]) {
  1457. code += R"(
  1458. // Emission: Enabled
  1459. )";
  1460. if (flags[FLAG_EMISSION_ON_UV2]) {
  1461. if (flags[FLAG_UV2_USE_TRIPLANAR]) {
  1462. code += " vec3 emission_tex = triplanar_texture(texture_emission, uv2_power_normal, uv2_triplanar_pos).rgb;\n";
  1463. } else {
  1464. code += " vec3 emission_tex = texture(texture_emission, base_uv2).rgb;\n";
  1465. }
  1466. } else {
  1467. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1468. code += " vec3 emission_tex = triplanar_texture(texture_emission, uv1_power_normal, uv1_triplanar_pos).rgb;\n";
  1469. } else {
  1470. code += " vec3 emission_tex = texture(texture_emission, base_uv).rgb;\n";
  1471. }
  1472. }
  1473. if (emission_op == EMISSION_OP_ADD) {
  1474. code += R"( // Emission Operator: Add
  1475. EMISSION = (emission.rgb + emission_tex) * emission_energy;
  1476. )";
  1477. } else {
  1478. code += R"( // Emission Operator: Multiply
  1479. EMISSION = (emission.rgb * emission_tex) * emission_energy;
  1480. )";
  1481. }
  1482. }
  1483. if (features[FEATURE_REFRACTION]) {
  1484. if (features[FEATURE_NORMAL_MAPPING]) {
  1485. code += R"(
  1486. // Refraction: Enabled (with normal map texture)
  1487. vec3 unpacked_normal = NORMAL_MAP;
  1488. unpacked_normal.xy = unpacked_normal.xy * 2.0 - 1.0;
  1489. unpacked_normal.z = sqrt(max(0.0, 1.0 - dot(unpacked_normal.xy, unpacked_normal.xy)));
  1490. vec3 ref_normal = normalize(mix(
  1491. NORMAL,
  1492. TANGENT * unpacked_normal.x + BINORMAL * unpacked_normal.y + NORMAL * unpacked_normal.z,
  1493. NORMAL_MAP_DEPTH));
  1494. )";
  1495. } else {
  1496. code += R"(
  1497. // Refraction: Enabled
  1498. vec3 ref_normal = NORMAL;
  1499. )";
  1500. }
  1501. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1502. code += " vec2 ref_ofs = SCREEN_UV - ref_normal.xy * dot(triplanar_texture(texture_refraction, uv1_power_normal, uv1_triplanar_pos), refraction_texture_channel) * refraction;\n";
  1503. } else {
  1504. code += " vec2 ref_ofs = SCREEN_UV - ref_normal.xy * dot(texture(texture_refraction, base_uv), refraction_texture_channel) * refraction;\n";
  1505. }
  1506. code += R"(
  1507. float ref_amount = 1.0 - albedo.a * albedo_tex.a;
  1508. float refraction_depth_tex = textureLod(depth_texture, ref_ofs, 0.0).r;
  1509. vec4 refraction_view_pos = INV_PROJECTION_MATRIX * vec4(SCREEN_UV * 2.0 - 1.0, refraction_depth_tex, 1.0);
  1510. refraction_view_pos.xyz /= refraction_view_pos.w;
  1511. // If the depth buffer is lower then the model's Z position, use the refracted UV, otherwise use the normal screen UV.
  1512. // At low depth differences, decrease refraction intensity to avoid sudden discontinuities.
  1513. EMISSION += textureLod(screen_texture, mix(SCREEN_UV, ref_ofs, smoothstep(0.0, 1.0, VERTEX.z - refraction_view_pos.z)), ROUGHNESS * 8.0).rgb * ref_amount * EXPOSURE;
  1514. ALBEDO *= 1.0 - ref_amount;
  1515. // Force transparency on the material (required for refraction).
  1516. ALPHA = 1.0;
  1517. )";
  1518. } else if (transparency != TRANSPARENCY_DISABLED || flags[FLAG_USE_SHADOW_TO_OPACITY] || (distance_fade == DISTANCE_FADE_PIXEL_ALPHA) || proximity_fade_enabled) {
  1519. code += " ALPHA *= albedo.a * albedo_tex.a;\n";
  1520. }
  1521. if (transparency == TRANSPARENCY_ALPHA_HASH) {
  1522. code += " ALPHA_HASH_SCALE = alpha_hash_scale;\n";
  1523. } else if (transparency == TRANSPARENCY_ALPHA_SCISSOR) {
  1524. code += " ALPHA_SCISSOR_THRESHOLD = alpha_scissor_threshold;\n";
  1525. }
  1526. if (alpha_antialiasing_mode != ALPHA_ANTIALIASING_OFF && (transparency == TRANSPARENCY_ALPHA_HASH || transparency == TRANSPARENCY_ALPHA_SCISSOR)) {
  1527. code += " ALPHA_ANTIALIASING_EDGE = alpha_antialiasing_edge;\n";
  1528. code += " ALPHA_TEXTURE_COORDINATE = UV * vec2(albedo_texture_size);\n";
  1529. }
  1530. if (proximity_fade_enabled) {
  1531. code += R"(
  1532. // Proximity Fade: Enabled
  1533. float proximity_depth_tex = textureLod(depth_texture, SCREEN_UV, 0.0).r;
  1534. vec4 proximity_view_pos = INV_PROJECTION_MATRIX * vec4(SCREEN_UV * 2.0 - 1.0, proximity_depth_tex, 1.0);
  1535. proximity_view_pos.xyz /= proximity_view_pos.w;
  1536. ALPHA *= clamp(1.0 - smoothstep(proximity_view_pos.z + proximity_fade_distance, proximity_view_pos.z, VERTEX.z), 0.0, 1.0);
  1537. )";
  1538. }
  1539. if (distance_fade != DISTANCE_FADE_DISABLED) {
  1540. // Use the slightly more expensive circular fade (distance to the object) instead of linear
  1541. // (Z distance), so that the fade is always the same regardless of the camera angle.
  1542. if ((distance_fade == DISTANCE_FADE_OBJECT_DITHER || distance_fade == DISTANCE_FADE_PIXEL_DITHER)) {
  1543. code += "\n {";
  1544. if (distance_fade == DISTANCE_FADE_OBJECT_DITHER) {
  1545. code += R"(
  1546. // Distance Fade: Object Dither
  1547. float fade_distance = length((VIEW_MATRIX * MODEL_MATRIX[3]));
  1548. )";
  1549. } else {
  1550. code += R"(
  1551. // Distance Fade: Pixel Dither
  1552. float fade_distance = length(VERTEX);
  1553. )";
  1554. }
  1555. code += R"(
  1556. // Use interleaved gradient noise, which is fast but still looks good.
  1557. const vec3 magic = vec3(0.06711056, 0.00583715, 52.9829189);
  1558. float fade = clamp(smoothstep(distance_fade_min, distance_fade_max, fade_distance), 0.0, 1.0);
  1559. // Use a hard cap to prevent a few stray pixels from remaining when past the fade-out distance.
  1560. if (fade < 0.001 || fade < fract(magic.z * fract(dot(FRAGCOORD.xy, magic.xy)))) {
  1561. discard;
  1562. }
  1563. }
  1564. )";
  1565. } else {
  1566. code += R"(
  1567. // Distance Fade: Pixel Alpha
  1568. ALPHA *= clamp(smoothstep(distance_fade_min, distance_fade_max, length(VERTEX)), 0.0, 1.0);
  1569. )";
  1570. }
  1571. }
  1572. if (features[FEATURE_RIM]) {
  1573. code += R"(
  1574. // Rim: Enabled
  1575. )";
  1576. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1577. code += " vec2 rim_tex = triplanar_texture(texture_rim, uv1_power_normal, uv1_triplanar_pos).xy;\n";
  1578. } else {
  1579. code += " vec2 rim_tex = texture(texture_rim, base_uv).xy;\n";
  1580. }
  1581. code += R"( RIM = rim * rim_tex.x;
  1582. RIM_TINT = rim_tint * rim_tex.y;
  1583. )";
  1584. }
  1585. if (features[FEATURE_CLEARCOAT]) {
  1586. code += R"(
  1587. // Clearcoat: Enabled
  1588. )";
  1589. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1590. code += " vec2 clearcoat_tex = triplanar_texture(texture_clearcoat, uv1_power_normal, uv1_triplanar_pos).xy;\n";
  1591. } else {
  1592. code += " vec2 clearcoat_tex = texture(texture_clearcoat, base_uv).xy;\n";
  1593. }
  1594. code += R"( CLEARCOAT = clearcoat * clearcoat_tex.x;
  1595. CLEARCOAT_ROUGHNESS = clearcoat_roughness * clearcoat_tex.y;
  1596. )";
  1597. }
  1598. if (features[FEATURE_ANISOTROPY]) {
  1599. code += R"(
  1600. // Anisotropy: Enabled
  1601. )";
  1602. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1603. code += " vec3 anisotropy_tex = triplanar_texture(texture_flowmap, uv1_power_normal, uv1_triplanar_pos).rga;\n";
  1604. } else {
  1605. code += " vec3 anisotropy_tex = texture(texture_flowmap, base_uv).rga;\n";
  1606. }
  1607. code += R"( ANISOTROPY = anisotropy_ratio * anisotropy_tex.b;
  1608. ANISOTROPY_FLOW = anisotropy_tex.rg * 2.0 - 1.0;
  1609. )";
  1610. }
  1611. if (features[FEATURE_AMBIENT_OCCLUSION]) {
  1612. code += R"(
  1613. // Ambient Occlusion: Enabled
  1614. )";
  1615. if (!orm) {
  1616. if (flags[FLAG_AO_ON_UV2]) {
  1617. if (flags[FLAG_UV2_USE_TRIPLANAR]) {
  1618. code += " AO = dot(triplanar_texture(texture_ambient_occlusion, uv2_power_normal, uv2_triplanar_pos), ao_texture_channel);\n";
  1619. } else {
  1620. code += " AO = dot(texture(texture_ambient_occlusion, base_uv2), ao_texture_channel);\n";
  1621. }
  1622. } else {
  1623. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1624. code += " AO = dot(triplanar_texture(texture_ambient_occlusion, uv1_power_normal, uv1_triplanar_pos), ao_texture_channel);\n";
  1625. } else {
  1626. code += " AO = dot(texture(texture_ambient_occlusion, base_uv), ao_texture_channel);\n";
  1627. }
  1628. }
  1629. } else {
  1630. code += " AO = orm_tex.r;\n";
  1631. }
  1632. code += " AO_LIGHT_AFFECT = ao_light_affect;\n";
  1633. }
  1634. if (features[FEATURE_SUBSURFACE_SCATTERING]) {
  1635. code += R"(
  1636. // Subsurface Scattering: Enabled
  1637. )";
  1638. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1639. code += " float sss_tex = triplanar_texture(texture_subsurface_scattering, uv1_power_normal, uv1_triplanar_pos).r;\n";
  1640. } else {
  1641. code += " float sss_tex = texture(texture_subsurface_scattering, base_uv).r;\n";
  1642. }
  1643. code += " SSS_STRENGTH = subsurface_scattering_strength * sss_tex;\n";
  1644. }
  1645. if (features[FEATURE_SUBSURFACE_TRANSMITTANCE]) {
  1646. code += R"(
  1647. // Subsurface Scattering Transmittance: Enabled
  1648. )";
  1649. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1650. code += " vec4 trans_color_tex = triplanar_texture(texture_subsurface_transmittance, uv1_power_normal, uv1_triplanar_pos);\n";
  1651. } else {
  1652. code += " vec4 trans_color_tex = texture(texture_subsurface_transmittance, base_uv);\n";
  1653. }
  1654. code += " SSS_TRANSMITTANCE_COLOR = transmittance_color * trans_color_tex;\n";
  1655. code += R"( SSS_TRANSMITTANCE_DEPTH = transmittance_depth;
  1656. SSS_TRANSMITTANCE_BOOST = transmittance_boost;
  1657. )";
  1658. }
  1659. if (features[FEATURE_BACKLIGHT]) {
  1660. code += R"(
  1661. // Backlight: Enabled
  1662. )";
  1663. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1664. code += " vec3 backlight_tex = triplanar_texture(texture_backlight, uv1_power_normal, uv1_triplanar_pos).rgb;\n";
  1665. } else {
  1666. code += " vec3 backlight_tex = texture(texture_backlight, base_uv).rgb;\n";
  1667. }
  1668. code += " BACKLIGHT = (backlight.rgb + backlight_tex);\n";
  1669. }
  1670. if (features[FEATURE_DETAIL]) {
  1671. code += R"(
  1672. // Detail: Enabled
  1673. )";
  1674. const bool triplanar = (flags[FLAG_UV1_USE_TRIPLANAR] && detail_uv == DETAIL_UV_1) || (flags[FLAG_UV2_USE_TRIPLANAR] && detail_uv == DETAIL_UV_2);
  1675. if (triplanar) {
  1676. const String tp_uv = detail_uv == DETAIL_UV_1 ? "uv1" : "uv2";
  1677. code += vformat(R"( vec4 detail_tex = triplanar_texture(texture_detail_albedo, %s_power_normal, %s_triplanar_pos);
  1678. vec4 detail_norm_tex = triplanar_texture(texture_detail_normal, %s_power_normal, %s_triplanar_pos);
  1679. )",
  1680. tp_uv, tp_uv, tp_uv, tp_uv);
  1681. } else {
  1682. const String det_uv = detail_uv == DETAIL_UV_1 ? "base_uv" : "base_uv2";
  1683. code += vformat(R"( vec4 detail_tex = texture(texture_detail_albedo, %s);
  1684. vec4 detail_norm_tex = texture(texture_detail_normal, %s);
  1685. )",
  1686. det_uv, det_uv);
  1687. }
  1688. if (flags[FLAG_UV1_USE_TRIPLANAR]) {
  1689. code += " vec4 detail_mask_tex = triplanar_texture(texture_detail_mask, uv1_power_normal, uv1_triplanar_pos);\n";
  1690. } else {
  1691. code += " vec4 detail_mask_tex = texture(texture_detail_mask, base_uv);\n";
  1692. }
  1693. switch (detail_blend_mode) {
  1694. case BLEND_MODE_MIX: {
  1695. code += R"(
  1696. // Detail Blend Mode: Mix
  1697. vec3 detail = mix(ALBEDO.rgb, detail_tex.rgb, detail_tex.a);
  1698. )";
  1699. } break;
  1700. case BLEND_MODE_ADD: {
  1701. code += R"(
  1702. // Detail Blend Mode: Add
  1703. vec3 detail = mix(ALBEDO.rgb, ALBEDO.rgb + detail_tex.rgb, detail_tex.a);
  1704. )";
  1705. } break;
  1706. case BLEND_MODE_SUB: {
  1707. code += R"(
  1708. // Detail Blend Mode: Subtract
  1709. vec3 detail = mix(ALBEDO.rgb, ALBEDO.rgb - detail_tex.rgb, detail_tex.a);
  1710. )";
  1711. } break;
  1712. case BLEND_MODE_MUL: {
  1713. code += R"(
  1714. // Detail Blend Mode: Multiply
  1715. vec3 detail = mix(ALBEDO.rgb, ALBEDO.rgb * detail_tex.rgb, detail_tex.a);
  1716. )";
  1717. } break;
  1718. case BLEND_MODE_PREMULT_ALPHA: {
  1719. // This is unlikely to ever be used for detail textures, and in order for it to function in the editor, another bit must be used in MaterialKey,
  1720. // but there are only 5 bits left, so I'm going to leave this disabled unless it's actually requested.
  1721. //code += "\tvec3 detail = (1.0-detail_tex.a)*ALBEDO.rgb+detail_tex.rgb;\n";
  1722. } break;
  1723. case BLEND_MODE_MAX:
  1724. break; // Internal value, skip.
  1725. }
  1726. code += R"( vec3 detail_norm = mix(NORMAL_MAP, detail_norm_tex.rgb, detail_tex.a);
  1727. NORMAL_MAP = mix(NORMAL_MAP, detail_norm, detail_mask_tex.r);
  1728. ALBEDO.rgb = mix(ALBEDO.rgb, detail, detail_mask_tex.r);
  1729. )";
  1730. }
  1731. code += "}\n";
  1732. ShaderData shader_data;
  1733. shader_data.shader = RS::get_singleton()->shader_create_from_code(code);
  1734. shader_data.users = 1;
  1735. shader_map[mk] = shader_data;
  1736. shader_rid = shader_data.shader;
  1737. if (_get_material().is_valid()) {
  1738. RS::get_singleton()->material_set_shader(_get_material(), shader_rid);
  1739. }
  1740. }
  1741. void BaseMaterial3D::_check_material_rid() {
  1742. MutexLock lock(material_rid_mutex);
  1743. if (_get_material().is_null()) {
  1744. RID next_pass_rid;
  1745. if (get_next_pass().is_valid()) {
  1746. next_pass_rid = get_next_pass()->get_rid();
  1747. }
  1748. _set_material(RS::get_singleton()->material_create_from_shader(next_pass_rid, get_render_priority(), shader_rid));
  1749. for (KeyValue<StringName, Variant> param : pending_params) {
  1750. RS::get_singleton()->material_set_param(_get_material(), param.key, param.value);
  1751. }
  1752. pending_params.clear();
  1753. }
  1754. }
  1755. void BaseMaterial3D::_material_set_param(const StringName &p_name, const Variant &p_value) {
  1756. if (_get_material().is_valid()) {
  1757. RS::get_singleton()->material_set_param(_get_material(), p_name, p_value);
  1758. } else {
  1759. pending_params[p_name] = p_value;
  1760. }
  1761. }
  1762. void BaseMaterial3D::set_albedo(const Color &p_albedo) {
  1763. albedo = p_albedo;
  1764. _material_set_param(shader_names->albedo, p_albedo);
  1765. }
  1766. Color BaseMaterial3D::get_albedo() const {
  1767. return albedo;
  1768. }
  1769. void BaseMaterial3D::set_specular(float p_specular) {
  1770. specular = p_specular;
  1771. _material_set_param(shader_names->specular, p_specular);
  1772. }
  1773. float BaseMaterial3D::get_specular() const {
  1774. return specular;
  1775. }
  1776. void BaseMaterial3D::set_roughness(float p_roughness) {
  1777. roughness = p_roughness;
  1778. _material_set_param(shader_names->roughness, p_roughness);
  1779. }
  1780. float BaseMaterial3D::get_roughness() const {
  1781. return roughness;
  1782. }
  1783. void BaseMaterial3D::set_metallic(float p_metallic) {
  1784. metallic = p_metallic;
  1785. _material_set_param(shader_names->metallic, p_metallic);
  1786. }
  1787. float BaseMaterial3D::get_metallic() const {
  1788. return metallic;
  1789. }
  1790. void BaseMaterial3D::set_emission(const Color &p_emission) {
  1791. emission = p_emission;
  1792. _material_set_param(shader_names->emission, p_emission);
  1793. }
  1794. Color BaseMaterial3D::get_emission() const {
  1795. return emission;
  1796. }
  1797. void BaseMaterial3D::set_emission_energy_multiplier(float p_emission_energy_multiplier) {
  1798. emission_energy_multiplier = p_emission_energy_multiplier;
  1799. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  1800. _material_set_param(shader_names->emission_energy, p_emission_energy_multiplier * emission_intensity);
  1801. } else {
  1802. _material_set_param(shader_names->emission_energy, p_emission_energy_multiplier);
  1803. }
  1804. }
  1805. float BaseMaterial3D::get_emission_energy_multiplier() const {
  1806. return emission_energy_multiplier;
  1807. }
  1808. void BaseMaterial3D::set_emission_intensity(float p_emission_intensity) {
  1809. ERR_FAIL_COND_EDMSG(!GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units"), "Cannot set material emission intensity when Physical Light Units disabled.");
  1810. emission_intensity = p_emission_intensity;
  1811. _material_set_param(shader_names->emission_energy, emission_energy_multiplier * emission_intensity);
  1812. }
  1813. float BaseMaterial3D::get_emission_intensity() const {
  1814. return emission_intensity;
  1815. }
  1816. void BaseMaterial3D::set_normal_scale(float p_normal_scale) {
  1817. normal_scale = p_normal_scale;
  1818. _material_set_param(shader_names->normal_scale, p_normal_scale);
  1819. }
  1820. float BaseMaterial3D::get_normal_scale() const {
  1821. return normal_scale;
  1822. }
  1823. void BaseMaterial3D::set_rim(float p_rim) {
  1824. rim = p_rim;
  1825. _material_set_param(shader_names->rim, p_rim);
  1826. }
  1827. float BaseMaterial3D::get_rim() const {
  1828. return rim;
  1829. }
  1830. void BaseMaterial3D::set_rim_tint(float p_rim_tint) {
  1831. rim_tint = p_rim_tint;
  1832. _material_set_param(shader_names->rim_tint, p_rim_tint);
  1833. }
  1834. float BaseMaterial3D::get_rim_tint() const {
  1835. return rim_tint;
  1836. }
  1837. void BaseMaterial3D::set_ao_light_affect(float p_ao_light_affect) {
  1838. ao_light_affect = p_ao_light_affect;
  1839. _material_set_param(shader_names->ao_light_affect, p_ao_light_affect);
  1840. }
  1841. float BaseMaterial3D::get_ao_light_affect() const {
  1842. return ao_light_affect;
  1843. }
  1844. void BaseMaterial3D::set_clearcoat(float p_clearcoat) {
  1845. clearcoat = p_clearcoat;
  1846. _material_set_param(shader_names->clearcoat, p_clearcoat);
  1847. }
  1848. float BaseMaterial3D::get_clearcoat() const {
  1849. return clearcoat;
  1850. }
  1851. void BaseMaterial3D::set_clearcoat_roughness(float p_clearcoat_roughness) {
  1852. clearcoat_roughness = p_clearcoat_roughness;
  1853. _material_set_param(shader_names->clearcoat_roughness, p_clearcoat_roughness);
  1854. }
  1855. float BaseMaterial3D::get_clearcoat_roughness() const {
  1856. return clearcoat_roughness;
  1857. }
  1858. void BaseMaterial3D::set_anisotropy(float p_anisotropy) {
  1859. anisotropy = p_anisotropy;
  1860. _material_set_param(shader_names->anisotropy, p_anisotropy);
  1861. }
  1862. float BaseMaterial3D::get_anisotropy() const {
  1863. return anisotropy;
  1864. }
  1865. void BaseMaterial3D::set_heightmap_scale(float p_heightmap_scale) {
  1866. heightmap_scale = p_heightmap_scale;
  1867. _material_set_param(shader_names->heightmap_scale, p_heightmap_scale);
  1868. }
  1869. float BaseMaterial3D::get_heightmap_scale() const {
  1870. return heightmap_scale;
  1871. }
  1872. void BaseMaterial3D::set_subsurface_scattering_strength(float p_subsurface_scattering_strength) {
  1873. subsurface_scattering_strength = p_subsurface_scattering_strength;
  1874. _material_set_param(shader_names->subsurface_scattering_strength, subsurface_scattering_strength);
  1875. }
  1876. float BaseMaterial3D::get_subsurface_scattering_strength() const {
  1877. return subsurface_scattering_strength;
  1878. }
  1879. void BaseMaterial3D::set_transmittance_color(const Color &p_color) {
  1880. transmittance_color = p_color;
  1881. _material_set_param(shader_names->transmittance_color, p_color);
  1882. }
  1883. Color BaseMaterial3D::get_transmittance_color() const {
  1884. return transmittance_color;
  1885. }
  1886. void BaseMaterial3D::set_transmittance_depth(float p_depth) {
  1887. transmittance_depth = p_depth;
  1888. _material_set_param(shader_names->transmittance_depth, p_depth);
  1889. }
  1890. float BaseMaterial3D::get_transmittance_depth() const {
  1891. return transmittance_depth;
  1892. }
  1893. void BaseMaterial3D::set_transmittance_boost(float p_boost) {
  1894. transmittance_boost = p_boost;
  1895. _material_set_param(shader_names->transmittance_boost, p_boost);
  1896. }
  1897. float BaseMaterial3D::get_transmittance_boost() const {
  1898. return transmittance_boost;
  1899. }
  1900. void BaseMaterial3D::set_backlight(const Color &p_backlight) {
  1901. backlight = p_backlight;
  1902. _material_set_param(shader_names->backlight, backlight);
  1903. }
  1904. Color BaseMaterial3D::get_backlight() const {
  1905. return backlight;
  1906. }
  1907. void BaseMaterial3D::set_refraction(float p_refraction) {
  1908. refraction = p_refraction;
  1909. _material_set_param(shader_names->refraction, refraction);
  1910. }
  1911. float BaseMaterial3D::get_refraction() const {
  1912. return refraction;
  1913. }
  1914. void BaseMaterial3D::set_detail_uv(DetailUV p_detail_uv) {
  1915. if (detail_uv == p_detail_uv) {
  1916. return;
  1917. }
  1918. detail_uv = p_detail_uv;
  1919. _mark_dirty();
  1920. }
  1921. BaseMaterial3D::DetailUV BaseMaterial3D::get_detail_uv() const {
  1922. return detail_uv;
  1923. }
  1924. void BaseMaterial3D::set_blend_mode(BlendMode p_mode) {
  1925. if (blend_mode == p_mode) {
  1926. return;
  1927. }
  1928. blend_mode = p_mode;
  1929. _mark_dirty();
  1930. }
  1931. BaseMaterial3D::BlendMode BaseMaterial3D::get_blend_mode() const {
  1932. return blend_mode;
  1933. }
  1934. void BaseMaterial3D::set_detail_blend_mode(BlendMode p_mode) {
  1935. detail_blend_mode = p_mode;
  1936. _mark_dirty();
  1937. }
  1938. BaseMaterial3D::BlendMode BaseMaterial3D::get_detail_blend_mode() const {
  1939. return detail_blend_mode;
  1940. }
  1941. void BaseMaterial3D::set_transparency(Transparency p_transparency) {
  1942. if (transparency == p_transparency) {
  1943. return;
  1944. }
  1945. transparency = p_transparency;
  1946. _mark_dirty();
  1947. notify_property_list_changed();
  1948. }
  1949. BaseMaterial3D::Transparency BaseMaterial3D::get_transparency() const {
  1950. return transparency;
  1951. }
  1952. void BaseMaterial3D::set_alpha_antialiasing(AlphaAntiAliasing p_alpha_aa) {
  1953. if (alpha_antialiasing_mode == p_alpha_aa) {
  1954. return;
  1955. }
  1956. alpha_antialiasing_mode = p_alpha_aa;
  1957. _mark_dirty();
  1958. notify_property_list_changed();
  1959. }
  1960. BaseMaterial3D::AlphaAntiAliasing BaseMaterial3D::get_alpha_antialiasing() const {
  1961. return alpha_antialiasing_mode;
  1962. }
  1963. void BaseMaterial3D::set_shading_mode(ShadingMode p_shading_mode) {
  1964. if (shading_mode == p_shading_mode) {
  1965. return;
  1966. }
  1967. shading_mode = p_shading_mode;
  1968. _mark_dirty();
  1969. notify_property_list_changed();
  1970. }
  1971. BaseMaterial3D::ShadingMode BaseMaterial3D::get_shading_mode() const {
  1972. return shading_mode;
  1973. }
  1974. void BaseMaterial3D::set_depth_draw_mode(DepthDrawMode p_mode) {
  1975. if (depth_draw_mode == p_mode) {
  1976. return;
  1977. }
  1978. depth_draw_mode = p_mode;
  1979. _mark_dirty();
  1980. }
  1981. BaseMaterial3D::DepthDrawMode BaseMaterial3D::get_depth_draw_mode() const {
  1982. return depth_draw_mode;
  1983. }
  1984. void BaseMaterial3D::set_cull_mode(CullMode p_mode) {
  1985. if (cull_mode == p_mode) {
  1986. return;
  1987. }
  1988. cull_mode = p_mode;
  1989. _mark_dirty();
  1990. }
  1991. BaseMaterial3D::CullMode BaseMaterial3D::get_cull_mode() const {
  1992. return cull_mode;
  1993. }
  1994. void BaseMaterial3D::set_diffuse_mode(DiffuseMode p_mode) {
  1995. if (diffuse_mode == p_mode) {
  1996. return;
  1997. }
  1998. diffuse_mode = p_mode;
  1999. _mark_dirty();
  2000. }
  2001. BaseMaterial3D::DiffuseMode BaseMaterial3D::get_diffuse_mode() const {
  2002. return diffuse_mode;
  2003. }
  2004. void BaseMaterial3D::set_specular_mode(SpecularMode p_mode) {
  2005. if (specular_mode == p_mode) {
  2006. return;
  2007. }
  2008. specular_mode = p_mode;
  2009. _mark_dirty();
  2010. }
  2011. BaseMaterial3D::SpecularMode BaseMaterial3D::get_specular_mode() const {
  2012. return specular_mode;
  2013. }
  2014. void BaseMaterial3D::set_flag(Flags p_flag, bool p_enabled) {
  2015. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  2016. if (flags[p_flag] == p_enabled) {
  2017. return;
  2018. }
  2019. flags[p_flag] = p_enabled;
  2020. if (
  2021. p_flag == FLAG_USE_SHADOW_TO_OPACITY ||
  2022. p_flag == FLAG_USE_TEXTURE_REPEAT ||
  2023. p_flag == FLAG_SUBSURFACE_MODE_SKIN ||
  2024. p_flag == FLAG_USE_POINT_SIZE ||
  2025. p_flag == FLAG_UV1_USE_TRIPLANAR ||
  2026. p_flag == FLAG_UV2_USE_TRIPLANAR) {
  2027. notify_property_list_changed();
  2028. }
  2029. if (p_flag == FLAG_PARTICLE_TRAILS_MODE) {
  2030. update_configuration_warning();
  2031. }
  2032. _mark_dirty();
  2033. }
  2034. bool BaseMaterial3D::get_flag(Flags p_flag) const {
  2035. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  2036. return flags[p_flag];
  2037. }
  2038. void BaseMaterial3D::set_feature(Feature p_feature, bool p_enabled) {
  2039. ERR_FAIL_INDEX(p_feature, FEATURE_MAX);
  2040. if (features[p_feature] == p_enabled) {
  2041. return;
  2042. }
  2043. features[p_feature] = p_enabled;
  2044. notify_property_list_changed();
  2045. _mark_dirty();
  2046. }
  2047. bool BaseMaterial3D::get_feature(Feature p_feature) const {
  2048. ERR_FAIL_INDEX_V(p_feature, FEATURE_MAX, false);
  2049. return features[p_feature];
  2050. }
  2051. void BaseMaterial3D::set_texture(TextureParam p_param, const Ref<Texture2D> &p_texture) {
  2052. ERR_FAIL_INDEX(p_param, TEXTURE_MAX);
  2053. textures[p_param] = p_texture;
  2054. Variant rid = p_texture.is_valid() ? Variant(p_texture->get_rid()) : Variant();
  2055. _material_set_param(shader_names->texture_names[p_param], rid);
  2056. if (p_texture.is_valid() && p_param == TEXTURE_ALBEDO) {
  2057. _material_set_param(shader_names->albedo_texture_size, Vector2i(p_texture->get_width(), p_texture->get_height()));
  2058. }
  2059. notify_property_list_changed();
  2060. _mark_dirty();
  2061. }
  2062. Ref<Texture2D> BaseMaterial3D::get_texture(TextureParam p_param) const {
  2063. ERR_FAIL_INDEX_V(p_param, TEXTURE_MAX, Ref<Texture2D>());
  2064. return textures[p_param];
  2065. }
  2066. Ref<Texture2D> BaseMaterial3D::get_texture_by_name(const StringName &p_name) const {
  2067. for (int i = 0; i < (int)BaseMaterial3D::TEXTURE_MAX; i++) {
  2068. TextureParam param = TextureParam(i);
  2069. if (p_name == shader_names->texture_names[param]) {
  2070. return textures[param];
  2071. }
  2072. }
  2073. return Ref<Texture2D>();
  2074. }
  2075. void BaseMaterial3D::set_texture_filter(TextureFilter p_filter) {
  2076. texture_filter = p_filter;
  2077. _mark_dirty();
  2078. }
  2079. BaseMaterial3D::TextureFilter BaseMaterial3D::get_texture_filter() const {
  2080. return texture_filter;
  2081. }
  2082. void BaseMaterial3D::_validate_feature(const String &text, Feature feature, PropertyInfo &property) const {
  2083. if (property.name.begins_with(text) && property.name != text + "_enabled" && !features[feature]) {
  2084. property.usage = PROPERTY_USAGE_NO_EDITOR;
  2085. }
  2086. }
  2087. void BaseMaterial3D::_validate_property(PropertyInfo &p_property) const {
  2088. _validate_feature("normal", FEATURE_NORMAL_MAPPING, p_property);
  2089. _validate_feature("emission", FEATURE_EMISSION, p_property);
  2090. _validate_feature("rim", FEATURE_RIM, p_property);
  2091. _validate_feature("clearcoat", FEATURE_CLEARCOAT, p_property);
  2092. _validate_feature("anisotropy", FEATURE_ANISOTROPY, p_property);
  2093. _validate_feature("ao", FEATURE_AMBIENT_OCCLUSION, p_property);
  2094. _validate_feature("heightmap", FEATURE_HEIGHT_MAPPING, p_property);
  2095. _validate_feature("subsurf_scatter", FEATURE_SUBSURFACE_SCATTERING, p_property);
  2096. _validate_feature("backlight", FEATURE_BACKLIGHT, p_property);
  2097. _validate_feature("refraction", FEATURE_REFRACTION, p_property);
  2098. _validate_feature("detail", FEATURE_DETAIL, p_property);
  2099. if (p_property.name == "emission_intensity" && !GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  2100. p_property.usage = PROPERTY_USAGE_NONE;
  2101. }
  2102. if (p_property.name.begins_with("particles_anim_") && billboard_mode != BILLBOARD_PARTICLES) {
  2103. p_property.usage = PROPERTY_USAGE_NONE;
  2104. }
  2105. if (p_property.name == "billboard_keep_scale" && billboard_mode == BILLBOARD_DISABLED) {
  2106. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  2107. }
  2108. if (p_property.name == "grow_amount" && !grow_enabled) {
  2109. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  2110. }
  2111. if (p_property.name == "point_size" && !flags[FLAG_USE_POINT_SIZE]) {
  2112. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  2113. }
  2114. if (p_property.name == "proximity_fade_distance" && !proximity_fade_enabled) {
  2115. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  2116. }
  2117. if (p_property.name == "msdf_pixel_range" && !flags[FLAG_ALBEDO_TEXTURE_MSDF]) {
  2118. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  2119. }
  2120. if (p_property.name == "msdf_outline_size" && !flags[FLAG_ALBEDO_TEXTURE_MSDF]) {
  2121. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  2122. }
  2123. if ((p_property.name == "distance_fade_max_distance" || p_property.name == "distance_fade_min_distance") && distance_fade == DISTANCE_FADE_DISABLED) {
  2124. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  2125. }
  2126. if ((p_property.name == "uv1_triplanar_sharpness" || p_property.name == "uv1_world_triplanar") && !flags[FLAG_UV1_USE_TRIPLANAR]) {
  2127. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  2128. }
  2129. if ((p_property.name == "uv2_triplanar_sharpness" || p_property.name == "uv2_world_triplanar") && !flags[FLAG_UV2_USE_TRIPLANAR]) {
  2130. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  2131. }
  2132. // you can only enable anti-aliasing (in materials) on alpha scissor and alpha hash
  2133. const bool can_select_aa = (transparency == TRANSPARENCY_ALPHA_SCISSOR || transparency == TRANSPARENCY_ALPHA_HASH);
  2134. // alpha anti aliasiasing is only enabled when you can select aa
  2135. const bool alpha_aa_enabled = (alpha_antialiasing_mode != ALPHA_ANTIALIASING_OFF) && can_select_aa;
  2136. // alpha scissor slider isn't needed when alpha antialiasing is enabled
  2137. if (p_property.name == "alpha_scissor_threshold" && transparency != TRANSPARENCY_ALPHA_SCISSOR) {
  2138. p_property.usage = PROPERTY_USAGE_NONE;
  2139. }
  2140. // alpha hash scale slider is only needed if transparency is alpha hash
  2141. if (p_property.name == "alpha_hash_scale" && transparency != TRANSPARENCY_ALPHA_HASH) {
  2142. p_property.usage = PROPERTY_USAGE_NONE;
  2143. }
  2144. if (p_property.name == "alpha_antialiasing_mode" && !can_select_aa) {
  2145. p_property.usage = PROPERTY_USAGE_NONE;
  2146. }
  2147. // we can't choose an antialiasing mode if alpha isn't possible
  2148. if (p_property.name == "alpha_antialiasing_edge" && !alpha_aa_enabled) {
  2149. p_property.usage = PROPERTY_USAGE_NONE;
  2150. }
  2151. if (p_property.name == "blend_mode" && alpha_aa_enabled) {
  2152. p_property.usage = PROPERTY_USAGE_NONE;
  2153. }
  2154. if ((p_property.name == "heightmap_min_layers" || p_property.name == "heightmap_max_layers") && !deep_parallax) {
  2155. p_property.usage = PROPERTY_USAGE_NONE;
  2156. }
  2157. if (flags[FLAG_SUBSURFACE_MODE_SKIN] && (p_property.name == "subsurf_scatter_transmittance_color" || p_property.name == "subsurf_scatter_transmittance_texture")) {
  2158. p_property.usage = PROPERTY_USAGE_NONE;
  2159. }
  2160. if (orm) {
  2161. if (p_property.name == "shading_mode") {
  2162. // Vertex not supported in ORM mode, since no individual roughness.
  2163. p_property.hint_string = "Unshaded,Per-Pixel";
  2164. }
  2165. if (p_property.name.begins_with("roughness") || p_property.name.begins_with("metallic") || p_property.name.begins_with("ao_texture")) {
  2166. p_property.usage = PROPERTY_USAGE_NONE;
  2167. }
  2168. } else {
  2169. if (p_property.name == "orm_texture") {
  2170. p_property.usage = PROPERTY_USAGE_NONE;
  2171. }
  2172. }
  2173. if (shading_mode != SHADING_MODE_PER_PIXEL) {
  2174. if (shading_mode != SHADING_MODE_PER_VERTEX) {
  2175. //these may still work per vertex
  2176. if (p_property.name.begins_with("ao")) {
  2177. p_property.usage = PROPERTY_USAGE_NONE;
  2178. }
  2179. if (p_property.name.begins_with("emission")) {
  2180. p_property.usage = PROPERTY_USAGE_NONE;
  2181. }
  2182. if (p_property.name.begins_with("metallic")) {
  2183. p_property.usage = PROPERTY_USAGE_NONE;
  2184. }
  2185. if (p_property.name.begins_with("rim")) {
  2186. p_property.usage = PROPERTY_USAGE_NONE;
  2187. }
  2188. if (p_property.name.begins_with("roughness")) {
  2189. p_property.usage = PROPERTY_USAGE_NONE;
  2190. }
  2191. if (p_property.name.begins_with("subsurf_scatter")) {
  2192. p_property.usage = PROPERTY_USAGE_NONE;
  2193. }
  2194. }
  2195. //these definitely only need per pixel
  2196. if (p_property.name.begins_with("anisotropy")) {
  2197. p_property.usage = PROPERTY_USAGE_NONE;
  2198. }
  2199. if (p_property.name.begins_with("clearcoat")) {
  2200. p_property.usage = PROPERTY_USAGE_NONE;
  2201. }
  2202. if (p_property.name.begins_with("normal")) {
  2203. p_property.usage = PROPERTY_USAGE_NONE;
  2204. }
  2205. if (p_property.name.begins_with("backlight")) {
  2206. p_property.usage = PROPERTY_USAGE_NONE;
  2207. }
  2208. if (p_property.name.begins_with("transmittance")) {
  2209. p_property.usage = PROPERTY_USAGE_NONE;
  2210. }
  2211. }
  2212. }
  2213. void BaseMaterial3D::set_point_size(float p_point_size) {
  2214. point_size = p_point_size;
  2215. _material_set_param(shader_names->point_size, p_point_size);
  2216. }
  2217. float BaseMaterial3D::get_point_size() const {
  2218. return point_size;
  2219. }
  2220. void BaseMaterial3D::set_uv1_scale(const Vector3 &p_scale) {
  2221. uv1_scale = p_scale;
  2222. _material_set_param(shader_names->uv1_scale, p_scale);
  2223. }
  2224. Vector3 BaseMaterial3D::get_uv1_scale() const {
  2225. return uv1_scale;
  2226. }
  2227. void BaseMaterial3D::set_uv1_offset(const Vector3 &p_offset) {
  2228. uv1_offset = p_offset;
  2229. _material_set_param(shader_names->uv1_offset, p_offset);
  2230. }
  2231. Vector3 BaseMaterial3D::get_uv1_offset() const {
  2232. return uv1_offset;
  2233. }
  2234. void BaseMaterial3D::set_uv1_triplanar_blend_sharpness(float p_sharpness) {
  2235. // Negative values or values higher than 150 can result in NaNs, leading to broken rendering.
  2236. uv1_triplanar_sharpness = CLAMP(p_sharpness, 0.0, 150.0);
  2237. _material_set_param(shader_names->uv1_blend_sharpness, uv1_triplanar_sharpness);
  2238. }
  2239. float BaseMaterial3D::get_uv1_triplanar_blend_sharpness() const {
  2240. return uv1_triplanar_sharpness;
  2241. }
  2242. void BaseMaterial3D::set_uv2_scale(const Vector3 &p_scale) {
  2243. uv2_scale = p_scale;
  2244. _material_set_param(shader_names->uv2_scale, p_scale);
  2245. }
  2246. Vector3 BaseMaterial3D::get_uv2_scale() const {
  2247. return uv2_scale;
  2248. }
  2249. void BaseMaterial3D::set_uv2_offset(const Vector3 &p_offset) {
  2250. uv2_offset = p_offset;
  2251. _material_set_param(shader_names->uv2_offset, p_offset);
  2252. }
  2253. Vector3 BaseMaterial3D::get_uv2_offset() const {
  2254. return uv2_offset;
  2255. }
  2256. void BaseMaterial3D::set_uv2_triplanar_blend_sharpness(float p_sharpness) {
  2257. // Negative values or values higher than 150 can result in NaNs, leading to broken rendering.
  2258. uv2_triplanar_sharpness = CLAMP(p_sharpness, 0.0, 150.0);
  2259. _material_set_param(shader_names->uv2_blend_sharpness, uv2_triplanar_sharpness);
  2260. }
  2261. float BaseMaterial3D::get_uv2_triplanar_blend_sharpness() const {
  2262. return uv2_triplanar_sharpness;
  2263. }
  2264. void BaseMaterial3D::set_billboard_mode(BillboardMode p_mode) {
  2265. billboard_mode = p_mode;
  2266. _mark_dirty();
  2267. notify_property_list_changed();
  2268. }
  2269. BaseMaterial3D::BillboardMode BaseMaterial3D::get_billboard_mode() const {
  2270. return billboard_mode;
  2271. }
  2272. void BaseMaterial3D::set_particles_anim_h_frames(int p_frames) {
  2273. particles_anim_h_frames = p_frames;
  2274. _material_set_param(shader_names->particles_anim_h_frames, p_frames);
  2275. }
  2276. int BaseMaterial3D::get_particles_anim_h_frames() const {
  2277. return particles_anim_h_frames;
  2278. }
  2279. void BaseMaterial3D::set_particles_anim_v_frames(int p_frames) {
  2280. particles_anim_v_frames = p_frames;
  2281. _material_set_param(shader_names->particles_anim_v_frames, p_frames);
  2282. }
  2283. int BaseMaterial3D::get_particles_anim_v_frames() const {
  2284. return particles_anim_v_frames;
  2285. }
  2286. void BaseMaterial3D::set_particles_anim_loop(bool p_loop) {
  2287. particles_anim_loop = p_loop;
  2288. _material_set_param(shader_names->particles_anim_loop, particles_anim_loop);
  2289. }
  2290. bool BaseMaterial3D::get_particles_anim_loop() const {
  2291. return particles_anim_loop;
  2292. }
  2293. void BaseMaterial3D::set_heightmap_deep_parallax(bool p_enable) {
  2294. deep_parallax = p_enable;
  2295. _mark_dirty();
  2296. notify_property_list_changed();
  2297. }
  2298. bool BaseMaterial3D::is_heightmap_deep_parallax_enabled() const {
  2299. return deep_parallax;
  2300. }
  2301. void BaseMaterial3D::set_heightmap_deep_parallax_min_layers(int p_layer) {
  2302. deep_parallax_min_layers = p_layer;
  2303. _material_set_param(shader_names->heightmap_min_layers, p_layer);
  2304. }
  2305. int BaseMaterial3D::get_heightmap_deep_parallax_min_layers() const {
  2306. return deep_parallax_min_layers;
  2307. }
  2308. void BaseMaterial3D::set_heightmap_deep_parallax_max_layers(int p_layer) {
  2309. deep_parallax_max_layers = p_layer;
  2310. _material_set_param(shader_names->heightmap_max_layers, p_layer);
  2311. }
  2312. int BaseMaterial3D::get_heightmap_deep_parallax_max_layers() const {
  2313. return deep_parallax_max_layers;
  2314. }
  2315. void BaseMaterial3D::set_heightmap_deep_parallax_flip_tangent(bool p_flip) {
  2316. heightmap_parallax_flip_tangent = p_flip;
  2317. _material_set_param(shader_names->heightmap_flip, Vector2(heightmap_parallax_flip_tangent ? -1 : 1, heightmap_parallax_flip_binormal ? -1 : 1));
  2318. }
  2319. bool BaseMaterial3D::get_heightmap_deep_parallax_flip_tangent() const {
  2320. return heightmap_parallax_flip_tangent;
  2321. }
  2322. void BaseMaterial3D::set_heightmap_deep_parallax_flip_binormal(bool p_flip) {
  2323. heightmap_parallax_flip_binormal = p_flip;
  2324. _material_set_param(shader_names->heightmap_flip, Vector2(heightmap_parallax_flip_tangent ? -1 : 1, heightmap_parallax_flip_binormal ? -1 : 1));
  2325. }
  2326. bool BaseMaterial3D::get_heightmap_deep_parallax_flip_binormal() const {
  2327. return heightmap_parallax_flip_binormal;
  2328. }
  2329. void BaseMaterial3D::set_grow_enabled(bool p_enable) {
  2330. grow_enabled = p_enable;
  2331. _mark_dirty();
  2332. notify_property_list_changed();
  2333. }
  2334. bool BaseMaterial3D::is_grow_enabled() const {
  2335. return grow_enabled;
  2336. }
  2337. void BaseMaterial3D::set_alpha_scissor_threshold(float p_threshold) {
  2338. alpha_scissor_threshold = p_threshold;
  2339. _material_set_param(shader_names->alpha_scissor_threshold, p_threshold);
  2340. }
  2341. float BaseMaterial3D::get_alpha_scissor_threshold() const {
  2342. return alpha_scissor_threshold;
  2343. }
  2344. void BaseMaterial3D::set_alpha_hash_scale(float p_scale) {
  2345. alpha_hash_scale = p_scale;
  2346. _material_set_param(shader_names->alpha_hash_scale, p_scale);
  2347. }
  2348. float BaseMaterial3D::get_alpha_hash_scale() const {
  2349. return alpha_hash_scale;
  2350. }
  2351. void BaseMaterial3D::set_alpha_antialiasing_edge(float p_edge) {
  2352. alpha_antialiasing_edge = p_edge;
  2353. _material_set_param(shader_names->alpha_antialiasing_edge, p_edge);
  2354. }
  2355. float BaseMaterial3D::get_alpha_antialiasing_edge() const {
  2356. return alpha_antialiasing_edge;
  2357. }
  2358. void BaseMaterial3D::set_grow(float p_grow) {
  2359. grow = p_grow;
  2360. _material_set_param(shader_names->grow, p_grow);
  2361. }
  2362. float BaseMaterial3D::get_grow() const {
  2363. return grow;
  2364. }
  2365. static Vector4 _get_texture_mask(BaseMaterial3D::TextureChannel p_channel) {
  2366. static const Vector4 masks[5] = {
  2367. Vector4(1, 0, 0, 0),
  2368. Vector4(0, 1, 0, 0),
  2369. Vector4(0, 0, 1, 0),
  2370. Vector4(0, 0, 0, 1),
  2371. Vector4(0.3333333, 0.3333333, 0.3333333, 0),
  2372. };
  2373. return masks[p_channel];
  2374. }
  2375. void BaseMaterial3D::set_metallic_texture_channel(TextureChannel p_channel) {
  2376. ERR_FAIL_INDEX(p_channel, 5);
  2377. metallic_texture_channel = p_channel;
  2378. _material_set_param(shader_names->metallic_texture_channel, _get_texture_mask(p_channel));
  2379. }
  2380. BaseMaterial3D::TextureChannel BaseMaterial3D::get_metallic_texture_channel() const {
  2381. return metallic_texture_channel;
  2382. }
  2383. void BaseMaterial3D::set_roughness_texture_channel(TextureChannel p_channel) {
  2384. ERR_FAIL_INDEX(p_channel, 5);
  2385. roughness_texture_channel = p_channel;
  2386. _mark_dirty();
  2387. }
  2388. BaseMaterial3D::TextureChannel BaseMaterial3D::get_roughness_texture_channel() const {
  2389. return roughness_texture_channel;
  2390. }
  2391. void BaseMaterial3D::set_ao_texture_channel(TextureChannel p_channel) {
  2392. ERR_FAIL_INDEX(p_channel, 5);
  2393. ao_texture_channel = p_channel;
  2394. _material_set_param(shader_names->ao_texture_channel, _get_texture_mask(p_channel));
  2395. }
  2396. BaseMaterial3D::TextureChannel BaseMaterial3D::get_ao_texture_channel() const {
  2397. return ao_texture_channel;
  2398. }
  2399. void BaseMaterial3D::set_refraction_texture_channel(TextureChannel p_channel) {
  2400. ERR_FAIL_INDEX(p_channel, 5);
  2401. refraction_texture_channel = p_channel;
  2402. _material_set_param(shader_names->refraction_texture_channel, _get_texture_mask(p_channel));
  2403. }
  2404. BaseMaterial3D::TextureChannel BaseMaterial3D::get_refraction_texture_channel() const {
  2405. return refraction_texture_channel;
  2406. }
  2407. Ref<Material> BaseMaterial3D::get_material_for_2d(bool p_shaded, Transparency p_transparency, bool p_double_sided, bool p_billboard, bool p_billboard_y, bool p_msdf, bool p_no_depth, bool p_fixed_size, TextureFilter p_filter, AlphaAntiAliasing p_alpha_antialiasing_mode, RID *r_shader_rid) {
  2408. uint64_t key = 0;
  2409. key |= ((int8_t)p_shaded & 0x01) << 0;
  2410. key |= ((int8_t)p_transparency & 0x07) << 1; // Bits 1-3.
  2411. key |= ((int8_t)p_double_sided & 0x01) << 4;
  2412. key |= ((int8_t)p_billboard & 0x01) << 5;
  2413. key |= ((int8_t)p_billboard_y & 0x01) << 6;
  2414. key |= ((int8_t)p_msdf & 0x01) << 7;
  2415. key |= ((int8_t)p_no_depth & 0x01) << 8;
  2416. key |= ((int8_t)p_fixed_size & 0x01) << 9;
  2417. key |= ((int8_t)p_filter & 0x07) << 10; // Bits 10-12.
  2418. key |= ((int8_t)p_alpha_antialiasing_mode & 0x07) << 13; // Bits 13-15.
  2419. if (materials_for_2d.has(key)) {
  2420. if (r_shader_rid) {
  2421. *r_shader_rid = materials_for_2d[key]->get_shader_rid();
  2422. }
  2423. return materials_for_2d[key];
  2424. }
  2425. Ref<StandardMaterial3D> material;
  2426. material.instantiate();
  2427. material->set_shading_mode(p_shaded ? SHADING_MODE_PER_PIXEL : SHADING_MODE_UNSHADED);
  2428. material->set_transparency(p_transparency);
  2429. material->set_cull_mode(p_double_sided ? CULL_DISABLED : CULL_BACK);
  2430. material->set_flag(FLAG_SRGB_VERTEX_COLOR, true);
  2431. material->set_flag(FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  2432. material->set_flag(FLAG_ALBEDO_TEXTURE_MSDF, p_msdf);
  2433. material->set_flag(FLAG_DISABLE_DEPTH_TEST, p_no_depth);
  2434. material->set_flag(FLAG_FIXED_SIZE, p_fixed_size);
  2435. material->set_alpha_antialiasing(p_alpha_antialiasing_mode);
  2436. material->set_texture_filter(p_filter);
  2437. if (p_billboard || p_billboard_y) {
  2438. material->set_flag(FLAG_BILLBOARD_KEEP_SCALE, true);
  2439. material->set_billboard_mode(p_billboard_y ? BILLBOARD_FIXED_Y : BILLBOARD_ENABLED);
  2440. }
  2441. materials_for_2d[key] = material;
  2442. if (r_shader_rid) {
  2443. *r_shader_rid = materials_for_2d[key]->get_shader_rid();
  2444. }
  2445. return materials_for_2d[key];
  2446. }
  2447. void BaseMaterial3D::set_on_top_of_alpha() {
  2448. set_transparency(TRANSPARENCY_DISABLED);
  2449. set_render_priority(RENDER_PRIORITY_MAX);
  2450. set_flag(FLAG_DISABLE_DEPTH_TEST, true);
  2451. }
  2452. void BaseMaterial3D::set_proximity_fade_enabled(bool p_enable) {
  2453. proximity_fade_enabled = p_enable;
  2454. _mark_dirty();
  2455. notify_property_list_changed();
  2456. }
  2457. bool BaseMaterial3D::is_proximity_fade_enabled() const {
  2458. return proximity_fade_enabled;
  2459. }
  2460. void BaseMaterial3D::set_proximity_fade_distance(float p_distance) {
  2461. proximity_fade_distance = MAX(p_distance, 0.01);
  2462. _material_set_param(shader_names->proximity_fade_distance, proximity_fade_distance);
  2463. }
  2464. float BaseMaterial3D::get_proximity_fade_distance() const {
  2465. return proximity_fade_distance;
  2466. }
  2467. void BaseMaterial3D::set_msdf_pixel_range(float p_range) {
  2468. msdf_pixel_range = p_range;
  2469. _material_set_param(shader_names->msdf_pixel_range, p_range);
  2470. }
  2471. float BaseMaterial3D::get_msdf_pixel_range() const {
  2472. return msdf_pixel_range;
  2473. }
  2474. void BaseMaterial3D::set_msdf_outline_size(float p_size) {
  2475. msdf_outline_size = p_size;
  2476. _material_set_param(shader_names->msdf_outline_size, p_size);
  2477. }
  2478. float BaseMaterial3D::get_msdf_outline_size() const {
  2479. return msdf_outline_size;
  2480. }
  2481. void BaseMaterial3D::set_distance_fade(DistanceFadeMode p_mode) {
  2482. distance_fade = p_mode;
  2483. _mark_dirty();
  2484. notify_property_list_changed();
  2485. }
  2486. BaseMaterial3D::DistanceFadeMode BaseMaterial3D::get_distance_fade() const {
  2487. return distance_fade;
  2488. }
  2489. void BaseMaterial3D::set_distance_fade_max_distance(float p_distance) {
  2490. distance_fade_max_distance = p_distance;
  2491. _material_set_param(shader_names->distance_fade_max, distance_fade_max_distance);
  2492. }
  2493. float BaseMaterial3D::get_distance_fade_max_distance() const {
  2494. return distance_fade_max_distance;
  2495. }
  2496. void BaseMaterial3D::set_distance_fade_min_distance(float p_distance) {
  2497. distance_fade_min_distance = p_distance;
  2498. _material_set_param(shader_names->distance_fade_min, distance_fade_min_distance);
  2499. }
  2500. float BaseMaterial3D::get_distance_fade_min_distance() const {
  2501. return distance_fade_min_distance;
  2502. }
  2503. void BaseMaterial3D::set_emission_operator(EmissionOperator p_op) {
  2504. if (emission_op == p_op) {
  2505. return;
  2506. }
  2507. emission_op = p_op;
  2508. _mark_dirty();
  2509. }
  2510. BaseMaterial3D::EmissionOperator BaseMaterial3D::get_emission_operator() const {
  2511. return emission_op;
  2512. }
  2513. RID BaseMaterial3D::get_rid() const {
  2514. const_cast<BaseMaterial3D *>(this)->_update_shader();
  2515. const_cast<BaseMaterial3D *>(this)->_check_material_rid();
  2516. return _get_material();
  2517. }
  2518. RID BaseMaterial3D::get_shader_rid() const {
  2519. const_cast<BaseMaterial3D *>(this)->_update_shader();
  2520. return shader_rid;
  2521. }
  2522. Shader::Mode BaseMaterial3D::get_shader_mode() const {
  2523. return Shader::MODE_SPATIAL;
  2524. }
  2525. void BaseMaterial3D::_bind_methods() {
  2526. static_assert(sizeof(MaterialKey) == 16, "MaterialKey should be 16 bytes");
  2527. ClassDB::bind_method(D_METHOD("set_albedo", "albedo"), &BaseMaterial3D::set_albedo);
  2528. ClassDB::bind_method(D_METHOD("get_albedo"), &BaseMaterial3D::get_albedo);
  2529. ClassDB::bind_method(D_METHOD("set_transparency", "transparency"), &BaseMaterial3D::set_transparency);
  2530. ClassDB::bind_method(D_METHOD("get_transparency"), &BaseMaterial3D::get_transparency);
  2531. ClassDB::bind_method(D_METHOD("set_alpha_antialiasing", "alpha_aa"), &BaseMaterial3D::set_alpha_antialiasing);
  2532. ClassDB::bind_method(D_METHOD("get_alpha_antialiasing"), &BaseMaterial3D::get_alpha_antialiasing);
  2533. ClassDB::bind_method(D_METHOD("set_alpha_antialiasing_edge", "edge"), &BaseMaterial3D::set_alpha_antialiasing_edge);
  2534. ClassDB::bind_method(D_METHOD("get_alpha_antialiasing_edge"), &BaseMaterial3D::get_alpha_antialiasing_edge);
  2535. ClassDB::bind_method(D_METHOD("set_shading_mode", "shading_mode"), &BaseMaterial3D::set_shading_mode);
  2536. ClassDB::bind_method(D_METHOD("get_shading_mode"), &BaseMaterial3D::get_shading_mode);
  2537. ClassDB::bind_method(D_METHOD("set_specular", "specular"), &BaseMaterial3D::set_specular);
  2538. ClassDB::bind_method(D_METHOD("get_specular"), &BaseMaterial3D::get_specular);
  2539. ClassDB::bind_method(D_METHOD("set_metallic", "metallic"), &BaseMaterial3D::set_metallic);
  2540. ClassDB::bind_method(D_METHOD("get_metallic"), &BaseMaterial3D::get_metallic);
  2541. ClassDB::bind_method(D_METHOD("set_roughness", "roughness"), &BaseMaterial3D::set_roughness);
  2542. ClassDB::bind_method(D_METHOD("get_roughness"), &BaseMaterial3D::get_roughness);
  2543. ClassDB::bind_method(D_METHOD("set_emission", "emission"), &BaseMaterial3D::set_emission);
  2544. ClassDB::bind_method(D_METHOD("get_emission"), &BaseMaterial3D::get_emission);
  2545. ClassDB::bind_method(D_METHOD("set_emission_energy_multiplier", "emission_energy_multiplier"), &BaseMaterial3D::set_emission_energy_multiplier);
  2546. ClassDB::bind_method(D_METHOD("get_emission_energy_multiplier"), &BaseMaterial3D::get_emission_energy_multiplier);
  2547. ClassDB::bind_method(D_METHOD("set_emission_intensity", "emission_energy_multiplier"), &BaseMaterial3D::set_emission_intensity);
  2548. ClassDB::bind_method(D_METHOD("get_emission_intensity"), &BaseMaterial3D::get_emission_intensity);
  2549. ClassDB::bind_method(D_METHOD("set_normal_scale", "normal_scale"), &BaseMaterial3D::set_normal_scale);
  2550. ClassDB::bind_method(D_METHOD("get_normal_scale"), &BaseMaterial3D::get_normal_scale);
  2551. ClassDB::bind_method(D_METHOD("set_rim", "rim"), &BaseMaterial3D::set_rim);
  2552. ClassDB::bind_method(D_METHOD("get_rim"), &BaseMaterial3D::get_rim);
  2553. ClassDB::bind_method(D_METHOD("set_rim_tint", "rim_tint"), &BaseMaterial3D::set_rim_tint);
  2554. ClassDB::bind_method(D_METHOD("get_rim_tint"), &BaseMaterial3D::get_rim_tint);
  2555. ClassDB::bind_method(D_METHOD("set_clearcoat", "clearcoat"), &BaseMaterial3D::set_clearcoat);
  2556. ClassDB::bind_method(D_METHOD("get_clearcoat"), &BaseMaterial3D::get_clearcoat);
  2557. ClassDB::bind_method(D_METHOD("set_clearcoat_roughness", "clearcoat_roughness"), &BaseMaterial3D::set_clearcoat_roughness);
  2558. ClassDB::bind_method(D_METHOD("get_clearcoat_roughness"), &BaseMaterial3D::get_clearcoat_roughness);
  2559. ClassDB::bind_method(D_METHOD("set_anisotropy", "anisotropy"), &BaseMaterial3D::set_anisotropy);
  2560. ClassDB::bind_method(D_METHOD("get_anisotropy"), &BaseMaterial3D::get_anisotropy);
  2561. ClassDB::bind_method(D_METHOD("set_heightmap_scale", "heightmap_scale"), &BaseMaterial3D::set_heightmap_scale);
  2562. ClassDB::bind_method(D_METHOD("get_heightmap_scale"), &BaseMaterial3D::get_heightmap_scale);
  2563. ClassDB::bind_method(D_METHOD("set_subsurface_scattering_strength", "strength"), &BaseMaterial3D::set_subsurface_scattering_strength);
  2564. ClassDB::bind_method(D_METHOD("get_subsurface_scattering_strength"), &BaseMaterial3D::get_subsurface_scattering_strength);
  2565. ClassDB::bind_method(D_METHOD("set_transmittance_color", "color"), &BaseMaterial3D::set_transmittance_color);
  2566. ClassDB::bind_method(D_METHOD("get_transmittance_color"), &BaseMaterial3D::get_transmittance_color);
  2567. ClassDB::bind_method(D_METHOD("set_transmittance_depth", "depth"), &BaseMaterial3D::set_transmittance_depth);
  2568. ClassDB::bind_method(D_METHOD("get_transmittance_depth"), &BaseMaterial3D::get_transmittance_depth);
  2569. ClassDB::bind_method(D_METHOD("set_transmittance_boost", "boost"), &BaseMaterial3D::set_transmittance_boost);
  2570. ClassDB::bind_method(D_METHOD("get_transmittance_boost"), &BaseMaterial3D::get_transmittance_boost);
  2571. ClassDB::bind_method(D_METHOD("set_backlight", "backlight"), &BaseMaterial3D::set_backlight);
  2572. ClassDB::bind_method(D_METHOD("get_backlight"), &BaseMaterial3D::get_backlight);
  2573. ClassDB::bind_method(D_METHOD("set_refraction", "refraction"), &BaseMaterial3D::set_refraction);
  2574. ClassDB::bind_method(D_METHOD("get_refraction"), &BaseMaterial3D::get_refraction);
  2575. ClassDB::bind_method(D_METHOD("set_point_size", "point_size"), &BaseMaterial3D::set_point_size);
  2576. ClassDB::bind_method(D_METHOD("get_point_size"), &BaseMaterial3D::get_point_size);
  2577. ClassDB::bind_method(D_METHOD("set_detail_uv", "detail_uv"), &BaseMaterial3D::set_detail_uv);
  2578. ClassDB::bind_method(D_METHOD("get_detail_uv"), &BaseMaterial3D::get_detail_uv);
  2579. ClassDB::bind_method(D_METHOD("set_blend_mode", "blend_mode"), &BaseMaterial3D::set_blend_mode);
  2580. ClassDB::bind_method(D_METHOD("get_blend_mode"), &BaseMaterial3D::get_blend_mode);
  2581. ClassDB::bind_method(D_METHOD("set_depth_draw_mode", "depth_draw_mode"), &BaseMaterial3D::set_depth_draw_mode);
  2582. ClassDB::bind_method(D_METHOD("get_depth_draw_mode"), &BaseMaterial3D::get_depth_draw_mode);
  2583. ClassDB::bind_method(D_METHOD("set_cull_mode", "cull_mode"), &BaseMaterial3D::set_cull_mode);
  2584. ClassDB::bind_method(D_METHOD("get_cull_mode"), &BaseMaterial3D::get_cull_mode);
  2585. ClassDB::bind_method(D_METHOD("set_diffuse_mode", "diffuse_mode"), &BaseMaterial3D::set_diffuse_mode);
  2586. ClassDB::bind_method(D_METHOD("get_diffuse_mode"), &BaseMaterial3D::get_diffuse_mode);
  2587. ClassDB::bind_method(D_METHOD("set_specular_mode", "specular_mode"), &BaseMaterial3D::set_specular_mode);
  2588. ClassDB::bind_method(D_METHOD("get_specular_mode"), &BaseMaterial3D::get_specular_mode);
  2589. ClassDB::bind_method(D_METHOD("set_flag", "flag", "enable"), &BaseMaterial3D::set_flag);
  2590. ClassDB::bind_method(D_METHOD("get_flag", "flag"), &BaseMaterial3D::get_flag);
  2591. ClassDB::bind_method(D_METHOD("set_texture_filter", "mode"), &BaseMaterial3D::set_texture_filter);
  2592. ClassDB::bind_method(D_METHOD("get_texture_filter"), &BaseMaterial3D::get_texture_filter);
  2593. ClassDB::bind_method(D_METHOD("set_feature", "feature", "enable"), &BaseMaterial3D::set_feature);
  2594. ClassDB::bind_method(D_METHOD("get_feature", "feature"), &BaseMaterial3D::get_feature);
  2595. ClassDB::bind_method(D_METHOD("set_texture", "param", "texture"), &BaseMaterial3D::set_texture);
  2596. ClassDB::bind_method(D_METHOD("get_texture", "param"), &BaseMaterial3D::get_texture);
  2597. ClassDB::bind_method(D_METHOD("set_detail_blend_mode", "detail_blend_mode"), &BaseMaterial3D::set_detail_blend_mode);
  2598. ClassDB::bind_method(D_METHOD("get_detail_blend_mode"), &BaseMaterial3D::get_detail_blend_mode);
  2599. ClassDB::bind_method(D_METHOD("set_uv1_scale", "scale"), &BaseMaterial3D::set_uv1_scale);
  2600. ClassDB::bind_method(D_METHOD("get_uv1_scale"), &BaseMaterial3D::get_uv1_scale);
  2601. ClassDB::bind_method(D_METHOD("set_uv1_offset", "offset"), &BaseMaterial3D::set_uv1_offset);
  2602. ClassDB::bind_method(D_METHOD("get_uv1_offset"), &BaseMaterial3D::get_uv1_offset);
  2603. ClassDB::bind_method(D_METHOD("set_uv1_triplanar_blend_sharpness", "sharpness"), &BaseMaterial3D::set_uv1_triplanar_blend_sharpness);
  2604. ClassDB::bind_method(D_METHOD("get_uv1_triplanar_blend_sharpness"), &BaseMaterial3D::get_uv1_triplanar_blend_sharpness);
  2605. ClassDB::bind_method(D_METHOD("set_uv2_scale", "scale"), &BaseMaterial3D::set_uv2_scale);
  2606. ClassDB::bind_method(D_METHOD("get_uv2_scale"), &BaseMaterial3D::get_uv2_scale);
  2607. ClassDB::bind_method(D_METHOD("set_uv2_offset", "offset"), &BaseMaterial3D::set_uv2_offset);
  2608. ClassDB::bind_method(D_METHOD("get_uv2_offset"), &BaseMaterial3D::get_uv2_offset);
  2609. ClassDB::bind_method(D_METHOD("set_uv2_triplanar_blend_sharpness", "sharpness"), &BaseMaterial3D::set_uv2_triplanar_blend_sharpness);
  2610. ClassDB::bind_method(D_METHOD("get_uv2_triplanar_blend_sharpness"), &BaseMaterial3D::get_uv2_triplanar_blend_sharpness);
  2611. ClassDB::bind_method(D_METHOD("set_billboard_mode", "mode"), &BaseMaterial3D::set_billboard_mode);
  2612. ClassDB::bind_method(D_METHOD("get_billboard_mode"), &BaseMaterial3D::get_billboard_mode);
  2613. ClassDB::bind_method(D_METHOD("set_particles_anim_h_frames", "frames"), &BaseMaterial3D::set_particles_anim_h_frames);
  2614. ClassDB::bind_method(D_METHOD("get_particles_anim_h_frames"), &BaseMaterial3D::get_particles_anim_h_frames);
  2615. ClassDB::bind_method(D_METHOD("set_particles_anim_v_frames", "frames"), &BaseMaterial3D::set_particles_anim_v_frames);
  2616. ClassDB::bind_method(D_METHOD("get_particles_anim_v_frames"), &BaseMaterial3D::get_particles_anim_v_frames);
  2617. ClassDB::bind_method(D_METHOD("set_particles_anim_loop", "loop"), &BaseMaterial3D::set_particles_anim_loop);
  2618. ClassDB::bind_method(D_METHOD("get_particles_anim_loop"), &BaseMaterial3D::get_particles_anim_loop);
  2619. ClassDB::bind_method(D_METHOD("set_heightmap_deep_parallax", "enable"), &BaseMaterial3D::set_heightmap_deep_parallax);
  2620. ClassDB::bind_method(D_METHOD("is_heightmap_deep_parallax_enabled"), &BaseMaterial3D::is_heightmap_deep_parallax_enabled);
  2621. ClassDB::bind_method(D_METHOD("set_heightmap_deep_parallax_min_layers", "layer"), &BaseMaterial3D::set_heightmap_deep_parallax_min_layers);
  2622. ClassDB::bind_method(D_METHOD("get_heightmap_deep_parallax_min_layers"), &BaseMaterial3D::get_heightmap_deep_parallax_min_layers);
  2623. ClassDB::bind_method(D_METHOD("set_heightmap_deep_parallax_max_layers", "layer"), &BaseMaterial3D::set_heightmap_deep_parallax_max_layers);
  2624. ClassDB::bind_method(D_METHOD("get_heightmap_deep_parallax_max_layers"), &BaseMaterial3D::get_heightmap_deep_parallax_max_layers);
  2625. ClassDB::bind_method(D_METHOD("set_heightmap_deep_parallax_flip_tangent", "flip"), &BaseMaterial3D::set_heightmap_deep_parallax_flip_tangent);
  2626. ClassDB::bind_method(D_METHOD("get_heightmap_deep_parallax_flip_tangent"), &BaseMaterial3D::get_heightmap_deep_parallax_flip_tangent);
  2627. ClassDB::bind_method(D_METHOD("set_heightmap_deep_parallax_flip_binormal", "flip"), &BaseMaterial3D::set_heightmap_deep_parallax_flip_binormal);
  2628. ClassDB::bind_method(D_METHOD("get_heightmap_deep_parallax_flip_binormal"), &BaseMaterial3D::get_heightmap_deep_parallax_flip_binormal);
  2629. ClassDB::bind_method(D_METHOD("set_grow", "amount"), &BaseMaterial3D::set_grow);
  2630. ClassDB::bind_method(D_METHOD("get_grow"), &BaseMaterial3D::get_grow);
  2631. ClassDB::bind_method(D_METHOD("set_emission_operator", "operator"), &BaseMaterial3D::set_emission_operator);
  2632. ClassDB::bind_method(D_METHOD("get_emission_operator"), &BaseMaterial3D::get_emission_operator);
  2633. ClassDB::bind_method(D_METHOD("set_ao_light_affect", "amount"), &BaseMaterial3D::set_ao_light_affect);
  2634. ClassDB::bind_method(D_METHOD("get_ao_light_affect"), &BaseMaterial3D::get_ao_light_affect);
  2635. ClassDB::bind_method(D_METHOD("set_alpha_scissor_threshold", "threshold"), &BaseMaterial3D::set_alpha_scissor_threshold);
  2636. ClassDB::bind_method(D_METHOD("get_alpha_scissor_threshold"), &BaseMaterial3D::get_alpha_scissor_threshold);
  2637. ClassDB::bind_method(D_METHOD("set_alpha_hash_scale", "threshold"), &BaseMaterial3D::set_alpha_hash_scale);
  2638. ClassDB::bind_method(D_METHOD("get_alpha_hash_scale"), &BaseMaterial3D::get_alpha_hash_scale);
  2639. ClassDB::bind_method(D_METHOD("set_grow_enabled", "enable"), &BaseMaterial3D::set_grow_enabled);
  2640. ClassDB::bind_method(D_METHOD("is_grow_enabled"), &BaseMaterial3D::is_grow_enabled);
  2641. ClassDB::bind_method(D_METHOD("set_metallic_texture_channel", "channel"), &BaseMaterial3D::set_metallic_texture_channel);
  2642. ClassDB::bind_method(D_METHOD("get_metallic_texture_channel"), &BaseMaterial3D::get_metallic_texture_channel);
  2643. ClassDB::bind_method(D_METHOD("set_roughness_texture_channel", "channel"), &BaseMaterial3D::set_roughness_texture_channel);
  2644. ClassDB::bind_method(D_METHOD("get_roughness_texture_channel"), &BaseMaterial3D::get_roughness_texture_channel);
  2645. ClassDB::bind_method(D_METHOD("set_ao_texture_channel", "channel"), &BaseMaterial3D::set_ao_texture_channel);
  2646. ClassDB::bind_method(D_METHOD("get_ao_texture_channel"), &BaseMaterial3D::get_ao_texture_channel);
  2647. ClassDB::bind_method(D_METHOD("set_refraction_texture_channel", "channel"), &BaseMaterial3D::set_refraction_texture_channel);
  2648. ClassDB::bind_method(D_METHOD("get_refraction_texture_channel"), &BaseMaterial3D::get_refraction_texture_channel);
  2649. ClassDB::bind_method(D_METHOD("set_proximity_fade_enabled", "enabled"), &BaseMaterial3D::set_proximity_fade_enabled);
  2650. ClassDB::bind_method(D_METHOD("is_proximity_fade_enabled"), &BaseMaterial3D::is_proximity_fade_enabled);
  2651. ClassDB::bind_method(D_METHOD("set_proximity_fade_distance", "distance"), &BaseMaterial3D::set_proximity_fade_distance);
  2652. ClassDB::bind_method(D_METHOD("get_proximity_fade_distance"), &BaseMaterial3D::get_proximity_fade_distance);
  2653. ClassDB::bind_method(D_METHOD("set_msdf_pixel_range", "range"), &BaseMaterial3D::set_msdf_pixel_range);
  2654. ClassDB::bind_method(D_METHOD("get_msdf_pixel_range"), &BaseMaterial3D::get_msdf_pixel_range);
  2655. ClassDB::bind_method(D_METHOD("set_msdf_outline_size", "size"), &BaseMaterial3D::set_msdf_outline_size);
  2656. ClassDB::bind_method(D_METHOD("get_msdf_outline_size"), &BaseMaterial3D::get_msdf_outline_size);
  2657. ClassDB::bind_method(D_METHOD("set_distance_fade", "mode"), &BaseMaterial3D::set_distance_fade);
  2658. ClassDB::bind_method(D_METHOD("get_distance_fade"), &BaseMaterial3D::get_distance_fade);
  2659. ClassDB::bind_method(D_METHOD("set_distance_fade_max_distance", "distance"), &BaseMaterial3D::set_distance_fade_max_distance);
  2660. ClassDB::bind_method(D_METHOD("get_distance_fade_max_distance"), &BaseMaterial3D::get_distance_fade_max_distance);
  2661. ClassDB::bind_method(D_METHOD("set_distance_fade_min_distance", "distance"), &BaseMaterial3D::set_distance_fade_min_distance);
  2662. ClassDB::bind_method(D_METHOD("get_distance_fade_min_distance"), &BaseMaterial3D::get_distance_fade_min_distance);
  2663. ADD_GROUP("Transparency", "");
  2664. ADD_PROPERTY(PropertyInfo(Variant::INT, "transparency", PROPERTY_HINT_ENUM, "Disabled,Alpha,Alpha Scissor,Alpha Hash,Depth Pre-Pass"), "set_transparency", "get_transparency");
  2665. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "alpha_scissor_threshold", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_alpha_scissor_threshold", "get_alpha_scissor_threshold");
  2666. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "alpha_hash_scale", PROPERTY_HINT_RANGE, "0,2,0.01"), "set_alpha_hash_scale", "get_alpha_hash_scale");
  2667. ADD_PROPERTY(PropertyInfo(Variant::INT, "alpha_antialiasing_mode", PROPERTY_HINT_ENUM, "Disabled,Alpha Edge Blend,Alpha Edge Clip"), "set_alpha_antialiasing", "get_alpha_antialiasing");
  2668. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "alpha_antialiasing_edge", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_alpha_antialiasing_edge", "get_alpha_antialiasing_edge");
  2669. ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Mix,Add,Subtract,Multiply,Premultiplied Alpha"), "set_blend_mode", "get_blend_mode");
  2670. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mode", PROPERTY_HINT_ENUM, "Back,Front,Disabled"), "set_cull_mode", "get_cull_mode");
  2671. ADD_PROPERTY(PropertyInfo(Variant::INT, "depth_draw_mode", PROPERTY_HINT_ENUM, "Opaque Only,Always,Never"), "set_depth_draw_mode", "get_depth_draw_mode");
  2672. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "no_depth_test"), "set_flag", "get_flag", FLAG_DISABLE_DEPTH_TEST);
  2673. ADD_GROUP("Shading", "");
  2674. ADD_PROPERTY(PropertyInfo(Variant::INT, "shading_mode", PROPERTY_HINT_ENUM, "Unshaded,Per-Pixel,Per-Vertex"), "set_shading_mode", "get_shading_mode");
  2675. ADD_PROPERTY(PropertyInfo(Variant::INT, "diffuse_mode", PROPERTY_HINT_ENUM, "Burley,Lambert,Lambert Wrap,Toon"), "set_diffuse_mode", "get_diffuse_mode");
  2676. ADD_PROPERTY(PropertyInfo(Variant::INT, "specular_mode", PROPERTY_HINT_ENUM, "SchlickGGX,Toon,Disabled"), "set_specular_mode", "get_specular_mode");
  2677. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "disable_ambient_light"), "set_flag", "get_flag", FLAG_DISABLE_AMBIENT_LIGHT);
  2678. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "disable_fog"), "set_flag", "get_flag", FLAG_DISABLE_FOG);
  2679. ADD_GROUP("Vertex Color", "vertex_color");
  2680. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "vertex_color_use_as_albedo"), "set_flag", "get_flag", FLAG_ALBEDO_FROM_VERTEX_COLOR);
  2681. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "vertex_color_is_srgb"), "set_flag", "get_flag", FLAG_SRGB_VERTEX_COLOR);
  2682. ADD_GROUP("Albedo", "albedo_");
  2683. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "albedo_color"), "set_albedo", "get_albedo");
  2684. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "albedo_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_ALBEDO);
  2685. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "albedo_texture_force_srgb"), "set_flag", "get_flag", FLAG_ALBEDO_TEXTURE_FORCE_SRGB);
  2686. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "albedo_texture_msdf"), "set_flag", "get_flag", FLAG_ALBEDO_TEXTURE_MSDF);
  2687. ADD_GROUP("ORM", "orm_");
  2688. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "orm_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_ORM);
  2689. ADD_GROUP("Metallic", "metallic_");
  2690. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "metallic", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_metallic", "get_metallic");
  2691. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "metallic_specular", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_specular", "get_specular");
  2692. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "metallic_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_METALLIC);
  2693. ADD_PROPERTY(PropertyInfo(Variant::INT, "metallic_texture_channel", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Gray"), "set_metallic_texture_channel", "get_metallic_texture_channel");
  2694. ADD_GROUP("Roughness", "roughness_");
  2695. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "roughness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_roughness", "get_roughness");
  2696. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "roughness_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_ROUGHNESS);
  2697. ADD_PROPERTY(PropertyInfo(Variant::INT, "roughness_texture_channel", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Gray"), "set_roughness_texture_channel", "get_roughness_texture_channel");
  2698. ADD_GROUP("Emission", "emission_");
  2699. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "emission_enabled"), "set_feature", "get_feature", FEATURE_EMISSION);
  2700. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "emission", PROPERTY_HINT_COLOR_NO_ALPHA), "set_emission", "get_emission");
  2701. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_energy_multiplier", PROPERTY_HINT_RANGE, "0,16,0.01,or_greater"), "set_emission_energy_multiplier", "get_emission_energy_multiplier");
  2702. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_intensity", PROPERTY_HINT_RANGE, "0,100000.0,0.01,or_greater,suffix:nt"), "set_emission_intensity", "get_emission_intensity");
  2703. ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_operator", PROPERTY_HINT_ENUM, "Add,Multiply"), "set_emission_operator", "get_emission_operator");
  2704. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "emission_on_uv2"), "set_flag", "get_flag", FLAG_EMISSION_ON_UV2);
  2705. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "emission_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_EMISSION);
  2706. ADD_GROUP("Normal Map", "normal_");
  2707. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "normal_enabled"), "set_feature", "get_feature", FEATURE_NORMAL_MAPPING);
  2708. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "normal_scale", PROPERTY_HINT_RANGE, "-16,16,0.01"), "set_normal_scale", "get_normal_scale");
  2709. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "normal_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_NORMAL);
  2710. ADD_GROUP("Rim", "rim_");
  2711. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "rim_enabled"), "set_feature", "get_feature", FEATURE_RIM);
  2712. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rim", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_rim", "get_rim");
  2713. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rim_tint", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_rim_tint", "get_rim_tint");
  2714. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "rim_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_RIM);
  2715. ADD_GROUP("Clearcoat", "clearcoat_");
  2716. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "clearcoat_enabled"), "set_feature", "get_feature", FEATURE_CLEARCOAT);
  2717. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "clearcoat", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_clearcoat", "get_clearcoat");
  2718. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "clearcoat_roughness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_clearcoat_roughness", "get_clearcoat_roughness");
  2719. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "clearcoat_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_CLEARCOAT);
  2720. ADD_GROUP("Anisotropy", "anisotropy_");
  2721. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "anisotropy_enabled"), "set_feature", "get_feature", FEATURE_ANISOTROPY);
  2722. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "anisotropy", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_anisotropy", "get_anisotropy");
  2723. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anisotropy_flowmap", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_FLOWMAP);
  2724. ADD_GROUP("Ambient Occlusion", "ao_");
  2725. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "ao_enabled"), "set_feature", "get_feature", FEATURE_AMBIENT_OCCLUSION);
  2726. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ao_light_affect", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_ao_light_affect", "get_ao_light_affect");
  2727. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "ao_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_AMBIENT_OCCLUSION);
  2728. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "ao_on_uv2"), "set_flag", "get_flag", FLAG_AO_ON_UV2);
  2729. ADD_PROPERTY(PropertyInfo(Variant::INT, "ao_texture_channel", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Gray"), "set_ao_texture_channel", "get_ao_texture_channel");
  2730. ADD_GROUP("Height", "heightmap_");
  2731. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "heightmap_enabled"), "set_feature", "get_feature", FEATURE_HEIGHT_MAPPING);
  2732. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "heightmap_scale", PROPERTY_HINT_RANGE, "-16,16,0.001"), "set_heightmap_scale", "get_heightmap_scale");
  2733. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "heightmap_deep_parallax"), "set_heightmap_deep_parallax", "is_heightmap_deep_parallax_enabled");
  2734. ADD_PROPERTY(PropertyInfo(Variant::INT, "heightmap_min_layers", PROPERTY_HINT_RANGE, "1,64,1"), "set_heightmap_deep_parallax_min_layers", "get_heightmap_deep_parallax_min_layers");
  2735. ADD_PROPERTY(PropertyInfo(Variant::INT, "heightmap_max_layers", PROPERTY_HINT_RANGE, "1,64,1"), "set_heightmap_deep_parallax_max_layers", "get_heightmap_deep_parallax_max_layers");
  2736. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "heightmap_flip_tangent"), "set_heightmap_deep_parallax_flip_tangent", "get_heightmap_deep_parallax_flip_tangent");
  2737. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "heightmap_flip_binormal"), "set_heightmap_deep_parallax_flip_binormal", "get_heightmap_deep_parallax_flip_binormal");
  2738. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "heightmap_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_HEIGHTMAP);
  2739. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "heightmap_flip_texture"), "set_flag", "get_flag", FLAG_INVERT_HEIGHTMAP);
  2740. ADD_GROUP("Subsurface Scattering", "subsurf_scatter_");
  2741. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "subsurf_scatter_enabled"), "set_feature", "get_feature", FEATURE_SUBSURFACE_SCATTERING);
  2742. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "subsurf_scatter_strength", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_subsurface_scattering_strength", "get_subsurface_scattering_strength");
  2743. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "subsurf_scatter_skin_mode"), "set_flag", "get_flag", FLAG_SUBSURFACE_MODE_SKIN);
  2744. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "subsurf_scatter_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_SUBSURFACE_SCATTERING);
  2745. ADD_SUBGROUP("Transmittance", "subsurf_scatter_transmittance_");
  2746. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "subsurf_scatter_transmittance_enabled"), "set_feature", "get_feature", FEATURE_SUBSURFACE_TRANSMITTANCE);
  2747. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "subsurf_scatter_transmittance_color"), "set_transmittance_color", "get_transmittance_color");
  2748. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "subsurf_scatter_transmittance_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_SUBSURFACE_TRANSMITTANCE);
  2749. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "subsurf_scatter_transmittance_depth", PROPERTY_HINT_RANGE, "0.001,8,0.001,or_greater"), "set_transmittance_depth", "get_transmittance_depth");
  2750. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "subsurf_scatter_transmittance_boost", PROPERTY_HINT_RANGE, "0.00,1.0,0.01"), "set_transmittance_boost", "get_transmittance_boost");
  2751. ADD_GROUP("Back Lighting", "backlight_");
  2752. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "backlight_enabled"), "set_feature", "get_feature", FEATURE_BACKLIGHT);
  2753. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "backlight", PROPERTY_HINT_COLOR_NO_ALPHA), "set_backlight", "get_backlight");
  2754. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "backlight_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_BACKLIGHT);
  2755. ADD_GROUP("Refraction", "refraction_");
  2756. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "refraction_enabled"), "set_feature", "get_feature", FEATURE_REFRACTION);
  2757. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "refraction_scale", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_refraction", "get_refraction");
  2758. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "refraction_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_REFRACTION);
  2759. ADD_PROPERTY(PropertyInfo(Variant::INT, "refraction_texture_channel", PROPERTY_HINT_ENUM, "Red,Green,Blue,Alpha,Gray"), "set_refraction_texture_channel", "get_refraction_texture_channel");
  2760. ADD_GROUP("Detail", "detail_");
  2761. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "detail_enabled"), "set_feature", "get_feature", FEATURE_DETAIL);
  2762. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "detail_mask", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_DETAIL_MASK);
  2763. ADD_PROPERTY(PropertyInfo(Variant::INT, "detail_blend_mode", PROPERTY_HINT_ENUM, "Mix,Add,Subtract,Multiply"), "set_detail_blend_mode", "get_detail_blend_mode");
  2764. ADD_PROPERTY(PropertyInfo(Variant::INT, "detail_uv_layer", PROPERTY_HINT_ENUM, "UV1,UV2"), "set_detail_uv", "get_detail_uv");
  2765. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "detail_albedo", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_DETAIL_ALBEDO);
  2766. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "detail_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture", TEXTURE_DETAIL_NORMAL);
  2767. ADD_GROUP("UV1", "uv1_");
  2768. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "uv1_scale", PROPERTY_HINT_LINK), "set_uv1_scale", "get_uv1_scale");
  2769. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "uv1_offset"), "set_uv1_offset", "get_uv1_offset");
  2770. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "uv1_triplanar"), "set_flag", "get_flag", FLAG_UV1_USE_TRIPLANAR);
  2771. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "uv1_triplanar_sharpness", PROPERTY_HINT_EXP_EASING), "set_uv1_triplanar_blend_sharpness", "get_uv1_triplanar_blend_sharpness");
  2772. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "uv1_world_triplanar"), "set_flag", "get_flag", FLAG_UV1_USE_WORLD_TRIPLANAR);
  2773. ADD_GROUP("UV2", "uv2_");
  2774. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "uv2_scale", PROPERTY_HINT_LINK), "set_uv2_scale", "get_uv2_scale");
  2775. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "uv2_offset"), "set_uv2_offset", "get_uv2_offset");
  2776. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "uv2_triplanar"), "set_flag", "get_flag", FLAG_UV2_USE_TRIPLANAR);
  2777. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "uv2_triplanar_sharpness", PROPERTY_HINT_EXP_EASING), "set_uv2_triplanar_blend_sharpness", "get_uv2_triplanar_blend_sharpness");
  2778. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "uv2_world_triplanar"), "set_flag", "get_flag", FLAG_UV2_USE_WORLD_TRIPLANAR);
  2779. ADD_GROUP("Sampling", "texture_");
  2780. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_filter", PROPERTY_HINT_ENUM, "Nearest,Linear,Nearest Mipmap,Linear Mipmap,Nearest Mipmap Anisotropic,Linear Mipmap Anisotropic"), "set_texture_filter", "get_texture_filter");
  2781. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "texture_repeat"), "set_flag", "get_flag", FLAG_USE_TEXTURE_REPEAT);
  2782. ADD_GROUP("Shadows", "");
  2783. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "disable_receive_shadows"), "set_flag", "get_flag", FLAG_DONT_RECEIVE_SHADOWS);
  2784. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "shadow_to_opacity"), "set_flag", "get_flag", FLAG_USE_SHADOW_TO_OPACITY);
  2785. ADD_GROUP("Billboard", "billboard_");
  2786. ADD_PROPERTY(PropertyInfo(Variant::INT, "billboard_mode", PROPERTY_HINT_ENUM, "Disabled,Enabled,Y-Billboard,Particle Billboard"), "set_billboard_mode", "get_billboard_mode");
  2787. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "billboard_keep_scale"), "set_flag", "get_flag", FLAG_BILLBOARD_KEEP_SCALE);
  2788. ADD_GROUP("Particles Anim", "particles_anim_");
  2789. 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");
  2790. 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");
  2791. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "particles_anim_loop"), "set_particles_anim_loop", "get_particles_anim_loop");
  2792. ADD_GROUP("Grow", "grow_");
  2793. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "grow"), "set_grow_enabled", "is_grow_enabled");
  2794. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "grow_amount", PROPERTY_HINT_RANGE, "-16,16,0.001,suffix:m"), "set_grow", "get_grow");
  2795. ADD_GROUP("Transform", "");
  2796. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "fixed_size"), "set_flag", "get_flag", FLAG_FIXED_SIZE);
  2797. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "use_point_size"), "set_flag", "get_flag", FLAG_USE_POINT_SIZE);
  2798. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "point_size", PROPERTY_HINT_RANGE, "0.1,128,0.1,suffix:px"), "set_point_size", "get_point_size");
  2799. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "use_particle_trails"), "set_flag", "get_flag", FLAG_PARTICLE_TRAILS_MODE);
  2800. ADD_GROUP("Proximity Fade", "proximity_fade_");
  2801. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "proximity_fade_enabled"), "set_proximity_fade_enabled", "is_proximity_fade_enabled");
  2802. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "proximity_fade_distance", PROPERTY_HINT_RANGE, "0.01,4096,0.01,suffix:m"), "set_proximity_fade_distance", "get_proximity_fade_distance");
  2803. ADD_GROUP("MSDF", "msdf_");
  2804. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "msdf_pixel_range", PROPERTY_HINT_RANGE, "1,100,1"), "set_msdf_pixel_range", "get_msdf_pixel_range");
  2805. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "msdf_outline_size", PROPERTY_HINT_RANGE, "0,250,1"), "set_msdf_outline_size", "get_msdf_outline_size");
  2806. ADD_GROUP("Distance Fade", "distance_fade_");
  2807. ADD_PROPERTY(PropertyInfo(Variant::INT, "distance_fade_mode", PROPERTY_HINT_ENUM, "Disabled,PixelAlpha,PixelDither,ObjectDither"), "set_distance_fade", "get_distance_fade");
  2808. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "distance_fade_min_distance", PROPERTY_HINT_RANGE, "0,4096,0.01,suffix:m"), "set_distance_fade_min_distance", "get_distance_fade_min_distance");
  2809. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "distance_fade_max_distance", PROPERTY_HINT_RANGE, "0,4096,0.01,suffix:m"), "set_distance_fade_max_distance", "get_distance_fade_max_distance");
  2810. BIND_ENUM_CONSTANT(TEXTURE_ALBEDO);
  2811. BIND_ENUM_CONSTANT(TEXTURE_METALLIC);
  2812. BIND_ENUM_CONSTANT(TEXTURE_ROUGHNESS);
  2813. BIND_ENUM_CONSTANT(TEXTURE_EMISSION);
  2814. BIND_ENUM_CONSTANT(TEXTURE_NORMAL);
  2815. BIND_ENUM_CONSTANT(TEXTURE_RIM);
  2816. BIND_ENUM_CONSTANT(TEXTURE_CLEARCOAT);
  2817. BIND_ENUM_CONSTANT(TEXTURE_FLOWMAP);
  2818. BIND_ENUM_CONSTANT(TEXTURE_AMBIENT_OCCLUSION);
  2819. BIND_ENUM_CONSTANT(TEXTURE_HEIGHTMAP);
  2820. BIND_ENUM_CONSTANT(TEXTURE_SUBSURFACE_SCATTERING);
  2821. BIND_ENUM_CONSTANT(TEXTURE_SUBSURFACE_TRANSMITTANCE);
  2822. BIND_ENUM_CONSTANT(TEXTURE_BACKLIGHT);
  2823. BIND_ENUM_CONSTANT(TEXTURE_REFRACTION);
  2824. BIND_ENUM_CONSTANT(TEXTURE_DETAIL_MASK);
  2825. BIND_ENUM_CONSTANT(TEXTURE_DETAIL_ALBEDO);
  2826. BIND_ENUM_CONSTANT(TEXTURE_DETAIL_NORMAL);
  2827. BIND_ENUM_CONSTANT(TEXTURE_ORM);
  2828. BIND_ENUM_CONSTANT(TEXTURE_MAX);
  2829. BIND_ENUM_CONSTANT(TEXTURE_FILTER_NEAREST);
  2830. BIND_ENUM_CONSTANT(TEXTURE_FILTER_LINEAR);
  2831. BIND_ENUM_CONSTANT(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  2832. BIND_ENUM_CONSTANT(TEXTURE_FILTER_LINEAR_WITH_MIPMAPS);
  2833. BIND_ENUM_CONSTANT(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC);
  2834. BIND_ENUM_CONSTANT(TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC);
  2835. BIND_ENUM_CONSTANT(TEXTURE_FILTER_MAX);
  2836. BIND_ENUM_CONSTANT(DETAIL_UV_1);
  2837. BIND_ENUM_CONSTANT(DETAIL_UV_2);
  2838. BIND_ENUM_CONSTANT(TRANSPARENCY_DISABLED);
  2839. BIND_ENUM_CONSTANT(TRANSPARENCY_ALPHA);
  2840. BIND_ENUM_CONSTANT(TRANSPARENCY_ALPHA_SCISSOR);
  2841. BIND_ENUM_CONSTANT(TRANSPARENCY_ALPHA_HASH);
  2842. BIND_ENUM_CONSTANT(TRANSPARENCY_ALPHA_DEPTH_PRE_PASS);
  2843. BIND_ENUM_CONSTANT(TRANSPARENCY_MAX);
  2844. BIND_ENUM_CONSTANT(SHADING_MODE_UNSHADED);
  2845. BIND_ENUM_CONSTANT(SHADING_MODE_PER_PIXEL);
  2846. BIND_ENUM_CONSTANT(SHADING_MODE_PER_VERTEX);
  2847. BIND_ENUM_CONSTANT(SHADING_MODE_MAX);
  2848. BIND_ENUM_CONSTANT(FEATURE_EMISSION);
  2849. BIND_ENUM_CONSTANT(FEATURE_NORMAL_MAPPING);
  2850. BIND_ENUM_CONSTANT(FEATURE_RIM);
  2851. BIND_ENUM_CONSTANT(FEATURE_CLEARCOAT);
  2852. BIND_ENUM_CONSTANT(FEATURE_ANISOTROPY);
  2853. BIND_ENUM_CONSTANT(FEATURE_AMBIENT_OCCLUSION);
  2854. BIND_ENUM_CONSTANT(FEATURE_HEIGHT_MAPPING);
  2855. BIND_ENUM_CONSTANT(FEATURE_SUBSURFACE_SCATTERING);
  2856. BIND_ENUM_CONSTANT(FEATURE_SUBSURFACE_TRANSMITTANCE);
  2857. BIND_ENUM_CONSTANT(FEATURE_BACKLIGHT);
  2858. BIND_ENUM_CONSTANT(FEATURE_REFRACTION);
  2859. BIND_ENUM_CONSTANT(FEATURE_DETAIL);
  2860. BIND_ENUM_CONSTANT(FEATURE_MAX);
  2861. BIND_ENUM_CONSTANT(BLEND_MODE_MIX);
  2862. BIND_ENUM_CONSTANT(BLEND_MODE_ADD);
  2863. BIND_ENUM_CONSTANT(BLEND_MODE_SUB);
  2864. BIND_ENUM_CONSTANT(BLEND_MODE_MUL);
  2865. BIND_ENUM_CONSTANT(BLEND_MODE_PREMULT_ALPHA);
  2866. BIND_ENUM_CONSTANT(ALPHA_ANTIALIASING_OFF);
  2867. BIND_ENUM_CONSTANT(ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE);
  2868. BIND_ENUM_CONSTANT(ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE_AND_TO_ONE);
  2869. BIND_ENUM_CONSTANT(DEPTH_DRAW_OPAQUE_ONLY);
  2870. BIND_ENUM_CONSTANT(DEPTH_DRAW_ALWAYS);
  2871. BIND_ENUM_CONSTANT(DEPTH_DRAW_DISABLED);
  2872. BIND_ENUM_CONSTANT(CULL_BACK);
  2873. BIND_ENUM_CONSTANT(CULL_FRONT);
  2874. BIND_ENUM_CONSTANT(CULL_DISABLED);
  2875. BIND_ENUM_CONSTANT(FLAG_DISABLE_DEPTH_TEST);
  2876. BIND_ENUM_CONSTANT(FLAG_ALBEDO_FROM_VERTEX_COLOR);
  2877. BIND_ENUM_CONSTANT(FLAG_SRGB_VERTEX_COLOR);
  2878. BIND_ENUM_CONSTANT(FLAG_USE_POINT_SIZE);
  2879. BIND_ENUM_CONSTANT(FLAG_FIXED_SIZE);
  2880. BIND_ENUM_CONSTANT(FLAG_BILLBOARD_KEEP_SCALE);
  2881. BIND_ENUM_CONSTANT(FLAG_UV1_USE_TRIPLANAR);
  2882. BIND_ENUM_CONSTANT(FLAG_UV2_USE_TRIPLANAR);
  2883. BIND_ENUM_CONSTANT(FLAG_UV1_USE_WORLD_TRIPLANAR);
  2884. BIND_ENUM_CONSTANT(FLAG_UV2_USE_WORLD_TRIPLANAR);
  2885. BIND_ENUM_CONSTANT(FLAG_AO_ON_UV2);
  2886. BIND_ENUM_CONSTANT(FLAG_EMISSION_ON_UV2);
  2887. BIND_ENUM_CONSTANT(FLAG_ALBEDO_TEXTURE_FORCE_SRGB);
  2888. BIND_ENUM_CONSTANT(FLAG_DONT_RECEIVE_SHADOWS);
  2889. BIND_ENUM_CONSTANT(FLAG_DISABLE_AMBIENT_LIGHT);
  2890. BIND_ENUM_CONSTANT(FLAG_USE_SHADOW_TO_OPACITY);
  2891. BIND_ENUM_CONSTANT(FLAG_USE_TEXTURE_REPEAT);
  2892. BIND_ENUM_CONSTANT(FLAG_INVERT_HEIGHTMAP);
  2893. BIND_ENUM_CONSTANT(FLAG_SUBSURFACE_MODE_SKIN);
  2894. BIND_ENUM_CONSTANT(FLAG_PARTICLE_TRAILS_MODE);
  2895. BIND_ENUM_CONSTANT(FLAG_ALBEDO_TEXTURE_MSDF);
  2896. BIND_ENUM_CONSTANT(FLAG_DISABLE_FOG);
  2897. BIND_ENUM_CONSTANT(FLAG_MAX);
  2898. BIND_ENUM_CONSTANT(DIFFUSE_BURLEY);
  2899. BIND_ENUM_CONSTANT(DIFFUSE_LAMBERT);
  2900. BIND_ENUM_CONSTANT(DIFFUSE_LAMBERT_WRAP);
  2901. BIND_ENUM_CONSTANT(DIFFUSE_TOON);
  2902. BIND_ENUM_CONSTANT(SPECULAR_SCHLICK_GGX);
  2903. BIND_ENUM_CONSTANT(SPECULAR_TOON);
  2904. BIND_ENUM_CONSTANT(SPECULAR_DISABLED);
  2905. BIND_ENUM_CONSTANT(BILLBOARD_DISABLED);
  2906. BIND_ENUM_CONSTANT(BILLBOARD_ENABLED);
  2907. BIND_ENUM_CONSTANT(BILLBOARD_FIXED_Y);
  2908. BIND_ENUM_CONSTANT(BILLBOARD_PARTICLES);
  2909. BIND_ENUM_CONSTANT(TEXTURE_CHANNEL_RED);
  2910. BIND_ENUM_CONSTANT(TEXTURE_CHANNEL_GREEN);
  2911. BIND_ENUM_CONSTANT(TEXTURE_CHANNEL_BLUE);
  2912. BIND_ENUM_CONSTANT(TEXTURE_CHANNEL_ALPHA);
  2913. BIND_ENUM_CONSTANT(TEXTURE_CHANNEL_GRAYSCALE);
  2914. BIND_ENUM_CONSTANT(EMISSION_OP_ADD);
  2915. BIND_ENUM_CONSTANT(EMISSION_OP_MULTIPLY);
  2916. BIND_ENUM_CONSTANT(DISTANCE_FADE_DISABLED);
  2917. BIND_ENUM_CONSTANT(DISTANCE_FADE_PIXEL_ALPHA);
  2918. BIND_ENUM_CONSTANT(DISTANCE_FADE_PIXEL_DITHER);
  2919. BIND_ENUM_CONSTANT(DISTANCE_FADE_OBJECT_DITHER);
  2920. }
  2921. BaseMaterial3D::BaseMaterial3D(bool p_orm) {
  2922. orm = p_orm;
  2923. // Initialize to the same values as the shader
  2924. set_albedo(Color(1.0, 1.0, 1.0, 1.0));
  2925. set_specular(0.5);
  2926. set_roughness(1.0);
  2927. set_metallic(0.0);
  2928. set_emission(Color(0, 0, 0));
  2929. set_emission_energy_multiplier(1.0);
  2930. set_normal_scale(1);
  2931. set_rim(1.0);
  2932. set_rim_tint(0.5);
  2933. set_clearcoat(1);
  2934. set_clearcoat_roughness(0.5);
  2935. set_anisotropy(0);
  2936. set_heightmap_scale(5.0);
  2937. set_subsurface_scattering_strength(0);
  2938. set_backlight(Color(0, 0, 0));
  2939. set_transmittance_color(Color(1, 1, 1, 1));
  2940. set_transmittance_depth(0.1);
  2941. set_transmittance_boost(0.0);
  2942. set_refraction(0.05);
  2943. set_point_size(1);
  2944. set_uv1_offset(Vector3(0, 0, 0));
  2945. set_uv1_scale(Vector3(1, 1, 1));
  2946. set_uv1_triplanar_blend_sharpness(1);
  2947. set_uv2_offset(Vector3(0, 0, 0));
  2948. set_uv2_scale(Vector3(1, 1, 1));
  2949. set_uv2_triplanar_blend_sharpness(1);
  2950. set_billboard_mode(BILLBOARD_DISABLED);
  2951. set_particles_anim_h_frames(1);
  2952. set_particles_anim_v_frames(1);
  2953. set_particles_anim_loop(false);
  2954. set_transparency(TRANSPARENCY_DISABLED);
  2955. set_alpha_antialiasing(ALPHA_ANTIALIASING_OFF);
  2956. // Alpha scissor threshold of 0.5 matches the glTF specification and Label3D default.
  2957. // <https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#_material_alphacutoff>
  2958. set_alpha_scissor_threshold(0.5);
  2959. set_alpha_hash_scale(1.0);
  2960. set_alpha_antialiasing_edge(0.3);
  2961. set_proximity_fade_distance(1);
  2962. set_distance_fade_min_distance(0);
  2963. set_distance_fade_max_distance(10);
  2964. set_ao_light_affect(0.0);
  2965. set_metallic_texture_channel(TEXTURE_CHANNEL_RED);
  2966. set_roughness_texture_channel(TEXTURE_CHANNEL_RED);
  2967. set_ao_texture_channel(TEXTURE_CHANNEL_RED);
  2968. set_refraction_texture_channel(TEXTURE_CHANNEL_RED);
  2969. set_grow(0.0);
  2970. set_msdf_pixel_range(4.0);
  2971. set_msdf_outline_size(0.0);
  2972. set_heightmap_deep_parallax_min_layers(8);
  2973. set_heightmap_deep_parallax_max_layers(32);
  2974. set_heightmap_deep_parallax_flip_tangent(false); //also sets binormal
  2975. flags[FLAG_ALBEDO_TEXTURE_MSDF] = false;
  2976. flags[FLAG_USE_TEXTURE_REPEAT] = true;
  2977. current_key.invalid_key = 1;
  2978. _mark_dirty();
  2979. }
  2980. BaseMaterial3D::~BaseMaterial3D() {
  2981. ERR_FAIL_NULL(RS::get_singleton());
  2982. {
  2983. MutexLock lock(shader_map_mutex);
  2984. if (shader_map.has(current_key)) {
  2985. shader_map[current_key].users--;
  2986. if (shader_map[current_key].users == 0) {
  2987. // Deallocate shader which is no longer in use.
  2988. RS::get_singleton()->free(shader_map[current_key].shader);
  2989. shader_map.erase(current_key);
  2990. }
  2991. }
  2992. }
  2993. if (_get_material().is_valid()) {
  2994. RS::get_singleton()->material_set_shader(_get_material(), RID());
  2995. }
  2996. }
  2997. //////////////////////
  2998. #ifndef DISABLE_DEPRECATED
  2999. // Kept for compatibility from 3.x to 4.0.
  3000. bool StandardMaterial3D::_set(const StringName &p_name, const Variant &p_value) {
  3001. if (p_name == "flags_transparent") {
  3002. bool transparent = p_value;
  3003. if (transparent) {
  3004. set_transparency(TRANSPARENCY_ALPHA);
  3005. }
  3006. return true;
  3007. } else if (p_name == "flags_unshaded") {
  3008. bool unshaded = p_value;
  3009. if (unshaded) {
  3010. set_shading_mode(SHADING_MODE_UNSHADED);
  3011. }
  3012. return true;
  3013. } else if (p_name == "flags_vertex_lighting") {
  3014. bool vertex_lit = p_value;
  3015. if (vertex_lit && get_shading_mode() != SHADING_MODE_UNSHADED) {
  3016. set_shading_mode(SHADING_MODE_PER_VERTEX);
  3017. }
  3018. return true;
  3019. } else if (p_name == "params_use_alpha_scissor") {
  3020. bool use_scissor = p_value;
  3021. if (use_scissor) {
  3022. set_transparency(TRANSPARENCY_ALPHA_SCISSOR);
  3023. }
  3024. return true;
  3025. } else if (p_name == "params_use_alpha_hash") {
  3026. bool use_hash = p_value;
  3027. if (use_hash) {
  3028. set_transparency(TRANSPARENCY_ALPHA_HASH);
  3029. }
  3030. return true;
  3031. } else if (p_name == "params_depth_draw_mode") {
  3032. int mode = p_value;
  3033. if (mode == 3) {
  3034. set_transparency(TRANSPARENCY_ALPHA_DEPTH_PRE_PASS);
  3035. }
  3036. return true;
  3037. } else if (p_name == "depth_enabled") {
  3038. bool enabled = p_value;
  3039. if (enabled) {
  3040. set_feature(FEATURE_HEIGHT_MAPPING, true);
  3041. set_flag(FLAG_INVERT_HEIGHTMAP, true);
  3042. }
  3043. return true;
  3044. } else {
  3045. static const Pair<const char *, const char *> remaps[] = {
  3046. { "flags_use_shadow_to_opacity", "shadow_to_opacity" },
  3047. { "flags_use_shadow_to_opacity", "shadow_to_opacity" },
  3048. { "flags_no_depth_test", "no_depth_test" },
  3049. { "flags_use_point_size", "use_point_size" },
  3050. { "flags_fixed_size", "fixed_size" },
  3051. { "flags_albedo_tex_force_srgb", "albedo_texture_force_srgb" },
  3052. { "flags_do_not_receive_shadows", "disable_receive_shadows" },
  3053. { "flags_disable_ambient_light", "disable_ambient_light" },
  3054. { "params_diffuse_mode", "diffuse_mode" },
  3055. { "params_specular_mode", "specular_mode" },
  3056. { "params_blend_mode", "blend_mode" },
  3057. { "params_cull_mode", "cull_mode" },
  3058. { "params_depth_draw_mode", "params_depth_draw_mode" },
  3059. { "params_point_size", "point_size" },
  3060. { "params_billboard_mode", "billboard_mode" },
  3061. { "params_billboard_keep_scale", "billboard_keep_scale" },
  3062. { "params_grow", "grow" },
  3063. { "params_grow_amount", "grow_amount" },
  3064. { "params_alpha_scissor_threshold", "alpha_scissor_threshold" },
  3065. { "params_alpha_hash_scale", "alpha_hash_scale" },
  3066. { "params_alpha_antialiasing_edge", "alpha_antialiasing_edge" },
  3067. { "depth_scale", "heightmap_scale" },
  3068. { "depth_deep_parallax", "heightmap_deep_parallax" },
  3069. { "depth_min_layers", "heightmap_min_layers" },
  3070. { "depth_max_layers", "heightmap_max_layers" },
  3071. { "depth_flip_tangent", "heightmap_flip_tangent" },
  3072. { "depth_flip_binormal", "heightmap_flip_binormal" },
  3073. { "depth_texture", "heightmap_texture" },
  3074. { "emission_energy", "emission_energy_multiplier" },
  3075. { nullptr, nullptr },
  3076. };
  3077. int idx = 0;
  3078. while (remaps[idx].first) {
  3079. if (p_name == remaps[idx].first) {
  3080. set(remaps[idx].second, p_value);
  3081. return true;
  3082. }
  3083. idx++;
  3084. }
  3085. WARN_PRINT("Godot 3.x SpatialMaterial remapped parameter not found: " + String(p_name));
  3086. return true;
  3087. }
  3088. }
  3089. #endif // DISABLE_DEPRECATED
  3090. ///////////////////////