雾化效果的实现是通过已经生成的像素的颜色和像素到镜头的距离来确定一个常量色来实现的,使用雾化i更不会改变已经混合的像素的透明度值,只是改变了RGB的值。
Demo:
通过玩家选择雾的模型来修改雾的形态,雾共有三种形式,分别为指数、指数平方、线性三种由远及近的更换模式,本Demo就是通过修改雾的模式来观测者三者的不同,
1.步骤:
搭建环境、灯光、模型摆放等如图
2.进行雾化的基本设置
选择Window->Lighting->Setting->other Setting,在下拉菜单中勾选fog
建立脚本命名为Pop
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pop : MonoBehaviour
{
private float ypos1 = 0.0f;//声明边框一的位置
private float ypos2 = 0.0f;//生命边框二的位置
private float ypos3 = 0.0f;//声明边框三的位置
private float ypos4 = 0.0f;//声明边框四的位置
private bool isshowDropdownButton;//声明默认下拉按钮值
private bool isshowDropupButton;//声明回弹按钮值
public float speed = 500.0f;//声明弹出弹回速度
private string St = "FogMode";
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(isshowDropdownButton == true)
{
ypos1 += Time.deltaTime * speed;
ypos2 += Time.deltaTime * speed;
ypos3 += Time.deltaTime * speed;
ypos4 += Time.deltaTime * speed;
if(ypos1>=60)
{
ypos1 = 60;
}
if(ypos2>=120)
{
ypos2 = 120;
}
if(ypos3>=180)
{
ypos3 = 180;
}
if(ypos4 >= 240)
{
ypos4 = 240;
}
if(isshowDropupButton == true)
{
ypos1 -= Time.deltaTime * speed;
ypos2 -= Time.deltaTime * speed;
ypos3 -= Time.deltaTime * speed;
ypos4 -= Time.deltaTime * speed;
if(ypos1>=0||ypos2>=0||ypos3>=0||ypos4>=0)
{
isshowDropupButton = false;
isshowDropdownButton = false;
ypos1 = 0;
ypos2 = 0;
ypos3 = 0;
ypos4 = 0;
}
}
}
}
private void OnGUI()
{
if(isshowDropdownButton == false)
{
if(GUI.RepeatButton(new Rect(50,0,200,60),St))
{
isshowDropdownButton = true;
}
}
if(isshowDropdownButton == true)
{
if(GUI.Button(new Rect(50,0,200,60),St))
{
isshowDropdownButton = false;
isshowDropupButton = true;
}
if(GUI.Button(new Rect(50,ypos1,200,60),"Linear"))
{
isshowDropupButton = true;
isshowDropdownButton = false;
RenderSettings.fogMode = FogMode.Linear;
}
if(GUI.Button(new Rect(50,ypos2,200,60),"Exp2"))
{
isshowDropupButton = true;
isshowDropdownButton = false;
RenderSettings.fogMode = FogMode.ExponentialSquared;
}
if(GUI.Button(new Rect(50,ypos3,200,60),"Exp"))
{
isshowDropupButton = true;
isshowDropdownButton = false;
RenderSettings.fogMode = FogMode.Exponential;
}
if(GUI.Button(new Rect(50,ypos4,200,60),"None"))
{
isshowDropupButton = true;
isshowDropdownButton = false;
RenderSettings.fogMode = 0;//无雾模式
St = "FogMode";
}
}
}
}
实现效果如下
通过点击按钮选择不同的雾化状态。