3#include <Tempest/Application>
4#include <Tempest/CommandBuffer>
5#include <Tempest/Platform>
6#include <Tempest/Fence>
15using namespace Tempest;
38static float smoothstep(
float edge0,
float edge1,
float x) {
39 float t = std::min(std::max((x - edge0) / (edge1 - edge0), 0.f), 1.f);
40 return t * t * (3.f - 2.f * t);
43static float linearstep(
float edge0,
float edge1,
float x) {
44 float t = std::min(std::max((x - edge0) / (edge1 - edge0), 0.f), 1.f);
50 auto wname = world.
name();
51 auto dot = wname.rfind(
'.');
52 auto name = dot==std::string::npos ? wname : wname.substr(0,dot);
53 for(
size_t i=0; i<2; ++i) {
54 clouds[0].
lay[i] = skyTexture(name,
true, i);
55 clouds[1].
lay[i] = skyTexture(name,
false,i);
90 const float shadowLength = 0.56f;
93 if(rise<=now && now<meridian){
94 pulse = 0.f + float(now-rise)/float(meridian-rise);
96 else if(meridian<=now && now<set){
97 pulse = 1.f - float(now-meridian)/float(set-meridian);
100 pulse = 0.f - float(now-set)/float(midnight-set);
103 pulse = -1.f + (float(now)/float(rise));
116 float k = float(now)/float(midnight);
117 float ax = 360-360*std::fmod(k+0.25f,1.f);
118 ax = ax*float(M_PI/180.0);
119 sun.
setDir(-std::sin(ax)*shadowLength, pulse, std::cos(ax)*shadowLength);
123 static float sunMul = 1;
124 static float ambMul = 1;
127 static auto groundAlbedo = Vec3(0.3f);
133 const float sunOcclude =
smoothstep(0.0f, 0.01f, sun.
dir().y);
138 ambient =
DirectSunLux * float(1.0/M_PI) * groundAlbedo * sunOcclude;
143 ambient *= 2.0*float(M_PI);
145 ambient *= float(1.0/M_PI);
150 ambient = ambient*ambMul;
155 auto t0 = float(ticks%90000 )/90000.f;
156 auto t1 = float(ticks%270000)/270000.f;
165const Texture2d* Sky::skyTexture(std::string_view name,
bool day,
size_t id) {
166 if(
auto t = implSkyTexture(name,day,
id))
168 if(
auto t = implSkyTexture(
"",day,
id))
173const Texture2d* Sky::implSkyTexture(std::string_view name,
bool day,
size_t id) {
177 tex =
string_frm(
"SKYDAY_LAYER",
int(
id),
"_A0.TGA");
else
178 tex =
string_frm(
"SKYNIGHT_LAYER",
int(
id),
"_A0.TGA");
181 tex =
string_frm(
"SKYDAY_", name,
"_LAYER",
int(
id),
"_A0.TGA");
else
182 tex =
string_frm(
"SKYNIGHT_",name,
"_LAYER",
int(
id),
"_A0.TGA");
185 i = char(std::toupper(i));
static std::string_view settingsGetS(std::string_view sec, std::string_view name)
void setDir(const Tempest::Vec3 &d)
Tempest::Vec3 dir() const
void setColor(const Tempest::Vec3 &cl)
static const Tempest::Texture2d & fallbackTexture()
static const Tempest::Texture2d * loadTexture(std::string_view name, bool forceMips=false)
static const Tempest::Texture2d & fallbackBlack()
Tempest::Vec2 cloudsOffset(int layer) const
Sky(const SceneGlobals &scene, const World &world)
void updateLight(const int64_t now)
std::string_view name() const
static float linearstep(float edge0, float edge1, float x)
static float smoothstep(float edge0, float edge1, float x)
static const float DirectMoonLux
static float linearstep(float edge0, float edge1, float x)
static float smoothstep(float edge0, float edge1, float x)
static const float DirectSunLux
const Tempest::Texture2d * lay[2]