浏览代码

HDRenderLoop: Clean some code and move file - AdditionalLightData now required on lights

/main
Sebastien Lagarde 8 年前
当前提交
47769667
共有 11 个文件被更改,包括 179 次插入173 次删除
  1. 79
      Assets/ScriptableRenderLoop/AdditionalLightData.cs
  2. 174
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  3. 3
      Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/SinglePass/SinglePassLoop.hlsl
  4. 22
      Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.hlsl
  5. 65
      Assets/TestScenes/HDTest/HDRenderLoopTest.unity
  6. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders.meta
  7. 0
      /Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LtcData.DisneyDiffuse.cs
  8. 0
      /Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LtcData.DisneyDiffuse.cs.meta
  9. 0
      /Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LtcData.GGX.cs
  10. 0
      /Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LtcData.GGX.cs.meta

79
Assets/ScriptableRenderLoop/AdditionalLightData.cs


public int shadowResolution = DefaultShadowResolution;
public static int GetShadowResolution(AdditionalLightData lightData)
{
if (lightData != null)
return lightData.shadowResolution;
else
return DefaultShadowResolution;
}
public float innerSpotPercent = 0.0F;
private float m_innerSpotPercent = 0.0F;
public float GetInnerSpotPercent01()
{
return Mathf.Clamp(m_innerSpotPercent, 0.0f, 100.0f) / 100.0f;
}
[RangeAttribute(0.0F, 1.0F)]
public float shadowDimmer = 1.0F;

[RangeAttribute(0.0f, 20.0f)]
public float areaLightWidth = 0.0f;
public static float GetInnerSpotPercent01(AdditionalLightData lightData)
{
if (lightData != null)
return Mathf.Clamp(lightData.innerSpotPercent, 0.0f, 100.0f) / 100.0f;
else
return 0.0F;
}
public static bool GetAffectDiffuse(AdditionalLightData lightData)
{
if (lightData != null)
return lightData.affectDiffuse;
else
return true;
}
public static bool GetAffectSpecular(AdditionalLightData lightData)
{
if (lightData != null)
return lightData.affectSpecular;
else
return true;
}
public static float GetShadowDimmer(AdditionalLightData lightData)
{
if (lightData != null)
return Mathf.Clamp(lightData.shadowDimmer, 0F, 1F);
else
return 1.0F;
}
public static int GetShadowResolution(AdditionalLightData lightData)
{
if (lightData != null)
return lightData.shadowResolution;
else
return DefaultShadowResolution;
}
public static bool GetTreatAsAreaLight(AdditionalLightData lightData)
{
if (lightData != null)
return lightData.treatAsAreaLight;
else
return false;
}
public static bool GetIsDoubleSided(AdditionalLightData lightData)
{
if (lightData != null)
return lightData.isDoubleSided;
else
return false;
}
public static Vector2 GetAreaLightDimensions(AdditionalLightData lightData)
{
if (lightData != null)
return new Vector2(lightData.areaLightLength, lightData.areaLightWidth);
else
return new Vector2(0.0f, 0.0f);
}
}
}

174
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


{
var light = visibleLights[lightIndex];
if (light.lightType == LightType.Area) {
// We only process light with additional data
var additionalData = light.light.GetComponent<AdditionalLightData>();
if (additionalData == null)
{
Debug.LogWarning("Light entity detected without additional data, will not be taken into account " + light.light.name);
continue;
}
if (light.lightType == LightType.Area)
{
// Skip area lights which are currently only used for baking.
continue;
}

var lightColorG = light.light.intensity * Mathf.GammaToLinearSpace(light.light.color.g);
var lightColorB = light.light.intensity * Mathf.GammaToLinearSpace(light.light.color.b);
var additionalData = light.light.GetComponent<AdditionalLightData>();
if (AdditionalLightData.GetTreatAsAreaLight(additionalData))
if (additionalData.treatAsAreaLight)
lightData.shapeType = AreaShapeType.Rectangle;
lightData.size = new Vector2(additionalData.areaLightLength, additionalData.areaLightWidth);
lightData.twoSided = additionalData.isDoubleSided ? 1.0f : 0.0f;
lightData.shapeType = AreaShapeType.Rectangle;
lightData.size = new Vector2(additionalData.areaLightLength, additionalData.areaLightWidth);
lightData.twoSided = additionalData.isDoubleSided ? 1.0f : 0.0f;
lightData.positionWS = light.light.transform.position;
lightData.forward = light.light.transform.forward; // Note: Light direction is oriented backward (-Z)
lightData.up = light.light.transform.up;
lightData.right = light.light.transform.right;
lightData.positionWS = light.light.transform.position;
lightData.forward = light.light.transform.forward; // Note: Light direction is oriented backward (-Z)
lightData.up = light.light.transform.up;
lightData.right = light.light.transform.right;
lightData.color = new Vector3(lightColorR, lightColorG, lightColorB);
lightData.diffuseScale = additionalData.affectDiffuse ? 1.0f : 0.0f;
lightData.color = new Vector3(lightColorR, lightColorG, lightColorB);
lightData.diffuseScale = additionalData.affectDiffuse ? 1.0f : 0.0f;
lightData.shadowDimmer = additionalData.shadowDimmer;
lightData.shadowDimmer = additionalData.shadowDimmer;
lightData.invSqrAttenuationRadius = 1.0f / (light.range * light.range);

continue;
}
var l = new PunctualLightData();
if (light.lightType == LightType.Directional)
{
l.useDistanceAttenuation = 0.0f;
// positionWS store Light direction for directional and is opposite to the forward direction
l.positionWS = -light.light.transform.forward;
l.invSqrAttenuationRadius = 0.0f;
l.useDistanceAttenuation = 1.0f;
l.positionWS = light.light.transform.position;
l.invSqrAttenuationRadius = 1.0f / (light.range * light.range);
}
var l = new PunctualLightData();
if (light.lightType == LightType.Directional)
{
l.useDistanceAttenuation = 0.0f;
// positionWS store Light direction for directional and is opposite to the forward direction
l.positionWS = -light.light.transform.forward;
l.invSqrAttenuationRadius = 0.0f;
}
else
{
l.useDistanceAttenuation = 1.0f;
l.positionWS = light.light.transform.position;
l.invSqrAttenuationRadius = 1.0f / (light.range * light.range);
}
l.color.Set(lightColorR, lightColorG, lightColorB);
l.color = new Vector3(lightColorR, lightColorG, lightColorB);
l.forward = light.light.transform.forward; // Note: Light direction is oriented backward (-Z)
l.up = light.light.transform.up;
l.right = light.light.transform.right;
l.forward = light.light.transform.forward; // Note: Light direction is oriented backward (-Z)
l.up = light.light.transform.up;
l.right = light.light.transform.right;
if (light.lightType == LightType.Spot)
{
var spotAngle = light.light.spotAngle;
if (light.lightType == LightType.Spot)
{
var spotAngle = light.light.spotAngle;
var innerConePercent = AdditionalLightData.GetInnerSpotPercent01(additionalData);
var cosSpotOuterHalfAngle = Mathf.Clamp(Mathf.Cos(spotAngle * 0.5f * Mathf.Deg2Rad), 0.0f, 1.0f);
var cosSpotInnerHalfAngle = Mathf.Clamp(Mathf.Cos(spotAngle * 0.5f * innerConePercent * Mathf.Deg2Rad), 0.0f, 1.0f); // inner cone
var val = Mathf.Max(0.001f, (cosSpotInnerHalfAngle - cosSpotOuterHalfAngle));
l.angleScale = 1.0f / val;
l.angleOffset = -cosSpotOuterHalfAngle * l.angleScale;
}
else
{
// 1.0f, 2.0f are neutral value allowing GetAngleAnttenuation in shader code to return 1.0
l.angleScale = 1.0f;
l.angleOffset = 2.0f;
}
var innerConePercent = additionalData.GetInnerSpotPercent01();
var cosSpotOuterHalfAngle = Mathf.Clamp(Mathf.Cos(spotAngle * 0.5f * Mathf.Deg2Rad), 0.0f, 1.0f);
var cosSpotInnerHalfAngle = Mathf.Clamp(Mathf.Cos(spotAngle * 0.5f * innerConePercent * Mathf.Deg2Rad), 0.0f, 1.0f); // inner cone
l.diffuseScale = AdditionalLightData.GetAffectDiffuse(additionalData) ? 1.0f : 0.0f;
l.specularScale = AdditionalLightData.GetAffectSpecular(additionalData) ? 1.0f : 0.0f;
l.shadowDimmer = AdditionalLightData.GetShadowDimmer(additionalData);
var val = Mathf.Max(0.001f, (cosSpotInnerHalfAngle - cosSpotOuterHalfAngle));
l.angleScale = 1.0f / val;
l.angleOffset = -cosSpotOuterHalfAngle * l.angleScale;
}
else
{
// 1.0f, 2.0f are neutral value allowing GetAngleAnttenuation in shader code to return 1.0
l.angleScale = 1.0f;
l.angleOffset = 2.0f;
}
l.IESIndex = -1;
l.cookieIndex = -1;
l.shadowIndex = -1;
l.diffuseScale = additionalData.affectDiffuse ? 1.0f : 0.0f;
l.specularScale = additionalData.affectSpecular ? 1.0f : 0.0f;
l.shadowDimmer = additionalData.shadowDimmer;
// Setup shadow data arrays
bool hasShadows = shadowOutput.GetShadowSliceCountLightIndex(lightIndex) != 0;
bool hasNotReachMaxLimit = shadows.Count + (light.lightType == LightType.Point ? 6 : 1) <= MaxShadows;
l.IESIndex = -1;
l.cookieIndex = -1;
l.shadowIndex = -1;
if (hasShadows && hasNotReachMaxLimit) // Note < MaxShadows should be check at shadowOutput creation
{
// When we have a point light, we assumed that there is 6 consecutive PunctualShadowData
l.shadowIndex = shadows.Count;
// Setup shadow data arrays
bool hasShadows = shadowOutput.GetShadowSliceCountLightIndex(lightIndex) != 0;
bool hasNotReachMaxLimit = shadows.Count + (light.lightType == LightType.Point ? 6 : 1) <= MaxShadows;
for (int sliceIndex = 0; sliceIndex < shadowOutput.GetShadowSliceCountLightIndex(lightIndex); ++sliceIndex)
if (hasShadows && hasNotReachMaxLimit) // Note < MaxShadows should be check at shadowOutput creation
PunctualShadowData s = new PunctualShadowData();
// When we have a point light, we assumed that there is 6 consecutive PunctualShadowData
l.shadowIndex = shadows.Count;
for (int sliceIndex = 0; sliceIndex < shadowOutput.GetShadowSliceCountLightIndex(lightIndex); ++sliceIndex)
{
PunctualShadowData s = new PunctualShadowData();
int shadowSliceIndex = shadowOutput.GetShadowSliceIndex(lightIndex, sliceIndex);
s.worldToShadow = shadowOutput.shadowSlices[shadowSliceIndex].shadowTransform.transpose; // Transpose for hlsl reading ?
int shadowSliceIndex = shadowOutput.GetShadowSliceIndex(lightIndex, sliceIndex);
s.worldToShadow = shadowOutput.shadowSlices[shadowSliceIndex].shadowTransform.transpose; // Transpose for hlsl reading ?
if (light.lightType == LightType.Spot)
{
s.shadowType = ShadowType.Spot;
}
else if (light.lightType == LightType.Point)
{
s.shadowType = ShadowType.Point;
}
else
{
s.shadowType = ShadowType.Directional;
}
if (light.lightType == LightType.Spot)
{
s.shadowType = ShadowType.Spot;
}
else if (light.lightType == LightType.Point)
{
s.shadowType = ShadowType.Point;
}
else
{
s.shadowType = ShadowType.Directional;
}
s.bias = light.light.shadowBias;
s.bias = light.light.shadowBias;
shadows.Add(s);
shadows.Add(s);
}
pLights.Add(l);
pLights.Add(l);
}
s_punctualLightList.SetData(pLights.ToArray());

3
Assets/ScriptableRenderLoop/HDRenderLoop/Lighting/SinglePass/SinglePassLoop.hlsl


float3 iblDiffuseLighting = float3(0.0, 0.0, 0.0);
float3 iblSpecularLighting = float3(0.0, 0.0, 0.0);
float weightDiffuse = 0.0;
float weightSpecular = 0.0;
for (i = 0; i < _EnvLightCount; ++i)
{

diffuseLighting += iblDiffuseLighting;
specularLighting += iblSpecularLighting;
// Add indirect diffuse + emissive (if any)
diffuseLighting += bakeDiffuseLighting;
}

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


// TODO: Check if anisotropy with a dynamic if on anisotropy > 0 is performant. Because it may mean we always calculate both isotropy and anisotropy case.
// Maybe we should always calculate anisotropy in case of standard ? Don't think the compile can optimize correctly.
// TODO: How can I declare a sampler for this one that is bilinear filtering
// TODO: I haven't configure this sampler in the code, we should be able to do it (but Unity don't allow it for now...)
// By default Unity provide MIG_MAG_LINEAR_POINT sampler, so it fit with our need.
#define SRL_PointSampler sampler_PreIntegratedFGD // Used for all textures
#define SRL_BilinearSampler sampler_PreIntegratedFGD // Used for all textures
// TODO: This one should be set into a constant Buffer at pass frequency (with _Screensize)
TEXTURE2D(_PreIntegratedFGD);

// _PreIntegratedFGD.y = Gv * Fc
// Pre integrate DisneyDiffuse FGD:
// _PreIntegratedFGD.z = DisneyDiffuse
float3 preFGD = SAMPLE_TEXTURE2D_LOD(_PreIntegratedFGD, SRL_PointSampler, float2(NdotV, perceptualRoughness), 0).xyz;
float3 preFGD = SAMPLE_TEXTURE2D_LOD(_PreIntegratedFGD, SRL_BilinearSampler, float2(NdotV, perceptualRoughness), 0).xyz;
// f0 * Gv * (1 - Fc) + Gv * Fc
specularFGD = fresnel0 * preFGD.x + preFGD.y;

// UVs for sampling the LUTs
// TODO: Test with fastAcos
float theta = acos(dot(bsdfData.normalWS, V));
// Scale and bias for the current precomputed table
// Scale and bias for the current precomputed table - the constant use here are the one that have been use when the table in LtcData.DisneyDiffuse.cs and LtcData.GGX.cs was use
float2 uv = 0.0078125 + 0.984375 * float2(bsdfData.perceptualRoughness, theta * INV_HALF_PI);
// Get the inverse LTC matrix for GGX

preLightData.ltcXformGGX._m00_m02_m11_m20 = SAMPLE_TEXTURE2D_LOD(_LtcGGXMatrix, SRL_PointSampler, uv, 0);
preLightData.ltcXformGGX._m00_m02_m11_m20 = SAMPLE_TEXTURE2D_LOD(_LtcGGXMatrix, SRL_BilinearSampler, uv, 0);
preLightData.ltcXformDisneyDiffuse._m00_m02_m11_m20 = SAMPLE_TEXTURE2D_LOD(_LtcDisneyDiffuseMatrix, SRL_PointSampler, uv, 0);
preLightData.ltcXformDisneyDiffuse._m00_m02_m11_m20 = SAMPLE_TEXTURE2D_LOD(_LtcDisneyDiffuseMatrix, SRL_BilinearSampler, uv, 0);
preLightData.ltcGGXFresnelMagnitudeDiff = SAMPLE_TEXTURE2D_LOD(_LtcMultiGGXFresnelDisneyDiffuse, SRL_PointSampler, uv, 0).r;
preLightData.ltcGGXFresnelMagnitude = SAMPLE_TEXTURE2D_LOD(_LtcMultiGGXFresnelDisneyDiffuse, SRL_PointSampler, uv, 0).g;
preLightData.ltcDisneyDiffuseMagnitude = SAMPLE_TEXTURE2D_LOD(_LtcMultiGGXFresnelDisneyDiffuse, SRL_PointSampler, uv, 0).b;
float3 ltcMagnitude = SAMPLE_TEXTURE2D_LOD(_LtcMultiGGXFresnelDisneyDiffuse, SRL_BilinearSampler, uv, 0).r;
preLightData.ltcGGXFresnelMagnitudeDiff = ltcMagnitude.r;
preLightData.ltcGGXFresnelMagnitude = ltcMagnitude.g;
preLightData.ltcDisneyDiffuseMagnitude = ltcMagnitude.b;
// Shadow
preLightData.unPositionSS = coord.unPositionSS;

ltcValue *= lightData.specularScale;
specularLighting = fresnelTerm * lightData.color * ltcValue;
}
#endif
}

65
Assets/TestScenes/HDTest/HDRenderLoopTest.unity


m_Name:
m_EditorClassIdentifier:
shadowResolution: 512
innerSpotPercent: 49
shadowDimmer: 1
affectDiffuse: 1
affectSpecular: 1

m_Component:
- component: {fileID: 605581071}
- component: {fileID: 605581070}
- component: {fileID: 605581072}
m_Layer: 0
m_Name: Directional light
m_TagString: Untagged

m_Father: {fileID: 0}
m_RootOrder: 11
m_LocalEulerAnglesHint: {x: 142.28, y: 106.256996, z: -160.70999}
--- !u!114 &605581072
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 605581069}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7a68c43fe1f2a47cfa234b5eeaa98012, type: 3}
m_Name:
m_EditorClassIdentifier:
shadowResolution: 512
shadowDimmer: 1
affectDiffuse: 1
affectSpecular: 1
treatAsAreaLight: 0
isDoubleSided: 0
areaLightLength: 0
areaLightWidth: 0
--- !u!1 &609148480
GameObject:
m_ObjectHideFlags: 0

m_Name:
m_EditorClassIdentifier:
shadowResolution: 512
innerSpotPercent: 0
shadowDimmer: 1
affectDiffuse: 1
affectSpecular: 1

m_Component:
- component: {fileID: 785457126}
- component: {fileID: 785457125}
- component: {fileID: 785457127}
m_Layer: 0
m_Name: Point light (2)
m_TagString: Untagged

m_Father: {fileID: 0}
m_RootOrder: 18
m_LocalEulerAnglesHint: {x: -0.39200002, y: 1.033, z: -0.92}
--- !u!114 &785457127
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 785457124}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7a68c43fe1f2a47cfa234b5eeaa98012, type: 3}
m_Name:
m_EditorClassIdentifier:
shadowResolution: 512
shadowDimmer: 1
affectDiffuse: 1
affectSpecular: 1
treatAsAreaLight: 0
isDoubleSided: 0
areaLightLength: 0
areaLightWidth: 0
--- !u!1 &818701168
GameObject:
m_ObjectHideFlags: 0

m_Name:
m_EditorClassIdentifier:
shadowResolution: 512
innerSpotPercent: 0
shadowDimmer: 1
affectDiffuse: 1
affectSpecular: 1

m_Name:
m_EditorClassIdentifier:
shadowResolution: 512
innerSpotPercent: 49
shadowDimmer: 1
affectDiffuse: 1
affectSpecular: 1

m_Component:
- component: {fileID: 1220024535}
- component: {fileID: 1220024534}
- component: {fileID: 1220024536}
m_Layer: 0
m_Name: Point light
m_TagString: Untagged

m_Father: {fileID: 0}
m_RootOrder: 8
m_LocalEulerAnglesHint: {x: -0.39200002, y: 1.033, z: -0.92}
--- !u!114 &1220024536
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1220024533}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7a68c43fe1f2a47cfa234b5eeaa98012, type: 3}
m_Name:
m_EditorClassIdentifier:
shadowResolution: 512
shadowDimmer: 1
affectDiffuse: 1
affectSpecular: 1
treatAsAreaLight: 0
isDoubleSided: 0
areaLightLength: 0
areaLightWidth: 0
--- !u!1 &1247488469
GameObject:
m_ObjectHideFlags: 0

m_Name:
m_EditorClassIdentifier:
shadowResolution: 512
innerSpotPercent: 0
shadowDimmer: 1
affectDiffuse: 1
affectSpecular: 1

9
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders.meta


fileFormatVersion: 2
guid: fafbb144d7f66074785b7727293d89c5
folderAsset: yes
timeCreated: 1474297943
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

/Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LtcData.DisneyDiffuse.cs → /Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LtcData.DisneyDiffuse.cs

/Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LtcData.DisneyDiffuse.cs.meta → /Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LtcData.DisneyDiffuse.cs.meta

/Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LtcData.GGX.cs → /Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LtcData.GGX.cs

/Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/Lit/LtcData.GGX.cs.meta → /Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/LtcData.GGX.cs.meta

正在加载...
取消
保存