[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("Cull Mode", float) = 2
		[Enum(Off, 0, On, 1)] _Inverse("inverse", float) = 0
	}
		SubShader
		{
			Tags { "RenderType" = "Opaque" }
			LOD 100


			Pass
			{
				Cull[_Cull]
				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#include "UnityCG.cginc"


				struct a2v
				{
					float4 vertex : POSITION;
					float3 normal : NORMAL;
				};


				struct v2f
				{
					float4 pos : SV_POSITION;
					float3 normal : TEXCOORD0;
					float3 wPos:TEXCOORD1;
				};


				sampler2D _MainTex;
				float4 _MainTex_ST;
				float _Cull;
				float _Inverse;
				v2f vert(a2v v)
				{
					v2f o;
					o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
					o.normal = v.normal;
					o.wPos = mul(unity_ObjectToWorld, v.vertex);
					return o;
				}


				fixed4 frag(v2f i) : SV_Target
				{
					float3 nor = normalize(i.normal);
					float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.wPos);
					float3 refl = (_Inverse > 0 ? 1 : -1) * reflect(viewDir,nor);
					float u = atan2(refl.x,refl.z) / UNITY_PI;
					u = (u + 1.0)*0.5;
					float v = acos(refl.y) / UNITY_PI;
					float2 uv = float2(u, v)*_MainTex_ST.xy + _MainTex_ST.zw;
					fixed4 col = tex2D(_MainTex,uv,float2(0,0), float2(0,0));
					return col;
				}
				ENDCG
			}
		}
}


你可能感兴趣的:(Unity3D,Shader)