From 4408efade3211cf7b0dfff2a5f15b6c2f0be397b Mon Sep 17 00:00:00 2001 From: clayjohn Date: Sat, 16 May 2020 12:55:25 -0700 Subject: [PATCH] Add night sky to PhysicalSkyMaterial --- doc/classes/PhysicalSkyMaterial.xml | 3 +++ scene/resources/sky_material.cpp | 17 ++++++++++++++++- scene/resources/sky_material.h | 4 ++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/doc/classes/PhysicalSkyMaterial.xml b/doc/classes/PhysicalSkyMaterial.xml index 89b43158dca..2e0f9c52f28 100644 --- a/doc/classes/PhysicalSkyMaterial.xml +++ b/doc/classes/PhysicalSkyMaterial.xml @@ -31,6 +31,9 @@ Controls the direction of the mie scattering. A value of [code]1[/code] means that when light hits a particle it passing through straight forward. A value of [code]-1[/code] means that all light is scatter backwards. + + [Texture2D] for the night sky. This is added to the sky, so if it is bright enough, it may be visible during the day. + Controls the strength of the rayleigh scattering. Rayleigh scattering results from light colliding with small particles. It is responsible for the blue color of the sky. diff --git a/scene/resources/sky_material.cpp b/scene/resources/sky_material.cpp index 92c5151d7a0..69e8e0b5bd9 100644 --- a/scene/resources/sky_material.cpp +++ b/scene/resources/sky_material.cpp @@ -410,6 +410,15 @@ float PhysicalSkyMaterial::get_dither_strength() const { return dither_strength; } +void PhysicalSkyMaterial::set_night_sky(const Ref &p_night_sky) { + night_sky = p_night_sky; + RS::get_singleton()->material_set_param(_get_material(), "night_sky", night_sky); +} + +Ref PhysicalSkyMaterial::get_night_sky() const { + return night_sky; +} + bool PhysicalSkyMaterial::_can_do_next_pass() const { return false; } @@ -453,6 +462,9 @@ void PhysicalSkyMaterial::_bind_methods() { ClassDB::bind_method(D_METHOD("set_dither_strength", "strength"), &PhysicalSkyMaterial::set_dither_strength); ClassDB::bind_method(D_METHOD("get_dither_strength"), &PhysicalSkyMaterial::get_dither_strength); + ClassDB::bind_method(D_METHOD("set_night_sky", "night_sky"), &PhysicalSkyMaterial::set_night_sky); + ClassDB::bind_method(D_METHOD("get_night_sky"), &PhysicalSkyMaterial::get_night_sky); + ADD_GROUP("Rayleigh", "rayleigh_"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rayleigh_coefficient", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_rayleigh_coefficient", "get_rayleigh_coefficient"); ADD_PROPERTY(PropertyInfo(Variant::COLOR, "rayleigh_color"), "set_rayleigh_color", "get_rayleigh_color"); @@ -467,6 +479,7 @@ void PhysicalSkyMaterial::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::COLOR, "ground_color"), "set_ground_color", "get_ground_color"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "exposure", PROPERTY_HINT_RANGE, "0,128,0.01"), "set_exposure", "get_exposure"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dither_strength", PROPERTY_HINT_RANGE, "0,10,0.01"), "set_dither_strength", "get_dither_strength"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "night_sky", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_night_sky", "get_night_sky"); } PhysicalSkyMaterial::PhysicalSkyMaterial() { @@ -484,6 +497,8 @@ PhysicalSkyMaterial::PhysicalSkyMaterial() { code += "uniform float exposure : hint_range(0, 128) = 0.1;\n"; code += "uniform float dither_strength : hint_range(0, 10) = 1.0;\n\n"; + code += "uniform sampler2D night_sky : hint_black;"; + code += "const float PI = 3.141592653589793238462643383279502884197169;\n"; code += "const vec3 UP = vec3( 0.0, 1.0, 0.0 );\n\n"; @@ -547,7 +562,7 @@ PhysicalSkyMaterial::PhysicalSkyMaterial() { code += "\tfloat sunAngularDiameterCos2 = cos(LIGHT0_SIZE * sun_disk_scale*0.5);\n"; code += "\tfloat sundisk = smoothstep(sunAngularDiameterCos, sunAngularDiameterCos2, cos_theta);\n"; code += "\tvec3 L0 = (sun_energy * 1900.0 * extinction) * sundisk * LIGHT0_COLOR;\n"; - code += "\t// Note: Add nightime here: L0 += night_sky * extinction\n\n"; + code += "\tL0 += texture(night_sky, SKY_COORDS).xyz * extinction;\n\n"; code += "\tvec3 color = (Lin + L0) * 0.04;\n"; code += "\tCOLOR = pow(color, vec3(1.0 / (1.2 + (1.2 * sun_fade))));\n"; diff --git a/scene/resources/sky_material.h b/scene/resources/sky_material.h index cd245a28979..e470137d9e1 100644 --- a/scene/resources/sky_material.h +++ b/scene/resources/sky_material.h @@ -139,6 +139,7 @@ private: Color ground_color; float exposure; float dither_strength; + Ref night_sky; protected: static void _bind_methods(); @@ -175,6 +176,9 @@ public: void set_dither_strength(float p_dither_strength); float get_dither_strength() const; + void set_night_sky(const Ref &p_night_sky); + Ref get_night_sky() const; + virtual Shader::Mode get_shader_mode() const; RID get_shader_rid() const;