JingFengJi
for me
FRIENDS
llcat

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

2018-12-22

两种颜色的条纹状背景

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
        }
    }
}

效果图:

在这里插入图片描述

Author: Mr.Ding

Link: https://jingfengji.github.io/2018/12/22/Unity Shader编程之一:多颜色条纹背景/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
Unity编辑器拓展之二十四:基于Unity Node Editor、ScriptableObject的配置化新手引导系统
NextPost >
Unity编辑器拓展之二十三:基于ObjectField的FileField
CATALOG
  1. 1. 两种颜色的条纹状背景