《Unity Shader 入门精要》MyProject: letsStartShader笔记 01

 Static 写在前面

        咳咳,由于本人英语水平实在有点着急,而众所周知外文资料是一个逃不掉的话题……恰好现在开始接触shader,因此就打算用尽自己的中学英语水平记录下学shader的过程,这样多少能培养一点阅读英语文献的感觉吧……

        所以这系列文章本质上是个人学习用,毕竟全是些不严谨的用词、不严谨的思考,甚至还会有英语方面的语法错误……这段声明也算是为意外点进来的朋友们负责,这系列文章只是个人学习随笔,而非专业参考文章!此外我不会在文章中介绍部分代码的数学原理,毕竟数学这东西用中文都还没学明白……虽然文章没什么营养价值,但多少有我的一点点思考,也算是在整个互联网里留下点什么吧……OK该系列的全部中文部分到此为止,后面新的文章里不再声明!

        封面是个人一张风景速途练习,至于画原创场景厚涂那还早……

【如果有有缘人看到后面的文章后倒回来看这篇才看见这个声明,那送您一串开心的小符号 :)】

0、Before the rendering starts...

        To build a simplest unity shader project (I'll say it as "shader" later, but remember unity shader is never a real shader), we don't need to assign any Properties, RenderSetup and Tags. Let's start with CGPROGRAM (and I'm used to adding ENDCG at the same time)

Shader "please_write_down_a_proper_dir"{
    Properties{
        // not required now
    }
    SubShader{
        // RenderSetup and Tags
        Pass{
            // RenderSetup and Tags

            // Start from here!
            CGPROGRAM

            // coding...

            ENDCG
        }
    }
}

// All codes are edited in vscode. 
// I'm really amazed that vscode can even support ShaderLab!

All of the following codes are the content in //coding...

1、Where are your vertex shader and fragment shader?

        At the very beginning, you need to give your shader a vert() and frag(). It's sure that you can define your function with any name, but I think the name 'vert' and 'frag' is interesting as well...

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            float4 vert(){

            }

            float4 frag(){
                
            }

            ENDCG
        }

2、What Objects do you use in your shader?

        You need a "application to vertex" and a "vertex to fragment". The book call them "a2v" and "v2f". Names in unity shader are really interesting...

            struct a2v {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float4 texcoord : TEXCOORD0;
            };

            struct v2f {
                float4 pos : SV_POSITION;
                fixed3 color : COLOR0;
            };

        The first time I saw this structure was in CSS. They're not funtions, they are objects (This statement may not be precise, but I'd like understand it this way). If you put them into a line, it may be more clear. So you need a ';' at the end of the struct. (Well, I open my css document, and find there isn't a ';' in the end... But in ShaderLab you need.)

            struct a2v{float4 vertex : POSITION; float3 normal : NORMAL; float4 texcoord : TEXCOORD0;};
            // String a2v = "there are some properties.";

3、How do your vert() and frag() interact?

N. What is semantics?

        You are the beneficiary of Unity Shader. Actually Shader has defined some special words that:

        ·Gives you those most important data.

        ·Tells itself what to use when rendering. 

        You just need:

        ·Enter a capital word to traverse all data.

        ·assign your calculation result to a capital word.

        

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            struct a2v {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float4 texcoord : TEXCOORD0;
            };

            struct v2f {
                float4 pos : SV_POSITION;
                fixed3 color : COLOR0;
            };

            v2f vert (a2v v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.color = v.normal * 0.5 + fixed3(0.5, 0.5, 0.5);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target {
                fixed3 c = i.color;
                return fixed4(c, 1.0);
            }

            ENDCG

i. Vertex Shader startup 

        When the vert() starts, it access the a2v.

                When the struct a2v is created, it gets POSITION, NORMAL and TEXCOORD0 given by your unity project (you are just a shader, only part of the whole project!).

        vert() begins. First, it create a v2f.

                You must give the system SV_POSITION and COLOR0, and a v2f can help you do that.

        vert() ends. Return the result to v2f. Now your shader has known the SV_POSITION and COLOR0.

ii.Fragment Shader startup

        Note that the return of frag() will be assigned to SV_Target.

        In frag(), you access the COLOR0, add the fourth dimension and give the value 1.0, and return them to SV_Target.

Rendering successfully …… seemingly?

        Shader can help you do the complex things about mathematics and color sketch tones, but don't forget you haven't told the shader about your 3D object's inherent color!

 FINAL CODE 

Shader "Unlit/First Shader"
{
    Properties {
        _Color ("Color Tint", Color) = (1.0, 1.0, 1.0, 1.0)
    }


    SubShader {
        Pass {
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            fixed4 _Color;
            
            struct a2v {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float4 texcoord : TEXCOORD0;
            };

            struct v2f {
                float4 pos : SV_POSITION;
                fixed3 color : COLOR0;
            };

            v2f vert (a2v v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.color = v.normal * 0.5 + fixed3(0.5, 0.5, 0.5);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target {
                fixed3 c = i.color;
                c *= _Color.rgb;
                return fixed4(c, 1.0);
            }
            ENDCG
        }
    }
}

《Unity Shader 入门精要》MyProject: letsStartShader笔记 01_第1张图片

        Functions of RGB values in computer arts can always generate beautiful colors~ Don't you know why the color changes? You have calculated the value of light and shade, but when you return the SV_Target, you use the value to change the RGB value. So how to present the light and shade relationship? Let's do something about Diffuse in next passage!

你可能感兴趣的:(letsStartShader,学习,着色器,unity)