unity shader中一些常用函数总结

参考网址:http://www.manew.com/forum.php?mod=viewthread&tid=97247&extra=page%3D2%26filter%3Dtypeid%26typeid%3D132

蛮牛的专栏可以常看看:
http://www.manew.com/forum-47-132-2.html

第1个函数:step函数

Shader "MyShader/Step"
{
	Properties
	{
		_Variable("variable",range(1,10))=1
		_Threshold("threshold", int) = 5
	}
	SubShader
	{
		Pass
		{
			Cull off
			ZWrite off
			ZTest Always

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			
			struct appdata
			{
				float4 vertex : POSITION;
			};

			struct v2f
			{
				float4 vertex:SV_POSITION;
			};

			v2f vert(appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				return o;
			}

			int _Variable;
			int _Threshold;

			fixed4 frag(v2f i):SV_Target
			{
				fixed b = step(_Threshold, _Variable);
				if (b == 0)
				{
					return fixed4(1, 0, 0, 1);
				}
				else if (b == 1)
				{
					return fixed4(0, 1, 0, 1);
				}
				else
				{
					return fixed4(0, 0, 1, 1); //不会走到
				}
			}
			ENDCG
		}
	}
}

解释:
step(_Threshold, _Variable);
这个函数的意思就是如果_Variable<_Threshold,则返回0;_Variable>=_Threshold,则返回1;
其实就是分段函数。
在这里插入图片描述
效果:
在这里插入图片描述

设置阈值为5,当滑动slider的时候,小于5则返回红色;大于等于5则返回绿色。

第2个函数:clam(x,a,b):
x如果小于a返回a,如果大于b返回b,在a~b范围内返回x

第三个函数:smoothstep:
smoothstep(min,max,x):
另:Factor= (x-min)/(max-min);

res=3FactorFactor - 2FactorFactor*Factor,当x=min时返回0,当x=max时返回1
https://blog.csdn.net/u010333737/article/details/82859246
http://www.pianshen.com/article/5880316450/
其他函数:三角函数、平移、旋转、缩放函数见原文。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值