using System;
using System.Diagnostics;
namespace UnityEngine.Rendering.HighDefinition
{
///
/// Cloud Map Mode.
///
public enum CloudMapMode
{
/// One layer mode.
Single,
/// Two layer mode.
Double,
}
///
/// Cloud Distortion Mode.
///
public enum CloudDistortionMode
{
/// No distortion.
None,
/// Procedural distortion.
[InspectorName("Horizontal")]
Procedural,
/// Distortion from a flowmap.
Flowmap,
}
///
/// Resolution of the cloud texture.
///
public enum CloudResolution
{
/// Size 256
CloudResolution256 = 256,
/// Size 512
CloudResolution512 = 512,
/// Size 1024
CloudResolution1024 = 1024,
/// Size 2048
CloudResolution2048 = 2048,
/// Size 4096
CloudResolution4096 = 4096,
/// Size 8192
CloudResolution8192 = 8192,
}
///
/// Resolution of the cloud shadow.
///
public enum CloudShadowsResolution
{
/// Size 64
VeryLow = 64,
/// Size 128
Low = 128,
/// Size 256
Medium = 256,
/// Size 512
High = 512,
}
///
/// Cloud Layer Volume Component.
/// This component setups the Cloud Layer for rendering.
///
[VolumeComponentMenu("Sky/Cloud Layer")]
[SupportedOnRenderPipeline(typeof(HDRenderPipelineAsset))]
[CloudUniqueID((int)CloudType.CloudLayer)]
[HDRPHelpURL("Override-Cloud-Layer")]
public partial class CloudLayer : CloudSettings
{
/// Controls the global opacity of the cloud layer.
[Tooltip("Controls the global opacity of the cloud layer.")]
public ClampedFloatParameter opacity = new ClampedFloatParameter(1.0f, 0.0f, 1.0f);
/// Enable to cover only the upper part of the sky.
[AdditionalProperty]
[Tooltip("Check this box if the cloud layer covers only the upper part of the sky.")]
public BoolParameter upperHemisphereOnly = new BoolParameter(true);
/// Choose the number of cloud layers.
public VolumeParameter layers = new VolumeParameter();
/// Choose the resolution of the baked cloud texture.
[AdditionalProperty]
[Tooltip("Specifies the resolution of the texture HDRP uses to represent the clouds.")]
public EnumParameter resolution = new EnumParameter(CloudResolution.CloudResolution1024);
/// Controls the opacity of the cloud shadows.
[Header("Cloud Shadows")]
[Tooltip("Controls the opacity of the cloud shadows.")]
public MinFloatParameter shadowMultiplier = new MinFloatParameter(1.0f, 0.0f);
/// Controls the tint of the cloud shadows.
[Tooltip("Controls the tint of the cloud shadows.")]
public ColorParameter shadowTint = new ColorParameter(Color.black, false, false, true);
/// Choose the resolution of the texture for the cloud shadows.
[AdditionalProperty]
[Tooltip("Specifies the resolution of the texture HDRP uses to represent the cloud shadows.")]
public EnumParameter shadowResolution = new EnumParameter(CloudShadowsResolution.Medium);
/// Specifies the size of the projected shadows.
[Tooltip("Specifies the size of the projected shadows.")]
public MinFloatParameter shadowSize = new MinFloatParameter(500f, 0.0f);
///
/// Cloud Map Volume Parameters.
/// This groups parameters for one cloud map layer.
///
[Serializable]
public class CloudMap
{
internal static Texture s_DefaultTexture = null;
/// Texture used to render the clouds.
[Tooltip("Specify the texture HDRP uses to render the clouds (in LatLong layout).")]
public Texture2DParameter cloudMap = new Texture2DParameter(CloudMap.s_DefaultTexture);
/// Opacity multiplier for the red channel.
[Tooltip("Opacity multiplier for the red channel.")]
public ClampedFloatParameter opacityR = new ClampedFloatParameter(1.0f, 0.0f, 1.0f);
/// Opacity multiplier for the green channel.
[Tooltip("Opacity multiplier for the green channel.")]
public ClampedFloatParameter opacityG = new ClampedFloatParameter(0.0f, 0.0f, 1.0f);
/// Opacity multiplier for the blue channel.
[Tooltip("Opacity multiplier for the blue channel.")]
public ClampedFloatParameter opacityB = new ClampedFloatParameter(0.0f, 0.0f, 1.0f);
/// Opacity multiplier for the alpha channel.
[Tooltip("Opacity multiplier for the alpha channel.")]
public ClampedFloatParameter opacityA = new ClampedFloatParameter(0.0f, 0.0f, 1.0f);
/// Altitude of the bottom of the cloud layer in meters.
[Tooltip("Altitude of the bottom of the cloud layer in meters.")]
public MinFloatParameter altitude = new MinFloatParameter(2000.0f, 0.0f);
/// Rotation of the clouds.
[Tooltip("Sets the rotation of the clouds (in degrees).")]
public ClampedFloatParameter rotation = new ClampedFloatParameter(0.0f, 0.0f, 360.0f);
/// Color multiplier of the clouds.
[Tooltip("Specifies the color HDRP uses to tint the clouds.")]
public ColorParameter tint = new ColorParameter(Color.white, false, false, true);
/// Relative exposure of the clouds.
[InspectorName("Exposure Compensation")]
[Tooltip("Sets the exposure compensation of the clouds in EV.")]
public FloatParameter exposure = new FloatParameter(0.0f);
/// Distortion mode.
[InspectorName("Wind")]
[Tooltip("Distortion mode used to simulate cloud movement.\nIn Scene View, requires Always Refresh to be enabled.")]
public VolumeParameter distortionMode = new VolumeParameter();
/// Direction of the wind relative to the X world vector. This value can be relative to the Global Wind Orientation defined in the Visual Environment.
[InspectorName("Orientation")]
[Tooltip("Controls the orientation of the wind relative to the X world vector.\nThis value can be relative to the Global Wind Orientation defined in the Visual Environment.")]
public WindOrientationParameter scrollOrientation = new WindOrientationParameter();
/// Sets the wind speed in kilometers per hour. This value can be relative to the Global Wind Speed defined in the Visual Environment.
[InspectorName("Speed")]
[Tooltip("Sets the wind speed in kilometers per hour.\nThis value can be relative to the Global Wind Speed defined in the Visual Environment.")]
public WindSpeedParameter scrollSpeed = new WindSpeedParameter();
/// Texture used to distort the UVs for the cloud layer.
[Tooltip("Specify the flowmap HDRP uses for cloud distortion (in LatLong layout).")]
public Texture2DParameter flowmap = new Texture2DParameter(null);
/// Simulates cloud self-shadowing using raymarching.
[InspectorName("Raymarching")]
[Tooltip("Simulates cloud self-shadowing using raymarching.")]
public BoolParameter lighting = new BoolParameter(true);
/// Number of raymarching steps.
[Tooltip("Number of raymarching steps.")]
public ClampedIntParameter steps = new ClampedIntParameter(6, 2, 32);
/// Density of the cloud layer.
[InspectorName("Density")]
[Tooltip("Density of the cloud layer.")]
public ClampedFloatParameter thickness = new ClampedFloatParameter(0.5f, 0.0f, 1.0f);
/// Ambient probe multiplier.
[Tooltip("Controls the influence of the ambient probe on the cloud layer volume. A lower value will suppress the ambient light and produce darker clouds overall.")]
public ClampedFloatParameter ambientProbeDimmer = new ClampedFloatParameter(1.0f, 0.0f, 1.0f);
/// Enable to cast shadows.
[Tooltip("Projects a portion of the clouds around the sun light to simulate cloud shadows. This will override the cookie of your directional light.")]
public BoolParameter castShadows = new BoolParameter(false);
internal float scrollFactor = 0.0f;
internal int NumSteps => lighting.value ? steps.value : 0;
internal Vector4 Opacities => new Vector4(opacityR.value, opacityG.value, opacityB.value, opacityA.value);
internal Color Color => tint.value * ColorUtils.ConvertEV100ToExposure(-exposure.value);
internal Vector4 GetRenderingParameters(HDCamera camera)
{
float angle = Mathf.Deg2Rad * scrollOrientation.GetValue(camera);
return new Vector3(-Mathf.Cos(angle), -Mathf.Sin(angle), scrollFactor);
}
internal (Vector4, Vector4) GetBakingParameters()
{
Vector4 params2 = new Vector4(
rotation.value / 360.0f,
NumSteps,
thickness.value * (0.1f - 0.005f) + 0.005f, // [0.005, 0.1]
altitude.value
);
return (Opacities, params2);
}
internal int GetBakingHashCode()
{
int hash = 17;
unchecked
{
hash = hash * 23 + cloudMap.GetHashCode();
hash = hash * 23 + opacityR.GetHashCode();
hash = hash * 23 + opacityG.GetHashCode();
hash = hash * 23 + opacityB.GetHashCode();
hash = hash * 23 + opacityA.GetHashCode();
hash = hash * 23 + rotation.GetHashCode();
hash = hash * 23 + castShadows.GetHashCode();
if (lighting.value)
{
hash = hash * 23 + lighting.GetHashCode();
hash = hash * 23 + steps.GetHashCode();
hash = hash * 23 + altitude.GetHashCode();
hash = hash * 23 + thickness.GetHashCode();
}
}
return hash;
}
///
/// Returns the hash code of the CloudMap parameters.
///
/// The hash code of the CloudMap parameters.
public override int GetHashCode()
{
int hash = 17;
unchecked
{
hash = hash * 23 + cloudMap.GetHashCode();
hash = hash * 23 + opacityR.GetHashCode();
hash = hash * 23 + opacityG.GetHashCode();
hash = hash * 23 + opacityB.GetHashCode();
hash = hash * 23 + opacityA.GetHashCode();
hash = hash * 23 + altitude.GetHashCode();
hash = hash * 23 + rotation.GetHashCode();
hash = hash * 23 + tint.GetHashCode();
hash = hash * 23 + exposure.GetHashCode();
hash = hash * 23 + distortionMode.GetHashCode();
hash = hash * 23 + scrollOrientation.GetHashCode();
hash = hash * 23 + scrollSpeed.GetHashCode();
hash = hash * 23 + flowmap.GetHashCode();
hash = hash * 23 + lighting.GetHashCode();
hash = hash * 23 + steps.GetHashCode();
hash = hash * 23 + thickness.GetHashCode();
hash = hash * 23 + ambientProbeDimmer.GetHashCode();
hash = hash * 23 + castShadows.GetHashCode();
}
return hash;
}
}
/// Layer A.
public CloudMap layerA = new CloudMap();
/// Layer B.
public CloudMap layerB = new CloudMap();
internal int NumLayers => (layers == CloudMapMode.Single) ? 1 : 2;
internal bool CastShadows => layerA.castShadows.value || (layers.value == CloudMapMode.Double && layerB.castShadows.value);
Vector3Int CastToInt3(Vector3 vec) => new Vector3Int((int)vec.x, (int)vec.y, (int)vec.z);
internal int GetBakingHashCode(Light sunLight)
{
int hash = 17;
bool lighting = layerA.lighting.value;
bool shadows = sunLight != null && layerA.castShadows.value;
unchecked
{
hash = hash * 23 + upperHemisphereOnly.GetHashCode();
hash = hash * 23 + layers.GetHashCode();
hash = hash * 23 + resolution.GetHashCode();
hash = hash * 23 + layerA.GetBakingHashCode();
if (layers.value == CloudMapMode.Double)
{
hash = hash * 23 + layerB.GetBakingHashCode();
lighting |= layerB.lighting.value;
shadows |= layerB.castShadows.value;
}
if (lighting && sunLight != null)
hash = hash * 23 + CastToInt3(sunLight.transform.rotation.eulerAngles).GetHashCode();
if (shadows)
hash = hash * 23 + shadowResolution.GetHashCode();
}
return hash;
}
///
/// Returns the hash code of the CloudLayer parameters.
///
/// The hash code of the CloudLayer parameters.
public override int GetHashCode()
{
int hash = 17;
unchecked
{
hash = hash * 23 + opacity.GetHashCode();
hash = hash * 23 + upperHemisphereOnly.GetHashCode();
hash = hash * 23 + layers.GetHashCode();
hash = hash * 23 + resolution.GetHashCode();
hash = hash * 23 + layerA.GetHashCode();
if (layers.value == CloudMapMode.Double)
hash = hash * 23 + layerB.GetHashCode();
}
return hash;
}
///
/// Returns CloudLayerRenderer type.
///
/// CloudLayerRenderer type.
public override Type GetCloudRendererType() { return typeof(CloudLayerRenderer); }
///
/// Called though reflection by the VolumeManager.
///
static void Init()
{
if (GraphicsSettings.TryGetRenderPipelineSettings(out var runtimeTextures))
{
CloudMap.s_DefaultTexture = runtimeTextures.defaultCloudMap;
}
}
}
}