当前位置:网站首页>unity 自定义天空球模型防止被裁剪
unity 自定义天空球模型防止被裁剪
2022-07-17 06:53:00 【暮志未晚Webgl】
在unity中,有时候不想使用内置的天空球去渲染,那么,我们就会使用一个球体去渲染天空球。为了保证游戏场景模型都放置在天空球内。球体就会放大的很大,那么问题就来了,这样会导致球体超出了相机的可视范围,导致天空球无法渲染,调整相机的裁减远面,会影响性能,还影响渲染效果。
这里有个解决方案,就是我们可以在材质里面去修改球体的顶点着色器输出的裁减坐标,将它的值限制在-1到1的范围内,来解决无法被渲染的问题。
这是一个基于unlit修改的防止被裁剪的天空球shader
Shader "Unlit/SkyBox"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {
}
}
SubShader
{
Tags {
"RenderType" = "Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
//将输出位置的z坐标限制在-1到1的范围内,这样可以防止自定义的天空球被裁剪掉
#if UNITY_REVERSED_Z //这个宏是用来判断平台的,有个平台最远裁剪值是1,有的是-1
o.vertex.z = o.vertex.w * 0.000001f;
#else
o.vertex.z = o.vertex.w * 0.999999f;
#endif
return o;
}
half4 frag(v2f i) : SV_Target
{
// sample the texture
half4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
}
边栏推荐
猜你喜欢
随机推荐
CCF-CSP《202206-2—寻宝!大冒险!》
Shenzhen Prudential written examination record
Yolov5 label and establish your own data set
Flowable query, complete, void, delete tasks
openvino机器学习初体验
redis主从复制
Random forest of machine learning
[C# Console]-C# 控制臺類
912. Sort array (array sort)
ObjectARX--自定义圆的实现
通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
175. Combine two tables (MySQL database connection)
2022 review questions and mock exams for main principals of hazardous chemical business units
redis事务
Modify radio style
How to check whether the app has user information and data leakage vulnerabilities
Conversation technology [dark horse introduction series]
Redis data persistence
【C语言】自定义类型详解:结构体、枚举、联合
JS array intersection, subtraction and union







