Texture 紋理貼圖

基礎貼圖Shader:只有紋理函數

1. 在屬性中聲明紋理貼圖: _MainTex ("Texture", 2D) = "white" {} spa

2. 在Pass中聲明變量: sampler2D _MainTex; float4 _MainTex_ST; 這個是成對出現,_MainTex_ST 用與計算座標偏移offset3d

3. 在Vertex Function函數中進行紋理座標採樣: o.tex = v.texcoord; code

4. 在Fragment Function 函數中採樣紋理,並輸出: float4 tex = tex2D(_MainTex,i.tex.xy*_MainTex_ST.xy+_MainTex_ST.zw); return float4(tex); orm

 源代碼:blog

Shader "Unlit/Textures_Base"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"


            sampler2D _MainTex;
            float4 _MainTex_ST;

            //輸入結構體
            struct vertexInput{
                float4 vertex:POSITION;
                float4 texcoord:TEXCOORD0;
            };

            //輸出結構體
            struct vertexOutput{
                float4 pos:SV_POSITION;
                float4 tex:TEXCOORD0;
            };
            
            vertexOutput vert (vertexInput v)
            {
                vertexOutput o;

                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                o.tex = v.texcoord;

                return o;
            }
            
            fixed4 frag (vertexOutput i) : COLOR
            {
                //Texture Map
                float4 tex  = tex2D(_MainTex,i.tex.xy*_MainTex_ST.xy+_MainTex_ST.zw);
                return float4(tex);
            }
            ENDCG
        }
    }
}

紋理(Texture)+高光(Specular)+邊緣光(Rim)+漫反射(diffuse)+環境光(ambient)+多光源(Lighting)(1個平行光,1個點光源):flash

源代碼:it

Shader "JQM/Textures"
{
    Properties
    {
        _Color("Color", color) = (1.0,1.0,1.0,1.0)
        _MainTex ("Texture", 2D) = "white" {}                
        _SpecColor("Specular Color", color) = (1.0,1.0,1.0,1.0)
        _Shininess("Shininess",float) = 10
        _RimColor("Rim Coloe Color", color) = (1.0,1.0,1.0,1.0)
        _RimPower("Rim Power",Range(0.1,10.0)) = 3.0
    }

    SubShader
    {

        Pass
        {
            Tags { "LightMode"="ForwardBase" }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            //#pragma exclude_renderers flash //給指定平臺編譯
            
            #include "UnityCG.cginc"

            //使用自定義變量
            sampler2D _MainTex;
            float4 _MainTex_ST;
            uniform float4 _Color;
            uniform float4 _SpecColor;
            uniform float4 _RimColor;
            uniform float _Shininess;
            uniform float _RimPower;

            //使用Unity定義的變量
            uniform float4 _LightColor0;

            //輸入結構體
            struct vertexInput{
                float4 vertex:POSITION;
                float3 normal:NORMAL;
                float4 texcoord:TEXCOORD0;
            };

            //輸出結構體
            struct vertexOutput{
                float4 pos:SV_POSITION;
                float4 tex:TEXCOORD0;
                float4 posWorld:TEXCOORD1;
                float3 normalDir:TEXCOORD2;
            };


            
            vertexOutput vert (vertexInput v)
            {
                vertexOutput o;

                o.posWorld = mul(_Object2World, v.vertex);
                o.normalDir =  normalize( mul(float4(v.normal,0.0),_World2Object).xyz);
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                o.tex = v.texcoord;

                return o;
            }
            
            fixed4 frag (vertexOutput i) : COLOR
            {
                float3 normalDirection = i.normalDir;
                float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz- i.posWorld.xyz);
                float3 lightDirection; 
                float atten;

                if(_WorldSpaceLightPos0.w==0.0)//平行光
                {
                    atten = 1.0;
                    lightDirection = normalize(_WorldSpaceLightPos0.xyz);
                }
                else
                {
                    float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz -i.posWorld.xyz;
                    float distance = length(fragmentToLightSource);
                    atten  = 1.0/distance;
                    lightDirection = normalize(fragmentToLightSource);
                }

                //燈光
                float3 diffuseReflection = atten * _LightColor0.xyz *  saturate( dot(normalDirection,lightDirection));
                float3 specularReflection = atten * _LightColor0.xyz * _SpecColor.rgb*saturate( dot(normalDirection,lightDirection))*pow(saturate(dot(reflect(-lightDirection,normalDirection),viewDirection)),_Shininess);
                
                //Rim Light
                float rim= 1-dot(normalize(viewDirection),normalDirection);
                float3 rimLighting = atten * _LightColor0.xyz * _RimColor.rgb*saturate(dot(normalDirection,lightDirection))*pow(rim,_RimPower);

                float3 lightFinal = rimLighting + diffuseReflection+specularReflection+UNITY_LIGHTMODEL_AMBIENT.xyz;

                //Texture Map
                float4 tex  = tex2D(_MainTex,i.tex.xy*_MainTex_ST.xy+_MainTex_ST.zw);
                return float4(tex*lightFinal*_Color.xyz,1.0);
                
            }
            ENDCG
        }

        Pass
        {
            Tags { "LightMode"="ForwardAdd" }
            Blend One One

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            //#pragma exclude_renderers flash //給指定平臺編譯
            
            #include "UnityCG.cginc"

            //使用自定義變量
            sampler2D _MainTex;
            float4 _MainTex_ST;
            uniform float4 _Color;
            uniform float4 _SpecColor;
            uniform float4 _RimColor;
            uniform float _Shininess;
            uniform float _RimPower;

            //使用Unity定義的變量
            uniform float4 _LightColor0;

            //輸入結構體
            struct vertexInput{
                float4 vertex:POSITION;
                float3 normal:NORMAL;
                float4 texcoord:TEXCOORD0;
            };

            //輸出結構體
            struct vertexOutput{
                float4 pos:SV_POSITION;
                float4 tex:TEXCOORD0;
                float4 posWorld:TEXCOORD1;
                float3 normalDir:TEXCOORD2;
            };


            
            vertexOutput vert (vertexInput v)
            {
                vertexOutput o;

                o.posWorld = mul(_Object2World, v.vertex);
                o.normalDir =  normalize( mul(float4(v.normal,0.0),_World2Object).xyz);
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                o.tex = v.texcoord;

                return o;
            }
            
            fixed4 frag (vertexOutput i) : COLOR
            {
                float3 normalDirection = i.normalDir;
                float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz- i.posWorld.xyz);
                float3 lightDirection; 
                float atten;

                if(_WorldSpaceLightPos0.w==0.0)//平行光
                {
                    atten = 1.0;
                    lightDirection = normalize(_WorldSpaceLightPos0.xyz);
                }
                else
                {
                    float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz -i.posWorld.xyz;
                    float distance = length(fragmentToLightSource);
                    atten  = 1.0/distance;
                    lightDirection = normalize(fragmentToLightSource);
                }

                //燈光
                float3 diffuseReflection = atten * _LightColor0.xyz *  saturate( dot(normalDirection,lightDirection));
                float3 specularReflection = atten * _LightColor0.xyz * _SpecColor.rgb*saturate( dot(normalDirection,lightDirection))*pow(saturate(dot(reflect(-lightDirection,normalDirection),viewDirection)),_Shininess);
                
                //Rim Light
                float rim= 1-dot(normalize(viewDirection),normalDirection);
                float3 rimLighting = atten * _LightColor0.xyz * _RimColor.rgb*saturate(dot(normalDirection,lightDirection))*pow(rim,_RimPower);

                float3 lightFinal = rimLighting + diffuseReflection+specularReflection+UNITY_LIGHTMODEL_AMBIENT.xyz;

                //Texture Map
                float4 tex  = tex2D(_MainTex,i.tex.xy*_MainTex_ST.xy+_MainTex_ST.zw);
                return float4(tex*lightFinal*_Color.xyz,1.0);
                
            }
            ENDCG
        }
    }
}
相關文章
相關標籤/搜索