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

本文介绍了Unity中Shader的基础知识,包括使用纹理处理漫反射、重点讲解了最重要的法线映射,解释了为何需要法线映射以及坐标范围差异,并探讨了在切线空间和世界空间中计算光照模型的差异。同时提到了Ramp纹理和Mask纹理在Shader中的应用。
摘要由CSDN通过智能技术生成

1. Texture for Diffuse

step 1 : prepare resources

        Load the texture in your shader:

    // Properties
        _MainTex ("Main Tex", 2D) = "while" {}

        // Pass in the first SubShader
            sampler2D _MainTex;
            float4 _MainTex_ST; // Scale:Tiling Trans:Offset

step 2 : prepare containers

        Notice how the uv coordinates are stored.

            struct a2v {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float4 texcoord : TEXCOORD0;
                // The TEXCOORD0 in a2v tells you where a vertex are on the given texture
                // The UV coordinate will be saved in v2f, in a container TEXCOORDn.
            };

            struct v2f {
                float4 pos : SV_POSITION;
                float3 worldNormal : TEXCOORD0;
                float3 worldPos : TEXCOORD1;
                float2 uv : TEXCOORD2;
            };

            v2f vert(a2v v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.worldNormal = UnityObjectToWorldNormal(v.normal); 
                // if there's Non-uniform Scale, you must pay attention to normal transformation.
                // o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);

                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;

                o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
                // o.uv = TRANSFORM_TEX(v.texcoord, _MainTex)
                // up to now it's just a set of uv coordinate, no rgba information.

                return o;
            }

step 3 : sample the picture and calculate the color

        tex2D(Texture from sampler2D, uv coordinate)  returns rgba(fixed4) per coordinate.

            fixed4 frag(v2f i) : SV_Target {
                fixed3 worldNormal = normalize(i.worldNormal);
                fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));

                fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb; // TexColor and MaterialColor

                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;

                fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(worldNormal, worldLightDir));
                
                // Blinn-Phong
                fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
                fixed3 halfDir = normalize(worldLightDir + viewDir);
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(worldNormal, halfDir)), _Gloss);

                return fixed4(ambient + diffuse + specular, 1.0);
                }

2. Bump Mapping (the MOST IMPORTANT !!!)

0.1 . Why bump mapping?

        We calculate light and shadow by dot(lightDir, normalDir). Imagine that the normals o

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值