Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camera override API #170

Merged
merged 8 commits into from
May 6, 2020
Prev Previous commit
Next Next commit
Fixed camera override in scene view
  • Loading branch information
alelievr committed Apr 20, 2020
commit b386cc5f12589731f0d2516bf008822e1d45a034
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ internal void Update(FrameSettings currentFrameSettings, HDRenderPipeline hdrp,
}
else
{
finalViewport = new Rect(camera.pixelRect.x, camera.pixelRect.y, camera.pixelWidth, camera.pixelHeight);
finalViewport = GetPixelRect();
}

actualWidth = Math.Max((int)finalViewport.size.x, 1);
Expand Down Expand Up @@ -769,6 +769,9 @@ internal void UpdateCurrentSky(SkyManager skyManager)
}
}
}

internal void OverridePixelRect(Rect newPixelRect) => m_OverridePixelRect = newPixelRect;
internal void ResetPixelRect() => m_OverridePixelRect = null;
#endregion

#region Private API
Expand Down Expand Up @@ -800,6 +803,7 @@ public RTHandle Allocator(string id, int frameIndex, RTHandleSystem rtHandleSyst
IEnumerator<Action<RenderTargetIdentifier, CommandBuffer>> m_RecorderCaptureActions;
int m_RecorderTempRT = Shader.PropertyToID("TempRecorder");
MaterialPropertyBlock m_RecorderPropertyBlock = new MaterialPropertyBlock();
Rect? m_OverridePixelRect = null;

void SetupCurrentMaterialQuality(CommandBuffer cmd)
{
Expand Down Expand Up @@ -1234,6 +1238,14 @@ void ReleaseHistoryBuffer()
{
m_HistoryRTSystem.ReleaseAll();
}

Rect GetPixelRect()
{
if (m_OverridePixelRect != null)
return m_OverridePixelRect.Value;
else
return camera.pixelRect;
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4766,6 +4766,8 @@ public struct OverrideCameraRendering : IDisposable
{
CommandBuffer cmd;
Camera overrideCamera;
HDCamera overrideHDCamera;
float originalAspect;

/// <summary>
/// Overrides the current camera, changing all the matrices and view parameters for the new one.
Expand All @@ -4784,17 +4786,27 @@ public OverrideCameraRendering(CommandBuffer cmd, Camera overrideCamera)
{
this.cmd = cmd;
this.overrideCamera = overrideCamera;
this.overrideHDCamera = null;
this.originalAspect = 0;

if (!IsContextValid(overrideCamera))
return;

var hdrp = HDRenderPipeline.currentPipeline;
var hdCamera = HDCamera.GetOrCreate(overrideCamera);
overrideHDCamera = HDCamera.GetOrCreate(overrideCamera);

// We need to patch the pixel rect of the camera because by default the camera size is synchronized
// with the game view and so it breaks in the scene view. Note that we can't use Camera.pixelRect here
// because when we assign it, the change is not instantaneous and is not reflected in pixelWidth/pixelHeight.
overrideHDCamera.OverridePixelRect(hdrp.m_CurrentHDCamera.camera.pixelRect);
// We also sync the aspect ratio of the camera, this time using the camera instead of HDCamera.
// This will update the projection matrix to match the aspect of the current rendering camera.
originalAspect = overrideCamera.aspect;
overrideCamera.aspect = (float)hdrp.m_CurrentHDCamera.camera.pixelRect.width / (float)hdrp.m_CurrentHDCamera.camera.pixelRect.height;

// Update HDCamera datas
hdCamera.Update(hdCamera.frameSettings, hdrp, hdrp.m_MSAASamples, hdrp.m_XRSystem.emptyPass);
hdCamera.UpdateAllViewConstants(false);
hdCamera.UpdateShaderVariablesGlobalCB(ref hdrp.m_ShaderVariablesGlobalCB, hdrp.m_FrameCount);
overrideHDCamera.Update(overrideHDCamera.frameSettings, hdrp, hdrp.m_MSAASamples, hdrp.m_XRSystem.emptyPass);
overrideHDCamera.UpdateShaderVariablesGlobalCB(ref hdrp.m_ShaderVariablesGlobalCB, hdrp.m_FrameCount);

ConstantBuffer.PushGlobal(cmd, hdrp.m_ShaderVariablesGlobalCB, HDShaderIDs._ShaderVariablesGlobal);
}
Expand Down Expand Up @@ -4823,6 +4835,9 @@ void IDisposable.Dispose()
if (!IsContextValid(overrideCamera))
return;

overrideHDCamera.ResetPixelRect();
overrideCamera.aspect = originalAspect;

var hdrp = HDRenderPipeline.currentPipeline;
hdrp.m_CurrentHDCamera.UpdateShaderVariablesGlobalCB(ref hdrp.m_ShaderVariablesGlobalCB, hdrp.m_FrameCount);
ConstantBuffer.PushGlobal(cmd, hdrp.m_ShaderVariablesGlobalCB, HDShaderIDs._ShaderVariablesGlobal);
Expand Down