Skip to content

Commit f0c198a

Browse files
committed
fixed Offscreen rendering mutli-camera issues.
1 parent d651229 commit f0c198a

File tree

5 files changed

+83
-68
lines changed

5 files changed

+83
-68
lines changed

source/FrameRecorder/Core/Engine/Recorder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public virtual void SessionCreated(RecordingSession session)
7979
var input = Activator.CreateInstance(inputSettings.inputType) as RecorderInput;
8080
input.settings = inputSettings;
8181
m_Inputs.Add(input);
82+
SignalInputsOfStage(ERecordingSessionStage.SessionCreated, session);
8283
}
8384
}
8485

source/FrameRecorder/Core/Engine/RecordingSession.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public bool SessionCreated()
4141
m_RecordingStartTS = (Time.time / Time.timeScale);
4242

4343
m_Recorder.SessionCreated(this);
44-
m_Recorder.SignalInputsOfStage(ERecordingSessionStage.SessionCreated, this);
4544
return true;
4645
}
4746

source/FrameRecorder/Inputs/Adam/Engine/AdamBeautyInput.cs

Lines changed: 54 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,12 @@ public override void BeginRecording(RecordingSession session)
124124
m_normalizeMaterial.hideFlags = HideFlags.DontSave;
125125

126126
m_renderRT = new RenderTexture(m_renderWidth, m_renderHeight, 24, RenderTextureFormat.DefaultHDR, RenderTextureReadWrite.Linear);
127-
if (adamSettings.m_SuperSampling != ESuperSamplingCount.x1)
127+
m_renderRT.wrapMode = TextureWrapMode.Clamp;
128+
for (int i = 0; i < 2; ++i)
128129
{
129-
for (int i = 0; i < 2; ++i)
130-
{
131-
m_accumulateRTs[i] = new RenderTexture(m_renderWidth, m_renderHeight, 0, RenderTextureFormat.DefaultHDR, RenderTextureReadWrite.Linear);
132-
m_accumulateRTs[i].Create();
133-
}
130+
m_accumulateRTs[i] = new RenderTexture(m_renderWidth, m_renderHeight, 0, RenderTextureFormat.DefaultHDR, RenderTextureReadWrite.Linear);
131+
m_accumulateRTs[i].wrapMode = TextureWrapMode.Clamp;
132+
m_accumulateRTs[i].Create();
134133
}
135134
var rt = new RenderTexture(m_outputWidth, m_outputHeight, 0, RenderTextureFormat.Default, RenderTextureReadWrite.sRGB);
136135
rt.Create();
@@ -158,17 +157,19 @@ public override void NewFrameStarting(RecordingSession session)
158157
// Should we keep it?
159158
if (cam.targetDisplay != 0 || !cam.enabled)
160159
{
160+
UnityHelpers.Destroy(cam.targetTexture);
161161
cam.targetTexture = hookedCam.textureBackup;
162162
m_hookedCameras.Remove(hookedCam);
163163
}
164164
continue;
165165
}
166166

167-
if (!cam.enabled || cam.targetDisplay != 0)
167+
if (!cam.enabled || !cam.gameObject.active || cam.targetDisplay != 0)
168168
continue;
169169

170170
hookedCam = new HookedCamera() { camera = cam, textureBackup = cam.targetTexture };
171-
cam.targetTexture = m_renderRT;
171+
var camRT = new RenderTexture((int)(m_renderWidth * cam.rect.width), (int)(m_renderHeight * cam.rect.height), 24, RenderTextureFormat.DefaultHDR, RenderTextureReadWrite.Linear);
172+
cam.targetTexture = camRT;
172173
m_hookedCameras.Add(hookedCam);
173174
sort = true;
174175
}
@@ -219,6 +220,8 @@ protected override void Dispose(bool disposing)
219220
{
220221
if (c != null)
221222
{
223+
if (c.camera.rect.width == 1f && c.camera.rect.height == 1f)
224+
UnityHelpers.Destroy(c.camera.targetTexture);
222225
c.camera.targetTexture = c.textureBackup;
223226
}
224227
}
@@ -268,79 +271,67 @@ void ShiftProjectionMatrix(Camera camera, Vector2 sample)
268271
camera.projectionMatrix = projectionMatrix;
269272
}
270273

271-
RenderTexture PerformSubSampling(Camera cam)
274+
bool CameraUsingPartialViewport(Camera cam)
272275
{
273-
var src = cam.targetTexture;
274-
src.wrapMode = TextureWrapMode.Clamp;
275-
src.filterMode = FilterMode.Point;
276-
277-
RenderTexture dst = null;
278-
279-
Graphics.SetRenderTarget(m_accumulateRTs[0]);
280-
GL.Clear(false, true, Color.black);
281-
282-
// Render n times the camera and accumulate renders.
283-
var oldProjectionMatrix = cam.projectionMatrix;
284-
for (int i = 0, n = (int)adamSettings.m_SuperSampling; i < n; i++)
285-
{
286-
ShiftProjectionMatrix(cam, m_samples[i] - new Vector2(0.5f, 0.5f));
287-
cam.Render();
288-
289-
var accumulateInto = m_accumulateRTs[(i + 1) % 2];
290-
var accumulatedWith = m_accumulateRTs[i % 2];
291-
m_accumulateMaterial.SetTexture("_PreviousTexture", accumulatedWith);
292-
Graphics.Blit(src, accumulateInto, m_accumulateMaterial);
293-
294-
dst = accumulateInto;
295-
}
296-
cam.projectionMatrix = oldProjectionMatrix;
297-
298-
return dst;
276+
return cam.rect.width != 1 || cam.rect.height != 1 || cam.rect.x != 0 || cam.rect.y != 0;
299277
}
300278

301279
void PerformSubSampling()
302280
{
303-
if (adamSettings.m_SuperSampling == ESuperSamplingCount.x1)
304-
return;
281+
RenderTexture accumulateInto = null;
282+
m_renderRT.wrapMode = TextureWrapMode.Clamp;
283+
m_renderRT.filterMode = FilterMode.Point;
305284

306-
if (m_hookedCameras.Count == 1)
307-
{
308-
Graphics.Blit(PerformSubSampling(m_hookedCameras[0].camera), m_renderRT);
309-
}
310-
else
311-
{
312-
RenderTexture accumulateInto = null;
313-
m_renderRT.wrapMode = TextureWrapMode.Clamp;
314-
m_renderRT.filterMode = FilterMode.Point;
285+
int x = 0;
286+
Graphics.SetRenderTarget(m_accumulateRTs[0]);
287+
GL.Clear(false, true, Color.black);
315288

316-
Graphics.SetRenderTarget(m_accumulateRTs[0]);
317-
GL.Clear(false, true, Color.black);
289+
foreach (var hookedCam in m_hookedCameras)
290+
{
291+
var cam = hookedCam.camera;
318292

319293
for (int i = 0, n = (int)adamSettings.m_SuperSampling; i < n; i++)
320294
{
321-
foreach (var hookedCam in m_hookedCameras)
322-
{
323-
var cam = hookedCam.camera;
295+
var oldProjectionMatrix = cam.projectionMatrix;
296+
var oldRect = cam.rect;
297+
cam.rect =new Rect(0f,0f,1f,1f);
298+
ShiftProjectionMatrix(cam, m_samples[i] - new Vector2(0.5f, 0.5f));
299+
cam.Render();
300+
cam.projectionMatrix = oldProjectionMatrix;
301+
cam.rect = oldRect;
302+
303+
accumulateInto = m_accumulateRTs[(x + 1) % 2];
304+
var accumulatedWith = m_accumulateRTs[x % 2];
305+
m_accumulateMaterial.SetTexture("_PreviousTexture", accumulatedWith);
324306

325-
// Render n times the camera and accumulate renders.
326-
var oldProjectionMatrix = cam.projectionMatrix;
327-
ShiftProjectionMatrix(cam, m_samples[i] - new Vector2(0.5f, 0.5f));
328-
cam.Render();
329-
cam.projectionMatrix = oldProjectionMatrix;
307+
if (CameraUsingPartialViewport(cam))
308+
{
309+
m_accumulateMaterial.SetFloat("_OfsX", cam.rect.x );
310+
m_accumulateMaterial.SetFloat("_OfsY", cam.rect.y );
311+
m_accumulateMaterial.SetFloat("_Width", cam.rect.width );
312+
m_accumulateMaterial.SetFloat("_Height", cam.rect.height );
313+
m_accumulateMaterial.SetFloat("_Scale", cam.targetTexture.width / (float)m_renderRT.width );
330314
}
331-
332-
accumulateInto = m_accumulateRTs[(i + 1) % 2];
333-
var accumulatedWith = m_accumulateRTs[i % 2];
334-
m_accumulateMaterial.SetTexture("_PreviousTexture", accumulatedWith);
335-
Graphics.Blit(m_renderRT, accumulateInto, m_accumulateMaterial);
315+
else
316+
{
317+
m_accumulateMaterial.SetFloat("_OfsX", 0 );
318+
m_accumulateMaterial.SetFloat("_OfsY", 0 );
319+
m_accumulateMaterial.SetFloat("_Width", 1 );
320+
m_accumulateMaterial.SetFloat("_Height", 1 );
321+
m_accumulateMaterial.SetFloat("_Scale", 1 );
322+
}
323+
m_accumulateMaterial.SetInt("_Pass", i);
324+
Graphics.Blit(cam.targetTexture, accumulateInto, m_accumulateMaterial);
325+
x++;
336326
}
337-
338-
Graphics.Blit(accumulateInto, m_renderRT);
339327
}
328+
329+
Graphics.Blit(accumulateInto, m_renderRT);
340330
}
341331

342332
void SaveRT(RenderTexture input)
343333
{
334+
if (input == null) return;
344335

345336
var width = input.width;
346337
var height = input.height;

source/FrameRecorder/Inputs/Adam/Shaders/BS4Accumulate.shader

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
Shader "Hidden/BeautyShot/Accumulate" {
44
Properties {
55
_MainTex("Diffuse", 2D) = "white" {}
6+
_OfsX("OfsX", Float) = 0
7+
_OfsY("OfsY", Float) = 0
8+
_Width("Width", Float) = 1
9+
_Height("Height", Float) = 1
10+
_Scale("Scale", Float) = 1
11+
_Pass("Pass", int) = 0
612
}
713

814
CGINCLUDE
@@ -27,12 +33,30 @@ uniform sampler2D _MainTex;
2733
uniform float4 _MainTex_TexelSize;
2834

2935
uniform sampler2D _PreviousTexture;
36+
Float _OfsX;
37+
Float _OfsY;
38+
Float _Width;
39+
Float _Height;
40+
Float _Scale;
41+
int _Pass;
3042

3143
float4 frag(v2f i) : SV_Target {
3244
float4 previous = tex2D(_PreviousTexture, i.uv);
33-
float4 current = tex2D(_MainTex, i.uv);
34-
35-
return previous + current;
45+
float2 tmp = i.uv;
46+
float4 current = {0,0,0,0};
47+
if( i.uv.x >= _OfsX && i.uv.x <= (_OfsX+ _Width) && i.uv.y >= _OfsY && i.uv.y <= (_OfsY + _Height))
48+
{
49+
tmp.x = (tmp.x - _OfsX) / _Scale;
50+
tmp.y = (tmp.y - _OfsY) / _Scale;
51+
current = tex2D(_MainTex, tmp);
52+
53+
if (_Pass == 0)
54+
return current;
55+
else
56+
return previous + current;
57+
}
58+
else
59+
return previous;
3660
}
3761

3862
ENDCG

source/FrameRecorder/Inputs/CBRenderTexture/Editor/CBRenderTextureInputSettingsEditor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public override void OnInspectorGUI()
6060
}
6161
}
6262

63-
EditorGUILayout.PropertyField(m_Transparency, new GUIContent("Preserve transparency"));
63+
EditorGUILayout.PropertyField(m_Transparency, new GUIContent("Capture alpha"));
6464
EditorGUILayout.PropertyField(m_FlipVertically, new GUIContent("Flip vertically"));
6565

6666
serializedObject.ApplyModifiedProperties();

0 commit comments

Comments
 (0)