Files
Dec2017-Script-Dump/Assembly-CSharp-firstpass/ImageEffects/CameraMotionBlur.cs
T
2026-06-04 11:42:34 +02:00

433 lines
16 KiB
C#

using System;
using UnityEngine;
namespace UnityStandardAssets.ImageEffects
{
// Token: 0x0200005D RID: 93
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Image Effects/Camera/Camera Motion Blur")]
public class CameraMotionBlur : PostEffectsBase
{
// Token: 0x060000EC RID: 236 RVA: 0x00009DA8 File Offset: 0x000081A8
private void CalculateViewProjection()
{
Matrix4x4 worldToCameraMatrix = this._camera.worldToCameraMatrix;
Matrix4x4 gpuprojectionMatrix = GL.GetGPUProjectionMatrix(this._camera.projectionMatrix, true);
this.currentViewProjMat = gpuprojectionMatrix * worldToCameraMatrix;
if (this._camera.stereoEnabled)
{
for (int i = 0; i < 2; i++)
{
Matrix4x4 stereoViewMatrix = this._camera.GetStereoViewMatrix((i != 0) ? Camera.StereoscopicEye.Right : Camera.StereoscopicEye.Left);
Matrix4x4 matrix4x = this._camera.GetStereoProjectionMatrix((i != 0) ? Camera.StereoscopicEye.Right : Camera.StereoscopicEye.Left);
matrix4x = GL.GetGPUProjectionMatrix(matrix4x, true);
this.currentStereoViewProjMat[i] = matrix4x * stereoViewMatrix;
}
}
}
// Token: 0x060000ED RID: 237 RVA: 0x00009E58 File Offset: 0x00008258
private new void Start()
{
this.CheckResources();
if (this._camera == null)
{
this._camera = base.GetComponent<Camera>();
}
this.wasActive = base.gameObject.activeInHierarchy;
this.currentStereoViewProjMat = new Matrix4x4[2];
this.prevStereoViewProjMat = new Matrix4x4[2];
this.CalculateViewProjection();
this.Remember();
this.wasActive = false;
}
// Token: 0x060000EE RID: 238 RVA: 0x00009EC5 File Offset: 0x000082C5
private void OnEnable()
{
if (this._camera == null)
{
this._camera = base.GetComponent<Camera>();
}
this._camera.depthTextureMode |= DepthTextureMode.Depth;
}
// Token: 0x060000EF RID: 239 RVA: 0x00009EF8 File Offset: 0x000082F8
private void OnDisable()
{
if (null != this.motionBlurMaterial)
{
global::UnityEngine.Object.DestroyImmediate(this.motionBlurMaterial);
this.motionBlurMaterial = null;
}
if (null != this.dx11MotionBlurMaterial)
{
global::UnityEngine.Object.DestroyImmediate(this.dx11MotionBlurMaterial);
this.dx11MotionBlurMaterial = null;
}
if (null != this.tmpCam)
{
global::UnityEngine.Object.DestroyImmediate(this.tmpCam);
this.tmpCam = null;
}
}
// Token: 0x060000F0 RID: 240 RVA: 0x00009F70 File Offset: 0x00008370
public override bool CheckResources()
{
base.CheckSupport(true, true);
this.motionBlurMaterial = base.CheckShaderAndCreateMaterial(this.shader, this.motionBlurMaterial);
if (this.supportDX11 && this.filterType == CameraMotionBlur.MotionBlurFilter.ReconstructionDX11)
{
this.dx11MotionBlurMaterial = base.CheckShaderAndCreateMaterial(this.dx11MotionBlurShader, this.dx11MotionBlurMaterial);
}
if (!this.isSupported)
{
base.ReportAutoDisable();
}
return this.isSupported;
}
// Token: 0x060000F1 RID: 241 RVA: 0x00009FE4 File Offset: 0x000083E4
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (!this.CheckResources())
{
Graphics.Blit(source, destination);
return;
}
if (this.filterType == CameraMotionBlur.MotionBlurFilter.CameraMotion)
{
this.StartFrame();
}
RenderTextureFormat renderTextureFormat = ((!SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.RGHalf)) ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.RGHalf);
RenderTexture temporary = RenderTexture.GetTemporary(CameraMotionBlur.divRoundUp(source.width, this.velocityDownsample), CameraMotionBlur.divRoundUp(source.height, this.velocityDownsample), 0, renderTextureFormat);
this.maxVelocity = Mathf.Max(2f, this.maxVelocity);
float num = this.maxVelocity;
bool flag = this.filterType == CameraMotionBlur.MotionBlurFilter.ReconstructionDX11 && this.dx11MotionBlurMaterial == null;
int num2;
int num3;
if (this.filterType == CameraMotionBlur.MotionBlurFilter.Reconstruction || flag || this.filterType == CameraMotionBlur.MotionBlurFilter.ReconstructionDisc)
{
this.maxVelocity = Mathf.Min(this.maxVelocity, CameraMotionBlur.MAX_RADIUS);
num2 = CameraMotionBlur.divRoundUp(temporary.width, (int)this.maxVelocity);
num3 = CameraMotionBlur.divRoundUp(temporary.height, (int)this.maxVelocity);
num = (float)(temporary.width / num2);
}
else
{
num2 = CameraMotionBlur.divRoundUp(temporary.width, (int)this.maxVelocity);
num3 = CameraMotionBlur.divRoundUp(temporary.height, (int)this.maxVelocity);
num = (float)(temporary.width / num2);
}
RenderTexture temporary2 = RenderTexture.GetTemporary(num2, num3, 0, renderTextureFormat);
RenderTexture temporary3 = RenderTexture.GetTemporary(num2, num3, 0, renderTextureFormat);
temporary.filterMode = FilterMode.Point;
temporary2.filterMode = FilterMode.Point;
temporary3.filterMode = FilterMode.Point;
if (this.noiseTexture)
{
this.noiseTexture.filterMode = FilterMode.Point;
}
source.wrapMode = TextureWrapMode.Clamp;
temporary.wrapMode = TextureWrapMode.Clamp;
temporary3.wrapMode = TextureWrapMode.Clamp;
temporary2.wrapMode = TextureWrapMode.Clamp;
this.CalculateViewProjection();
if (base.gameObject.activeInHierarchy && !this.wasActive)
{
this.Remember();
}
this.wasActive = base.gameObject.activeInHierarchy;
Matrix4x4 matrix4x = Matrix4x4.Inverse(this.currentViewProjMat);
this.motionBlurMaterial.SetMatrix("_InvViewProj", matrix4x);
this.motionBlurMaterial.SetMatrix("_PrevViewProj", this.prevViewProjMat);
this.motionBlurMaterial.SetMatrix("_ToPrevViewProjCombined", this.prevViewProjMat * matrix4x);
if (this._camera.stereoEnabled)
{
Matrix4x4[] array = new Matrix4x4[]
{
Matrix4x4.Inverse(this.currentStereoViewProjMat[0]),
Matrix4x4.Inverse(this.currentStereoViewProjMat[1])
};
Matrix4x4 matrix4x2 = this.prevStereoViewProjMat[0] * array[0];
this.motionBlurMaterial.SetMatrix("_StereoToPrevViewProjCombined0", matrix4x2);
this.motionBlurMaterial.SetMatrix("_StereoToPrevViewProjCombined1", this.prevStereoViewProjMat[1] * array[1]);
}
this.motionBlurMaterial.SetFloat("_MaxVelocity", num);
this.motionBlurMaterial.SetFloat("_MaxRadiusOrKInPaper", num);
this.motionBlurMaterial.SetFloat("_MinVelocity", this.minVelocity);
this.motionBlurMaterial.SetFloat("_VelocityScale", this.velocityScale);
this.motionBlurMaterial.SetFloat("_Jitter", this.jitter);
this.motionBlurMaterial.SetTexture("_NoiseTex", this.noiseTexture);
this.motionBlurMaterial.SetTexture("_VelTex", temporary);
this.motionBlurMaterial.SetTexture("_NeighbourMaxTex", temporary3);
this.motionBlurMaterial.SetTexture("_TileTexDebug", temporary2);
if (this.preview)
{
Matrix4x4 worldToCameraMatrix = this._camera.worldToCameraMatrix;
Matrix4x4 identity = Matrix4x4.identity;
identity.SetTRS(this.previewScale * 0.3333f, Quaternion.identity, Vector3.one);
Matrix4x4 gpuprojectionMatrix = GL.GetGPUProjectionMatrix(this._camera.projectionMatrix, true);
this.prevViewProjMat = gpuprojectionMatrix * identity * worldToCameraMatrix;
this.motionBlurMaterial.SetMatrix("_PrevViewProj", this.prevViewProjMat);
this.motionBlurMaterial.SetMatrix("_ToPrevViewProjCombined", this.prevViewProjMat * matrix4x);
}
if (this.filterType == CameraMotionBlur.MotionBlurFilter.CameraMotion)
{
Vector4 zero = Vector4.zero;
float num4 = Vector3.Dot(base.transform.up, Vector3.up);
Vector3 vector = this.prevFramePos - base.transform.position;
float magnitude = vector.magnitude;
float num5 = Vector3.Angle(base.transform.up, this.prevFrameUp) / this._camera.fieldOfView * ((float)source.width * 0.75f);
zero.x = this.rotationScale * num5;
num5 = Vector3.Angle(base.transform.forward, this.prevFrameForward) / this._camera.fieldOfView * ((float)source.width * 0.75f);
zero.y = this.rotationScale * num4 * num5;
num5 = Vector3.Angle(base.transform.forward, this.prevFrameForward) / this._camera.fieldOfView * ((float)source.width * 0.75f);
zero.z = this.rotationScale * (1f - num4) * num5;
if (magnitude > Mathf.Epsilon && this.movementScale > Mathf.Epsilon)
{
zero.w = this.movementScale * Vector3.Dot(base.transform.forward, vector) * ((float)source.width * 0.5f);
zero.x += this.movementScale * Vector3.Dot(base.transform.up, vector) * ((float)source.width * 0.5f);
zero.y += this.movementScale * Vector3.Dot(base.transform.right, vector) * ((float)source.width * 0.5f);
}
if (this.preview)
{
this.motionBlurMaterial.SetVector("_BlurDirectionPacked", new Vector4(this.previewScale.y, this.previewScale.x, 0f, this.previewScale.z) * 0.5f * this._camera.fieldOfView);
}
else
{
this.motionBlurMaterial.SetVector("_BlurDirectionPacked", zero);
}
}
else
{
Graphics.Blit(source, temporary, this.motionBlurMaterial, 0);
Camera camera = null;
if (this.excludeLayers.value != 0)
{
camera = this.GetTmpCam();
}
if (camera && this.excludeLayers.value != 0 && this.replacementClear && this.replacementClear.isSupported)
{
camera.targetTexture = temporary;
camera.cullingMask = this.excludeLayers;
camera.RenderWithShader(this.replacementClear, string.Empty);
}
}
if (!this.preview && Time.frameCount != this.prevFrameCount)
{
this.prevFrameCount = Time.frameCount;
this.Remember();
}
source.filterMode = FilterMode.Bilinear;
if (this.showVelocity)
{
this.motionBlurMaterial.SetFloat("_DisplayVelocityScale", this.showVelocityScale);
Graphics.Blit(temporary, destination, this.motionBlurMaterial, 1);
}
else if (this.filterType == CameraMotionBlur.MotionBlurFilter.ReconstructionDX11 && !flag)
{
this.dx11MotionBlurMaterial.SetFloat("_MinVelocity", this.minVelocity);
this.dx11MotionBlurMaterial.SetFloat("_VelocityScale", this.velocityScale);
this.dx11MotionBlurMaterial.SetFloat("_Jitter", this.jitter);
this.dx11MotionBlurMaterial.SetTexture("_NoiseTex", this.noiseTexture);
this.dx11MotionBlurMaterial.SetTexture("_VelTex", temporary);
this.dx11MotionBlurMaterial.SetTexture("_NeighbourMaxTex", temporary3);
this.dx11MotionBlurMaterial.SetFloat("_SoftZDistance", Mathf.Max(0.00025f, this.softZDistance));
this.dx11MotionBlurMaterial.SetFloat("_MaxRadiusOrKInPaper", num);
Graphics.Blit(temporary, temporary2, this.dx11MotionBlurMaterial, 0);
Graphics.Blit(temporary2, temporary3, this.dx11MotionBlurMaterial, 1);
Graphics.Blit(source, destination, this.dx11MotionBlurMaterial, 2);
}
else if (this.filterType == CameraMotionBlur.MotionBlurFilter.Reconstruction || flag)
{
this.motionBlurMaterial.SetFloat("_SoftZDistance", Mathf.Max(0.00025f, this.softZDistance));
Graphics.Blit(temporary, temporary2, this.motionBlurMaterial, 2);
Graphics.Blit(temporary2, temporary3, this.motionBlurMaterial, 3);
Graphics.Blit(source, destination, this.motionBlurMaterial, 4);
}
else if (this.filterType == CameraMotionBlur.MotionBlurFilter.CameraMotion)
{
Graphics.Blit(source, destination, this.motionBlurMaterial, 6);
}
else if (this.filterType == CameraMotionBlur.MotionBlurFilter.ReconstructionDisc)
{
this.motionBlurMaterial.SetFloat("_SoftZDistance", Mathf.Max(0.00025f, this.softZDistance));
Graphics.Blit(temporary, temporary2, this.motionBlurMaterial, 2);
Graphics.Blit(temporary2, temporary3, this.motionBlurMaterial, 3);
Graphics.Blit(source, destination, this.motionBlurMaterial, 7);
}
else
{
Graphics.Blit(source, destination, this.motionBlurMaterial, 5);
}
RenderTexture.ReleaseTemporary(temporary);
RenderTexture.ReleaseTemporary(temporary2);
RenderTexture.ReleaseTemporary(temporary3);
}
// Token: 0x060000F2 RID: 242 RVA: 0x0000A978 File Offset: 0x00008D78
private void Remember()
{
this.prevViewProjMat = this.currentViewProjMat;
this.prevFrameForward = base.transform.forward;
this.prevFrameUp = base.transform.up;
this.prevFramePos = base.transform.position;
this.prevStereoViewProjMat[0] = this.currentStereoViewProjMat[0];
this.prevStereoViewProjMat[1] = this.currentStereoViewProjMat[1];
}
// Token: 0x060000F3 RID: 243 RVA: 0x0000AA08 File Offset: 0x00008E08
private Camera GetTmpCam()
{
if (this.tmpCam == null)
{
string text = "_" + this._camera.name + "_MotionBlurTmpCam";
GameObject gameObject = GameObject.Find(text);
if (null == gameObject)
{
this.tmpCam = new GameObject(text, new Type[] { typeof(Camera) });
}
else
{
this.tmpCam = gameObject;
}
}
this.tmpCam.hideFlags = HideFlags.DontSave;
this.tmpCam.transform.position = this._camera.transform.position;
this.tmpCam.transform.rotation = this._camera.transform.rotation;
this.tmpCam.transform.localScale = this._camera.transform.localScale;
this.tmpCam.GetComponent<Camera>().CopyFrom(this._camera);
this.tmpCam.GetComponent<Camera>().enabled = false;
this.tmpCam.GetComponent<Camera>().depthTextureMode = DepthTextureMode.None;
this.tmpCam.GetComponent<Camera>().clearFlags = CameraClearFlags.Nothing;
return this.tmpCam.GetComponent<Camera>();
}
// Token: 0x060000F4 RID: 244 RVA: 0x0000AB40 File Offset: 0x00008F40
private void StartFrame()
{
this.prevFramePos = Vector3.Slerp(this.prevFramePos, base.transform.position, 0.75f);
}
// Token: 0x060000F5 RID: 245 RVA: 0x0000AB63 File Offset: 0x00008F63
private static int divRoundUp(int x, int d)
{
return (x + d - 1) / d;
}
// Token: 0x040001F3 RID: 499
private static float MAX_RADIUS = 10f;
// Token: 0x040001F4 RID: 500
public CameraMotionBlur.MotionBlurFilter filterType = CameraMotionBlur.MotionBlurFilter.Reconstruction;
// Token: 0x040001F5 RID: 501
public bool preview;
// Token: 0x040001F6 RID: 502
public Vector3 previewScale = Vector3.one;
// Token: 0x040001F7 RID: 503
public float movementScale;
// Token: 0x040001F8 RID: 504
public float rotationScale = 1f;
// Token: 0x040001F9 RID: 505
public float maxVelocity = 8f;
// Token: 0x040001FA RID: 506
public float minVelocity = 0.1f;
// Token: 0x040001FB RID: 507
public float velocityScale = 0.375f;
// Token: 0x040001FC RID: 508
public float softZDistance = 0.005f;
// Token: 0x040001FD RID: 509
public int velocityDownsample = 1;
// Token: 0x040001FE RID: 510
public LayerMask excludeLayers = 0;
// Token: 0x040001FF RID: 511
private GameObject tmpCam;
// Token: 0x04000200 RID: 512
public Shader shader;
// Token: 0x04000201 RID: 513
public Shader dx11MotionBlurShader;
// Token: 0x04000202 RID: 514
public Shader replacementClear;
// Token: 0x04000203 RID: 515
private Material motionBlurMaterial;
// Token: 0x04000204 RID: 516
private Material dx11MotionBlurMaterial;
// Token: 0x04000205 RID: 517
public Texture2D noiseTexture;
// Token: 0x04000206 RID: 518
public float jitter = 0.05f;
// Token: 0x04000207 RID: 519
public bool showVelocity;
// Token: 0x04000208 RID: 520
public float showVelocityScale = 1f;
// Token: 0x04000209 RID: 521
private Matrix4x4 currentViewProjMat;
// Token: 0x0400020A RID: 522
private Matrix4x4[] currentStereoViewProjMat;
// Token: 0x0400020B RID: 523
private Matrix4x4 prevViewProjMat;
// Token: 0x0400020C RID: 524
private Matrix4x4[] prevStereoViewProjMat;
// Token: 0x0400020D RID: 525
private int prevFrameCount;
// Token: 0x0400020E RID: 526
private bool wasActive;
// Token: 0x0400020F RID: 527
private Vector3 prevFrameForward = Vector3.forward;
// Token: 0x04000210 RID: 528
private Vector3 prevFrameUp = Vector3.up;
// Token: 0x04000211 RID: 529
private Vector3 prevFramePos = Vector3.zero;
// Token: 0x04000212 RID: 530
private Camera _camera;
// Token: 0x0200005E RID: 94
public enum MotionBlurFilter
{
// Token: 0x04000214 RID: 532
CameraMotion,
// Token: 0x04000215 RID: 533
LocalBlur,
// Token: 0x04000216 RID: 534
Reconstruction,
// Token: 0x04000217 RID: 535
ReconstructionDX11,
// Token: 0x04000218 RID: 536
ReconstructionDisc
}
}
}