效果:(参数还需要调调……看多了感觉眼睛要瞎了……)
如果不需要在游戏过程中通过script改变效果的话,只需要看1~6步就可以了
代码部分在最底下
1. Asset store->Post processing stack,unity自家的一个免费插件,贼好用 (图1), 有梯子的同学可以看一下这里https://www.youtube.com/watch?v=a0OQvWAPeuo&t=587s,很好的简介
2. Import之后找到Post Processing Behavior这个script (图2)
3. 把第二步里的script作为component给camera安上
4. 找到上方工具栏里的Assets -> Create -> Post processing profile
5. 起一个做作的名字,拉到Camera的Post processing behavior的script对应Profile的位置里
6. 在inspector里看到创建的profile的各种选项后,通过改变右边的数值,就可以不明觉厉了 (图3)
7. 在要操控右边这堆参数的script里,加入using UnityEngine.Rendering和using UnityEngine.PostProcessing
8. 以public ,[SerializeField]或者是GetComponent的方式想办法得到一个PostProcessingProfile的reference
9. 开头的图里用到了motionBlur和blooming,所以分别通过
var motionBlur = postProcessingProfile.motionBlur.settings 和
var bloom = postProcessingProfile.bloom.settings
得到相对应的设置,存起来收好
10. 简单讲一下这俩里面的几个参数:
MotionBlur里的Shutter Angle控制动态模糊的强烈程度(越大越强),Sample count越大就越平滑。Multiframe blending开始没怎么理解,后来看了教程后这个参数可以理解为”喝的有多大“,也就是调的越高,视角在动态的时候越有喝大了的感觉 (第一步的教程里有讲)
Bloom里的intensity可以理解为物体有多亮(效果图里那种模模糊糊的亮),threshold可以理解为强化亮度的最低要求,调的越低亮的东西也就多一些
11. 根据正在操控的物体的rigidbody得到目前velocity的magnitude,和之前一次FixedUpdate的值进行对比,得到一个差值
12. 以这个差为基础,通过motionBlur.shutterAngle = myValue; 和 bloom.bloom.intensity = myValue; 对两个参数进行改变
13. intensity可以自行加个上限,数值太高了效果不太好
14. 完成 (又在大佬们跟前现眼了
代码部分:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.PostProcessing; using UnityEngine.Rendering; public class BlurController : MonoBehaviour { //PostProcessing attributes [SerializeField] PostProcessingProfile postProcessingProfile; public float blurMagnitude = 1f; public float bloomMagnitude = 1f;//for the ease to edit in editor mode public float maxBloom = 200f; public float maxBlur = 200f; public float motionBlurValue = 1f; public float bloomValue = 1f; /* * * Need : Bloom and blur should rise when moving in a direction, namely proportional to the velocity * * Solution : Since being called at every fixedupdate, record previous velocity's magnitude, use as base * * */ public void CalculateBlur(float relativeVelocity) { var motionBlur = postProcessingProfile.motionBlur.settings; float addon = relativeVelocity * blurMagnitude; motionBlurValue = motionBlurValue + addon < 0 ? 0 : Mathf.Min(motionBlurValue + addon, maxBlur); motionBlur.shutterAngle = motionBlurValue; postProcessingProfile.motionBlur.settings = motionBlur; } public void CalculateBloom(float relativeVelocity) { var bloom = postProcessingProfile.bloom.settings; float addon = relativeVelocity * bloomMagnitude; bloomValue = bloomValue + addon < 0 ? 0 : Mathf.Min(bloomValue + addon, maxBloom); bloom.bloom.intensity = bloomValue; postProcessingProfile.bloom.settings = bloom; } }
含蓄分割
using System.Collections; using System.Collections.Generic; using UnityEngine.PostProcessing; using UnityEngine.Rendering; using UnityEngine; public class PlayerControl : MonoBehaviour { //External scripts BlurController blurController; //Physics related attributes float speed = 10f; float accelerator = 1f; Rigidbody rb; bool jumped = false; float previousVelocity; private void Start() { rb = GetComponent<Rigidbody>(); blurController = GetComponent<BlurController> (); previousVelocity = rb.velocity.magnitude; } void FixedUpdate() { rb.AddForce(MovementHolder(CalculateJump())); } float CalculateJump() { float jump = !jumped && (Input.GetKeyDown(KeyCode.Space)) ? 40f : 0f; jumped = jump > 0 ? true : false; return jump; } Vector3 MovementHolder(float jump) { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector3 movement = new Vector3(moveHorizontal, jump, moveVertical); accelerator = Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKey(KeyCode.LeftShift) ? accelerator + 0.01f : Mathf.Max(1f, accelerator - 0.15f); float newVelocity = rb.velocity.magnitude; float relativeVelocity = newVelocity - previousVelocity; blurController.CalculateBlur(relativeVelocity); blurController.CalculateBloom(relativeVelocity); previousVelocity = newVelocity; return movement * accelerator * speed; } }
暂无关于此日志的评论。