浏览代码

cluster display lens flares work

/main
etienne cella 5 年前
当前提交
ac80bf0b
共有 4 个文件被更改,包括 62 次插入24 次删除
  1. 48
      Assets/ClusterDisplay/Scripts/CustomDepthBuffer.cs
  2. 15
      Assets/Scripts/LensFlare/HDRPLensFlare.cs
  3. 19
      Assets/Scripts/LensFlare/Shader/LensFlareHDRPCommon.hlsl
  4. 4
      Assets/Scripts/LensFlare/Shader/LensFlareHDRPPremultiplied.shader

48
Assets/ClusterDisplay/Scripts/CustomDepthBuffer.cs


[SerializeField]
bool m_ShowDebugUI;
RenderTexture m_DepthBuffer;
public RenderTexture target { get { return m_DepthBuffer; } }
Vector4 m_ZBufferParams;
public Vector4 zBufferParams { get { return m_ZBufferParams; } }
RenderTexture m_DepthBuffer;
public RenderTexture target { get { return m_DepthBuffer; } }
struct InstancedDrawArgs
{

return instance.target;
}
return null;
}
public static Vector4 GetZBufferParams(string name)
{
// we expect very few instances, optimize if it were to change
foreach (var instance in s_Instances)
{
if (instance.id == name)
return instance.zBufferParams;
}
return Vector4.zero;
}
class InstancingDataGenerationVisitor

m_CmdBuffer.SetGlobalMatrix("_CameraViewProjMatrix", viewProjectionMatrix);
m_CmdBuffer.SetGlobalVector("_WorldSpaceCameraPos", Vector3.zero);
// TODO fix saturated depth
var f = camera.farClipPlane;
var n = camera.nearClipPlane;
var zBufferParams = new Vector4(1 - f / n, f / n, 1 / f - 1 / n, 1 / n);
//var zBufferParams = new Vector4(-1 + f / n, 1, -1 / f + 1 / n, 1 / f);
//m_CmdBuffer.SetGlobalVector("_ZBufferParams", zBufferParams);
//m_CmdBuffer.SetGlobalDepthBias(0, 0);
foreach (var args in m_RenderingData)
{
for (var i = 0; i != args.mesh.subMeshCount; ++i)

static Vector4 GetZBufferParams(Camera camera)
{
float n = camera.nearClipPlane;
float f = camera.farClipPlane;
bool reverseZ = false;
// http://www.humus.name/temp/Linearize%20depth.txt
if (true)
{
return new Vector4(-1 + f / n, 1, -1 / f + 1 / n, 1 / f);
}
else
{
return new Vector4(1 - f / n, f / n, 1 / f - 1 / n, 1 / n);
}
}
m_ZBufferParams = GetZBufferParams(camera);
UpdateCommandBuffer(camera);
Graphics.ExecuteCommandBuffer(m_CmdBuffer);
}

15
Assets/Scripts/LensFlare/HDRPLensFlare.cs


[SerializeField]
Light m_Light;
[SerializeField]
string m_CustomDepthBufferId;
[Header("Global Settings")]
public float OcclusionRadius = 1.0f;
public float NearFadeStartDistance = 1.0f;

{
// Lazy!
UpdateVaryingAttributes();
// TODO optimize!
var depthBuffer = CustomDepthBuffer.GetTarget(m_CustomDepthBufferId);
var depthBufferZParams = CustomDepthBuffer.GetZBufferParams(m_CustomDepthBufferId);
if (depthBuffer != null)
{
foreach (var mat in m_MeshRenderer.materials)
{
mat.SetTexture("_CustomDepthTex", depthBuffer);
mat.SetVector("_CustomDepthZBufferParams", depthBufferZParams);
}
}
}
Mesh InitMesh()

19
Assets/Scripts/LensFlare/Shader/LensFlareHDRPCommon.hlsl


};
sampler2D _MainTex;
sampler2D _CustomDepthTex;
float4 _CustomDepthTex_TexelSize;
float4 _CustomDepthZBufferParams;
float _OccludedSizeScale;
// thanks, internets

float2 pos = screenPos + (samples[i] * radius * ratioScale);
pos = pos * 0.5 + 0.5;
pos.y = 1 - pos.y;
//float sampledDepth = LinearEyeDepth(SAMPLE_TEXTURE2D_LOD(_CameraDepthTexture, sampler_CameraDepthTexture, pos, 0).r, _ZBufferParams);
float sampledDepth = LinearEyeDepth(SampleCameraDepth(pos), _ZBufferParams);
float sampledDepth = LinearEyeDepth(tex2Dlod(_CustomDepthTex, float4(pos, 0, 0)).r, _CustomDepthZBufferParams);
if (sampledDepth >= depth)
contrib += sample_Contrib;
}

float4 extent = TransformWorldToHClip(GetCameraRelativePositionWS(v.worldPosRadius.xyz + cameraUp * v.worldPosRadius.w));
float2 screenPosLocal = clip.xy / clip.w;
float2 screenPos = CLIP_SPACE_GLOBAL(screenPosLocal);
float2 extentPos = extent.xy / extent.w;
float2 screenPos = CLIP_SPACE_GLOBAL(clip.xy / clip.w);
float2 extentPos = CLIP_SPACE_GLOBAL(extent.xy / extent.w);
float ratio = _ScreenParams.x / _ScreenParams.y;
float occlusion = GetOcclusion(screenPosLocal, depth - v.worldPosRadius.w, radius, ratio);
float ratio = _CustomDepthTex_TexelSize.z / _CustomDepthTex_TexelSize.w;
float occlusion = GetOcclusion(screenPos, depth - v.worldPosRadius.w, radius, ratio);
// Distance Fade
float4 d = v.lensflare_fadeData;

o.vertex.z = 1;
o.uv = v.uv;
o.color = v.color;// * occlusion * distanceFade * saturate(length(screenPos * 2));
o.color = v.color * occlusion * distanceFade * saturate(length(screenPos * 2));
return o;
}

4
Assets/Scripts/LensFlare/Shader/LensFlareHDRPPremultiplied.shader


float4 frag (v2f i) : SV_Target
{
float4 col = tex2D(_MainTex, i.uv);
return i.color;
float4 col = tex2D(_MainTex, i.uv);
return col * i.color;
}
ENDHLSL

正在加载...
取消
保存