unity如何动态控制后处理程序的参数

public PostProcessVolume volume;
		public DepthOfField dof;

		// Use this for initialization
		void Start()
		{
			PostProcessLayer ppsLayer = GetComponent();
			if (ppsLayer != null)
			{
				LayerMask layerMask = ppsLayer.volumeLayer;
				PostProcessVolume[] ppsVolumes = FindObjectsOfType();
				if (ppsVolumes != null && ppsVolumes.Length > 0)
				{
					for (int k = 0; k < ppsVolumes.Length; k++)
					{
						PostProcessVolume ppsVolume = ppsVolumes[k];
						int layer = 1 << ppsVolume.gameObject.layer;
						bool inLayer = (layerMask.value & layer) == layer;
						if (ppsVolume != null && inLayer && ppsVolume.profile != null && ppsVolume.profile.TryGetSettings(out dof))
						{
							volume = ppsVolume;
							break;
						}
					}
				}
			}
			if (dof == null)
			{
				Debug.LogError("找不到景深");
			}

			 
		}

using UnityEngine.Rendering.PostProcessing;

引入后处理的命名空间,先获取PostProcessVolume对象,从PostProcessVolume中尝试获取对应的后处理脚本对象,并修改参数

 

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DM_UtilityTools;
using System.IO.Ports;
using UnityEngine.Rendering.PostProcessing;

namespace DM_FollowFocus
{
	[RequireComponent(typeof(Camera))]
	public class FollowFocus : MonoBehaviour
	{
		//焦距,单位毫米
		public float focalLength = 50;
		//光圈值
		public float aperture;

		//对焦距离,单位米,一般会有最小最大对焦距离
		public float maxFocusDistance = 0;
		public float minFocusDistance = 0;
		public float focusDistance = 10;
		[Range(0,270)]
		public int maxF = 0;
		public int minF = 0;
		public int curF;

		public bool autoFocus = false;

		[HideInInspector]
		public int portNum = 0;
		
		private int baudRate = 115200;
		private int dataBits = 8;
		private StopBits stopBits = StopBits.One;
		private Parity parity = Parity.None;//Parity.Odd;//校验位

		private ConnectCom com;

		public PostProcessVolume volume;
		public DepthOfField dof;

		// Use this for initialization
		void Start()
		{
			PostProcessLayer ppsLayer = GetComponent();
			if (ppsLayer != null)
			{
				LayerMask layerMask = ppsLayer.volumeLayer;
				PostProcessVolume[] ppsVolumes = FindObjectsOfType();
				if (ppsVolumes != null && ppsVolumes.Length > 0)
				{
					for (int k = 0; k < ppsVolumes.Length; k++)
					{
						PostProcessVolume ppsVolume = ppsVolumes[k];
						int layer = 1 << ppsVolume.gameObject.layer;
						bool inLayer = (layerMask.value & layer) == layer;
						if (ppsVolume != null && inLayer && ppsVolume.profile != null && ppsVolume.profile.TryGetSettings(out dof))
						{
							volume = ppsVolume;
							break;
						}
					}
				}
			}
			if (dof == null)
			{
				Debug.LogError("找不到景深");
			}

			com = ConnectCom.create(portNum, baudRate, dataBits, stopBits, parity);
			com.openPort();
		}

		// Update is called once per frame
		void LateUpdate()
		{
			int tempF = getFocusValue();
			if (tempF >= 0)
			{
				curF = tempF;
				//Debug.Log("curF:" + curF + "," + minF + "," + maxF);
				if (autoFocus && minFocusDistance < maxFocusDistance)// && minF < maxF)
				{
					focusDistance = Mathf.Lerp(minFocusDistance, maxFocusDistance, Mathf.InverseLerp(minF, maxF, curF));
					//使对焦数据应用在景深上
					updateCameraPPS_Dof();
				}
			}
		}

		//更新相机PostProcessingStack
		private void updateCameraPPS_Dof()
		{
			if (dof == null || !dof.enabled)
			{
				return;
			}
			dof.aperture.value = aperture;
			dof.focalLength.value = focalLength;
			dof.focusDistance.value = focusDistance;
		}

		public void setMaxFocusDistance()
		{
			maxFocusDistance = focusDistance;
			maxF = curF;
		}

		public void setMinFocusDistance()
		{
			minFocusDistance = focusDistance;
			minF = curF;
		}

		private const int MinValue = 0;
		private const int MaxValue = 270;
		//获取过滤后的对焦值
		private int getFocusValue()
		{
			string msg = com.Msg.Trim();
			//Debug.Log("msgInit:" + msg + "END");
			if (!string.IsNullOrEmpty(msg) && msg.Length == 6)
			{
				if (msg.IndexOf("F1 ") == 0)//只关注第一个  || msg.IndexOf("F2 ") >= 0)
				{
					string str = msg.Substring(3, 3);
					if (string.IsNullOrEmpty(str.Trim()) || str.Trim().Length != 3)
					{
						return -2;
					}
					int num;
					if (!int.TryParse(str, out num))
					{
						return -3;
					}
					//return Mathf.InverseLerp(MinValue, MaxValue, num);

					//Debug.Log("strKK:" + str + ",msgKK:" + msg);

					return num;
				}
			}
			return -1;
		}

		void OnDestroy()
		{
			if (com != null)
			{
				com.close();
			}
		}
	}
}

 

你可能感兴趣的:(C#,Unity3D,Shader)