Translating.cs
using UnityEngine; using System.Collections; public class Translating : MonoBehaviour { public float speed = 10.0f; public Vector3 startPoint = Vector3.zero; public Vector3 endPoint = Vector3.zero; public Vector3 lookAt = Vector3.zero; public bool pingpong = true; private Vector3 curEndPoint = Vector3.zero; // Use this for initialization void Start () { transform.position = startPoint; curEndPoint = endPoint; } // Update is called once per frame void Update () { transform.position = Vector3.Slerp(transform.position, curEndPoint, Time.deltaTime * speed); transform.LookAt(lookAt); if (pingpong) { if (Vector3.Distance(transform.position, curEndPoint) < 0.001f) { curEndPoint = Vector3.Distance(curEndPoint, endPoint) < Vector3.Distance(curEndPoint, startPoint) ? startPoint : endPoint; } } } }
PostEffectsBase.cs
using UnityEngine; using System.Collections; [ExecuteInEditMode] [RequireComponent (typeof(Camera))] public class PostEffectsBase : MonoBehaviour { // Called when start protected void CheckResources() { bool isSupported = CheckSupport(); if (isSupported == false) { NotSupported(); } } // Called in CheckResources to check support on this platform protected bool CheckSupport() { if (SystemInfo.supportsImageEffects == false || SystemInfo.supportsRenderTextures == false) { Debug.LogWarning("This platform does not support image effects or render textures."); return false; } return true; } // Called when the platform doesn't support this effect protected void NotSupported() { enabled = false; } protected void Start() { CheckResources(); } // Called when need to create the material used by this effect protected Material CheckShaderAndCreateMaterial(Shader shader, Material material) { if (shader == null) { return null; } if (shader.isSupported && material && material.shader == shader) return material; if (!shader.isSupported) { return null; } else { material = new Material(shader); material.hideFlags = HideFlags.DontSave; if (material) return material; else return null; } } }
MotionBlur.cs
using UnityEngine; using System.Collections; public class MotionBlur : PostEffectsBase { public Shader motionBlurShader; private Material motionBlurMaterial = null; public Material material { get { motionBlurMaterial = CheckShaderAndCreateMaterial(motionBlurShader, motionBlurMaterial); return motionBlurMaterial; } } [Range(0.0f, 0.9f)] public float blurAmount = 0.5f; private RenderTexture accumulationTexture; void OnDisable() { DestroyImmediate(accumulationTexture); } void OnRenderImage (RenderTexture src, RenderTexture dest) { if (material != null) { // Create the accumulation texture if (accumulationTexture == null || accumulationTexture.width != src.width || accumulationTexture.height != src.height) { DestroyImmediate(accumulationTexture); accumulationTexture = new RenderTexture(src.width, src.height, 0); accumulationTexture.hideFlags = HideFlags.HideAndDontSave; Graphics.Blit(src, accumulationTexture); } // We are accumulating motion over frames without clear/discard // by design, so silence any performance warnings from Unity accumulationTexture.MarkRestoreExpected(); material.SetFloat("_BlurAmount", 1.0f - blurAmount); Graphics.Blit (src, accumulationTexture, material); Graphics.Blit (accumulationTexture, dest); } else { Graphics.Blit(src, dest); } } }
内置渲染管线
Shader "SunY/C12-MotionBlur" { Properties { _MainTex("Main Tex", 2D) = "white"{} _BlurAmount("Blur Amount", float) = 1.0 } SubShader { CGINCLUDE #include "UnityCG.cginc" sampler2D _MainTex; fixed _BlurAmount; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata_img v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.texcoord; return o; } fixed4 fragRGB(v2f i) : SV_Target { return fixed4(tex2D(_MainTex, i.uv).rgb, _BlurAmount); } fixed4 fragA(v2f i) : SV_Target { return tex2D(_MainTex, i.uv); } ENDCG ZTest Always ZWrite Off Cull Off Pass { Blend SrcAlpha OneMinusSrcAlpha ColorMask RGB CGPROGRAM #pragma vertex vert #pragma fragment fragRGB ENDCG } Pass { Blend One Zero ColorMask A CGPROGRAM #pragma vertex vert #pragma fragment fragA ENDCG } } FallBack Off }
浙公网安备 33010602011771号