Unity Shader实现动态雾效果

所属分类: 软件编程 / C#教程 阅读数: 114
收藏 0 赞 0 分享

Unity Shader学习:动态雾,供大家参考,具体内容如下

先将相机近裁面四个角向量传给shader,再通过观察空间下的深度值和相机位置算出像素在世界坐标系的位置,通过世界空间高度值来设定雾的范围和浓度,然后通过噪声和uv偏移实现扰动效果。得到了类似寂静岭或恶灵附身1的效果。

C#部分:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class RayMarchingCamera : MonoBehaviour {
 private Matrix4x4 frustumCorners = Matrix4x4.identity;
 public Material material;
 public Camera myCamera;
 public Transform cameraTransform;
 // Use this for initialization
 private void Start()
 {
  myCamera.depthTextureMode = DepthTextureMode.Depth;
 }

 private void OnRenderImage(RenderTexture source, RenderTexture destination)
 {
  //field of view
  float fov = myCamera.fieldOfView;
  //近裁面距离
  float near = myCamera.nearClipPlane;
  //横纵比
  float aspect = myCamera.aspect;
  //近裁面一半的高度
  float halfHeight = near * Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad);
  //向上和向右的向量
  Vector3 toRight = myCamera.transform.right * halfHeight * aspect;
  Vector3 toTop = myCamera.transform.up * halfHeight;

  //分别得到相机到近裁面四个角的向量
  //depth/dist=near/|topLeft|
  //dist=depth*(|TL|/near)
  //scale=|TL|/near
  Vector3 topLeft = cameraTransform.forward * near + toTop - toRight;
  float scale = topLeft.magnitude / near;

  topLeft.Normalize();
  topLeft *= scale;

  Vector3 topRight = cameraTransform.forward * near + toTop + toRight;
  topRight.Normalize();
  topRight *= scale;

  Vector3 bottomLeft = cameraTransform.forward * near - toTop - toRight;
  bottomLeft.Normalize();
  bottomLeft *= scale;

  Vector3 bottomRight = cameraTransform.forward * near - toTop + toRight;
  bottomRight.Normalize();
  bottomRight *= scale;

  //给矩阵赋值
  frustumCorners.SetRow(0, bottomLeft);
  frustumCorners.SetRow(1, bottomRight);
  frustumCorners.SetRow(2, topRight);
  frustumCorners.SetRow(3, topLeft);
  //将向量传给定点着色器,将屏幕画面传个shader
  material.SetMatrix("_FrustumCornorsRay", frustumCorners);
  material.SetTexture("_MainTex", source);
  Graphics.Blit(source, destination,material,0);
 }
}

shader部分:

Shader "Unlit/Fog"
{
 Properties
 {
 _MainTex ("Texture", 2D) = "white" {}
 _NoiseTex("NoiseTex",2D)="white"{}
 //雾起始高度
 _FogStartHeight("FogStartHeight",float)=0.0
 //雾终点高度
 _FogEndHeight("FogEndHeight",float)=1.0
 //雾x位移速度
 _FogXSpeed("FogXSpeed",float)=0.1
 //雾y位移速度
 _FogYSpeed("FogYSpeed",float)=0.1
 }
 SubShader
 {
 Tags { "RenderType"="Opaque" }
 LOD 100

 Pass
 {
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 #include "UnityCG.cginc"
 sampler2D _MainTex;
 sampler2D _NoiseTex;
 sampler2D _CameraDepthTexture;
 float _FogStartHeight;
 float _FogEndHeight;
 float _FogXSpeed;
 float _FogYSpeed;
 //获得相机近裁面四个角向量
 float4x4 _FrustumCornorsRay;

 struct a2v{
 float4 vertex:POSITION;
 float2 uv:TEXCOORD0;
 };

 struct v2f{
 float4 pos:SV_POSITION;
 float2 uv:TEXCOORD0;
 float4 interpolatedRay:TEXCOORD1;
 };

 v2f vert(a2v v){
 v2f o;
 o.pos=UnityObjectToClipPos(v.vertex);
 o.uv=v.uv;
 int index=0;
 if (v.uv.x<0.5&&v.uv.y<0.5)
 {
  index = 0;
 }
 else if (v.uv.x>0.5&&v.uv.y<0.5) {
  index = 1;
 }
 else if (v.uv.x>0.5&&v.uv.y>0.5) {
  index = 2;
 }
 else {
  index = 3;
 }
 //安排uv4个角对应四个角向量
 o.interpolatedRay = _FrustumCornorsRay[index];
 return o;
 }

 float4 frag(v2f i):SV_Target{
 //观察空间下的线性深度值
 float linearDepth=LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture,i.uv));
 //像素世界坐标=世界空间相机位置+像素相对相机距离
 float3 worldPos=_WorldSpaceCameraPos+linearDepth*i.interpolatedRay.xyz;
 float speedX=_Time.y*_FogXSpeed;
 float speedY=_Time.y*_FogYSpeed;
 float noise=tex2D(_NoiseTex,i.uv+float2(speedX,speedY));
 //让噪声图向黑色靠拢,黑白差距不要太大
 noise=pow(noise,0.5);
 //雾浓度=世界高度/指定范围
 float fogDensity=(_FogEndHeight-worldPos.y)/(_FogEndHeight-_FogStartHeight);
 fogDensity=smoothstep(0.0,1.0,fogDensity*noise);
 float4 color=tex2D(_MainTex,i.uv);
 //根据雾浓度混合场景和雾颜色
 color.rgb=lerp(color.rgb,float3(0.5,0.5,0.5),fogDensity);
 return color;
 }

 ENDCG
 }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多