sky_material.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*************************************************************************/
  2. /* sky_material.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "sky_material.h"
  31. void ProceduralSkyMaterial::set_sky_top_color(const Color &p_sky_top) {
  32. sky_top_color = p_sky_top;
  33. RS::get_singleton()->material_set_param(_get_material(), "sky_top_color", sky_top_color.to_linear());
  34. }
  35. Color ProceduralSkyMaterial::get_sky_top_color() const {
  36. return sky_top_color;
  37. }
  38. void ProceduralSkyMaterial::set_sky_horizon_color(const Color &p_sky_horizon) {
  39. sky_horizon_color = p_sky_horizon;
  40. RS::get_singleton()->material_set_param(_get_material(), "sky_horizon_color", sky_horizon_color.to_linear());
  41. }
  42. Color ProceduralSkyMaterial::get_sky_horizon_color() const {
  43. return sky_horizon_color;
  44. }
  45. void ProceduralSkyMaterial::set_sky_curve(float p_curve) {
  46. sky_curve = p_curve;
  47. RS::get_singleton()->material_set_param(_get_material(), "sky_curve", sky_curve);
  48. }
  49. float ProceduralSkyMaterial::get_sky_curve() const {
  50. return sky_curve;
  51. }
  52. void ProceduralSkyMaterial::set_sky_energy(float p_energy) {
  53. sky_energy = p_energy;
  54. RS::get_singleton()->material_set_param(_get_material(), "sky_energy", sky_energy);
  55. }
  56. float ProceduralSkyMaterial::get_sky_energy() const {
  57. return sky_energy;
  58. }
  59. void ProceduralSkyMaterial::set_ground_bottom_color(const Color &p_ground_bottom) {
  60. ground_bottom_color = p_ground_bottom;
  61. RS::get_singleton()->material_set_param(_get_material(), "ground_bottom_color", ground_bottom_color.to_linear());
  62. }
  63. Color ProceduralSkyMaterial::get_ground_bottom_color() const {
  64. return ground_bottom_color;
  65. }
  66. void ProceduralSkyMaterial::set_ground_horizon_color(const Color &p_ground_horizon) {
  67. ground_horizon_color = p_ground_horizon;
  68. RS::get_singleton()->material_set_param(_get_material(), "ground_horizon_color", ground_horizon_color.to_linear());
  69. }
  70. Color ProceduralSkyMaterial::get_ground_horizon_color() const {
  71. return ground_horizon_color;
  72. }
  73. void ProceduralSkyMaterial::set_ground_curve(float p_curve) {
  74. ground_curve = p_curve;
  75. RS::get_singleton()->material_set_param(_get_material(), "ground_curve", ground_curve);
  76. }
  77. float ProceduralSkyMaterial::get_ground_curve() const {
  78. return ground_curve;
  79. }
  80. void ProceduralSkyMaterial::set_ground_energy(float p_energy) {
  81. ground_energy = p_energy;
  82. RS::get_singleton()->material_set_param(_get_material(), "ground_energy", ground_energy);
  83. }
  84. float ProceduralSkyMaterial::get_ground_energy() const {
  85. return ground_energy;
  86. }
  87. void ProceduralSkyMaterial::set_sun_angle_max(float p_angle) {
  88. sun_angle_max = p_angle;
  89. RS::get_singleton()->material_set_param(_get_material(), "sun_angle_max", Math::deg2rad(sun_angle_max));
  90. }
  91. float ProceduralSkyMaterial::get_sun_angle_max() const {
  92. return sun_angle_max;
  93. }
  94. void ProceduralSkyMaterial::set_sun_curve(float p_curve) {
  95. sun_curve = p_curve;
  96. RS::get_singleton()->material_set_param(_get_material(), "sun_curve", sun_curve);
  97. }
  98. float ProceduralSkyMaterial::get_sun_curve() const {
  99. return sun_curve;
  100. }
  101. bool ProceduralSkyMaterial::_can_do_next_pass() const {
  102. return false;
  103. }
  104. Shader::Mode ProceduralSkyMaterial::get_shader_mode() const {
  105. return Shader::MODE_SKY;
  106. }
  107. RID ProceduralSkyMaterial::get_shader_rid() const {
  108. return shader;
  109. }
  110. void ProceduralSkyMaterial::_bind_methods() {
  111. ClassDB::bind_method(D_METHOD("set_sky_top_color", "color"), &ProceduralSkyMaterial::set_sky_top_color);
  112. ClassDB::bind_method(D_METHOD("get_sky_top_color"), &ProceduralSkyMaterial::get_sky_top_color);
  113. ClassDB::bind_method(D_METHOD("set_sky_horizon_color", "color"), &ProceduralSkyMaterial::set_sky_horizon_color);
  114. ClassDB::bind_method(D_METHOD("get_sky_horizon_color"), &ProceduralSkyMaterial::get_sky_horizon_color);
  115. ClassDB::bind_method(D_METHOD("set_sky_curve", "curve"), &ProceduralSkyMaterial::set_sky_curve);
  116. ClassDB::bind_method(D_METHOD("get_sky_curve"), &ProceduralSkyMaterial::get_sky_curve);
  117. ClassDB::bind_method(D_METHOD("set_sky_energy", "energy"), &ProceduralSkyMaterial::set_sky_energy);
  118. ClassDB::bind_method(D_METHOD("get_sky_energy"), &ProceduralSkyMaterial::get_sky_energy);
  119. ClassDB::bind_method(D_METHOD("set_ground_bottom_color", "color"), &ProceduralSkyMaterial::set_ground_bottom_color);
  120. ClassDB::bind_method(D_METHOD("get_ground_bottom_color"), &ProceduralSkyMaterial::get_ground_bottom_color);
  121. ClassDB::bind_method(D_METHOD("set_ground_horizon_color", "color"), &ProceduralSkyMaterial::set_ground_horizon_color);
  122. ClassDB::bind_method(D_METHOD("get_ground_horizon_color"), &ProceduralSkyMaterial::get_ground_horizon_color);
  123. ClassDB::bind_method(D_METHOD("set_ground_curve", "curve"), &ProceduralSkyMaterial::set_ground_curve);
  124. ClassDB::bind_method(D_METHOD("get_ground_curve"), &ProceduralSkyMaterial::get_ground_curve);
  125. ClassDB::bind_method(D_METHOD("set_ground_energy", "energy"), &ProceduralSkyMaterial::set_ground_energy);
  126. ClassDB::bind_method(D_METHOD("get_ground_energy"), &ProceduralSkyMaterial::get_ground_energy);
  127. ClassDB::bind_method(D_METHOD("set_sun_angle_max", "degrees"), &ProceduralSkyMaterial::set_sun_angle_max);
  128. ClassDB::bind_method(D_METHOD("get_sun_angle_max"), &ProceduralSkyMaterial::get_sun_angle_max);
  129. ClassDB::bind_method(D_METHOD("set_sun_curve", "curve"), &ProceduralSkyMaterial::set_sun_curve);
  130. ClassDB::bind_method(D_METHOD("get_sun_curve"), &ProceduralSkyMaterial::get_sun_curve);
  131. ADD_GROUP("Sky", "sky_");
  132. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "sky_top_color"), "set_sky_top_color", "get_sky_top_color");
  133. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "sky_horizon_color"), "set_sky_horizon_color", "get_sky_horizon_color");
  134. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sky_curve", PROPERTY_HINT_EXP_EASING), "set_sky_curve", "get_sky_curve");
  135. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sky_energy", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_sky_energy", "get_sky_energy");
  136. ADD_GROUP("Ground", "ground_");
  137. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "ground_bottom_color"), "set_ground_bottom_color", "get_ground_bottom_color");
  138. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "ground_horizon_color"), "set_ground_horizon_color", "get_ground_horizon_color");
  139. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ground_curve", PROPERTY_HINT_EXP_EASING), "set_ground_curve", "get_ground_curve");
  140. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ground_energy", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_ground_energy", "get_ground_energy");
  141. ADD_GROUP("Sun", "sun_");
  142. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sun_angle_max", PROPERTY_HINT_RANGE, "0,360,0.01"), "set_sun_angle_max", "get_sun_angle_max");
  143. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sun_curve", PROPERTY_HINT_EXP_EASING), "set_sun_curve", "get_sun_curve");
  144. }
  145. ProceduralSkyMaterial::ProceduralSkyMaterial() {
  146. shader = RS::get_singleton()->shader_create();
  147. RS::get_singleton()->shader_set_code(shader, R"(
  148. shader_type sky;
  149. uniform vec4 sky_top_color : hint_color = vec4(0.35, 0.46, 0.71, 1.0);
  150. uniform vec4 sky_horizon_color : hint_color = vec4(0.55, 0.69, 0.81, 1.0);
  151. uniform float sky_curve : hint_range(0, 1) = 0.09;
  152. uniform float sky_energy = 1.0;
  153. uniform vec4 ground_bottom_color : hint_color = vec4(0.12, 0.12, 0.13, 1.0);
  154. uniform vec4 ground_horizon_color : hint_color = vec4(0.37, 0.33, 0.31, 1.0);
  155. uniform float ground_curve : hint_range(0, 1) = 0.02;
  156. uniform float ground_energy = 1.0;
  157. uniform float sun_angle_max = 1.74;
  158. uniform float sun_curve : hint_range(0, 1) = 0.05;
  159. void sky() {
  160. float v_angle = acos(clamp(EYEDIR.y, -1.0, 1.0));
  161. float c = (1.0 - v_angle / (PI * 0.5));
  162. vec3 sky = mix(sky_horizon_color.rgb, sky_top_color.rgb, clamp(1.0 - pow(1.0 - c, 1.0 / sky_curve), 0.0, 1.0));
  163. sky *= sky_energy;
  164. if (LIGHT0_ENABLED) {
  165. float sun_angle = acos(dot(LIGHT0_DIRECTION, EYEDIR));
  166. if (sun_angle < LIGHT0_SIZE) {
  167. sky = LIGHT0_COLOR * LIGHT0_ENERGY;
  168. } else if (sun_angle < sun_angle_max) {
  169. float c2 = (sun_angle - LIGHT0_SIZE) / (sun_angle_max - LIGHT0_SIZE);
  170. sky = mix(LIGHT0_COLOR * LIGHT0_ENERGY, sky, clamp(1.0 - pow(1.0 - c2, 1.0 / sun_curve), 0.0, 1.0));
  171. }
  172. }
  173. if (LIGHT1_ENABLED) {
  174. float sun_angle = acos(dot(LIGHT1_DIRECTION, EYEDIR));
  175. if (sun_angle < LIGHT1_SIZE) {
  176. sky = LIGHT1_COLOR * LIGHT1_ENERGY;
  177. } else if (sun_angle < sun_angle_max) {
  178. float c2 = (sun_angle - LIGHT1_SIZE) / (sun_angle_max - LIGHT1_SIZE);
  179. sky = mix(LIGHT1_COLOR * LIGHT1_ENERGY, sky, clamp(1.0 - pow(1.0 - c2, 1.0 / sun_curve), 0.0, 1.0));
  180. }
  181. }
  182. if (LIGHT2_ENABLED) {
  183. float sun_angle = acos(dot(LIGHT2_DIRECTION, EYEDIR));
  184. if (sun_angle < LIGHT2_SIZE) {
  185. sky = LIGHT2_COLOR * LIGHT2_ENERGY;
  186. } else if (sun_angle < sun_angle_max) {
  187. float c2 = (sun_angle - LIGHT2_SIZE) / (sun_angle_max - LIGHT2_SIZE);
  188. sky = mix(LIGHT2_COLOR * LIGHT2_ENERGY, sky, clamp(1.0 - pow(1.0 - c2, 1.0 / sun_curve), 0.0, 1.0));
  189. }
  190. }
  191. if (LIGHT3_ENABLED) {
  192. float sun_angle = acos(dot(LIGHT3_DIRECTION, EYEDIR));
  193. if (sun_angle < LIGHT3_SIZE) {
  194. sky = LIGHT3_COLOR * LIGHT3_ENERGY;
  195. } else if (sun_angle < sun_angle_max) {
  196. float c2 = (sun_angle - LIGHT3_SIZE) / (sun_angle_max - LIGHT3_SIZE);
  197. sky = mix(LIGHT3_COLOR * LIGHT3_ENERGY, sky, clamp(1.0 - pow(1.0 - c2, 1.0 / sun_curve), 0.0, 1.0));
  198. }
  199. }
  200. c = (v_angle - (PI * 0.5)) / (PI * 0.5);
  201. vec3 ground = mix(ground_horizon_color.rgb, ground_bottom_color.rgb, clamp(1.0 - pow(1.0 - c, 1.0 / ground_curve), 0.0, 1.0));
  202. ground *= ground_energy;
  203. COLOR = mix(ground, sky, step(0.0, EYEDIR.y));
  204. }
  205. )");
  206. RS::get_singleton()->material_set_shader(_get_material(), shader);
  207. set_sky_top_color(Color(0.35, 0.46, 0.71));
  208. set_sky_horizon_color(Color(0.55, 0.69, 0.81));
  209. set_sky_curve(0.09);
  210. set_sky_energy(1.0);
  211. set_ground_bottom_color(Color(0.12, 0.12, 0.13));
  212. set_ground_horizon_color(Color(0.37, 0.33, 0.31));
  213. set_ground_curve(0.02);
  214. set_ground_energy(1.0);
  215. set_sun_angle_max(100.0);
  216. set_sun_curve(0.05);
  217. }
  218. ProceduralSkyMaterial::~ProceduralSkyMaterial() {
  219. RS::get_singleton()->free(shader);
  220. RS::get_singleton()->material_set_shader(_get_material(), RID());
  221. }
  222. /////////////////////////////////////////
  223. /* PanoramaSkyMaterial */
  224. void PanoramaSkyMaterial::set_panorama(const Ref<Texture2D> &p_panorama) {
  225. panorama = p_panorama;
  226. RID tex_rid = p_panorama.is_valid() ? p_panorama->get_rid() : RID();
  227. RS::get_singleton()->material_set_param(_get_material(), "source_panorama", tex_rid);
  228. }
  229. Ref<Texture2D> PanoramaSkyMaterial::get_panorama() const {
  230. return panorama;
  231. }
  232. bool PanoramaSkyMaterial::_can_do_next_pass() const {
  233. return false;
  234. }
  235. Shader::Mode PanoramaSkyMaterial::get_shader_mode() const {
  236. return Shader::MODE_SKY;
  237. }
  238. RID PanoramaSkyMaterial::get_shader_rid() const {
  239. return shader;
  240. }
  241. void PanoramaSkyMaterial::_bind_methods() {
  242. ClassDB::bind_method(D_METHOD("set_panorama", "texture"), &PanoramaSkyMaterial::set_panorama);
  243. ClassDB::bind_method(D_METHOD("get_panorama"), &PanoramaSkyMaterial::get_panorama);
  244. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "panorama", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_panorama", "get_panorama");
  245. }
  246. PanoramaSkyMaterial::PanoramaSkyMaterial() {
  247. shader = RS::get_singleton()->shader_create();
  248. RS::get_singleton()->shader_set_code(shader, R"(
  249. shader_type sky;
  250. uniform sampler2D source_panorama : filter_linear;
  251. void sky() {
  252. COLOR = texture(source_panorama, SKY_COORDS).rgb;
  253. }
  254. )");
  255. RS::get_singleton()->material_set_shader(_get_material(), shader);
  256. }
  257. PanoramaSkyMaterial::~PanoramaSkyMaterial() {
  258. RS::get_singleton()->free(shader);
  259. RS::get_singleton()->material_set_shader(_get_material(), RID());
  260. }
  261. //////////////////////////////////
  262. /* PhysicalSkyMaterial */
  263. void PhysicalSkyMaterial::set_rayleigh_coefficient(float p_rayleigh) {
  264. rayleigh = p_rayleigh;
  265. RS::get_singleton()->material_set_param(_get_material(), "rayleigh", rayleigh);
  266. }
  267. float PhysicalSkyMaterial::get_rayleigh_coefficient() const {
  268. return rayleigh;
  269. }
  270. void PhysicalSkyMaterial::set_rayleigh_color(Color p_rayleigh_color) {
  271. rayleigh_color = p_rayleigh_color;
  272. RS::get_singleton()->material_set_param(_get_material(), "rayleigh_color", rayleigh_color);
  273. }
  274. Color PhysicalSkyMaterial::get_rayleigh_color() const {
  275. return rayleigh_color;
  276. }
  277. void PhysicalSkyMaterial::set_mie_coefficient(float p_mie) {
  278. mie = p_mie;
  279. RS::get_singleton()->material_set_param(_get_material(), "mie", mie);
  280. }
  281. float PhysicalSkyMaterial::get_mie_coefficient() const {
  282. return mie;
  283. }
  284. void PhysicalSkyMaterial::set_mie_eccentricity(float p_eccentricity) {
  285. mie_eccentricity = p_eccentricity;
  286. RS::get_singleton()->material_set_param(_get_material(), "mie_eccentricity", mie_eccentricity);
  287. }
  288. float PhysicalSkyMaterial::get_mie_eccentricity() const {
  289. return mie_eccentricity;
  290. }
  291. void PhysicalSkyMaterial::set_mie_color(Color p_mie_color) {
  292. mie_color = p_mie_color;
  293. RS::get_singleton()->material_set_param(_get_material(), "mie_color", mie_color);
  294. }
  295. Color PhysicalSkyMaterial::get_mie_color() const {
  296. return mie_color;
  297. }
  298. void PhysicalSkyMaterial::set_turbidity(float p_turbidity) {
  299. turbidity = p_turbidity;
  300. RS::get_singleton()->material_set_param(_get_material(), "turbidity", turbidity);
  301. }
  302. float PhysicalSkyMaterial::get_turbidity() const {
  303. return turbidity;
  304. }
  305. void PhysicalSkyMaterial::set_sun_disk_scale(float p_sun_disk_scale) {
  306. sun_disk_scale = p_sun_disk_scale;
  307. RS::get_singleton()->material_set_param(_get_material(), "sun_disk_scale", sun_disk_scale);
  308. }
  309. float PhysicalSkyMaterial::get_sun_disk_scale() const {
  310. return sun_disk_scale;
  311. }
  312. void PhysicalSkyMaterial::set_ground_color(Color p_ground_color) {
  313. ground_color = p_ground_color;
  314. RS::get_singleton()->material_set_param(_get_material(), "ground_color", ground_color);
  315. }
  316. Color PhysicalSkyMaterial::get_ground_color() const {
  317. return ground_color;
  318. }
  319. void PhysicalSkyMaterial::set_exposure(float p_exposure) {
  320. exposure = p_exposure;
  321. RS::get_singleton()->material_set_param(_get_material(), "exposure", exposure);
  322. }
  323. float PhysicalSkyMaterial::get_exposure() const {
  324. return exposure;
  325. }
  326. void PhysicalSkyMaterial::set_dither_strength(float p_dither_strength) {
  327. dither_strength = p_dither_strength;
  328. RS::get_singleton()->material_set_param(_get_material(), "dither_strength", dither_strength);
  329. }
  330. float PhysicalSkyMaterial::get_dither_strength() const {
  331. return dither_strength;
  332. }
  333. void PhysicalSkyMaterial::set_night_sky(const Ref<Texture2D> &p_night_sky) {
  334. night_sky = p_night_sky;
  335. RID tex_rid = p_night_sky.is_valid() ? p_night_sky->get_rid() : RID();
  336. RS::get_singleton()->material_set_param(_get_material(), "night_sky", tex_rid);
  337. }
  338. Ref<Texture2D> PhysicalSkyMaterial::get_night_sky() const {
  339. return night_sky;
  340. }
  341. bool PhysicalSkyMaterial::_can_do_next_pass() const {
  342. return false;
  343. }
  344. Shader::Mode PhysicalSkyMaterial::get_shader_mode() const {
  345. return Shader::MODE_SKY;
  346. }
  347. RID PhysicalSkyMaterial::get_shader_rid() const {
  348. return shader;
  349. }
  350. void PhysicalSkyMaterial::_bind_methods() {
  351. ClassDB::bind_method(D_METHOD("set_rayleigh_coefficient", "rayleigh"), &PhysicalSkyMaterial::set_rayleigh_coefficient);
  352. ClassDB::bind_method(D_METHOD("get_rayleigh_coefficient"), &PhysicalSkyMaterial::get_rayleigh_coefficient);
  353. ClassDB::bind_method(D_METHOD("set_rayleigh_color", "color"), &PhysicalSkyMaterial::set_rayleigh_color);
  354. ClassDB::bind_method(D_METHOD("get_rayleigh_color"), &PhysicalSkyMaterial::get_rayleigh_color);
  355. ClassDB::bind_method(D_METHOD("set_mie_coefficient", "mie"), &PhysicalSkyMaterial::set_mie_coefficient);
  356. ClassDB::bind_method(D_METHOD("get_mie_coefficient"), &PhysicalSkyMaterial::get_mie_coefficient);
  357. ClassDB::bind_method(D_METHOD("set_mie_eccentricity", "eccentricity"), &PhysicalSkyMaterial::set_mie_eccentricity);
  358. ClassDB::bind_method(D_METHOD("get_mie_eccentricity"), &PhysicalSkyMaterial::get_mie_eccentricity);
  359. ClassDB::bind_method(D_METHOD("set_mie_color", "color"), &PhysicalSkyMaterial::set_mie_color);
  360. ClassDB::bind_method(D_METHOD("get_mie_color"), &PhysicalSkyMaterial::get_mie_color);
  361. ClassDB::bind_method(D_METHOD("set_turbidity", "turbidity"), &PhysicalSkyMaterial::set_turbidity);
  362. ClassDB::bind_method(D_METHOD("get_turbidity"), &PhysicalSkyMaterial::get_turbidity);
  363. ClassDB::bind_method(D_METHOD("set_sun_disk_scale", "scale"), &PhysicalSkyMaterial::set_sun_disk_scale);
  364. ClassDB::bind_method(D_METHOD("get_sun_disk_scale"), &PhysicalSkyMaterial::get_sun_disk_scale);
  365. ClassDB::bind_method(D_METHOD("set_ground_color", "color"), &PhysicalSkyMaterial::set_ground_color);
  366. ClassDB::bind_method(D_METHOD("get_ground_color"), &PhysicalSkyMaterial::get_ground_color);
  367. ClassDB::bind_method(D_METHOD("set_exposure", "exposure"), &PhysicalSkyMaterial::set_exposure);
  368. ClassDB::bind_method(D_METHOD("get_exposure"), &PhysicalSkyMaterial::get_exposure);
  369. ClassDB::bind_method(D_METHOD("set_dither_strength", "strength"), &PhysicalSkyMaterial::set_dither_strength);
  370. ClassDB::bind_method(D_METHOD("get_dither_strength"), &PhysicalSkyMaterial::get_dither_strength);
  371. ClassDB::bind_method(D_METHOD("set_night_sky", "night_sky"), &PhysicalSkyMaterial::set_night_sky);
  372. ClassDB::bind_method(D_METHOD("get_night_sky"), &PhysicalSkyMaterial::get_night_sky);
  373. ADD_GROUP("Rayleigh", "rayleigh_");
  374. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rayleigh_coefficient", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_rayleigh_coefficient", "get_rayleigh_coefficient");
  375. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "rayleigh_color"), "set_rayleigh_color", "get_rayleigh_color");
  376. ADD_GROUP("Mie", "mie_");
  377. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mie_coefficient", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_mie_coefficient", "get_mie_coefficient");
  378. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mie_eccentricity", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_mie_eccentricity", "get_mie_eccentricity");
  379. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "mie_color"), "set_mie_color", "get_mie_color");
  380. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "turbidity", PROPERTY_HINT_RANGE, "0,1000,0.01"), "set_turbidity", "get_turbidity");
  381. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sun_disk_scale", PROPERTY_HINT_RANGE, "0,360,0.01"), "set_sun_disk_scale", "get_sun_disk_scale");
  382. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "ground_color"), "set_ground_color", "get_ground_color");
  383. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "exposure", PROPERTY_HINT_RANGE, "0,128,0.01"), "set_exposure", "get_exposure");
  384. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dither_strength", PROPERTY_HINT_RANGE, "0,10,0.01"), "set_dither_strength", "get_dither_strength");
  385. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "night_sky", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_night_sky", "get_night_sky");
  386. }
  387. PhysicalSkyMaterial::PhysicalSkyMaterial() {
  388. shader = RS::get_singleton()->shader_create();
  389. RS::get_singleton()->shader_set_code(shader, R"(
  390. shader_type sky;
  391. uniform float rayleigh : hint_range(0, 64) = 2.0;
  392. uniform vec4 rayleigh_color : hint_color = vec4(0.056, 0.14, 0.3, 1.0);
  393. uniform float mie : hint_range(0, 1) = 0.005;
  394. uniform float mie_eccentricity : hint_range(-1, 1) = 0.8;
  395. uniform vec4 mie_color : hint_color = vec4(0.36, 0.56, 0.82, 1.0);
  396. uniform float turbidity : hint_range(0, 1000) = 10.0;
  397. uniform float sun_disk_scale : hint_range(0, 360) = 1.0;
  398. uniform vec4 ground_color : hint_color = vec4(1.0);
  399. uniform float exposure : hint_range(0, 128) = 0.1;
  400. uniform float dither_strength : hint_range(0, 10) = 1.0;
  401. uniform sampler2D night_sky : hint_black;
  402. const vec3 UP = vec3( 0.0, 1.0, 0.0 );
  403. // Sun constants
  404. const float SUN_ENERGY = 1000.0;
  405. // Optical length at zenith for molecules.
  406. const float rayleigh_zenith_size = 8.4e3;
  407. const float mie_zenith_size = 1.25e3;
  408. float henyey_greenstein(float cos_theta, float g) {
  409. const float k = 0.0795774715459;
  410. return k * (1.0 - g * g) / (pow(1.0 + g * g - 2.0 * g * cos_theta, 1.5));
  411. }
  412. // From: https://www.shadertoy.com/view/4sfGzS credit to iq
  413. float hash(vec3 p) {
  414. p = fract( p * 0.3183099 + 0.1 );
  415. p *= 17.0;
  416. return fract(p.x * p.y * p.z * (p.x + p.y + p.z));
  417. }
  418. void sky() {
  419. if (LIGHT0_ENABLED) {
  420. float zenith_angle = clamp( dot(UP, normalize(LIGHT0_DIRECTION)), -1.0, 1.0 );
  421. float sun_energy = max(0.0, 1.0 - exp(-((PI * 0.5) - acos(zenith_angle)))) * SUN_ENERGY * LIGHT0_ENERGY;
  422. float sun_fade = 1.0 - clamp(1.0 - exp(LIGHT0_DIRECTION.y), 0.0, 1.0);
  423. // Rayleigh coefficients.
  424. float rayleigh_coefficient = rayleigh - ( 1.0 * ( 1.0 - sun_fade ) );
  425. vec3 rayleigh_beta = rayleigh_coefficient * rayleigh_color.rgb * 0.0001;
  426. // mie coefficients from Preetham
  427. vec3 mie_beta = turbidity * mie * mie_color.rgb * 0.000434;
  428. // Optical length.
  429. float zenith = acos(max(0.0, dot(UP, EYEDIR)));
  430. float optical_mass = 1.0 / (cos(zenith) + 0.15 * pow(93.885 - degrees(zenith), -1.253));
  431. float rayleigh_scatter = rayleigh_zenith_size * optical_mass;
  432. float mie_scatter = mie_zenith_size * optical_mass;
  433. // Light extinction based on thickness of atmosphere.
  434. vec3 extinction = exp(-(rayleigh_beta * rayleigh_scatter + mie_beta * mie_scatter));
  435. // In scattering.
  436. float cos_theta = dot(EYEDIR, normalize(LIGHT0_DIRECTION));
  437. float rayleigh_phase = (3.0 / (16.0 * PI)) * (1.0 + pow(cos_theta * 0.5 + 0.5, 2.0));
  438. vec3 betaRTheta = rayleigh_beta * rayleigh_phase;
  439. float mie_phase = henyey_greenstein(cos_theta, mie_eccentricity);
  440. vec3 betaMTheta = mie_beta * mie_phase;
  441. vec3 Lin = pow(sun_energy * ((betaRTheta + betaMTheta) / (rayleigh_beta + mie_beta)) * (1.0 - extinction), vec3(1.5));
  442. // Hack from https://github.com/mrdoob/three.js/blob/master/examples/jsm/objects/Sky.js
  443. Lin *= mix(vec3(1.0), pow(sun_energy * ((betaRTheta + betaMTheta) / (rayleigh_beta + mie_beta)) * extinction, vec3(0.5)), clamp(pow(1.0 - zenith_angle, 5.0), 0.0, 1.0));
  444. // Hack in the ground color.
  445. Lin *= mix(ground_color.rgb, vec3(1.0), smoothstep(-0.1, 0.1, dot(UP, EYEDIR)));
  446. // Solar disk and out-scattering.
  447. float sunAngularDiameterCos = cos(LIGHT0_SIZE * sun_disk_scale);
  448. float sunAngularDiameterCos2 = cos(LIGHT0_SIZE * sun_disk_scale*0.5);
  449. float sundisk = smoothstep(sunAngularDiameterCos, sunAngularDiameterCos2, cos_theta);
  450. vec3 L0 = (sun_energy * 1900.0 * extinction) * sundisk * LIGHT0_COLOR;
  451. L0 += texture(night_sky, SKY_COORDS).xyz * extinction;
  452. vec3 color = (Lin + L0) * 0.04;
  453. COLOR = pow(color, vec3(1.0 / (1.2 + (1.2 * sun_fade))));
  454. COLOR *= exposure;
  455. // Make optional, eliminates banding.
  456. COLOR += (hash(EYEDIR * 1741.9782) * 0.08 - 0.04) * 0.016 * dither_strength;
  457. } else {
  458. // There is no sun, so display night_sky and nothing else.
  459. COLOR = texture(night_sky, SKY_COORDS).xyz * 0.04;
  460. COLOR *= exposure;
  461. }
  462. }
  463. )");
  464. RS::get_singleton()->material_set_shader(_get_material(), shader);
  465. set_rayleigh_coefficient(2.0);
  466. set_rayleigh_color(Color(0.056, 0.14, 0.3));
  467. set_mie_coefficient(0.005);
  468. set_mie_eccentricity(0.8);
  469. set_mie_color(Color(0.36, 0.56, 0.82));
  470. set_turbidity(10.0);
  471. set_sun_disk_scale(1.0);
  472. set_ground_color(Color(1.0, 1.0, 1.0));
  473. set_exposure(0.1);
  474. set_dither_strength(1.0);
  475. }
  476. PhysicalSkyMaterial::~PhysicalSkyMaterial() {
  477. RS::get_singleton()->free(shader);
  478. }