Shader 는 정점 프로 그래 밍 을 바탕 으로 하 는 Lambert 빛, Phong 빛 과 Blinn - Phong 빛 을 기반 으로 합 니 다.

7759 단어 Unity3DShader
Lambert 일조 모델 (환경 광 + 난반사):
Shader "Custom/ShaderLambert" {

    SubShader {
        Pass{
            CGPROGRAM

            #include "unitycg.cginc"
            #include "lighting.cginc"
            #pragma vertex vert
            #pragma fragment frag   

            struct params{
                float4 Pos:POSITION;
                float4 Col:COLOR;
            };

            params vert(appdata_base ab)
            {
                params pms;
                pms.Pos = mul(UNITY_MATRIX_MVP,ab.vertex);
                pms.Col = UNITY_LIGHTMODEL_AMBIENT;       //   

                float3 N;
                float3 L;
                //    
                //N = normalize(UnityObjectToWorldNormal(ab.normal));   //    UnityObjectToWorldNormal:           

                N = normalize(ab.normal);     //       
                N = mul(float4(N,0),_World2Object).xyz; //                ,               ,   

                //    
                //L = normalize(WorldSpaceLightDir(ab.vertex));        //    WorldSpaceLightDir:             

                L = normalize(_WorldSpaceLightPos0);    //       ,   mul,                     

                float DotNL = saturate(dot(N,L));           //       

                pms.Col += _LightColor0 * DotNL;      //   +   

                return pms;

            }

            float4 frag(params INPUT):COLOR
            {
                return INPUT.Col;
            }

            ENDCG
        }
    }
}

Phong 광선 모델 (환경 광 + 난반사 + 거울 면 하 이 라이트):
Shader "Custom/ShaderPhong_Vertex" {
    Properties{
        _SpecularColor("SpecularColor",color) = (1,1,1,1)
    }
    SubShader {
        Pass{
            CGPROGRAM

            #include "unitycg.cginc"
            #include "lighting.cginc"
            #pragma vertex vert
            #pragma fragment frag

            float4 _SpecularColor;

            struct parmas{
                float4 Pos:POSITION;
                float4 Col:COLOR;
            };

            parmas vert(appdata_base ab)
            {
                parmas pms;
                pms.Pos = mul(UNITY_MATRIX_MVP,ab.vertex);
                pms.Col = UNITY_LIGHTMODEL_AMBIENT;    //   

                float3 N = normalize(UnityObjectToWorldNormal(ab.normal)).xyz;
                float3 L =normalize(WorldSpaceLightDir(ab.vertex)).xyz;

                float DotNL = saturate(dot(N,L));    //   [0,1]  

                pms.Col = pms.Col+_LightColor0 * DotNL;    //    +    


                //  Reflect       
                float3 R = normalize(reflect(-WorldSpaceLightDir(ab.vertex),N));  //  reflect()          
                //float3 R = normalize(2*dot(N,L)*N-L);    //     R
                float3 V = normalize(WorldSpaceViewDir(ab.vertex));

                float DotRV = max(0,dot(R,V)+0.1);    //    

                pms.Col = pms.Col*0.8+_SpecularColor*DotRV*0.1;   //   +   +      

                return pms;
            }

            float4 frag(parmas INPUT):COLOR
            {
                return INPUT.Col;
            }
            ENDCG
        }   
    }
}

Blinn - Phong 빛 사진 모델 (환경 광 + 난반사 + 반 각 거울 면 하 이 라이트):
Shader "Custom/ShaderBlinnPhong_Vertex" {
    Properties{
        _SpcularColor("SpecularColor",color) = (1,1,1,1)
    }
    SubShader {
        Pass{
            CGPROGRAM

            #include "unitycg.cginc"
            #include "lighting.cginc"
            #pragma vertex vert
            #pragma fragment frag

            float4 _SpcularColor;

            struct parmas
            {
                float4 Pos:POSITION;
                float4 Col:COLOR;
            };

            parmas vert(appdata_base ab)
            {
                parmas pms;

                pms.Pos = mul(UNITY_MATRIX_MVP,ab.vertex);
                pms.Col = UNITY_LIGHTMODEL_AMBIENT;   //   

                float3 N = normalize(UnityObjectToWorldNormal(ab.normal)).xyz;
                float3 L = normalize(WorldSpaceLightDir(ab.vertex)).xyz;
                float DotNL = saturate(dot(N,L));

                pms.Col = pms.Col + _LightColor0*DotNL;   //    +    

                //     
                float3 BJ = normalize(L + normalize(WorldSpaceViewDir(ab.vertex)));

                float DotLBJ = saturate(dot(BJ,N));

                pms.Col = pms.Col*0.8 + pow(_SpcularColor*DotLBJ*0.6,4);   //    +     +       

                return pms;
            }

            float4 frag(parmas INPUT):COLOR
            {
                return INPUT.Col;
            }

            ENDCG   
        }
    }
}

좋은 웹페이지 즐겨찾기