浏览代码

Implement directional light cookies

/main
Evgenii Golubev 8 年前
当前提交
6ff56821
共有 5 个文件被更改,包括 100 次插入20 次删除
  1. 25
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  2. 26
      Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightDefinition.cs
  3. 26
      Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightDefinition.cs.hlsl
  4. 40
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.hlsl
  5. 3
      Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl

25
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


var directionalLightData = new DirectionalLightData();
// Light direction for directional and is opposite to the forward direction
directionalLightData.direction = -light.light.transform.forward;
directionalLightData.direction = -light.light.transform.forward;
directionalLightData.up = -light.light.transform.up;
directionalLightData.positionWS = light.light.transform.position;
directionalLightData.invScaleX = 1.0f / light.light.transform.localScale.x;
directionalLightData.invScaleY = 1.0f / light.light.transform.localScale.y;
directionalLightData.cookieIndex = Int32.MinValue;
if (light.light.cookie != null)
{
if (light.light.cookie.dimension == TextureDimension.Tex2D)
{
directionalLightData.cookieIndex = m_CookieTexArray.FetchSlice(light.light.cookie);
}
else // Cube
{
// Note the bitwise (one's) complement operator which flips the bits.
directionalLightData.cookieIndex = ~m_CubeCookieTexArray.FetchSlice(light.light.cookie);
}
}
bool hasDirectionalShadows = light.light.shadows != LightShadows.None && shadowOutput.GetShadowSliceCountLightIndex(lightIndex) != 0;
bool hasDirectionalNotReachMaxLimit = lightList.directionalShadows.Count == 0; // Only one cascade shadow allowed

case LightType.Spot:
lightData.cookieIndex = m_CookieTexArray.FetchSlice(light.light.cookie);
break;
case LightType.Directional:
lightData.cookieIndex = m_CookieTexArray.FetchSlice(light.light.cookie);
break;
case LightType.Point:
lightData.cookieIndex = m_CubeCookieTexArray.FetchSlice(light.light.cookie);
break;

lightData.size = new Vector2(additionalData.areaLightLength,
additionalData.areaLightWidth);
// Area and line lights are both currently stored as regular lights on the GPU.
// Area and line lights are both currently stored as area lights on the GPU.
lightList.areaLights.Add(lightData);
lightList.areaCullIndices.Add(lightIndex);
}

26
Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightDefinition.cs


public enum GPULightType
{
Directional,
Spot,
Spot,
Point,
ProjectorOrtho,
ProjectorPyramid,

public float invSqrAttenuationRadius;
public Vector3 color;
public float angleScale; // Spot light
public float angleScale; // Spot light
public float angleOffset;
public float angleOffset; // Spot light
public float diffuseScale; // Spot light
public float diffuseScale;
public Vector3 right;
public float specularScale;

public Vector3 direction;
public float diffuseScale;
public Vector3 up;
public float invScaleX;
public Vector3 positionWS;
public float invScaleY;
public float specularScale;
public float specularScale;
// Sun disc size
public float cosAngle; // Distance to disk
public float sinAngle; // Disk radius
public int shadowIndex;
public float unused;
// Sun disc size
public float cosAngle; // Distance to the disk
public float sinAngle; // Disk radius
public int shadowIndex; // -1 if unused
public int cookieIndex; // INT_MIN if unused; (i >= 0) ? (i2D = i) : (iCube = ~i)
};

26
Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/LightDefinition.cs.hlsl


{
float3 direction;
float diffuseScale;
float3 up;
float invScaleX;
float3 positionWS;
float invScaleY;
float unused;
int cookieIndex;
};
// Generated from UnityEngine.Experimental.ScriptableRenderLoop.PunctualShadowData

{
return value.diffuseScale;
}
float3 GetUp(DirectionalLightData value)
{
return value.up;
}
float GetInvScaleX(DirectionalLightData value)
{
return value.invScaleX;
}
float3 GetPositionWS(DirectionalLightData value)
{
return value.positionWS;
}
float GetInvScaleY(DirectionalLightData value)
{
return value.invScaleY;
}
float3 GetColor(DirectionalLightData value)
{
return value.color;

{
return value.shadowIndex;
}
float GetUnused(DirectionalLightData value)
int GetCookieIndex(DirectionalLightData value)
return value.unused;
return value.cookieIndex;
}
//

40
Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.hlsl


float3 L = lightData.direction;
float illuminance = saturate(dot(bsdfData.normalWS, L));
diffuseLighting = float3(0.0, 0.0, 0.0);
specularLighting = float3(0.0, 0.0, 0.0);
diffuseLighting = float3(0.0, 0.0, 0.0);
specularLighting = float3(0.0, 0.0, 0.0);
float3 cookieColor = float3(1.0, 1.0, 1.0);
[branch] if (lightData.shadowIndex >= 0 && illuminance > 0.0f)
{

}
[branch] if (lightData.cookieIndex != INT_MIN && illuminance > 0.0)
{
float4 cookie;
[branch] if (lightData.cookieIndex >= 0)
{
// The cookie is a 2D texture.
float3 unL = positionWS - lightData.positionWS;
// Project 'unL' onto the light's axes.
float3 right = cross(lightData.up, lightData.direction);
float2 coord = float2(dot(unL, right), dot(unL, lightData.up));
// Rescale the texture.
coord.x *= lightData.invScaleX;
coord.y *= lightData.invScaleY;
// Remap the texture coordinates from [-1, 1]^2 to [0, 1]^2.
coord = coord * 0.5 + 0.5;
// Tile the texture via wrapping. TODO: the sampler should do this for us.
cookie = SampleCookie2D(lightLoopContext, frac(coord), lightData.cookieIndex);
}
else
{
// The cookie is a cubemap. We flip the bits to get the real index.
lightData.cookieIndex = ~lightData.cookieIndex;
cookie = SampleCookieCube(lightLoopContext, L, lightData.cookieIndex);
}
cookieColor = cookie.rgb;
illuminance *= cookie.a;
}
[branch] if (illuminance > 0.0f)

3
Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl


#define FLT_EPSILON 1.192092896e-07 // Smallest positive number, such that 1.0 + FLT_EPSILON != 1.0
#define FLT_MAX 3.402823466e+38 // Maximum representable floating-point number
#define INT_MIN -2147483648 // Minimum (signed) int value
#define INT_MAX 2147483647 // Maximum (signed) int value
#define MERGE_NAME(X, Y) X##Y
float DegToRad(float deg)

正在加载...
取消
保存