首页 技术 正文
技术 2022年11月21日
0 收藏 593 点赞 3,235 浏览 2676 个字

这里使用一种更高效地从深度纹理中重建世界坐标的方法。

首先计算摄像机的视锥体的四条射线向量进行线性插值,插值后的值便是该像素在世界空间坐标下到摄像机的方向。

然后通过与深度值相乘即可得到摄像机位置到该像素的向量,加上摄像机的位置则是该像素在世界空间中的位置。

转载请注明出处:https://www.cnblogs.com/jietian331/p/9443343.html

c#代码:

 using UnityEngine; public class HighFog2 : PostEffectRenderer
{
[SerializeField]
Color m_fogColor = Color.white;
[Range(0f, 1f)]
[SerializeField]
float m_fogDensity = 0.9f;
[SerializeField]
float m_fogPosY = 0.1f;
[SerializeField]
float m_fogDisappearHeight = ; protected override string ShaderName
{
get { return "Custom/Study/HighFog2"; }
} protected override void OnRenderImage(RenderTexture src, RenderTexture dest)
{
float fov = base.SelfCamera.fieldOfView;
float near = base.SelfCamera.nearClipPlane;
Transform camT = base.SelfCamera.transform; float halfFOV = fov * Mathf.PI / 360f;
float toTopDis = near * Mathf.Tan(halfFOV);
float toRightDis = toTopDis * (Screen.width / Screen.height);
Vector3 toTop = camT.up * toTopDis;
Vector3 toRight = camT.right * toRightDis;
Vector3 toCenter = camT.forward * near;
Vector3 topLeft = (toCenter + toTop - toRight) / near;
Vector3 topRight = (toCenter + toTop + toRight) / near;
Vector3 bottomLeft = (toCenter - toTop - toRight) / near;
Vector3 bottomRight = (toCenter - toTop - toRight) / near; Matrix4x4 matrix = Matrix4x4.identity;
matrix.SetRow(, bottomLeft);
matrix.SetRow(, bottomRight);
matrix.SetRow(, topLeft);
matrix.SetRow(, topRight); base.Mat.SetMatrix("_CameraRays", matrix);
base.Mat.SetColor("_FogColor", m_fogColor);
base.Mat.SetFloat("_FogDensity", m_fogDensity);
base.Mat.SetFloat("_FogPosY", m_fogPosY);
base.Mat.SetFloat("_FogDisappearHeight", m_fogDisappearHeight);
base.OnRenderImage(src, dest);
}
}

shader:

 Shader "Custom/Study/HighFog2"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
} SubShader
{
Pass
{
ZTest Always
ZWrite Off
Cull Off CGPROGRAM
#pragma vertex vert
#pragma fragment frag #include "UnityCG.cginc" struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
}; struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 ray : TEXCOORD1;
}; sampler2D _MainTex;
sampler2D _CameraDepthTexture;
uniform float4x4 _CameraRays;
uniform float4 _FogColor;
uniform float _FogDensity;
uniform float _FogPosY;
uniform float _FogDisappearHeight; v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv; int index;
if(v.vertex.x < 0.5 && v.vertex.y < 0.5)
{
index = ;
}
else if(v.vertex.x > 0.5 && v.vertex.y < 0.5)
{
index = ;
}
else if(v.vertex.x > 0.5 && v.vertex.y > 0.5)
{
index = ;
}
else
{
index = ;
}
o.ray = _CameraRays[index].xyz; return o;
} fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv); float d = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv));
float3 worldPos = _WorldSpaceCameraPos + i.ray * d; float fogWeight;
if(worldPos.y < _FogPosY)
{
fogWeight = ;
}
else if(worldPos.y > _FogPosY + _FogDisappearHeight)
{
fogWeight = ;
}
else
{
fogWeight = - (worldPos.y - _FogPosY) / _FogDisappearHeight;
} return lerp(col, _FogColor, fogWeight * _FogDensity);
} ENDCG
}
} Fallback Off
}

效果图如下:

Unity shader学习之屏幕后期处理效果之高度雾,重建world pos方法2

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,074
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892