美文网首页Unity Shader
Shaders101 - Intro to Shaders

Shaders101 - Intro to Shaders

作者: zitaoye | 来源:发表于2020-12-09 14:05 被阅读0次

    视频链接 Shaders 101 - Intro to Shaders


    Shader入门代码
    Index of /Shaders

    Unity Shader中的各种tag
    Unity Shader中的各种tag

    Unity的着色器输入语义
    向顶点程序提供顶点数据 - Unity 手册

    关于Unity Shader的不同混合
    Shader Blend混合效果

    一个很简单的color some pixel from some data from the mesh的

    // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    //这是被放在一个GameObject下面,用MeshRenderer还有Quad的Mesh Filter呈现的
    
    Shader "Unlit/basic"
    {
        Properties
        {
            _MainTex("Texture", 2D) = "white" {}
            _color123("Color",Color) = (1,1,1,1)
            _SecondTex("Second Texture", 2D) = "white" {}
            _Tween("Tween", Range(0, 1)) = 0
            
        }
    
        SubShader
        {
            Tags
            {
                "Queue" = "Transparent"
                "PreviewType" = "Plane"
            }
            Pass
            {
                //Blend SrcAlpha OneMinusSrcAlpha
    
                Blend One One
    
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
    
                #include "UnityCG.cginc"
    
                struct appdata
                {
                    float4 vertex : POSITION;
                    float2 uv : TEXCOORD0;
                };
    
                struct v2f
                {
                    float4 vertex : SV_POSITION;
                    float2 uv : TEXCOORD0;
                };
    
                v2f vert(appdata v)
                {
                    v2f o;
                    o.vertex = UnityObjectToClipPos(v.vertex);
                    o.uv = v.uv;
                    return o;
                }
    
                //局部定义 define main tex in the scope of our CG programm as a sampler2d 否则shader是不会compile的
                sampler2D _MainTex;
                sampler2D _SecondTex;
                float _Tween;
                float4 _color123;
    
                float4 frag(v2f i) : SV_Target
                {
                    //tex自己的颜色与颜色相乘再与uv的rgb值相乘
                    //float4 color1 = tex2D(_MainTex,i.uv)* _color123 * float4(i.uv.x,i.uv.y,0,1);
    
                    float4 color = tex2D(_MainTex, i.uv);
                    //float4 color2 = tex2D(_SecondTex, i.uv)* _color123 * float4(i.uv.y, i.uv.x, 1, 1);
                    
                    //float4 color = tex2D(_MainTex, i.uv * 2) * float4 (i.uv.r, i.uv.g, 1, 1);
                    //float4 color = lerp(color1, color2, _Tween);
                    //float4 color = float4(i.uv.x, i.uv.y,1,1);
    
    
                    //
                    //灰度
                    //float lum = color.r * 0.3 + color.g * 0.59 + color.b * 0.11;
                    //float4 grayscale = float4(lum, lum, lum, color.a);
                    //return grayscale*_color123;
                    
                    //使用Blend One One需要x透明通道
                    return color * color.a;
                }
                ENDCG
            }
        }
    }
    
    
    image.png

    Properties的命名,第一个是变量名,“”里的是显示名,数据格式,默认;

    https://docs.unity3d.com/cn/current/Manual/SL-Properties.html

    相关文章

      网友评论

        本文标题:Shaders101 - Intro to Shaders

        本文链接:https://www.haomeiwen.com/subject/gmqhgktx.html