using System;
using System.Runtime.InteropServices;
namespace UnityEngine.Rendering
{
///
/// Contains spherical harmonic coefficients used for lighting representation in the format
/// expected by DOTS_INSTANCING_ON shaders.
///
/// The size of the struct is padded to a power of two so arrays of such structs can be efficiently
/// indexed in shaders.
///
///
[StructLayout(LayoutKind.Sequential)]
[Serializable]
public struct SHCoefficients : IEquatable
{
///
/// Contains the SH coefficients that correspond to the unity_SHAr shader property.
///
public Vector4 SHAr;
///
/// Contains the SH coefficients that correspond to the unity_SHAg shader property.
///
public Vector4 SHAg;
///
/// Contains the SH coefficients that correspond to the unity_SHAb shader property.
///
public Vector4 SHAb;
///
/// Contains the SH coefficients that correspond to the unity_SHBr shader property.
///
public Vector4 SHBr;
///
/// Contains the SH coefficients that correspond to the unity_SHBg shader property.
///
public Vector4 SHBg;
///
/// Contains the SH coefficients that correspond to the unity_SHBb shader property.
///
public Vector4 SHBb;
///
/// Contains the SH coefficients that correspond to the unity_SHC shader property.
///
public Vector4 SHC;
///
/// Contains the baked shadowing data that corresponds to the unity_ProbesOcclusion shader property.
///
public Vector4 ProbesOcclusion;
///
/// Construct an instance of SHCoefficients that represents the same spherical
/// harmonic coefficients as the parameter.
///
/// The spherical harmonic coefficients to initialize with.
public SHCoefficients(SphericalHarmonicsL2 sh)
{
SHAr = GetSHA(sh, 0);
SHAg = GetSHA(sh, 1);
SHAb = GetSHA(sh, 2);
SHBr = GetSHB(sh, 0);
SHBg = GetSHB(sh, 1);
SHBb = GetSHB(sh, 2);
SHC = GetSHC(sh);
ProbesOcclusion = Vector4.one;
}
///
/// Construct an instance of SHCoefficients that represents the same spherical
/// harmonic coefficients as the parameter.
///
/// The spherical harmonic coefficients to initialize with.
/// The baked shadowing data to include with this set of spherical harmonic coefficients.
public SHCoefficients(SphericalHarmonicsL2 sh, Vector4 probesOcclusion)
: this(sh)
{
ProbesOcclusion = probesOcclusion;
}
static Vector4 GetSHA(SphericalHarmonicsL2 sh, int i)
{
return new Vector4(sh[i, 3], sh[i, 1], sh[i, 2], sh[i, 0] - sh[i, 6]);
}
static Vector4 GetSHB(SphericalHarmonicsL2 sh, int i)
{
return new Vector4(sh[i, 4], sh[i, 5], sh[i, 6] * 3f, sh[i, 7]);
}
static Vector4 GetSHC(SphericalHarmonicsL2 sh)
{
return new Vector4(sh[0, 8], sh[1, 8], sh[2, 8], 1);
}
///
/// Equals implementation.
///
/// Other SHCoefficients instance to comapre this against.
/// True if contents are equal, False otherwise.
public bool Equals(SHCoefficients other)
{
return SHAr.Equals(other.SHAr) && SHAg.Equals(other.SHAg) && SHAb.Equals(other.SHAb) && SHBr.Equals(other.SHBr) && SHBg.Equals(other.SHBg) && SHBb.Equals(other.SHBb) && SHC.Equals(other.SHC) && ProbesOcclusion.Equals(other.ProbesOcclusion);
}
///
/// Equals implementation.
///
/// Other object to compare this object against
/// True if contents are equal, False otherwise.
public override bool Equals(object obj)
{
return obj is SHCoefficients other && Equals(other);
}
///
/// GetHashCode implementation.
///
///
public override int GetHashCode()
{
return HashCode.Combine(SHAr, SHAg, SHAb, SHBr, SHBg, SHBb, SHC, ProbesOcclusion);
}
///
/// Equality operator implementation.
///
/// Left operand of comparison
/// Right operand of comparison
/// True if contents are equal, False otherwise.
public static bool operator ==(SHCoefficients left, SHCoefficients right)
{
return left.Equals(right);
}
///
/// Not equals operator implementation.
///
/// Left operand of comparison
/// Right operand of comparison
/// True if contents are not equal, False otherwise.
public static bool operator !=(SHCoefficients left, SHCoefficients right)
{
return !left.Equals(right);
}
}
///
/// Contains default values for built-in properties that the user is expected to manually
/// provide for DOTS_INSTANCING_ON shaders. The struct layout matches the
/// unity_DOTSInstanceGlobalValues constant buffer the shader expects the default
/// values in.
///
[Obsolete("BatchRendererGroupGlobals and associated cbuffer are now set automatically by Unity. Setting it manually is no longer necessary or supported.")]
[StructLayout(LayoutKind.Sequential)]
[Serializable]
public struct BatchRendererGroupGlobals : IEquatable
{
///
/// The string name of the constant buffer DOTS_INSTANCING_ON shaders use
/// to read default values for the built-in properties contained in this struct.
///
public const string kGlobalsPropertyName = "unity_DOTSInstanceGlobalValues";
///
/// The unique identifier for , retrieved using
/// .
///
///
public static readonly int kGlobalsPropertyId = Shader.PropertyToID(kGlobalsPropertyName);
///
/// The default value to use for the unity_ProbesOcclusion built-in shader property.
///
public Vector4 ProbesOcclusion;
///
/// The default value to use for the unity_SpecCube0_HDR built-in shader property.
///
public Vector4 SpecCube0_HDR;
///
/// The default value to use for the unity_SpecCube1_HDR built-in shader property.
///
public Vector4 SpecCube1_HDR;
///
/// The default values to use for the built-in spherical harmonics shader properties.
///
///
public SHCoefficients SHCoefficients;
///
/// Construct a struct with default values based on the currently active reflection probe
/// and ambient lighting settings.
///
public static BatchRendererGroupGlobals Default
{
get
{
var globals = new BatchRendererGroupGlobals();
globals.ProbesOcclusion = Vector4.one;
globals.SpecCube0_HDR = ReflectionProbe.defaultTextureHDRDecodeValues;
globals.SpecCube1_HDR = globals.SpecCube0_HDR;
globals.SHCoefficients = new SHCoefficients(RenderSettings.ambientProbe);
return globals;
}
}
///
/// Equals implementation.
///
/// Other BatchRendererGroupGlobals instance to comapre this against.
/// True if contents are equal, False otherwise.
public bool Equals(BatchRendererGroupGlobals other)
{
return ProbesOcclusion.Equals(other.ProbesOcclusion) && SpecCube0_HDR.Equals(other.SpecCube0_HDR) && SpecCube1_HDR.Equals(other.SpecCube1_HDR) && SHCoefficients.Equals(other.SHCoefficients);
}
///
/// Equals implementation.
///
/// Other object to comapre this against.
/// True if contents are equal, False otherwise.
public override bool Equals(object obj)
{
return obj is BatchRendererGroupGlobals other && Equals(other);
}
///
/// GetHashCode implementation.
///
///
public override int GetHashCode()
{
return HashCode.Combine(ProbesOcclusion, SpecCube0_HDR, SpecCube1_HDR, SHCoefficients);
}
///
/// Equality operator implementation.
///
/// Left operand of comparison
/// Right operand of comparison
/// True if contents are equal, False otherwise.
public static bool operator ==(BatchRendererGroupGlobals left, BatchRendererGroupGlobals right)
{
return left.Equals(right);
}
///
/// Not equals operator implementation.
///
/// Left operand of comparison
/// Right operand of comparison
/// True if contents are not equal, False otherwise.
public static bool operator !=(BatchRendererGroupGlobals left, BatchRendererGroupGlobals right)
{
return !left.Equals(right);
}
}
}