Unity Shader编程之一:多颜色条纹背景

博客迁移

个人博客站点,欢迎访问,www.jiingfengji.tech

两种颜色的条纹状背景

Shader "Custom/Flag"
{
	Properties
	{
		[PerRendererData]_MainTex ("Texture", 2D) = "white" {}
		_1stColor("1stColo",Color) = (0,0,0,1)
		_2ndColor("2ndColor",Color) = (1,1,1,1)
		_tileNum("tile num", Range (2, 20)) = 10
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog
			
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;
			};

			sampler2D _MainTex;
			fixed4 _1stColor;
			fixed4 _2ndColor;
			int _tileNum;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				//i.uv * _tileNum 将uv放大
				//frac函数,取其小数部分
				float2 newuv = frac(i.uv*_tileNum);
				//step函数小于0.5返回0,否则返回1,再lerp一下,也就是uv.x小于0.5,返回_1stColor,否则返回_2ndColor
				fixed4 color = lerp(_1stColor,_2ndColor,step(0.5,newuv.x));
				return color;
			}
			ENDCG
		}
	}
}

效果图:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值