123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- using System;
- using System.Collections.Generic;
- using UnityEngine.Rendering;
- namespace UnityEngine.PostProcessing
- {
- public sealed class BuiltinDebugViewsComponent : PostProcessingComponentCommandBuffer<BuiltinDebugViewsModel>
- {
- public override bool active
- {
- get
- {
- return base.model.IsModeActive(BuiltinDebugViewsModel.Mode.Depth) || base.model.IsModeActive(BuiltinDebugViewsModel.Mode.Normals) || base.model.IsModeActive(BuiltinDebugViewsModel.Mode.MotionVectors);
- }
- }
- public override DepthTextureMode GetCameraFlags()
- {
- BuiltinDebugViewsModel.Mode mode = base.model.settings.mode;
- DepthTextureMode depthTextureMode = DepthTextureMode.None;
- if (mode != BuiltinDebugViewsModel.Mode.Normals)
- {
- if (mode != BuiltinDebugViewsModel.Mode.MotionVectors)
- {
- if (mode == BuiltinDebugViewsModel.Mode.Depth)
- {
- depthTextureMode |= DepthTextureMode.Depth;
- }
- }
- else
- {
- depthTextureMode |= (DepthTextureMode.Depth | DepthTextureMode.MotionVectors);
- }
- }
- else
- {
- depthTextureMode |= DepthTextureMode.DepthNormals;
- }
- return depthTextureMode;
- }
- public override CameraEvent GetCameraEvent()
- {
- return (base.model.settings.mode != BuiltinDebugViewsModel.Mode.MotionVectors) ? CameraEvent.BeforeImageEffectsOpaque : CameraEvent.BeforeImageEffects;
- }
- public override string GetName()
- {
- return "Builtin Debug Views";
- }
- public override void PopulateCommandBuffer(CommandBuffer cb)
- {
- BuiltinDebugViewsModel.Settings settings = base.model.settings;
- Material material = this.context.materialFactory.Get("Hidden/Post FX/Builtin Debug Views");
- material.shaderKeywords = null;
- if (this.context.isGBufferAvailable)
- {
- material.EnableKeyword("SOURCE_GBUFFER");
- }
- BuiltinDebugViewsModel.Mode mode = settings.mode;
- if (mode != BuiltinDebugViewsModel.Mode.Depth)
- {
- if (mode != BuiltinDebugViewsModel.Mode.Normals)
- {
- if (mode == BuiltinDebugViewsModel.Mode.MotionVectors)
- {
- this.MotionVectorsPass(cb);
- }
- }
- else
- {
- this.DepthNormalsPass(cb);
- }
- }
- else
- {
- this.DepthPass(cb);
- }
- this.context.Interrupt();
- }
- private void DepthPass(CommandBuffer cb)
- {
- Material mat = this.context.materialFactory.Get("Hidden/Post FX/Builtin Debug Views");
- BuiltinDebugViewsModel.DepthSettings depth = base.model.settings.depth;
- cb.SetGlobalFloat(BuiltinDebugViewsComponent.Uniforms._DepthScale, 1f / depth.scale);
- cb.Blit(null, BuiltinRenderTextureType.CameraTarget, mat, 0);
- }
- private void DepthNormalsPass(CommandBuffer cb)
- {
- Material mat = this.context.materialFactory.Get("Hidden/Post FX/Builtin Debug Views");
- cb.Blit(null, BuiltinRenderTextureType.CameraTarget, mat, 1);
- }
- private void MotionVectorsPass(CommandBuffer cb)
- {
- Material material = this.context.materialFactory.Get("Hidden/Post FX/Builtin Debug Views");
- BuiltinDebugViewsModel.MotionVectorsSettings motionVectors = base.model.settings.motionVectors;
- int nameID = BuiltinDebugViewsComponent.Uniforms._TempRT;
- cb.GetTemporaryRT(nameID, this.context.width, this.context.height, 0, FilterMode.Bilinear);
- cb.SetGlobalFloat(BuiltinDebugViewsComponent.Uniforms._Opacity, motionVectors.sourceOpacity);
- cb.SetGlobalTexture(BuiltinDebugViewsComponent.Uniforms._MainTex, BuiltinRenderTextureType.CameraTarget);
- cb.Blit(BuiltinRenderTextureType.CameraTarget, nameID, material, 2);
- if (motionVectors.motionImageOpacity > 0f && motionVectors.motionImageAmplitude > 0f)
- {
- int tempRT = BuiltinDebugViewsComponent.Uniforms._TempRT2;
- cb.GetTemporaryRT(tempRT, this.context.width, this.context.height, 0, FilterMode.Bilinear);
- cb.SetGlobalFloat(BuiltinDebugViewsComponent.Uniforms._Opacity, motionVectors.motionImageOpacity);
- cb.SetGlobalFloat(BuiltinDebugViewsComponent.Uniforms._Amplitude, motionVectors.motionImageAmplitude);
- cb.SetGlobalTexture(BuiltinDebugViewsComponent.Uniforms._MainTex, nameID);
- cb.Blit(nameID, tempRT, material, 3);
- cb.ReleaseTemporaryRT(nameID);
- nameID = tempRT;
- }
- if (motionVectors.motionVectorsOpacity > 0f && motionVectors.motionVectorsAmplitude > 0f)
- {
- this.PrepareArrows();
- float num = 1f / (float)motionVectors.motionVectorsResolution;
- float x = num * (float)this.context.height / (float)this.context.width;
- cb.SetGlobalVector(BuiltinDebugViewsComponent.Uniforms._Scale, new Vector2(x, num));
- cb.SetGlobalFloat(BuiltinDebugViewsComponent.Uniforms._Opacity, motionVectors.motionVectorsOpacity);
- cb.SetGlobalFloat(BuiltinDebugViewsComponent.Uniforms._Amplitude, motionVectors.motionVectorsAmplitude);
- cb.DrawMesh(this.m_Arrows.mesh, Matrix4x4.identity, material, 0, 4);
- }
- cb.SetGlobalTexture(BuiltinDebugViewsComponent.Uniforms._MainTex, nameID);
- cb.Blit(nameID, BuiltinRenderTextureType.CameraTarget);
- cb.ReleaseTemporaryRT(nameID);
- }
- private void PrepareArrows()
- {
- int motionVectorsResolution = base.model.settings.motionVectors.motionVectorsResolution;
- int num = motionVectorsResolution * Screen.width / Screen.height;
- if (this.m_Arrows == null)
- {
- this.m_Arrows = new BuiltinDebugViewsComponent.ArrowArray();
- }
- if (this.m_Arrows.columnCount != num || this.m_Arrows.rowCount != motionVectorsResolution)
- {
- this.m_Arrows.Release();
- this.m_Arrows.BuildMesh(num, motionVectorsResolution);
- }
- }
- public override void OnDisable()
- {
- if (this.m_Arrows != null)
- {
- this.m_Arrows.Release();
- }
- this.m_Arrows = null;
- }
- private const string k_ShaderString = "Hidden/Post FX/Builtin Debug Views";
- private BuiltinDebugViewsComponent.ArrowArray m_Arrows;
- private static class Uniforms
- {
- internal static readonly int _DepthScale = Shader.PropertyToID("_DepthScale");
- internal static readonly int _TempRT = Shader.PropertyToID("_TempRT");
- internal static readonly int _Opacity = Shader.PropertyToID("_Opacity");
- internal static readonly int _MainTex = Shader.PropertyToID("_MainTex");
- internal static readonly int _TempRT2 = Shader.PropertyToID("_TempRT2");
- internal static readonly int _Amplitude = Shader.PropertyToID("_Amplitude");
- internal static readonly int _Scale = Shader.PropertyToID("_Scale");
- }
- private enum Pass
- {
- Depth,
- Normals,
- MovecOpacity,
- MovecImaging,
- MovecArrows
- }
- private class ArrowArray
- {
- public Mesh mesh { get; private set; }
- public int columnCount { get; private set; }
- public int rowCount { get; private set; }
- public void BuildMesh(int columns, int rows)
- {
- Vector3[] array = new Vector3[]
- {
- new Vector3(0f, 0f, 0f),
- new Vector3(0f, 1f, 0f),
- new Vector3(0f, 1f, 0f),
- new Vector3(-1f, 1f, 0f),
- new Vector3(0f, 1f, 0f),
- new Vector3(1f, 1f, 0f)
- };
- int num = 6 * columns * rows;
- List<Vector3> list = new List<Vector3>(num);
- List<Vector2> list2 = new List<Vector2>(num);
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < columns; j++)
- {
- Vector2 item = new Vector2((0.5f + (float)j) / (float)columns, (0.5f + (float)i) / (float)rows);
- for (int k = 0; k < 6; k++)
- {
- list.Add(array[k]);
- list2.Add(item);
- }
- }
- }
- int[] array2 = new int[num];
- for (int l = 0; l < num; l++)
- {
- array2[l] = l;
- }
- this.mesh = new Mesh
- {
- hideFlags = HideFlags.DontSave
- };
- this.mesh.SetVertices(list);
- this.mesh.SetUVs(0, list2);
- this.mesh.SetIndices(array2, MeshTopology.Lines, 0);
- this.mesh.UploadMeshData(true);
- this.columnCount = columns;
- this.rowCount = rows;
- }
- public void Release()
- {
- GraphicsUtils.Destroy(this.mesh);
- this.mesh = null;
- }
- }
- }
- }
|