Skip to content

Change Cloud Layer API #1237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ This example Cloud Map is a read-only **CustomRenderTexture**. This means that,

## Customizing the Cloud Map

The Cloud Map is a 2D texture in LatLong layout (sometimes called Cylindrical or Equirectangular) that contains cloud color in the RGB channel and cloud coverage in the alpha channel.
If **Upper Hemisphere Only** is checked, the map is interpreted as being the upper half of a LatLong texture.
The Cloud Map is a 2D texture in LatLong layout (sometimes called Cylindrical or Equirectangular) that contains cloud opacity in the red channel.
If **Upper Hemisphere Only** is checked, the map is interpreted as being the upper half of a LatLong texture. It means that it will only conver the sky above the horizon.

<a name="CustomizingFlowmap"></a>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Shader "Hidden/DefaultCloudLayer"

Pass
{
Name "Cloud Map"

CGPROGRAM

#include "UnityCustomRenderTexture.cginc"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ CustomRenderTexture:
m_AntiAliasing: 1
m_MipCount: -1
m_DepthFormat: 0
m_ColorFormat: 8
m_ColorFormat: 9
m_MipMap: 0
m_GenerateMips: 1
m_SRGB: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class CloudLayer : VolumeComponent
public ClampedFloatParameter scrollDirection = new ClampedFloatParameter(0.0f, 0.0f, 360.0f);
/// <summary>Speed of the distortion.</summary>
[Tooltip("Sets the cloud scrolling speed. The higher the value, the faster the clouds will move.")]
public MinFloatParameter scrollSpeed = new MinFloatParameter(2.0f, 0.0f);
public MinFloatParameter scrollSpeed = new MinFloatParameter(1.0f, 0.0f);


private float scrollFactor = 0.0f, lastTime = 0.0f;
Expand All @@ -60,9 +60,6 @@ public class CloudLayer : VolumeComponent
/// <returns>The shader parameters of the cloud layer.</returns>
public Vector4 GetParameters()
{
scrollFactor += scrollSpeed.value * (Time.time - lastTime) * 0.01f;
lastTime = Time.time;

float rot = -Mathf.Deg2Rad*scrollDirection.value;
float upper = upperHemisphereOnly.value ? 1.0f : -1.0f;
return new Vector4(upper * (rotation.value / 360.0f + 1), scrollFactor, Mathf.Cos(rot), Mathf.Sin(rot));
Expand All @@ -75,6 +72,9 @@ public static void Apply(CloudLayer layer, Material skyMaterial)
{
if (layer != null && layer.enabled.value == true)
{
layer.scrollFactor += layer.scrollSpeed.value * (Time.time - layer.lastTime) * 0.01f;
layer.lastTime = Time.time;

Vector4 cloudParam = layer.GetParameters();
Vector4 cloudParam2 = layer.tint.value;
cloudParam2.w = layer.intensityMultiplier.value;
Expand Down Expand Up @@ -112,33 +112,8 @@ public override int GetHashCode()

unchecked
{
#if UNITY_2019_3 // In 2019.3, when we call GetHashCode on a VolumeParameter it generate garbage (due to the boxing of the generic parameter)
hash = cloudMap.value != null ? hash * 23 + cloudMap.value.GetHashCode() : hash;
hash = flowmap.value != null ? hash * 23 + flowmap.value.GetHashCode() : hash;
hash = hash * 23 + enabled.value.GetHashCode();
hash = hash * 23 + upperHemisphereOnly.value.GetHashCode();
hash = hash * 23 + tint.value.GetHashCode();
hash = hash * 23 + intensityMultiplier.value.GetHashCode();
hash = hash * 23 + rotation.value.GetHashCode();
hash = hash * 23 + enableDistortion.value.GetHashCode();
hash = hash * 23 + procedural.value.GetHashCode();
hash = hash * 23 + scrollDirection.value.GetHashCode();
hash = hash * 23 + scrollSpeed.value.GetHashCode();

hash = cloudMap.value != null ? hash * 23 + cloudMap.overrideState.GetHashCode() : hash;
hash = flowmap.value != null ? hash * 23 + flowmap.overrideState.GetHashCode() : hash;
hash = hash * 23 + enabled.overrideState.GetHashCode();
hash = hash * 23 + upperHemisphereOnly.overrideState.GetHashCode();
hash = hash * 23 + tint.overrideState.GetHashCode();
hash = hash * 23 + intensityMultiplier.overrideState.GetHashCode();
hash = hash * 23 + rotation.overrideState.GetHashCode();
hash = hash * 23 + enableDistortion.overrideState.GetHashCode();
hash = hash * 23 + procedural.overrideState.GetHashCode();
hash = hash * 23 + scrollDirection.overrideState.GetHashCode();
hash = hash * 23 + scrollSpeed.overrideState.GetHashCode();
#else
hash = cloudMap.value != null ? hash * 23 + cloudMap.GetHashCode() : hash;
hash = flowmap.value != null ? hash * 23 + flowmap.GetHashCode() : hash;
hash = hash * 23 + cloudMap.GetHashCode();
hash = hash * 23 + flowmap.GetHashCode();
hash = hash * 23 + enabled.GetHashCode();
hash = hash * 23 + upperHemisphereOnly.GetHashCode();
hash = hash * 23 + tint.GetHashCode();
Expand All @@ -148,7 +123,6 @@ public override int GetHashCode()
hash = hash * 23 + procedural.GetHashCode();
hash = hash * 23 + scrollDirection.GetHashCode();
hash = hash * 23 + scrollSpeed.GetHashCode();
#endif
}

return hash;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ float4 _CloudParam2; // xyz tint, w intensity

#define USE_CLOUD_LAYER defined(USE_CLOUD_MAP) || (!defined(USE_CLOUD_MAP) && defined(USE_CLOUD_MOTION))

float3 sampleCloud(float3 dir, float3 sky)
float4 sampleCloud(float3 dir)
{
float2 coords = GetLatLongCoords(dir, _CloudUpperHemisphere);
coords.x = frac(coords.x + _CloudRotation);
float4 cloudLayerColor = SAMPLE_TEXTURE2D_LOD(_CloudMap, sampler_CloudMap, coords, 0);
return lerp(sky, sky + cloudLayerColor.rgb * _CloudTint * _CloudIntensity, cloudLayerColor.a);
float4 cloudLayerColor = SAMPLE_TEXTURE2D_LOD(_CloudMap, sampler_CloudMap, coords, 0).r;
cloudLayerColor.rgb *= _CloudTint * _CloudIntensity * cloudLayerColor.a;
return cloudLayerColor;
}

float3 CloudRotationUp(float3 p, float2 cos_sin)
Expand All @@ -35,46 +36,53 @@ float3 CloudRotationUp(float3 p, float2 cos_sin)
return float3(dot(rotDirX, p), p.y, dot(rotDirY, p));
}

float3 GetDistordedCloudColor(float3 dir, float3 sky)
float4 GetDistordedCloudColor(float3 dir)
{
#if USE_CLOUD_MOTION
if (dir.y >= 0 || !_CloudUpperHemisphere)
{
float2 alpha = frac(float2(_CloudScrollFactor, _CloudScrollFactor + 0.5)) - 0.5;
float2 alpha = frac(float2(_CloudScrollFactor, _CloudScrollFactor + 0.5)) - 0.5;

#ifdef USE_CLOUD_MAP
float3 tangent = normalize(cross(dir, float3(0.0, 1.0, 0.0)));
float3 bitangent = cross(tangent, dir);
float3 tangent = normalize(cross(dir, float3(0.0, 1.0, 0.0)));
float3 bitangent = cross(tangent, dir);

float3 windDir = CloudRotationUp(dir, _CloudScrollDirection);
float2 flow = SAMPLE_TEXTURE2D_LOD(_CloudFlowmap, sampler_CloudFlowmap, GetLatLongCoords(windDir, _CloudUpperHemisphere), 0).rg * 2.0 - 1.0;
float3 windDir = CloudRotationUp(dir, _CloudScrollDirection);
float2 flow = SAMPLE_TEXTURE2D_LOD(_CloudFlowmap, sampler_CloudFlowmap, GetLatLongCoords(windDir, _CloudUpperHemisphere), 0).rg * 2.0 - 1.0;

float3 dd = flow.x * tangent + flow.y * bitangent;
float3 dd = flow.x * tangent + flow.y * bitangent;
#else
float3 windDir = CloudRotationUp(float3(0, 0, 1), _CloudScrollDirection);
windDir.x *= -1.0;
float3 dd = windDir*sin(dir.y*PI*0.5);
float3 windDir = CloudRotationUp(float3(0, 0, 1), _CloudScrollDirection);
windDir.x *= -1.0;
float3 dd = windDir*sin(dir.y*PI*0.5);
#endif

// Sample twice
float3 color1 = sampleCloud(normalize(dir - alpha.x * dd), sky);
float3 color2 = sampleCloud(normalize(dir - alpha.y * dd), sky);
// Sample twice
float4 color1 = sampleCloud(normalize(dir - alpha.x * dd));
float4 color2 = sampleCloud(normalize(dir - alpha.y * dd));

// Blend color samples
sky = lerp(color1, color2, abs(2.0 * alpha.x));
}
return sky;
// Blend color samples
return lerp(color1, color2, abs(2.0 * alpha.x));

#else
return sampleCloud(dir, sky);
return sampleCloud(dir);
#endif
}

float GetCloudOpacity(float3 dir)
{
#if USE_CLOUD_LAYER
if (dir.y >= 0 || !_CloudUpperHemisphere)
return GetDistordedCloudColor(dir).a;
else
#endif

return 0;
}

float3 ApplyCloudLayer(float3 dir, float3 sky)
{
#if USE_CLOUD_LAYER
if (dir.y >= 0 || !_CloudUpperHemisphere)
sky = GetDistordedCloudColor(dir, sky);
sky += GetDistordedCloudColor(dir).rgb;
#endif

return sky;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ int ComputeSkyHash(HDCamera camera, SkyUpdateContext skyContext, Light sunLight,
sunHash = GetSunLightHashCode(sunLight);

int cloudHash = 0;
if (skyContext.cloudLayer != null && skyContext.skyRenderer.SupportCloudLayer)
if (skyContext.cloudLayer != null && skyContext.skyRenderer.SupportDynamicCloudLayer)
cloudHash = skyContext.cloudLayer.GetHashCode();

// For planar reflections we want to use the parent position for hash.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public abstract class SkyRenderer
/// <summary>Determines if the sky should be rendered when the sun light changes.</summary>
public bool SupportDynamicSunLight = true;
/// <summary>Determines if the sky should be rendered when the cloud layer changes.</summary>
public bool SupportCloudLayer = true;
public bool SupportDynamicCloudLayer = true;

/// <summary>
/// Called on startup. Create resources used by the renderer (shaders, materials, etc).
Expand Down