1 using System;
2 using
UnityEngine;
3
4 // This
class implements simple ghosting type Motion Blur.
5 // If Extra Blur
is selected, the scene will allways be a little blurred,
6 //
as it is scaled to a smaller resolution.
7 // The effect works
by accumulating the previous frames in an accumulation
8 // texture.

9 namespace
UnityStandardAssets.ImageEffects
10 {
11     
[ExecuteInEditMode]
12     
[AddComponentMenu("Image Effects/Blur/Motion Blur (Color Accumulation)")]
13     
[RequireComponent(typeof(Camera))]
14     
public class MotionBlur : ImageEffectBase
15     {
16         
[Range(0.0f, 0.92f)]
17         
public float blurAmount = 0.8f;
18         
public bool extraBlur = false;
19
20         
private RenderTexture accumTexture;
21
22         
override protected void Start()
23         {
24             
if (!SystemInfo.supportsRenderTextures)
25             {
26                 enabled =
false;
27                 
return;
28             }
29             
base.Start();
30         }
31
32         
override protected void OnDisable()
33         {
34             
base.OnDisable();
35             DestroyImmediate(accumTexture);
36         }
37
38         
// Called by camera to apply image effect
39         
void OnRenderImage (RenderTexture source, RenderTexture destination)
40         {
41             
// Create the accumulation texture
42             
if (accumTexture == null || accumTexture.width != source.width || accumTexture.height != source.height)
43             {
44                 DestroyImmediate(accumTexture);
45                 accumTexture =
new RenderTexture(source.width, source.height, 0);
46                 accumTexture.hideFlags = HideFlags.HideAndDontSave;
47                 Graphics.Blit( source, accumTexture );
48             }
49
50             
// If Extra Blur is selected, downscale the texture to 4x4 smaller resolution.
51             
if (extraBlur)
52             {
53                 RenderTexture blurbuffer = RenderTexture.GetTemporary(source.width/
4, source.height/4, 0);
54                 accumTexture.MarkRestoreExpected();
55                 Graphics.Blit(accumTexture, blurbuffer);
56                 Graphics.Blit(blurbuffer,accumTexture);
57                 RenderTexture.ReleaseTemporary(blurbuffer);
58             }
59
60             
// Clamp the motion blur variable, so it can never leave permanent trails in the image
61             blurAmount = Mathf.Clamp( blurAmount,
0.0f, 0.92f );
62
63             
// Setup the texture and floating point values in the shader
64             material.SetTexture(
"_MainTex", accumTexture);
65             material.SetFloat(
"_AccumOrig", 1.0F-blurAmount);
66
67             
// We are accumulating motion over frames without clear/discard
68             
// by design, so silence any performance warnings from Unity
69             accumTexture.MarkRestoreExpected();
70
71             
// Render the image using the motion blur shader
72             Graphics.Blit (source, accumTexture, material);
73             Graphics.Blit (accumTexture, destination);
74         }
75     }
76 }