美文网首页
【光能蜗牛的图形学之旅】Unity纹理初步

【光能蜗牛的图形学之旅】Unity纹理初步

作者: 光能蜗牛 | 来源:发表于2018-04-26 22:15 被阅读0次

    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;//
    
    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 ;
    

    相关文章

      网友评论

          本文标题:【光能蜗牛的图形学之旅】Unity纹理初步

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