【光能蝸牛的圖形學之旅】Unity紋理初步

weixin_33727510發表於2018-04-26

shader程式碼

這段程式碼是在之前的光照shader基礎之上弄的

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'

Shader "Custom/TextureFirst"
{
    Properties
    {
        _Color("_MainColor",Color)  = (1,1,1,1)
        _Diffuse ("_DiffuseColor", Color) = (1,1,1,1)
        _ValveIndex("ValveIndex",Range(0,1)) = 0.5
        _Specular("_SpecularColor",Color) = (1,1,1,1)
        _Glossness("Glossness",Range(1.0,256))=8.0
        _MainTex("MainTex", 2D) = "black"{}
    }
        SubShader
    {
        Tags { 
            "RenderType" = "Opaque" 
            "LightMode" = "ForwardBase"
        }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"
            #include "Lighting.cginc"

            fixed4 _Diffuse;
            float _ValveIndex;
            fixed4 _Specular;
            float _Glossness; 
            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _Color;

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

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 Pos : SV_POSITION;
                float3 color:COLOR;
                float3 worldNormal: TEXCOORD1;
                float3 WordPos: TEXCOORD2;
            };

            
            v2f vert (a2v v)
            {
                v2f o; 
                o.Pos = UnityObjectToClipPos(v.vertex);
                //o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
                o.worldNormal = normalize(mul((float3x3)unity_ObjectToWorld, v.normal));
                o.WordPos = normalize(mul((float3x3)unity_ObjectToWorld, v.vertex));
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
                { 

                float3 nDir = normalize(i.worldNormal);//用法線貼圖修正法線
                float3 lDir = normalize(UnityWorldSpaceLightDir(i.WordPos));
                float3 reflectDir = normalize(reflect(-lDir,i.worldNormal));
                float3 viewDir = normalize(UnityWorldSpaceViewDir(i.WordPos));

                fixed3 albedo = tex2D(_MainTex,i.uv).rgb * _Color.rgb;


                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo.rgb;
                fixed3 specular = _LightColor0 * albedo.rgb * pow(saturate(dot(viewDir, reflectDir)),_Glossness);
                fixed3 diffuse = _LightColor0 * _Diffuse * saturate(dot(nDir, lDir)* _ValveIndex + (1 - _ValveIndex) );
                float3 color = ambient + diffuse + specular;
             
                return fixed4(color,1);
            }
            ENDCG
        }
    }
}

需要注意的東西

這裡面主要以下幾個地方需要注意

  • 紋理的屬性宣告
        _MainTex("MainTex", 2D) = "black"{}
  • 紋理的取樣引用宣告

           sampler2D _MainTex;
  • 紋理ST宣告
    這個名字不是隨便取的,是在紋理取樣名字加上_ST字尾,
    _MainTex_ST中,其中_MainTex_ST.xy儲存的是紋理的縮放,_MainTex_ST.zw儲存的是紋理的位移

           float4 _MainTex_ST;//
6516379-8ac66d90d66b90db.jpg
QQ截圖20180424154944.jpg
  • 紋理轉換
    我們使用如下方式進行紋理轉換
                o.uv = v.uv.xy * _MainTex_ST.xy + _MainTex_ST.zw;

或者你可以直接呼叫unity的內部shader函式實現轉換功能

                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  • 紋理取樣

                fixed3 albedo = tex2D(_MainTex,i.uv).rgb ;

相關文章