浏览代码

implement two pass refelection probe rendering to debug issues with probes

/main
Filip Iliescu 7 年前
当前提交
e45b7d3c
共有 2 个文件被更改,包括 157 次插入55 次删除
  1. 157
      Assets/ScriptableRenderPipeline/MobileRenderPipeline/ClassicDeferred/ClassicDeferredPipeline.cs
  2. 55
      Assets/ScriptableRenderPipeline/MobileRenderPipeline/ClassicDeferred/Internal-DeferredReflections.shader

157
Assets/ScriptableRenderPipeline/MobileRenderPipeline/ClassicDeferred/ClassicDeferredPipeline.cs


private static int s_GBufferZ;
private static int s_CameraTarget;
private static int s_CameraDepthTexture;
//private static int s_CameraReflectionsTexture;
private static int s_CameraReflectionsTexture;
private static int m_ReflectionsApplyPassNdx;
private Material m_DeferredMaterial;
private Material m_DeferredReflectionMaterial;

m_BlitMaterial = new Material (finalPassShader) { hideFlags = HideFlags.HideAndDontSave };
m_DeferredMaterial = new Material (deferredShader) { hideFlags = HideFlags.HideAndDontSave };
m_DeferredReflectionMaterial = new Material (deferredReflectionShader) { hideFlags = HideFlags.HideAndDontSave };
//m_DeferredReflectionMaterial.SetTexture("_CameraReflectionsTexture", s_CameraReflectionsTexture);
m_ReflectionsPassNdx = m_DeferredMaterial.FindPass ("DEFERRED_REFLECTIONS");
m_ReflectionsPassNdx = m_DeferredReflectionMaterial.FindPass ("DEFERRED_REFLECTIONS");
m_ReflectionsApplyPassNdx = m_DeferredReflectionMaterial.FindPass ("DEFERRED_APPLY_REFLECTIONS");
s_CameraReflectionsTexture = Shader.PropertyToID ("_CameraReflectionsTexture");
//shadows
m_MatWorldToShadow = new Matrix4x4[k_MaxLights * k_MaxShadowmapPerLights];
m_DirShadowSplitSpheres = new Vector4[k_MaxDirectionalSplit];

void RenderReflections(Camera camera, CommandBuffer cmd, CullResults cullResults, ScriptableRenderContext loop)
{
var probes = cullResults.visibleReflectionProbes;
var worldToView = camera.worldToCameraMatrix; //WorldToCamera(camera);
var worldToView = camera.worldToCameraMatrix; //WorldToCamera(camera);
// set global state
// TODO: need these?
// built in loop sets: rt->FilterMode(filterNearest), black clearcolor/clear, backfaceMode(Off),
// const float nearDistanceFudged = camera.GetProjectionNear() * 1.001f;
// const float farDistanceFudged = camera.GetProjectionFar() * 0.999f;
// const Vector3f viewDir = -NormalizeSafe(camera.GetCameraToWorldMatrix().GetAxisZ());
// Plane eyePlane;
// eyePlane.SetNormalAndPosition(viewDir, m_Context->m_CurCameraPos);
// backfacesRasterStateDesc.cullMode = kCullFront;
// backfacesDepthStateDesc.depthWrite = false;
// backfacesDepthStateDesc.depthFunc = kFuncGreater;
// defaultReflDepthStateDesc.depthWrite = false;
// defaultReflDepthStateDesc.depthFunc = kFuncAlways;
// device.SetStencilState(devStDisabled, 0);
// draw the base probe
{
var props = new MaterialPropertyBlock ();
props.SetFloat ("_LightAsQuad", 1.0f);
props.SetFloat ("_SrcBlend", 1.0f);
props.SetFloat ("_DstBlend", 0.0f);
// base reflection probe
var topCube = ReflectionProbe.defaultTexture;
var defdecode = ReflectionProbe.defaultTextureHDRDecodeValues;
cmd.SetGlobalTexture ("unity_SpecCube0", topCube);
cmd.SetGlobalVector ("unity_SpecCube0_HDR", defdecode);
// AABB infAABB(Vector3f::zero, Vector3f::infinityVec);
// Vector4f infMin(infAABB.CalculateMin(), 1.0f);
// Vector4f infMax(infAABB.CalculateMax(), 1.0f);
// cmd.SetGlobalVector("unity_SpecCube0_BoxMin", infMin);
// cmd.SetGlobalVector("unity_SpecCube0_BoxMax", infMax);
cmd.SetGlobalVector ("unity_SpecCube0_ProbePosition", new Vector4 (0.0f, 0.0f, 0.0f, 0.0f));
cmd.SetGlobalVector ("unity_SpecCube1_ProbePosition", new Vector4 (0.0f, 0.0f, 0.0f, 1.0f));
// skip sky
// GfxStencilState stencil;
// stencil.stencilEnable = true;
// stencil.stencilFuncFront = stencil.stencilFuncBack = kFuncEqual;
// stencil.readMask = kStencilMaskSomething;
// int stencilRef = kStencilMaskSomething;
// const DeviceStencilState* devStCheck = device.CreateStencilState(stencil);
// device.SetStencilState(devStCheck, stencilRef);
// LoadFullScreenOrthoMatrix(0.0f, camera.GetProjectionFar(), device);
// screenspace
cmd.DrawMesh (m_QuadMesh, Matrix4x4.identity, m_DeferredReflectionMaterial, 0, m_ReflectionsPassNdx, props);
}
// TODO: need this? --> Set the ambient probe into the SH constants otherwise
// SetSHConstants(builtins, m_LightprobeContext.ambientProbe);
// render all probes
var volType = LightDefinitions.BOX_LIGHT;
var cubemap = rl.texture;
// always a box for now

var bnds = rl.bounds;
var boxOffset = rl.center; // reflection volume offset relative to cube map capture point
var blendDistance = rl.blendDistance;
// var blendDistance = rl.blendDistance;
// var boxProj = (rl.boxProjection != 0);
// var decodeVals = rl.hdr;
//var boxProj = (rl.boxProjection != 0);
//var decodeVals = rl.hdr;
// var e = bnds.extents; // 0.5f * Vector3.Max(-boxSizes[p], boxSizes[p]);
//Vector3 C = bnds.center; // P + boxOffset;
// var e = bnds.extents; // 0.5f * Vector3.Max(-boxSizes[p], boxSizes[p]);
// Vector3 C = bnds.center; // P + boxOffset;
var Cw = worldToView.MultiplyPoint(C);
// var combinedExtent = e + new Vector3(blendDistance, blendDistance, blendDistance);
//
// Vector3 vx = mat.GetColumn(0);
// Vector3 vy = mat.GetColumn(1);
// Vector3 vz = mat.GetColumn(2);
//
// // transform to camera space (becomes a left hand coordinate frame in Unity since Determinant(worldToView)<0)
// vx = worldToView.MultiplyVector(vx);
// vy = worldToView.MultiplyVector(vy);
// vz = worldToView.MultiplyVector(vz);
var Cw = worldToView.MultiplyPoint(C);
//Debug.Log (String.Format("Reflection probe bounds: {0}", bnds.ToString()));
//Debug.Log (String.Format("Reflection probe center: {0}", boxOffset.ToString()));
bool renderAsQuad = false;
props.SetVector ("_LightPos",Cw);
props.SetFloat ("_LightAsQuad", renderAsQuad ? 1 : 0);
//props.SetFloat ("_LightAsQuad", renderAsQuad ? 1 : 0);
//props.SetVector ("_LightDir", new Vector4(lightDir.x, lightDir.y, lightDir.z, 0.0f));
//props.SetVector ("_LightColor", light.finalColor);
//props.SetMatrix ("_WorldToLight", lightToWorld.inverse);
cmd.SetGlobalTexture("unity_SpecCube0", cubemap);
cmd.SetGlobalVector("unity_SpecCube0_HDR", rl.probe.textureHDRDecodeValues);
cmd.SetGlobalVector ("unity_SpecCube0_BoxMin", rl.bounds.min);
cmd.SetGlobalVector ("unity_SpecCube0_BoxMax", rl.bounds.max);
cmd.SetGlobalVector ("unity_SpecCube0_ProbePosition", Cw);
cmd.DrawMesh (m_BoxMesh, Matrix4x4.identity, m_DeferredReflectionMaterial, 0, m_ReflectionsPassNdx, props);
cmd.DrawMesh (m_BoxMesh, mat, m_DeferredReflectionMaterial, 0, m_ReflectionsPassNdx, props);
}
void RenderApplyReflections(Camera camera, CommandBuffer cmd, CullResults cullResults, ScriptableRenderContext loop)
{
// draw offscreen accumulation buffer onto emission buffer
var props = new MaterialPropertyBlock ();
// props.SetFloat ("_LightAsQuad", 1);
cmd.SetGlobalTexture ("_CameraReflectionsTexture", s_CameraReflectionsTexture);
cmd.DrawMesh (m_QuadMesh, Matrix4x4.identity, m_DeferredReflectionMaterial, 0, m_ReflectionsApplyPassNdx, props);
}
void RenderShadowMaps(CullResults cullResults, ScriptableRenderContext loop)

void RenderLighting (Camera camera, CullResults inputs, ScriptableRenderContext loop)
{
var cmd = new CommandBuffer { name = "Lighting" };
{
var cmd = new CommandBuffer { name = "Reflections" };
// IF PLATFORM_MAC -- cannot use framebuffer fetch
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
cmd.SetRenderTarget (new RenderTargetIdentifier(s_GBufferEmission), new RenderTargetIdentifier (s_GBufferZ));
#endif
// setup offscreen render target for reflections
cmd.GetTemporaryRT (s_CameraReflectionsTexture, camera.pixelWidth, camera.pixelHeight, 0, FilterMode.Point, RenderTextureFormat.DefaultHDR, RenderTextureReadWrite.Linear);
cmd.SetRenderTarget (new RenderTargetIdentifier (s_CameraReflectionsTexture), new RenderTargetIdentifier (s_GBufferZ));
foreach (var light in inputs.visibleLights)
{
RenderLightGeometry (camera, light, cmd, loop);
RenderReflections (camera, cmd, inputs, loop);
loop.ExecuteCommandBuffer (cmd);
cmd.Dispose ();
RenderReflections(camera, cmd, inputs, loop);
{
var cmd = new CommandBuffer { name = "Lighting" };
loop.ExecuteCommandBuffer (cmd);
cmd.Dispose ();
// IF PLATFORM_MAC -- cannot use framebuffer fetch
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
cmd.SetRenderTarget (new RenderTargetIdentifier (s_GBufferEmission), new RenderTargetIdentifier (s_GBufferZ));
#endif
foreach (var light in inputs.visibleLights) {
RenderLightGeometry (camera, light, cmd, loop);
}
RenderApplyReflections (camera, cmd, inputs, loop);
loop.ExecuteCommandBuffer (cmd);
cmd.Dispose ();
}
}
void RenderLightGeometry (Camera camera, VisibleLight light, CommandBuffer cmd, ScriptableRenderContext loop)

if (cookie != null)
cmd.EnableShaderKeyword ("DIRECTIONAL_COOKIE");
cmd.DrawMesh (m_QuadMesh, Matrix4x4.identity, m_DeferredMaterial, 0, m_quadLightingPassNdx, props);
}
}

55
Assets/ScriptableRenderPipeline/MobileRenderPipeline/ClassicDeferred/Internal-DeferredReflections.shader


ZWrite Off
ZTest LEqual
Blend [_SrcBlend] [_DstBlend]
Blend One Zero
#pragma vertex vert_deferred
#pragma vertex filip_vert_deferred
#pragma fragment frag
#include "UnityCG.cginc"

sampler2D _CameraGBufferTexture1;
sampler2D _CameraGBufferTexture2;
#endif
unity_v2f_deferred filip_vert_deferred (float4 vertex : POSITION, float3 normal : NORMAL)
{
bool lightAsQuad = _LightAsQuad!=0.0;
unity_v2f_deferred o;
// scaling quasd by two becuase built-in unity quad ranges from -0.5 to 0.5
o.pos = lightAsQuad ? float4(2.0*vertex.xy, 0.5, 1.0) : UnityObjectToClipPos(vertex);
o.uv = ComputeScreenPos(o.pos);
// normal contains a ray pointing from the camera to one of near plane's
// corners in camera space when we are drawing a full screen quad.
// Otherwise, when rendering 3D shapes, use the ray calculated here.
if (lightAsQuad){
float2 rayXY = mul(unity_CameraInvProjection, float4(o.pos.x, -o.pos.y, -1, 1)).xy;
o.ray = float3(rayXY, 1.0);
}
else
{
o.ray = UnityObjectToViewPos(vertex) * float3(-1,-1,1);
}
return o;
}
half3 distanceFromAABB(half3 p, half3 aabbMin, half3 aabbMax)
{

#ifdef UNITY_FRAMEBUFFER_FETCH_AVAILABLE
void frag (unity_v2f_deferred i,
in half4 outGBuffer0 : SV_Target0,

half4 frag (unity_v2f_deferred i) : SV_TARGET
#endif
{
//return half4(1.0, 0.0, 0.0, 1.0);
// Stripped from UnityDeferredCalculateLightParams, refactor into function ?
i.ray = i.ray * (_ProjectionParams.z / i.ray.z);
float2 uv = i.uv.xy / i.uv.w;

// Adds reflection buffer to the lighting buffer
Pass
{
Name "DEFERRED_APPLY_REFLECTIONS"
Blend [_SrcBlend] [_DstBlend]
Blend One One
#pragma vertex vert
#pragma vertex refl_apply_vert_deferred
#pragma fragment frag
#pragma multi_compile ___ UNITY_HDR_ON

float4 pos : SV_POSITION;
};
v2f vert (float4 vertex : POSITION)
{
v2f o;
o.pos = UnityObjectToClipPos(vertex);
o.uv = ComputeScreenPos (o.pos).xy;
return o;
}
v2f refl_apply_vert_deferred (float4 vertex : POSITION, float3 normal : NORMAL)
{
// scaling quasd by two becuase built-in unity quad ranges from -0.5 to 0.5
v2f o;
o.pos = float4(2.0*vertex.xy, 0.5, 1.0);
o.uv = ComputeScreenPos(o.pos);
return o;
}
return half4(1.0, 0.0, 0.0, 1.0);
half4 c = tex2D (_CameraReflectionsTexture, i.uv);
#ifdef UNITY_HDR_ON
return float4(c.rgb, 0.0f);

正在加载...
取消
保存