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


This class implements simple ghosting type Motion Blur.

If Extra Blur is selected, the scene will allways be a little blurred,

as it is scaled to a smaller resolution.

The effect works by accumulating the previous frames in an accumulation

texture.

Called by camera to apply image effect

Create the accumulation texture

If Extra Blur is selected, downscale the texture to 4x4 smaller resolution.

Clamp the motion blur variable, so it can never leave permanent trails in the image

Setup the texture and floating point values in the shader

We are accumulating motion over frames without cleardiscard

by design, so silence any performance warnings from Unity

Render the image using the motion blur shader



Gõ tìm kiếm nhanh...