Unity Shader学习之积雪练习

Shader "Custom/_Snow" {
	Properties {
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_Bump ("Bump", 2D) = "bump" {}

		_Snow ("Snow Level", Range(0,5)) = 0
		_SnowColor("Snow Color", Color) = (1,1,1,1)
		_SnowDir("Snow Direction",Vector) = (0,1,0)
		_SnowThick ("Snow thickness", Range(-1,1)) = 0
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Lambert vertex:vert

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;
		sampler2D _Bump;
		sampler2D _RampTex;

		struct Input {
			float2 uv_MainTex;
			float2 uv_Bump;
			float3 viewDir;
			float3 worldNormal; INTERNAL_DATA
		};

		fixed4 _Color;

		half _Snow;
		fixed4 _SnowColor;
		fixed3 _SnowDir;

		float _SnowThick;
		// tangent(切线)的效果是整体变化
		// normal(法线)或texcoord(第一个uv坐标)都是逐步变化
		// 顶点函数里对需要积雪的地方给一个厚度
		void vert (inout appdata_full v) {
			if(dot(v.normal, _SnowDir.xyz) > lerp(1,-1,_SnowThick)){
				v.vertex.xyz += v.normal * _SnowThick * (dot(v.normal, _SnowDir.xyz)*0.5 + 0.5) * 0.05;
			}
		}

		void surf (Input IN, inout SurfaceOutput o) {
			half4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    		o.Normal = UnpackNormal(tex2D(_Bump, IN.uv_Bump));

    		if (dot(WorldNormalVector(IN, o.Normal), _SnowDir.xyz) > lerp(1,-1,_Snow)) {
    			o.Albedo = _SnowColor.rgb * _Color;
    		} else {
				o.Albedo = c.rgb;
        	}
    		o.Alpha = c.a;
		}

		ENDCG
	}
	FallBack "Diffuse"
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值