Unity 全局雾效Shader:基于摄像机距离的雾效渲染
来源:互联网 发布:苹果cms插件 编辑:程序博客网 时间:2024/09/21 09:26
写在前面的话:
这个雾效大部分代码源自 冯乐乐的书籍:Unity shader 入门精要。
在书中的示例代码中,主要基于了屏幕高度,而对屏幕进行雾效的渲染。我将其中的部分shader代码修改后,实现了基于摄像机距离的雾效渲染。
先解释雾效shader代码的原理:
雾效的实现是基于屏幕后处理,即在摄像机上挂一个脚本,这个脚本中执行OnRenderImage函数,对摄像机所渲染得到的最终图像进行处理,进而表现出屏幕上有雾的效果。
而雾效shader中的处理方式是:先计算出屏幕上每一个像素基于摄像机的线性深度值,然后再通过插值的方式,根据摄像机在世界空间下的实际坐标,大致计算出屏幕上每一个像素的世界坐标。最后再根据像素的坐标,让它和雾的颜色进行一定程度的混合,例如像素越近显示得越清晰,而像素越远则雾的颜色越浓。
以下代码,c#脚本需要挂在场景中的主摄像机上,然后把shader文件拖到脚本对应的变量上就可以看最终效果,代码如下:
c#脚本 :
using System.Collections;using System.Collections.Generic;using UnityEngine;public class FogWithDepthTexture : MonoBehaviour{ public Shader fogShader; private Material fogMaterial = null; public Material material { get { //fogMaterial = CheckShaderAndCreateMaterial(fogShader, fogMaterial); if (fogShader == null) { return null; } if (fogShader.isSupported && fogMaterial && fogMaterial.shader == fogShader) return fogMaterial; if (!fogShader.isSupported) { return null; } else { fogMaterial = new Material(fogShader); fogMaterial.hideFlags = HideFlags.DontSave; if (fogMaterial) return fogMaterial; else return null; } //return fogMaterial; } } private Camera myCamera; public Camera camera { get { if(myCamera == null) { myCamera = transform.GetComponent<Camera>(); } return myCamera; } } public Transform cameraTransform { get { return camera.transform; } } [Range(0.0f, 3.0f)] public float fogDensity = 1.0f; public Color fogColor = Color.white; public float fogStart = 0.0f; public float fogEnd = 2.0f; private void OnEnable() { camera.depthTextureMode |= DepthTextureMode.Depth; } private void OnRenderImage(RenderTexture source, RenderTexture destination) { if(material != null) { Matrix4x4 frustumCorners = Matrix4x4.identity; float fov = camera.fieldOfView; float near = camera.nearClipPlane; float far = camera.farClipPlane; float aspect = camera.aspect; float halfHeight = near * Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad); Vector3 toRight = cameraTransform.right * halfHeight * aspect; Vector3 toTop = cameraTransform.up * halfHeight; Vector3 topLeft = cameraTransform.forward * near - toRight + toTop; 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 - toRight - toTop; bottomLeft.Normalize(); bottomLeft *= scale; Vector3 bottomRight = cameraTransform.forward * near + toRight - toTop; bottomRight.Normalize(); bottomRight *= scale; frustumCorners.SetRow(0, bottomLeft); frustumCorners.SetRow(1, bottomRight); frustumCorners.SetRow(2, topRight); frustumCorners.SetRow(3, topLeft); material.SetMatrix("_FrustumCornersRay", frustumCorners); material.SetMatrix("_ViewProjectionInverseMatrix", (camera.projectionMatrix * camera.worldToCameraMatrix).inverse); material.SetFloat("_FogDensity", fogDensity); material.SetColor("_FogColor", fogColor); material.SetFloat("_FogStart", fogStart); material.SetFloat("_FogEnd", fogEnd); Graphics.Blit(source, destination, material); } else { Graphics.Blit(source, destination); } }}
以下是shader代码:
Shader "Unlit/FogWithDepthTexture"{ Properties { _MainTex ("Texture", 2D) = "white" {} _FogDensity("FogDensity",Float) = 1.0 _FogColor("FogColor",Color) = (1,1,1,1) _FogStart("FogStart",Float) = 0.0 _FogEnd("FogEnd",Float) = 1.0 } SubShader { CGINCLUDE #include "UnityCG.cginc" float4x4 _FrustumCornersRay; sampler2D _MainTex; half4 _MainTex_TexelSize; sampler2D _CameraDepthTexture; half _FogDensity; fixed4 _FogColor; float _FogStart; float _FogEnd; struct v2f { float4 pos : SV_POSITION; half2 uv : TEXCOORD0; half2 uv_depth : TEXCOORD1; float4 interpolatedRay : TEXCOORD2; }; v2f vert(appdata_img v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.texcoord; o.uv_depth = v.texcoord; #if UNITY_UV_STARTS_AT_TOP if(_MainTex_TexelSize.y < 0) o.uv_depth.y = 1 - o.uv_depth.y; #endif int index = 0; if(v.texcoord.x < 0.5 && v.texcoord.y < 0.5) { index = 0; } else if(v.texcoord.x > 0.5 && v.texcoord.y < 0.5) { index = 1; } else if(v.texcoord.x > 0.5 && v.texcoord.y > 0.5) { index = 2; } else { index = 3; } #if UNITY_UV_STARTS_AT_TOP if(_MainTex_TexelSize.y < 0) index = 3 - index; #endif o.interpolatedRay = _FrustumCornersRay[index]; return o; } float GetFogRatioByDistance(float3 worldPos) { float f = 1 - (_FogEnd - abs(worldPos.z - _WorldSpaceCameraPos.z))/(_FogEnd - _FogStart); return f; } fixed4 frag(v2f i) : SV_Target { float linearDepth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture,i.uv_depth)); float3 worldPos = _WorldSpaceCameraPos + linearDepth * i.interpolatedRay.xyz; //float fogDensity = (_FogEnd - worldPos.y) / (_FogEnd - _FogStart); //若想基于屏幕高度来进行雾的渲染,可以使用这行代码,并屏蔽下面一行的代码 float fogDensity = GetFogRatioByDistance(worldPos); fogDensity = saturate(fogDensity * _FogDensity); fixed4 finalColor = tex2D(_MainTex,i.uv); finalColor.rgb = lerp(finalColor.rgb,_FogColor.rgb,fogDensity); return finalColor; } ENDCG Pass { Ztest Always Cull Off Zwrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag ENDCG } } Fallback Off}
其中,调整FogStart 和 FogEnd变量,可以调整雾显示的远近和范围。
另外,目前使用的雾效是线性的雾效,也可以使用指数算法来实现雾效,效果可能会更真实一些。具体实现可以参考开头作者的书中的内容,或者搜索相关内容。
如果以上代码不能运行或者报错,可以在博客下面留言,至少在我机器上可以正常使用;
有任何问题也可以相互交流。
阅读全文
0 0
- Unity 全局雾效Shader:基于摄像机距离的雾效渲染
- Unity Shader 学习笔记(25) 全局雾效
- Unity Shader之雾效
- unity双面渲染shader的编写
- Unity Shader 学习笔记(31) 基于物理的渲染技术(PBS)、BRDF
- 【Unity Shader】渲染流水线
- unity shader:渲染流程
- unity shader:渲染优化
- unity在shader中获取当前摄像机的方向向量
- 基于过程渲染的小地图shader
- 基于过程渲染的小地图shader
- unity开发之四:摄像机渲染纹理RenderTexture的使用
- 蛋哥的学习笔记之-基于Unity的Shader编程:0-2 基本3D图形渲染管线概述
- Unity Shader 四 渲染管线
- unity shader:非真实感渲染
- unity shader 固定管线实例(四) 渲染不一样的背面
- 【Unity Shader】新书封面 — Low Polygon风格的渲染
- 解决Unity Vertex Shader 渲染丢失的问题
- 数据库简单优化
- 【模板】快速幂
- Eclipse使用Maven搭建Spring Boot应用并访问数据库
- jsp在浏览器与服务器之间的交互原理
- Delphi类型区分——枚举
- Unity 全局雾效Shader:基于摄像机距离的雾效渲染
- NIFI 集群配置 Clustering Configuration
- 测试环境?
- 01背包使用滚动数组优化详细讲解
- 电热管该如何使用
- es集群搭建(2个节点)
- thymeleaf 学习笔记-基础篇(中文教程)
- google analytics 添加跟踪代码
- PostgreSQL PostGIS pgrouting 泰国(thailand)全国路网分析(1)数据准备