浏览代码

Add support for directional light

all in one formula for spot, light and directional light
/main
Sebastien Lagarde 8 年前
当前提交
ef992f04
共有 8 个文件被更改,包括 129 次插入15 次删除
  1. 21
      Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs
  2. 2
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/LightDefinition.cs
  3. 9
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/DisneyGGX.hlsl
  4. 10
      Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/PostProcess/FinalPass.shader
  5. 71
      Assets/TestScenes/HDTest/HDRenderLoopTest.unity
  6. 2
      Assets/TestScenes/HDTest/Material/Terrain.mat
  7. 20
      Assets/ScriptableRenderLoop/fptl/ClusteredUtils.h.meta
  8. 9
      Assets/ScriptableRenderLoop/fptl/LightDefinitions.cs.hlsl.meta

21
Assets/ScriptableRenderLoop/HDRenderLoop/HDRenderLoop.cs


for (int lightIndex = 0; lightIndex < Math.Min(activeLights.Length, MaxLights); lightIndex++)
{
ActiveLight light = activeLights[lightIndex];
if (light.lightType == LightType.Spot || light.lightType == LightType.Point)
if (light.lightType == LightType.Spot || light.lightType == LightType.Point || light.lightType == LightType.Directional)
l.positionWS = light.light.transform.position;
l.invSqrAttenuationRadius = 1.0f / (light.range * light.range);
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);
}
// Correct intensity calculation (Different from Unity)
float lightColorR = light.light.intensity * Mathf.GammaToLinearSpace(light.light.color.r);

l.color = new Vec3(lightColorR, lightColorG, lightColorB);
// Light direction is opposite to the forward direction...
// Light direction is opposite to the forward direction
l.forward = -light.light.transform.forward;
// CAUTION: For IES as we inverse forward maybe this will need rotation.
l.up = light.light.transform.up;

// 1.0f, 2.0f are neutral value allowing GetAngleAnttenuation in shader code to return 1.0
l.angleScale = 1.0f;
l.angleOffset = 2.0f;
}
}
lights.Add(l);
punctualLightCount++;

2
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/LightDefinition.cs


public float invSqrAttenuationRadius;
public Vec3 color;
public float unused;
public float useDistanceAttenuation;
public Vec3 forward;
public float diffuseScale;

9
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/Material/DisneyGGX.hlsl


out float4 diffuseLighting,
out float4 specularLighting)
{
float3 unL = light.positionWS - positionWS;
// All punctual light type in the same formula, attenuation is neutral depends on light type.
// light.positionWS is the normalize light direction in case of directional light and invSqrAttenuationRadius is 0
// mean dot(unL, unL) = 1 and mean GetDistanceAttenuation() will return 1
// For point light and directional GetAngleAttenuation() return 1
float3 unL = light.positionWS - positionWS * light.useDistanceAttenuation;
// Always done, directional have it neutral
// Always done, point and dir have it neutral
attenuation *= GetAngleAttenuation(L, light.forward, light.angleScale, light.angleOffset);
float illuminance = saturate(dot(material.normalWS, L)) * attenuation;

10
Assets/ScriptableRenderLoop/HDRenderLoop/Shaders/PostProcess/FinalPass.shader


{
float4 c = tex2D(_MainTex, input.texcoord);
// Gamma correction
return LinearToSRGB(c);
// TODO: Currenlt in the editor there a an additional pass were the result is copyed in a render target RGBA8_sRGB.
// So we must not correct the sRGB here else it will be done two time.
// To fix!
// return LinearToSRGB(c);
return c;
}
ENDCG

71
Assets/TestScenes/HDTest/HDRenderLoopTest.unity


m_BounceIntensity: 1
m_ShadowRadius: 0
m_ShadowAngle: 0
--- !u!1 &605581069
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 605581071}
- component: {fileID: 605581070}
m_Layer: 0
m_Name: Directional light
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!108 &605581070
Light:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 605581069}
m_Enabled: 1
serializedVersion: 7
m_Type: 1
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_Intensity: 0.25
m_Range: 10
m_SpotAngle: 30
m_CookieSize: 10
m_Shadows:
m_Type: 0
m_Resolution: -1
m_CustomResolution: -1
m_Strength: 1
m_Bias: 0.05
m_NormalBias: 0.4
m_NearPlane: 0.2
m_Cookie: {fileID: 0}
m_DrawHalo: 0
m_Flare: {fileID: 0}
m_RenderMode: 0
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_Lightmapping: 4
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 1
m_ShadowRadius: 0
m_ShadowAngle: 0
--- !u!4 &605581071
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 605581069}
m_LocalRotation: {x: -0.15981111, y: 0.6031111, z: -0.31805873, w: -0.71383196}
m_LocalPosition: {x: -2.78, y: 0.88, z: -6.91}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 10
m_LocalEulerAnglesHint: {x: 142.28, y: 106.256996, z: -160.70999}
--- !u!1001 &653798720
Prefab:
m_ObjectHideFlags: 0

serializedVersion: 7
m_Type: 0
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_Intensity: 3.63
m_Intensity: 8
m_Range: 40.6
m_SpotAngle: 80
m_CookieSize: 10

serializedVersion: 7
m_Type: 2
m_Color: {r: 0.3784602, g: 0.7352941, b: 0.4842799, a: 1}
m_Intensity: 1
m_Range: 10
m_Intensity: 8
m_Range: 6.01
m_SpotAngle: 30
m_CookieSize: 10
m_Shadows:

m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1220024533}
m_LocalRotation: {x: -0.0034919123, y: 0.0089843245, z: -0.007998787, w: 0.99992156}
m_LocalPosition: {x: -0.41172194, y: 1.3984102, z: 1.4866586}
m_LocalPosition: {x: 0.19, y: 1.46, z: 1.26}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}

2
Assets/TestScenes/HDTest/Material/Terrain.mat


second: {r: 0.5, g: 0.5, b: 0.5, a: 1}
- first:
name: _DiffuseColor
second: {r: 0.5808823, g: 0.5808823, b: 0.5808823, a: 1}
second: {r: 0.7176471, g: 0.7176471, b: 0.7176471, a: 1}
- first:
name: _EmissionColor
second: {r: 0, g: 0, b: 0, a: 1}

20
Assets/ScriptableRenderLoop/fptl/ClusteredUtils.h.meta


fileFormatVersion: 2
guid: cbef1fe8de4d82246b1f88d4dcc90833
timeCreated: 1475179985
licenseType: Pro
PluginImporter:
serializedVersion: 1
iconMap: {}
executionOrder: {}
isPreloaded: 0
platformData:
Any:
enabled: 1
settings: {}
Editor:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

9
Assets/ScriptableRenderLoop/fptl/LightDefinitions.cs.hlsl.meta


fileFormatVersion: 2
guid: 70f1451131ffdfa4798fd3c62bd0ec51
timeCreated: 1475179983
licenseType: Pro
ShaderImporter:
defaultTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存