material.cpp 91 KB

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