Skip to content

Commit

Permalink
Revert to old behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-de-tocqueville committed Apr 27, 2020
1 parent d880e05 commit b3c0fc6
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ class HDRISkyEditor
SerializedDataParameter m_hdriSky;
SerializedDataParameter m_UpperHemisphereLuxValue;
SerializedDataParameter m_UpperHemisphereLuxColor;
SerializedDataParameter m_EnableDistortion;

SerializedDataParameter m_EnableCloudMotion;
SerializedDataParameter m_Procedural;
SerializedDataParameter m_Flowmap;
SerializedDataParameter m_RotationDistortion;
SerializedDataParameter m_LoopTime;
SerializedDataParameter m_Amplitude;
SerializedDataParameter m_UpperHemisphereOnly;
SerializedDataParameter m_ScrollDirection;
SerializedDataParameter m_ScrollSpeed;

SerializedDataParameter m_EnableBackplate;
SerializedDataParameter m_BackplateType;
SerializedDataParameter m_GroundLevel;
Expand Down Expand Up @@ -52,12 +54,12 @@ public override void OnEnable()
m_UpperHemisphereLuxValue = Unpack(o.Find(x => x.upperHemisphereLuxValue));
m_UpperHemisphereLuxColor = Unpack(o.Find(x => x.upperHemisphereLuxColor));

m_EnableDistortion = Unpack(o.Find(x => x.enableDistortion));
m_EnableCloudMotion = Unpack(o.Find(x => x.enableDistortion));
m_Procedural = Unpack(o.Find(x => x.procedural));
m_Flowmap = Unpack(o.Find(x => x.flowmap));
m_RotationDistortion = Unpack(o.Find(x => x.rotationDistortion));
m_LoopTime = Unpack(o.Find(x => x.loopTime));
m_Amplitude = Unpack(o.Find(x => x.amplitude));
m_UpperHemisphereOnly = Unpack(o.Find(x => x.upperHemisphereOnly));
m_ScrollDirection = Unpack(o.Find(x => x.scrollDirection));
m_ScrollSpeed = Unpack(o.Find(x => x.scrollSpeed));

m_EnableBackplate = Unpack(o.Find(x => x.enableBackplate));
m_BackplateType = Unpack(o.Find(x => x.backplateType));
Expand Down Expand Up @@ -129,20 +131,22 @@ public override void OnInspectorGUI()
updateDefaultShadowTint = true;
}

PropertyField(m_EnableDistortion, new GUIContent("Distortion"));
if (m_EnableDistortion.value.boolValue)
PropertyField(m_EnableCloudMotion);
if (m_EnableCloudMotion.value.boolValue)
{
EditorGUI.indentLevel++;
PropertyField(m_Procedural, new GUIContent("Procedural"));

PropertyField(m_Procedural, new GUIContent("Procedural distortion"));
if (!m_Procedural.value.boolValue)
{
EditorGUI.indentLevel++;
PropertyField(m_Flowmap);
PropertyField(m_UpperHemisphereOnly);
EditorGUI.indentLevel--;
}
PropertyField(m_RotationDistortion, new GUIContent("Distortion Rotation"));
PropertyField(m_LoopTime);
PropertyField(m_Amplitude);

PropertyField(m_ScrollDirection);
PropertyField(m_ScrollSpeed);
EditorGUI.indentLevel--;
}
base.CommonSkySettingsGUI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,6 @@ static class HDShaderIDs
public static readonly int _ThicknessRemap = Shader.PropertyToID("_ThicknessRemap");

public static readonly int _Cubemap = Shader.PropertyToID("_Cubemap");
public static readonly int _Flowmap = Shader.PropertyToID("_Flowmap");
public static readonly int _InvOmegaP = Shader.PropertyToID("_InvOmegaP");
public static readonly int _DistortionParam = Shader.PropertyToID("_DistortionParam");
public static readonly int _SkyParam = Shader.PropertyToID("_SkyParam");
Expand All @@ -489,6 +488,9 @@ static class HDShaderIDs
public static readonly int _SkyIntensity = Shader.PropertyToID("_SkyIntensity");
public static readonly int _PixelCoordToViewDirWS = Shader.PropertyToID("_PixelCoordToViewDirWS");

public static readonly int _Flowmap = Shader.PropertyToID("_Flowmap");
public static readonly int _FlowmapParam = Shader.PropertyToID("_FlowmapParam");

public static readonly int _Size = Shader.PropertyToID("_Size");
public static readonly int _Source = Shader.PropertyToID("_Source");
public static readonly int _Destination = Shader.PropertyToID("_Destination");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@ public class HDRISky : SkySettings
/// <summary>Cubemap used to render the HDRI sky.</summary>
[Tooltip("Specify the cubemap HDRP uses to render the sky.")]
public CubemapParameter hdriSky = new CubemapParameter(null);
/// <summary>Enable Flowmap to have distorsion.</summary>
[Tooltip("Enable or disable UV distortion.")]

/// <summary>Enable to have sky distortion.</summary>
[Tooltip("Enable or disable sky distortion.")]
public BoolParameter enableDistortion = new BoolParameter(false);
/// <summary>Enable Flowmap to have distorsion.</summary>
[Tooltip("Enable or disable procedural distorsion.")]
/// <summary>Enable to have a simple, procedural distorsion.</summary>
[Tooltip("If enabled, the sky will be distorted by a constant wind.")]
public BoolParameter procedural = new BoolParameter(true);
/// <summary>Cubemap used to distort the uv for the HDRI sky.</summary>
[Tooltip("Specify the cubemap HDRP uses for UV distortion.")]
public CubemapParameter flowmap = new CubemapParameter(null);
/// <summary>Rotation of the distortion.</summary>
[Tooltip("Sets the rotation of the distortion.")]
public ClampedFloatParameter rotationDistortion = new ClampedFloatParameter(0.0f, 0.0f, 360.0f);
/// <summary>Time to do a full loop.</summary>
[Tooltip("Time in seconds to loop animation.")]
public FloatParameter loopTime = new FloatParameter(1.0f);
/// <summary>Multiplier for HDRI sky uv distortion.</summary>
[Tooltip("Amplitude of the distorsion.")]
public FloatParameter amplitude = new FloatParameter(1.0f);
/// <summary>Texture used to distort the uv for the HDRI sky.</summary>
[Tooltip("Specify the flowmap HDRP uses for sky distortion (in Latlong format).")]
public TextureParameter flowmap = new TextureParameter(null);
/// <summary>Enable to affect only the upper part of the sky.</summary>
[Tooltip("Enabled if the flowmap covers only the upper part of the sky.")]
public BoolParameter upperHemisphereOnly = new BoolParameter(true);
/// <summary>Direction of the wind.</summary>
[Tooltip("Sets the rotation of the distortion (in degrees).")]
public ClampedFloatParameter scrollDirection = new ClampedFloatParameter(0.0f, 0.0f, 360.0f);
/// <summary>Force of the wind.</summary>
[Tooltip("Sets the cloud movement speed. The higher the value, the faster the clouds will move.")]
public MinFloatParameter scrollSpeed = new MinFloatParameter(2.0f, 0.0f);

/// <summary>Enable Backplate to have it visible.</summary>
[Tooltip("Enable or disable the backplate.")]
public BoolParameter enableBackplate = new BoolParameter(false);
Expand Down Expand Up @@ -84,11 +86,12 @@ public override int GetHashCode()
#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 = hdriSky.value != null ? hash * 23 + hdriSky.value.GetHashCode() : hash;
hash = flowmap.value != null ? hash * 23 + flowmap.value.GetHashCode() : hash;
hash = hash * 23 + enableDistortion.value.GetHashCode();
hash = hash * 23 + enableCloudMotion.value.GetHashCode();
hash = hash * 23 + procedural.value.GetHashCode();
hash = hash * 23 + rotationDistortion.value.GetHashCode();
hash = hash * 23 + loopTime.value.GetHashCode();
hash = hash * 23 + amplitude.value.GetHashCode();
hash = hash * 23 + upperHemisphereOnly.value.GetHashCode();
hash = hash * 23 + windDirection.value.GetHashCode();
hash = hash * 23 + windForce.value.GetHashCode();

hash = hash * 23 + enableBackplate.value.GetHashCode();
hash = hash * 23 + backplateType.value.GetHashCode();
hash = hash * 23 + groundLevel.value.GetHashCode();
Expand All @@ -105,11 +108,12 @@ public override int GetHashCode()

hash = hdriSky.value != null ? hash * 23 + hdriSky.overrideState.GetHashCode() : hash;
hash = flowmap.value != null ? hash * 23 + flowmap.overrideState.GetHashCode() : hash;
hash = hash * 23 + enableDistortion.overrideState.GetHashCode();
hash = hash * 23 + enableCloudMotion.overrideState.GetHashCode();
hash = hash * 23 + procedural.overrideState.GetHashCode();
hash = hash * 23 + rotationDistortion.overrideState.GetHashCode();
hash = hash * 23 + loopTime.overrideState.GetHashCode();
hash = hash * 23 + amplitude.overrideState.GetHashCode();
hash = hash * 23 + upperHemisphereOnly.overrideState.GetHashCode();
hash = hash * 23 + windDirection.overrideState.GetHashCode();
hash = hash * 23 + windForce.overrideState.GetHashCode();

hash = hash * 23 + enableBackplate.overrideState.GetHashCode();
hash = hash * 23 + backplateType.overrideState.GetHashCode();
hash = hash * 23 + groundLevel.overrideState.GetHashCode();
Expand All @@ -128,9 +132,10 @@ public override int GetHashCode()
hash = flowmap.value != null ? hash * 23 + flowmap.GetHashCode() : hash;
hash = hash * 23 + enableDistortion.GetHashCode();
hash = hash * 23 + procedural.GetHashCode();
hash = hash * 23 + rotationDistortion.GetHashCode();
hash = hash * 23 + loopTime.GetHashCode();
hash = hash * 23 + amplitude.GetHashCode();
hash = hash * 23 + upperHemisphereOnly.GetHashCode();
hash = hash * 23 + scrollDirection.GetHashCode();
hash = hash * 23 + scrollSpeed.GetHashCode();

hash = hash * 23 + enableBackplate.GetHashCode();
hash = hash * 23 + backplateType.GetHashCode();
hash = hash * 23 + groundLevel.GetHashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Shader "Hidden/HDRP/Sky/HDRISky"

#define LIGHTLOOP_DISABLE_TILE_AND_CLUSTER

#pragma multi_compile_local NO_DISTORTION USE_FLOWMAP PROCEDURAL
#pragma multi_compile_local _ SKY_MOTION
#pragma multi_compile_local _ USE_FLOWMAP

#pragma multi_compile _ DEBUG_DISPLAY
#pragma multi_compile SHADOW_LOW SHADOW_MEDIUM SHADOW_HIGH
Expand Down Expand Up @@ -49,7 +50,7 @@ Shader "Hidden/HDRP/Sky/HDRISky"
TEXTURECUBE(_Cubemap);
SAMPLER(sampler_Cubemap);

TEXTURECUBE(_Flowmap);
TEXTURE2D(_Flowmap);
SAMPLER(sampler_Flowmap);

float4 _DistortionParam; // x time, y amplitude, zw rotation (cosPhi and sinPhi)
Expand All @@ -60,6 +61,8 @@ Shader "Hidden/HDRP/Sky/HDRISky"
float3 _BackplateShadowTint; // xyz: ShadowTint
uint _BackplateShadowFilter;

float4 _FlowmapParam; // x upper hemisphere only, y scroll speed, zw scroll direction (cosPhi and sinPhi)

#define _Intensity _SkyParam.x
#define _CosPhi _SkyParam.z
#define _SinPhi _SkyParam.w
Expand All @@ -82,9 +85,9 @@ Shader "Hidden/HDRP/Sky/HDRISky"
#define _OffsetTex _BackplateParameters2.zw
#define _ShadowTint _BackplateShadowTint.rgb
#define _ShadowFilter _BackplateShadowFilter
#define _FlowTime _DistortionParam.x
#define _FlowAmplitude _DistortionParam.y
#define _FlowCosSin _DistortionParam.zw
#define _UpperHemisphere _FlowmapParam.x
#define _ScrollFactor _FlowmapParam.y
#define _ScrollDirection _FlowmapParam.zw

struct Attributes
{
Expand Down Expand Up @@ -180,49 +183,45 @@ Shader "Hidden/HDRP/Sky/HDRISky"
return IsHit(sdf, dir.y);
}

float2 GetFlow(float3 dir)
float3 GetDistordedSkyColor(float3 dir)
{
dir = RotationUp(dir, _FlowCosSin);
#if SKY_MOTION
if (dir.y >= 0 || !_UpperHemisphere)
{
float3 tangent = cross(dir, float3(0.0, 1.0, 0.0));
float3 bitangent = cross(tangent, dir);

// Compute flow factor
float3 windDir = RotationUp(dir, _ScrollDirection);
#ifdef USE_FLOWMAP
return SAMPLE_TEXTURECUBE_LOD(_Flowmap, sampler_Flowmap, dir, 0).rg * 2.0 - 1.0;
#elif PROCEDURAL
// source: https://www.gdcvault.com/play/1020146/Moving-the-Heavens-An-Artistic
float3 d = float3(0, 1, 0) - dir;
return (dir.y > 0) * normalize(d - dot(d, dir) * dir).zx;
float2 flow = SAMPLE_TEXTURE2D_LOD(_Flowmap, sampler_Flowmap, GetLatLongCoords(windDir, _UpperHemisphere), 0).rg * 2.0 - 1.0;
#else
return float2(0.0, 0.0);
float2 flow = GenerateFlow(windDir);
#endif
}

float3 GetSkyColor(float3 dir)
{
#ifdef NO_DISTORTION
return SAMPLE_TEXTURECUBE_LOD(_Cubemap, sampler_Cubemap, dir, 0).rgb;
#else
// Compute distortion directions on the cube
float3 tangent = cross(dir, float3(0.0, 1.0, 0.0));
float3 bitangent = cross(tangent, dir);

// Compute flow factor
float2 flow = GetFlow(dir);
float2 alpha = frac(float2(_ScrollFactor, _ScrollFactor + 0.5)) - 0.5;

float time = _Time.y * _FlowTime;
float2 alpha = frac(float2(time, time + 0.5)) - 0.5;
float2 uv1 = alpha.x * _FlowAmplitude * flow;
float2 uv2 = alpha.y * _FlowAmplitude * flow;
float2 uv1 = alpha.x * flow;
float2 uv2 = alpha.y * flow;

// Sample twice
float3 dir1 = dir + uv1.x * tangent + uv1.y * bitangent;
float3 color1 = SAMPLE_TEXTURECUBE_LOD(_Cubemap, sampler_Cubemap, dir1, 0).rgb;
float3 dd1 = uv1.x * tangent + uv1.y * bitangent; //dd1.y = abs(dd1.y);
float3 dd2 = uv2.x * tangent + uv2.y * bitangent; //dd2.y = abs(dd2.y);

float3 dir2 = dir + uv2.x * tangent + uv2.y * bitangent;
float3 color2 = SAMPLE_TEXTURECUBE_LOD(_Cubemap, sampler_Cubemap, dir2, 0).rgb;
// Sample twice
float3 color1 = SAMPLE_TEXTURECUBE_LOD(_Cubemap, sampler_Cubemap, dir + dd1, 0).rgb;
float3 color2 = SAMPLE_TEXTURECUBE_LOD(_Cubemap, sampler_Cubemap, dir + dd2, 0).rgb;

// Blend color samples
//return float3(flow*0.5+0.5, 0);
return lerp(color1, color2, abs(2.0 * alpha.x));
// Blend color samples
return lerp(color1, color2, abs(2.0 * alpha.x));
}
#endif

return SAMPLE_TEXTURECUBE_LOD(_Cubemap, sampler_Cubemap, dir, 0).rgb;
}

float3 GetSkyColor(float3 dir)
{
return GetDistordedSkyColor(dir);
}

float4 GetColorWithRotation(float3 dir, float exposure, float2 cos_sin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class HDRISkyRenderer : SkyRenderer
Material m_SkyHDRIMaterial; // Renders a cubemap into a render texture (can be cube or 2D)
MaterialPropertyBlock m_PropertyBlock = new MaterialPropertyBlock();

float scrollFactor = 0.0f, lastTime = 0.0f;

private static int m_RenderCubemapID = 0; // FragBaking
private static int m_RenderFullscreenSkyID = 1; // FragRender
private static int m_RenderCubemapWithBackplateID = 2; // FragBakingBackplate
Expand Down Expand Up @@ -131,24 +133,25 @@ public override void RenderSky(BuiltinSkyParameters builtinParams, bool renderFo

if (hdriSky.enableDistortion.value == true)
{
m_SkyHDRIMaterial.DisableKeyword("NO_DISTORTION");
if (hdriSky.procedural.value == true)
{
m_SkyHDRIMaterial.EnableKeyword("PROCEDURAL");
m_SkyHDRIMaterial.DisableKeyword("USE_FLOWMAP");
}
else
m_SkyHDRIMaterial.EnableKeyword("SKY_MOTION");
if (hdriSky.procedural.value == false)
{
m_SkyHDRIMaterial.DisableKeyword("PROCEDURAL");
m_SkyHDRIMaterial.EnableKeyword("USE_FLOWMAP");
m_SkyHDRIMaterial.SetTexture(HDShaderIDs._Flowmap, hdriSky.flowmap.value);
}
float rot = -Mathf.Deg2Rad*hdriSky.rotationDistortion.value;
Vector4 distortion = new Vector4(0.5f / hdriSky.loopTime.value, hdriSky.amplitude.value, Mathf.Cos(rot), Mathf.Sin(rot));
m_SkyHDRIMaterial.SetVector(HDShaderIDs._DistortionParam, distortion);
else
m_SkyHDRIMaterial.DisableKeyword("USE_FLOWMAP");

float rot = -Mathf.Deg2Rad*hdriSky.scrollDirection.value;
Vector4 flowmapParam = new Vector4(hdriSky.upperHemisphereOnly.value ? 1.0f : 0.0f, scrollFactor, Mathf.Cos(rot), Mathf.Sin(rot));

m_SkyHDRIMaterial.SetVector(HDShaderIDs._FlowmapParam, flowmapParam);

scrollFactor += hdriSky.scrollSpeed.value * (Time.time - lastTime) * 0.01f;
lastTime = Time.time;
}
else
m_SkyHDRIMaterial.EnableKeyword("NO_DISTORTION");
m_SkyHDRIMaterial.DisableKeyword("SKY_MOTION");

m_SkyHDRIMaterial.SetTexture(HDShaderIDs._Cubemap, hdriSky.hdriSky.value);
m_SkyHDRIMaterial.SetVector(HDShaderIDs._SkyParam, new Vector4(intensity, 0.0f, Mathf.Cos(phi), Mathf.Sin(phi)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,21 @@ float3 GetSkyViewDirWS(float2 positionCS)
return normalize(viewDirWS.xyz);
}

// Returns latlong coords from view direction
float2 GetLatLongCoords(float3 dir, int upperHemisphereOnly)
{
float angle = atan2(dir.z, dir.x)/(2.0*PI) + 0.5;
float height = lerp(dir.y * 0.5 + 0.5, dir.y, upperHemisphereOnly);

return float2(angle, height);
}

// Generates a flow for a constant wind in the z direction
// source: https://www.gdcvault.com/play/1020146/Moving-the-Heavens-An-Artistic
float2 GenerateFlow(float3 dir)
{
float3 d = float3(0, 1, 0) - dir;
return (dir.y > 0) * normalize(d - dot(d, dir) * dir).zx;
}

#endif // __SKYUTILS_H__

0 comments on commit b3c0fc6

Please sign in to comment.