【Unity Shader实战】卡通风格的Shader(二)(2)

前提

o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
//Get the UV coordinates

o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);

o.uv2 = TRANSFORM_TEX (v.texcoord, _Bump);


// pass lighting information to pixel shader


TRANSFER_VERTEX_TO_FRAGMENT(o);


return o;
}

float4 frag(v2f i) : COLOR
{
//Get the color of the pixel from the texture
float4 c = tex2D (_MainTex, i.uv);
//Merge the colours
c.rgb = (floor(c.rgb*_Tooniness)/_Tooniness);

//Get the normal from the bump map
float3 n =UnpackNormal(tex2D (_Bump, i.uv2));

//Based on the ambient light
float3 lightColor = float3(0);

//Work out this distance of the light
float atten = LIGHT_ATTENUATION(i);

//Angle to the light
float diff = saturate (dot (n, normalize(i.lightDirection)));

//Perform our toon light mapping
diff = tex2D(_Ramp, float2(diff, 0.5));

//Update the colour
lightColor += _LightColor0.rgb * (diff * atten);

//Product the final color
c.rgb = lightColor * c.rgb * 2;

return c;

}

ENDCG
}
}
FallBack “Diffuse”
}

写在最后

    上一篇里的代码我也更新过了,添加了有无法线的两种shader。和上一篇里的还有一点不一样,上一篇里的渐变纹理使用了假的双向反射分布函数,即除了漫反射方向还有视角方向进行采样。这一篇里仅仅使用了漫反射方向对渐变纹理进行采样,对应的渐变纹理也是一维的,如下:

    读者有需要的可以自己添加上视角方向的采样,也就是说在v2f里添加一个新的变量viewDir,然后逐顶点计算后传递给frag函数。如果我后面有时间的话可能会回头添加上。当然,大家还是靠自己比较好。

更多精彩关注狗刨学习网: http://www.gopedu.com/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值