132 lines
4.4 KiB
C#
132 lines
4.4 KiB
C#
|
using System;
|
|||
|
using Thousandto.Launcher.ExternalLibs;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace UnityStandardAssets.ImageEffects
|
|||
|
{
|
|||
|
[ExecuteInEditMode]
|
|||
|
[RequireComponent(typeof(Camera))]
|
|||
|
[AddComponentMenu("Image Effects/DistortEffect/DistortEffect(Optimized)")]
|
|||
|
public class DistortEffect : PostEffectsBase
|
|||
|
{
|
|||
|
|
|||
|
[Range(0.0f, 1.0f)]
|
|||
|
public float DistortTimeFactor = 0.15f;
|
|||
|
//Ť<><C5A4><EFBFBD><EFBFBD>ǿ<EFBFBD><C7BF>
|
|||
|
[Range(0.0f, 0.2f)]
|
|||
|
public float DistortStrength = 0.01f;
|
|||
|
//<2F><><EFBFBD><EFBFBD>ͼ
|
|||
|
public Texture NoiseTexture = null;
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>
|
|||
|
public int downSample = 2;
|
|||
|
|
|||
|
public LayerMask _Cameralayer = 1 << 28;
|
|||
|
|
|||
|
private Camera mainCam = null;
|
|||
|
private Camera additionalCam = null;
|
|||
|
private RenderTexture renderTexture = null;
|
|||
|
private Material _Material = null;
|
|||
|
private Shader _Shader = null;
|
|||
|
//<2F><>ȾMaskͼ<6B><CDBC><EFBFBD>õ<EFBFBD>shader
|
|||
|
private Shader maskObjShader = null;
|
|||
|
public void OnRenderImage(RenderTexture source, RenderTexture destination)
|
|||
|
{
|
|||
|
|
|||
|
//<2F>ֱ<EFBFBD><D6B1>ʿ<EFBFBD><CABF>Ե<EFBFBD>һЩ
|
|||
|
if (renderTexture == null)
|
|||
|
renderTexture = RenderTexture.GetTemporary(source.width, source.height, 0);
|
|||
|
renderTexture.name = "RenderTexture59";
|
|||
|
renderTexture.antiAliasing = 1;
|
|||
|
|
|||
|
if (_Material)
|
|||
|
{
|
|||
|
_Material.SetTexture("_NoiseTex", NoiseTexture);
|
|||
|
_Material.SetFloat("_DistortTimeFactor", DistortTimeFactor);
|
|||
|
_Material.SetFloat("_DistortStrength", DistortStrength);
|
|||
|
_Material.SetTexture("_MaskTex", renderTexture);
|
|||
|
Graphics.Blit(source, destination, _Material);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Graphics.Blit(source, destination);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public override bool CheckResources()
|
|||
|
{
|
|||
|
CheckSupport(false);
|
|||
|
|
|||
|
_Shader = ShaderFactory.Find("Ares/DistortEffect/DistortEffect");
|
|||
|
maskObjShader = ShaderFactory.Find("Ares/DistortEffect/DistortEffectMask");
|
|||
|
_Material = CheckShaderAndCreateMaterial(_Shader, _Material);
|
|||
|
|
|||
|
if (!isSupported)
|
|||
|
ReportAutoDisable();
|
|||
|
return isSupported;
|
|||
|
}
|
|||
|
|
|||
|
void Awake()
|
|||
|
{
|
|||
|
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>͵<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>һ<EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
InitAdditionalCam();
|
|||
|
}
|
|||
|
private void InitAdditionalCam()
|
|||
|
{
|
|||
|
mainCam = GetComponent<Camera>();
|
|||
|
if (mainCam == null)
|
|||
|
return;
|
|||
|
Transform addCamTransform = mainCam.transform.Find("additionalDistortCam");
|
|||
|
if (addCamTransform != null)
|
|||
|
DestroyImmediate(addCamTransform.gameObject);
|
|||
|
GameObject additionalCamObj = new GameObject("additionalDistortCam");
|
|||
|
additionalCam = additionalCamObj.AddComponent<Camera>();
|
|||
|
SetAdditionalCam();
|
|||
|
}
|
|||
|
private void SetAdditionalCam()
|
|||
|
{
|
|||
|
if (additionalCam)
|
|||
|
{
|
|||
|
additionalCam.transform.parent = mainCam.transform;
|
|||
|
additionalCam.transform.localPosition = Vector3.zero;
|
|||
|
additionalCam.transform.localRotation = Quaternion.identity;
|
|||
|
additionalCam.transform.localScale = Vector3.one;
|
|||
|
additionalCam.farClipPlane = mainCam.farClipPlane;
|
|||
|
additionalCam.nearClipPlane = mainCam.nearClipPlane;
|
|||
|
additionalCam.fieldOfView = mainCam.fieldOfView;
|
|||
|
additionalCam.backgroundColor = Color.clear;
|
|||
|
additionalCam.clearFlags = CameraClearFlags.Color;
|
|||
|
additionalCam.cullingMask = _Cameralayer;
|
|||
|
additionalCam.depth = -999;
|
|||
|
}
|
|||
|
}
|
|||
|
void OnEnable()
|
|||
|
{
|
|||
|
SetAdditionalCam();
|
|||
|
additionalCam.enabled = true;
|
|||
|
}
|
|||
|
void OnDisable()
|
|||
|
{
|
|||
|
additionalCam.enabled = false;
|
|||
|
}
|
|||
|
void OnDestroy()
|
|||
|
{
|
|||
|
if (renderTexture)
|
|||
|
{
|
|||
|
RenderTexture.ReleaseTemporary(renderTexture);
|
|||
|
}
|
|||
|
DestroyImmediate(additionalCam.gameObject);
|
|||
|
}
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦǰ<C8BE>Ļص<C4BB><D8B5><EFBFBD><EFBFBD>˴<EFBFBD><CBB4><EFBFBD>ȾMask<73><6B><EFBFBD><EFBFBD>ͼ
|
|||
|
void OnPreRender()
|
|||
|
{
|
|||
|
//maskObjShader<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ
|
|||
|
|
|||
|
if (additionalCam.enabled)
|
|||
|
{
|
|||
|
additionalCam.targetTexture = renderTexture;
|
|||
|
additionalCam.RenderWithShader(maskObjShader, "");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|