[shader]动态切换天空球

最终效果


思路

改进之前的全景图,应用到天空球。
利用3d noise算法,生成噪声。

源代码

Shader "QQ/Sky/MultiSky"
{
	Properties
	{
		_Color("Color",Color) = (1,1,1,1)
		_Rotation("Rotation",Range(0,360)) = 360
		[NoScaleOffset]_MainTex("Texture1", 2D) = "white" {}
		[NoScaleOffset]_SecondTex("Texture2", 2D) = "white" {}
		_BlendColor("blend Color",Color) = (1,1,1,0)
		_Blend("blend",Range(0,1)) = 0.5
		_Hill("hill",Range(0,1)) = 0.1
	}
		SubShader
	{
		Tags{
		"Queue" = "Background"
		"RenderType" = "Background"
		"PreviewType" = "Skybox"
		}
		Zwrite Off
		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			struct a2v
			{
				float4 vertex : POSITION;
			};

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

			sampler2D _MainTex;
			float4 _MainTex_ST;
			sampler2D _SecondTex;

			float _Blend;
			float _Hill;
			fixed4 _Color;
			fixed4 _BlendColor;
			float _Rotation;

			sampler2D _TexUp;
			sampler2D _TexDown;
			float3x3 RotationY(float _y)
			{
				_y = radians(_y);
				float _sin = sin(_y);
				float _cos = cos(_y);
				return float3x3(_cos, 0, -_sin, 0, 1, 0, _sin, 0, _cos);
			}

			v2f vert(a2v v)
			{
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.wPos = mul(unity_ObjectToWorld, v.vertex);
				return o;
			}

			float3 hash23(float3 p)
			{
				const float3 k = float3(0.3183099, 0.3678794,0.57465413);
				p = p*k + p.zyx;
				return -1.0 + 2.0 * frac(16.0 * p * frac(p.x*p.y*p.z*(p.x + p.y+p.z)));
			}
			float perlinNoise(float3 p)
			{
				float3 i = floor(p);
				float3 f = frac(p);
				float3 u = f * f * (3.0 - 2.0 * f);
				float yx0 = lerp(dot(hash23(i + float3(0.0, 0.0, 0.0)), f - float3(0.0, 0.0, 0.0)), dot(hash23(i + float3(1.0, 0.0, 0.0)), f - float3(1.0, 0.0, 0.0)), u.x);
				float yx1 = lerp(dot(hash23(i + float3(0.0, 1.0, 0.0)), f - float3(0.0, 1.0, 0.0)), dot(hash23(i + float3(1.0, 1.0, 0.0)), f - float3(1.0, 1.0, 0.0)), u.x);
				float yy0 = lerp(yx0,yx1,u.y);
				float yz0 = lerp(dot(hash23(i + float3(0.0, 0.0, 1.0)), f - float3(0.0, 0.0, 1.0)), dot(hash23(i + float3(1.0, 0.0, 1.0)), f - float3(1.0, 0.0, 1.0)), u.x);
				float yz1 = lerp(dot(hash23(i + float3(0.0, 1.0, 1.0)), f - float3(0.0, 1.0, 1.0)), dot(hash23(i + float3(1.0, 1.0, 1.0)), f - float3(1.0, 1.0, 1.0)), u.x);
				float yy1 = lerp(yz0,yz1,u.y);
				return saturate((lerp(yy0,yy1,u.z)+0.6)*0.5);
			}
			//pc端可用次迭代出细腻的效果
			float noise_sum(float3 p,int num)
			{
				float f = 0.0;
				p *= 4.0;
				float c = 1.0;
				for (int i = 0; i < num; i++)
				{
					c *= 0.5;
					f += c * perlinNoise(p);
					p *= 2.0;
				}
				return f;
			}
			fixed4 frag(v2f i) : SV_Target
			{
				float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.wPos);
				viewDir = mul(RotationY(_Rotation), viewDir);
				float u = (atan2(viewDir.x, viewDir.z) + UNITY_PI) / UNITY_TWO_PI;
				float v = acos(viewDir.y) / UNITY_PI;
				float2 uv = float2(u, v);
				fixed4 tex0 = tex2D(_MainTex, uv, float2(0.00001, 0.0), float2(0.0, 0.0));
				fixed4 tex1 = tex2D(_SecondTex, uv, float2(0.00001, 0.0), float2(0.0, 0.0));

				float noise = perlinNoise(viewDir*8);
				_Blend = (_Blend - 0.4)*1.5;
				_Hill *= 0.4;
				float blend = smoothstep(_Blend - _Hill, _Blend + _Hill, noise);
				fixed4 col = lerp(tex1,tex0, blend);
				col *= _Color;
				col += (0.5 - abs(blend - 0.5))*3.0*_BlendColor*_BlendColor.a;
				return col;
			}
			ENDCG
		}
	}
}


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