浏览代码

Enable LTC area light code path

/main
Evgenii Golubev 8 年前
当前提交
731e1f23
共有 4 个文件被更改,包括 114 次插入35 次删除
  1. 34
      Assets/ScriptableRenderLoop/AdditionalLightData.cs
  2. 88
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  3. 6
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/SinglePass/SinglePass.hlsl
  4. 21
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/SinglePass/SinglePassLoop.hlsl

34
Assets/ScriptableRenderLoop/AdditionalLightData.cs


public bool affectDiffuse = true;
public bool affectSpecular = true;
// Area Light Hack
public bool treatAsAreaLight = false;
public bool isDoubleSided = false;
[RangeAttribute(0.0F, 20.0f)]
public float areaLightLength = 0.0f;
[RangeAttribute(0.0F, 20.0f)]
public float areaLightWidth = 0.0f;
public static float GetInnerSpotPercent01(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);
}
}
}

88
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


static private int s_CameraDepthBuffer;
static private ComputeBuffer s_punctualLightList;
static private ComputeBuffer s_areaLightList;
static private ComputeBuffer s_envLightList;
static private ComputeBuffer s_punctualShadowList;

if (s_punctualLightList != null)
s_punctualLightList.Release();
if (s_areaLightList != null)
s_areaLightList.Release();
if (s_punctualShadowList != null)
s_punctualShadowList.Release();

{
ClearComputeBuffers();
s_CameraColorBuffer = Shader.PropertyToID("_CameraColorTexture");
s_CameraDepthBuffer = Shader.PropertyToID("_CameraDepthTexture");
s_CameraColorBuffer = Shader.PropertyToID("_CameraColorTexture");
s_CameraDepthBuffer = Shader.PropertyToID("_CameraDepthTexture");
s_punctualLightList = new ComputeBuffer(MaxLights, System.Runtime.InteropServices.Marshal.SizeOf(typeof(PunctualLightData)));
s_envLightList = new ComputeBuffer(MaxLights, System.Runtime.InteropServices.Marshal.SizeOf(typeof(EnvLightData)));
s_punctualLightList = new ComputeBuffer(MaxLights, System.Runtime.InteropServices.Marshal.SizeOf(typeof(PunctualLightData)));
s_areaLightList = new ComputeBuffer(MaxLights, System.Runtime.InteropServices.Marshal.SizeOf(typeof(AreaLightData)));
s_envLightList = new ComputeBuffer(MaxLights, System.Runtime.InteropServices.Marshal.SizeOf(typeof(EnvLightData)));
m_DeferredMaterial = CreateEngineMaterial("Hidden/HDRenderLoop/Deferred");
m_FinalPassMaterial = CreateEngineMaterial("Hidden/HDRenderLoop/FinalPass");
m_DeferredMaterial = CreateEngineMaterial("Hidden/HDRenderLoop/Deferred");
m_FinalPassMaterial = CreateEngineMaterial("Hidden/HDRenderLoop/FinalPass");
// Debug
m_DebugViewMaterialGBuffer = CreateEngineMaterial("Hidden/HDRenderLoop/DebugViewMaterialGBuffer");

m_LitRenderLoop.OnDisable();
s_punctualLightList.Release();
s_areaLightList.Release();
if (m_DeferredMaterial) DestroyImmediate(m_DeferredMaterial);
if (m_DeferredMaterial) DestroyImmediate(m_DeferredMaterial);
if (m_FinalPassMaterial) DestroyImmediate(m_FinalPassMaterial);
m_cubeReflTexArray.Release();

void UpdatePunctualLights(VisibleLight[] visibleLights, ref ShadowOutput shadowOutput)
{
var lights = new List<PunctualLightData>();
var pLights = new List<PunctualLightData>();
var aLights = new List<AreaLightData>();
for (int lightIndex = 0; lightIndex < Math.Min(visibleLights.Length, MaxLights); lightIndex++)
for (int lightIndex = 0, numLights = Math.Min(visibleLights.Length, MaxLights); lightIndex < numLights; ++lightIndex)
if (light.lightType != LightType.Spot && light.lightType != LightType.Point && light.lightType != LightType.Directional)
if (light.lightType == LightType.Area) {
// Skip area lights which are currently only used for baking.
}
var additionalLightData = light.light.GetComponent<AdditionalLightData>();
// Correct intensity calculation (different from Unity)
var lightColorR = light.light.intensity * Mathf.GammaToLinearSpace(light.light.color.r);
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>();
// Test whether we should treat this punctual light as an area light.
// It's a temporary hack until the proper UI support is added.
if (AdditionalLightData.GetTreatAsAreaLight(additionalData))
{
AreaLightData lightData = new AreaLightData();
// TODO: add AreaShapeType.Line support for small widths.
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.color = new Vector3(lightColorR, lightColorG, lightColorB);
lightData.diffuseScale = additionalData.affectDiffuse ? 1.0f : 0.0f;
lightData.specularScale = additionalData.affectSpecular ? 1.0f : 0.0f;
lightData.shadowDimmer = additionalData.shadowDimmer;
lightData.invSqrAttenuationRadius = 1.0f / (light.range * light.range);
aLights.Add(lightData);
// TODO: shadows.
continue;
}
var l = new PunctualLightData();

l.invSqrAttenuationRadius = 1.0f / (light.range * light.range);
}
// Correct intensity calculation (Different from Unity)
var lightColorR = light.light.intensity * Mathf.GammaToLinearSpace(light.light.color.r);
var lightColorG = light.light.intensity * Mathf.GammaToLinearSpace(light.light.color.g);
var lightColorB = light.light.intensity * Mathf.GammaToLinearSpace(light.light.color.b);
l.color.Set(lightColorR, lightColorG, lightColorB);

{
var spotAngle = light.spotAngle;
var innerConePercent = AdditionalLightData.GetInnerSpotPercent01(additionalLightData);
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

l.angleOffset = 2.0f;
}
l.diffuseScale = AdditionalLightData.GetAffectDiffuse(additionalLightData) ? 1.0f : 0.0f;
l.specularScale = AdditionalLightData.GetAffectSpecular(additionalLightData) ? 1.0f : 0.0f;
l.shadowDimmer = AdditionalLightData.GetShadowDimmer(additionalLightData);
l.diffuseScale = AdditionalLightData.GetAffectDiffuse(additionalData) ? 1.0f : 0.0f;
l.specularScale = AdditionalLightData.GetAffectSpecular(additionalData) ? 1.0f : 0.0f;
l.shadowDimmer = AdditionalLightData.GetShadowDimmer(additionalData);
l.IESIndex = -1;
l.cookieIndex = -1;

}
}
lights.Add(l);
pLights.Add(l);
s_punctualLightList.SetData(lights.ToArray());
s_punctualLightList.SetData(pLights.ToArray());
s_areaLightList.SetData(aLights.ToArray());
Shader.SetGlobalInt("_PunctualLightCount", lights.Count);
Shader.SetGlobalBuffer("_AreaLightList", s_areaLightList);
Shader.SetGlobalInt("_PunctualLightCount", pLights.Count);
Shader.SetGlobalInt("_AreaLightCount", aLights.Count);
Shader.SetGlobalBuffer("_PunctualShadowList", s_punctualShadowList);
}

6
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/SinglePass/SinglePass.hlsl


// Constant and structure declaration
// ----------------------------------------------------------------------------
StructuredBuffer<PunctualLightData> _PunctualLightList;
StructuredBuffer<EnvLightData> _EnvLightList;
StructuredBuffer<PunctualLightData> _PunctualLightList;
StructuredBuffer<AreaLightData> _AreaLightList;
StructuredBuffer<EnvLightData> _EnvLightList;
StructuredBuffer<PunctualShadowData> _PunctualShadowList;
//TEXTURE2D_ARRAY(_ShadowArray);

CBUFFER_START(UnityPerLightLoop)
int _PunctualLightCount;
int _AreaLightCount;
int _EnvLightCount;
EnvLightData _EnvLightSky;
float4 _ShadowMapSize;

21
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Lighting/SinglePass/SinglePassLoop.hlsl


LightLoopContext context;
ZERO_INITIALIZE(LightLoopContext, context);
diffuseLighting = float4(0.0, 0.0, 0.0, 0.0);
diffuseLighting = float4(0.0, 0.0, 0.0, 0.0);
specularLighting = float4(0.0, 0.0, 0.0, 0.0);
for (int i = 0; i < _PunctualLightCount; ++i)

specularLighting += localSpecularLighting;
}
/*
for (int i = 0; i < 4; ++i)
for (int i = 0; i < _AreaLightCount; ++i)
float4 localDiffuseLighting;
float4 localSpecularLighting;
EvaluateBSDF_Area(0, V, positionWS, areaLightData[i], bsdfData, localDiffuseLighting, localSpecularLighting);
diffuseLighting += localDiffuseLighting;
specularLighting += localSpecularLighting;
float4 localDiffuseLighting, localSpecularLighting;
EvaluateBSDF_Area(context, V, positionWS, prelightData, _AreaLightList[i], bsdfData, localDiffuseLighting, localSpecularLighting);
diffuseLighting += localDiffuseLighting;
specularLighting += localSpecularLighting;
*/
float4 iblDiffuseLighting = float4(0.0, 0.0, 0.0, 0.0);
float4 iblDiffuseLighting = float4(0.0, 0.0, 0.0, 0.0);
float4 iblSpecularLighting = float4(0.0, 0.0, 0.0, 0.0);
for (int j = 0; j < _EnvLightCount; ++j)

}
*/
diffuseLighting += iblDiffuseLighting;
diffuseLighting += iblDiffuseLighting;
specularLighting += iblSpecularLighting;
diffuseLighting.rgb += bakeDiffuseLighting;
正在加载...
取消
保存