Skip to content

Instantly share code, notes, and snippets.

@stilllisisi
Created March 11, 2020 08:06
Show Gist options
  • Save stilllisisi/7e87ce34cb2edafda644c238b88668d9 to your computer and use it in GitHub Desktop.
Save stilllisisi/7e87ce34cb2edafda644c238b88668d9 to your computer and use it in GitHub Desktop.
【游戏-unity】Shader全景图实现。为了让unity项目中的建筑场景显的逼真,通常的做法是使用一个360°无死角的全景背景来避免穿帮,这篇文章给大家介绍使用shader实现全景图。
//思路
//1. 通过法线方向求平面坐标。x=atan(x/z),y=acos(y/r) r为半径;
//2. 通过ddx,ddy 偏导数模糊极坐标像素解决极坐标问题。
//3. 通过反射比较真实的扑捉视觉。
Shader"QQ/Sky/Latitude"
{
Properties
{
_MainTex("Texture",2D)="white"{}
[Enum(UnityEngine.Rendering.CullMode)]_Cull("CullMode",float)=2
[Enum(Off,0,On,1)]_Inverse("inverse",float)=0
}
SubShader
{
Tags{"RenderType"="Opaque"}
LOD100
Pass
{
Cull[_Cull]
CGPROGRAM
#pragmavertexvert
#pragmafragmentfrag
#include"UnityCG.cginc"
structa2v
{
float4vertex:POSITION;
float3normal:NORMAL;
};
structv2f
{
float4pos:SV_POSITION;
float3normal:TEXCOORD0;
float3wPos:TEXCOORD1;
};
sampler2D_MainTex;
float4_MainTex_ST;
float_Cull;
float_Inverse;
v2fvert(a2vv)
{
v2fo;
o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
o.normal=v.normal;
o.wPos=mul(unity_ObjectToWorld,v.vertex);
returno;
}
fixed4frag(v2fi):SV_Target
{
float3nor=normalize(i.normal);
float3viewDir=normalize(_WorldSpaceCameraPos.xyz-i.wPos);
float3refl=(_Inverse>0?1:-1)*reflect(viewDir,nor);
floatu=atan2(refl.x,refl.z)/UNITY_PI;
u=(u+1.0)*0.5;
floatv=acos(refl.y)/UNITY_PI;
float2uv=float2(u,v)*_MainTex_ST.xy+_MainTex_ST.zw;
fixed4col=tex2D(_MainTex,uv,float2(0,0),float2(0,0));
returncol;
}
ENDCG
}
}
}
@stilllisisi
Copy link
Author

@stilllisisi
Copy link
Author

1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment