Skip to content

Commit

Permalink
Traktor: Able to specify cloud colors.
Browse files Browse the repository at this point in the history
  • Loading branch information
apistol78 committed Jun 11, 2024
1 parent 506719f commit 10d9313
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 106 deletions.
34 changes: 17 additions & 17 deletions code/Weather/Sky/SkyComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const render::Handle s_handleWeather_SkyEyePosition(L"Weather_SkyEyePosition");
const render::Handle s_handleWeather_SkyCloudTexture(L"Weather_SkyCloudTexture");
const render::Handle s_handleWeather_SkyCloudTextureLast(L"Weather_SkyCloudTextureLast");
const render::Handle s_handleWeather_SkyTemporalBlend(L"Weather_SkyTemporalBlend");
const render::Handle s_handleWeather_SkyCloudAmbientTop(L"Weather_SkyCloudAmbientTop");
const render::Handle s_handleWeather_SkyCloudAmbientBottom(L"Weather_SkyCloudAmbientBottom");
const render::Handle s_handleWeather_InputTexture(L"Weather_InputTexture");
const render::Handle s_handleWeather_OutputTexture(L"Weather_OutputTexture");

Expand All @@ -65,20 +67,14 @@ const int32_t c_indexCount = c_triangleCount * 3;
T_IMPLEMENT_RTTI_CLASS(L"traktor.weather.SkyComponent", SkyComponent, world::IEntityComponent)

SkyComponent::SkyComponent(
const SkyComponentData& data,
const resource::Proxy< render::Shader >& shader,
const resource::Proxy< render::ITexture >& texture,
float intensity,
bool clouds,
const Color4f& overHorizon,
const Color4f& underHorizon
const resource::Proxy< render::ITexture >& texture
)
: m_shader(shader)
: m_data(data)
, m_shader(shader)
, m_texture(texture)
, m_transform(Transform::identity())
, m_intensity(intensity)
, m_clouds(clouds)
, m_overHorizon(overHorizon)
, m_underHorizon(underHorizon)
{
}

Expand Down Expand Up @@ -149,7 +145,7 @@ bool SkyComponent::create(resource::IResourceManager* resourceManager, render::I
c_vertexCount - 1
);

if (m_clouds)
if (m_data.m_clouds)
{
render::SimpleTextureCreateDesc stcd = {};
render::VolumeTextureCreateDesc vtcd = {};
Expand Down Expand Up @@ -237,7 +233,7 @@ void SkyComponent::setup(
{
render::RenderGraph& renderGraph = context.getRenderGraph();

if (m_clouds)
if (m_data.m_clouds)
{
if (m_shaderClouds2D.changed() || m_shaderClouds3D.changed())
{
Expand Down Expand Up @@ -325,6 +321,8 @@ void SkyComponent::setup(
renderBlock->programParams->setFloatParameter(s_handleWeather_SkyRadius, worldRenderView.getViewFrustum().getFarZ() - 10.0f);
renderBlock->programParams->setVectorParameter(s_handleWeather_SkySunColor, sunColor);
renderBlock->programParams->setVectorParameter(s_handleWeather_SkySunDirection, sunDirection);
renderBlock->programParams->setVectorParameter(s_handleWeather_SkyCloudAmbientTop, m_data.m_cloudAmbientTop);
renderBlock->programParams->setVectorParameter(s_handleWeather_SkyCloudAmbientBottom, m_data.m_cloudAmbientBottom);
renderBlock->programParams->setFloatParameter(s_handleWeather_SkyTemporalBlend, (worldRenderView.getSnapshot() || m_cloudFrame == 0) ? 1.0f : 0.2f);
renderBlock->programParams->setFloatParameter(world::s_handleTime, worldRenderView.getTime());
renderBlock->programParams->endParameters(renderContext);
Expand All @@ -349,7 +347,7 @@ void SkyComponent::build(
)
{
auto perm = worldRenderPass.getPermutation(m_shader);
m_shader->setCombination(c_handleWeather_CloudsEnable, m_clouds, perm);
m_shader->setCombination(c_handleWeather_CloudsEnable, m_data.m_clouds, perm);
auto sp = m_shader->getProgram(perm);
if (!sp)
return;
Expand Down Expand Up @@ -386,15 +384,17 @@ void SkyComponent::build(
worldRenderPass.setProgramParameters(renderBlock->programParams);
renderBlock->programParams->setFloatParameter(s_handleWeather_SkyRadius, worldRenderView.getViewFrustum().getFarZ() - 10.0f);
renderBlock->programParams->setFloatParameter(s_handleWeather_SkyRotation, rotation);
renderBlock->programParams->setFloatParameter(s_handleWeather_SkyIntensity, m_intensity);
renderBlock->programParams->setFloatParameter(s_handleWeather_SkyIntensity, m_data.m_intensity);
renderBlock->programParams->setVectorParameter(s_handleWeather_SkySunDirection, sunDirection);
renderBlock->programParams->setVectorParameter(s_handleWeather_SkySunColor, sunColor);
renderBlock->programParams->setVectorParameter(s_handleWeather_SkyOverHorizon, m_overHorizon);
renderBlock->programParams->setVectorParameter(s_handleWeather_SkyUnderHorizon, m_underHorizon);
renderBlock->programParams->setVectorParameter(s_handleWeather_SkyOverHorizon, m_data.m_skyOverHorizon);
renderBlock->programParams->setVectorParameter(s_handleWeather_SkyUnderHorizon, m_data.m_skyUnderHorizon);
renderBlock->programParams->setVectorParameter(s_handleWeather_SkyCloudAmbientTop, m_data.m_cloudAmbientTop);
renderBlock->programParams->setVectorParameter(s_handleWeather_SkyCloudAmbientBottom, m_data.m_cloudAmbientBottom);
renderBlock->programParams->setVectorParameter(s_handleWeather_SkyEyePosition, eyePosition);
renderBlock->programParams->setTextureParameter(s_handleWeather_SkyTexture, m_texture);

if (m_clouds)
if (m_data.m_clouds)
{
renderBlock->programParams->setFloatParameter(s_handleWeather_SkyCloudBlend, m_cloudBlend);
renderBlock->programParams->setTextureParameter(s_handleWeather_SkyCloud2D, m_cloudTextures[0]);
Expand Down
13 changes: 4 additions & 9 deletions code/Weather/Sky/SkyComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "Render/ITexture.h"
#include "Render/Shader.h"
#include "Resource/Proxy.h"
#include "Weather/Sky/SkyComponentData.h"
#include "World/IEntityComponent.h"

// import/export mechanism.
Expand Down Expand Up @@ -59,12 +60,9 @@ class T_DLLCLASS SkyComponent : public world::IEntityComponent

public:
explicit SkyComponent(
const SkyComponentData& data,
const resource::Proxy< render::Shader >& shader,
const resource::Proxy< render::ITexture >& texture,
float intensity,
bool clouds,
const Color4f& overHorizon,
const Color4f& underHorizon
const resource::Proxy< render::ITexture >& texture
);

virtual ~SkyComponent();
Expand Down Expand Up @@ -93,6 +91,7 @@ class T_DLLCLASS SkyComponent : public world::IEntityComponent
);

private:
const SkyComponentData m_data;
Ref< const render::IVertexLayout > m_vertexLayout;
Ref< render::Buffer > m_vertexBuffer;
Ref< render::Buffer > m_indexBuffer;
Expand All @@ -106,10 +105,6 @@ class T_DLLCLASS SkyComponent : public world::IEntityComponent
Ref< render::ITexture > m_cloudDomeTexture[2];
world::Entity* m_owner = nullptr;
Transform m_transform;
float m_intensity = 1.0f;
bool m_clouds = true;
Color4f m_overHorizon = Color4f(0.2f, 0.5f, 0.85f, 0.0f);
Color4f m_underHorizon = Color4f(0.1f, 0.1f, 0.12f, 0.0f);
int32_t m_count = 0;
int32_t m_cloudFrame = 0;
float m_cloudBlend = 0.0f;
Expand Down
34 changes: 22 additions & 12 deletions code/Weather/Sky/SkyComponentData.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/*
* TRAKTOR
* Copyright (c) 2022-2023 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <cmath>
#include "Core/Math/Const.h"
#include "Core/Serialization/AttributeHdr.h"
#include "Core/Serialization/AttributePrivate.h"
#include "Core/Serialization/AttributeRange.h"
#include "Core/Serialization/AttributeUnit.h"
Expand All @@ -29,7 +30,7 @@ const resource::Id< render::Shader > c_defaultShader(Guid(L"{4CF929EB-3A8B-C340-

}

T_IMPLEMENT_RTTI_EDIT_CLASS(L"traktor.weather.SkyComponentData", 6, SkyComponentData, world::IEntityComponentData)
T_IMPLEMENT_RTTI_EDIT_CLASS(L"traktor.weather.SkyComponentData", 7, SkyComponentData, world::IEntityComponentData)

SkyComponentData::SkyComponentData()
: m_shader(c_defaultShader)
Expand All @@ -50,12 +51,9 @@ Ref< SkyComponent > SkyComponentData::createComponent(resource::IResourceManager
}

Ref< SkyComponent > skyComponent = new SkyComponent(
*this,
shader,
texture,
m_intensity,
m_clouds,
m_overHorizon,
m_underHorizon
texture
);
skyComponent->create(resourceManager, renderSystem);
return skyComponent;
Expand Down Expand Up @@ -92,13 +90,25 @@ void SkyComponentData::serialize(ISerializer& s)
if (s.getVersion< SkyComponentData >() >= 4)
s >> Member< float >(L"intensity", m_intensity, AttributeRange(0.0f) | AttributeUnit(UnitType::Percent));

if (s.getVersion< SkyComponentData >() >= 5)
s >> Member< bool >(L"clouds", m_clouds);
if (s.getVersion< SkyComponentData >() >= 7)
{
s >> Member< Color4f >(L"skyOverHorizon", m_skyOverHorizon, AttributeHdr());
s >> Member< Color4f >(L"skyUnderHorizon", m_skyUnderHorizon, AttributeHdr());

if (s.getVersion< SkyComponentData >() >= 6)
s >> Member< bool >(L"clouds", m_clouds);
s >> Member< Color4f >(L"cloudAmbientTop", m_cloudAmbientTop, AttributeHdr());
s >> Member< Color4f >(L"cloudAmbientBottom", m_cloudAmbientBottom, AttributeHdr());
}
else
{
s >> Member< Color4f >(L"overHorizon", m_overHorizon);
s >> Member< Color4f >(L"underHorizon", m_underHorizon);
if (s.getVersion< SkyComponentData >() >= 5)
s >> Member< bool >(L"clouds", m_clouds);

if (s.getVersion< SkyComponentData >() == 6)
{
s >> Member< Color4f >(L"overHorizon", m_skyOverHorizon);
s >> Member< Color4f >(L"underHorizon", m_skyUnderHorizon);
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions code/Weather/Sky/SkyComponentData.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,18 @@ class T_DLLCLASS SkyComponentData : public world::IEntityComponentData
float getIntensity() const { return m_intensity; }

private:
friend class SkyComponent;

resource::Id< render::Shader > m_shader;
resource::Id< render::ITexture > m_texture;
float m_intensity = 1.0f;

Color4f m_skyOverHorizon = Color4f(0.2f, 0.5f, 0.85f, 1.0f);
Color4f m_skyUnderHorizon = Color4f(0.1f, 0.1f, 0.12f, 1.0f);

bool m_clouds = true;
Color4f m_overHorizon = Color4f(0.2f, 0.5f, 0.85f, 0.0f);
Color4f m_underHorizon = Color4f(0.1f, 0.1f, 0.12f, 0.0f);
Color4f m_cloudAmbientTop = Color4f(0.99f, 0.98f, 1.18f, 1.0f);
Color4f m_cloudAmbientBottom = Color4f(0.23f, 0.39f, 0.51f, 1.0f);
};

}
39 changes: 23 additions & 16 deletions data/Source/System/Weather/Sky/Modules/Sky.xdi
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,9 @@
vec3 GetSkyColor(vec3 sunDirection, vec3 sunColor, vec3 overHorizon, vec3 underHorizon, vec3 rd)
{
const float sundot = clamp(dot(rd, sunDirection), 0.0f, 1.0f);
// Over horizon
vec3 col = overHorizon - max(rd.y, 0.01f) * max(rd.y, 0.01f) * 0.5f;
// Under horizon
col = mix(col, underHorizon, pow(1.0f - max(rd.y, 0.0f), 6.0f));
// Add sun
col += sunColor * pow(max(sundot - 0.1, 0), 8.0f);
// col += clamp((0.1 - rd.y) * 10.0f, 0.0f, 1.0f) * underHorizon;
return col;
}
Expand Down Expand Up @@ -149,7 +141,21 @@ float volumetricShadow(vec3 from, vec3 offset, float sundotrd, float time, vec3
return shadow;
}
vec4 renderClouds(vec3 ro, vec3 rd, float time, vec3 sunDirection, vec3 sunColor, vec2 worldOffset, texture2D clouds2D, texture3D clouds3D, sampler cloudSampler, inout float dist)
struct CloudConfig
{
vec3 ambientTop;
vec3 ambientBottom;
};
CloudConfig GetDefaultCloudConfig()
{
CloudConfig c;
c.ambientTop = CLOUDS_AMBIENT_COLOR_TOP;
c.ambientBottom = CLOUDS_AMBIENT_COLOR_BOTTOM;
return c;
}
vec4 RenderClouds(const CloudConfig config, vec3 ro, vec3 rd, float time, vec3 sunDirection, vec3 sunColor, vec2 worldOffset, texture2D clouds2D, texture3D clouds3D, sampler cloudSampler, inout float dist)
{
if (rd.y < 0.0f)
return vec4(0.0f, 0.0f, 0.0f, 1.0f);
Expand Down Expand Up @@ -188,19 +194,20 @@ vec4 renderClouds(vec3 ro, vec3 rd, float time, vec3 sunDirection, vec3 sunColor
for (int s = 0; s < CLOUD_MARCH_STEPS; s++)
{
vec3 p = ro + d * rd;
const vec3 p = ro + d * rd;
float norY = clamp((length(p) - (EARTH_RADIUS + CLOUDS_BOTTOM)) * (1.0f / (CLOUDS_TOP - CLOUDS_BOTTOM)), 0.0f, 1.0f);
float alpha = cloudMap( p, offset, rd, norY, time, clouds2D, clouds3D, cloudSampler );
const float norY = clamp((length(p) - (EARTH_RADIUS + CLOUDS_BOTTOM)) * (1.0f / (CLOUDS_TOP - CLOUDS_BOTTOM)), 0.0f, 1.0f);
const float alpha = cloudMap( p, offset, rd, norY, time, clouds2D, clouds3D, cloudSampler );
if (alpha > 0.0f)
{
dist = min( dist, d);
vec3 ambientLight = mix( CLOUDS_AMBIENT_COLOR_BOTTOM, CLOUDS_AMBIENT_COLOR_TOP, norY );
const vec3 ambientLight = mix(config.ambientBottom, config.ambientTop, norY);
const vec3 S = (ambientLight + sunColor * (scattering * volumetricShadow(p, offset, sundotrd, time, sunDirection, clouds2D, clouds3D, cloudSampler))) * alpha;
const float dTrans = exp(-alpha * dD);
const vec3 Sint = (S - S * dTrans) * (1. / alpha);
vec3 S = (ambientLight + sunColor * (scattering * volumetricShadow(p, offset, sundotrd, time, sunDirection, clouds2D, clouds3D, cloudSampler))) * alpha;
float dTrans = exp(-alpha * dD);
vec3 Sint = (S - S * dTrans) * (1. / alpha);
scatteredLight += transmittance * Sint;
transmittance *= dTrans;
}
Expand Down
Loading

0 comments on commit 10d9313

Please sign in to comment.