@@ -124,13 +124,12 @@ public override void BeginRecording(RecordingSession session)
124
124
m_normalizeMaterial . hideFlags = HideFlags . DontSave ;
125
125
126
126
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 )
128
129
{
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 ( ) ;
134
133
}
135
134
var rt = new RenderTexture ( m_outputWidth , m_outputHeight , 0 , RenderTextureFormat . Default , RenderTextureReadWrite . sRGB ) ;
136
135
rt . Create ( ) ;
@@ -158,17 +157,19 @@ public override void NewFrameStarting(RecordingSession session)
158
157
// Should we keep it?
159
158
if ( cam . targetDisplay != 0 || ! cam . enabled )
160
159
{
160
+ UnityHelpers . Destroy ( cam . targetTexture ) ;
161
161
cam . targetTexture = hookedCam . textureBackup ;
162
162
m_hookedCameras . Remove ( hookedCam ) ;
163
163
}
164
164
continue ;
165
165
}
166
166
167
- if ( ! cam . enabled || cam . targetDisplay != 0 )
167
+ if ( ! cam . enabled || ! cam . gameObject . active || cam . targetDisplay != 0 )
168
168
continue ;
169
169
170
170
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 ;
172
173
m_hookedCameras . Add ( hookedCam ) ;
173
174
sort = true ;
174
175
}
@@ -219,6 +220,8 @@ protected override void Dispose(bool disposing)
219
220
{
220
221
if ( c != null )
221
222
{
223
+ if ( c . camera . rect . width == 1f && c . camera . rect . height == 1f )
224
+ UnityHelpers . Destroy ( c . camera . targetTexture ) ;
222
225
c . camera . targetTexture = c . textureBackup ;
223
226
}
224
227
}
@@ -268,79 +271,67 @@ void ShiftProjectionMatrix(Camera camera, Vector2 sample)
268
271
camera . projectionMatrix = projectionMatrix ;
269
272
}
270
273
271
- RenderTexture PerformSubSampling ( Camera cam )
274
+ bool CameraUsingPartialViewport ( Camera cam )
272
275
{
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 ;
299
277
}
300
278
301
279
void PerformSubSampling ( )
302
280
{
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 ;
305
284
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 ) ;
315
288
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 ;
318
292
319
293
for ( int i = 0 , n = ( int ) adamSettings . m_SuperSampling ; i < n ; i ++ )
320
294
{
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 ) ;
324
306
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 ) ;
330
314
}
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 ++ ;
336
326
}
337
-
338
- Graphics . Blit ( accumulateInto , m_renderRT ) ;
339
327
}
328
+
329
+ Graphics . Blit ( accumulateInto , m_renderRT ) ;
340
330
}
341
331
342
332
void SaveRT ( RenderTexture input )
343
333
{
334
+ if ( input == null ) return ;
344
335
345
336
var width = input . width ;
346
337
var height = input . height ;
0 commit comments