用Unity Shader实现漫反射

用Unity Shader实现漫反射

Untiy原来的效果

image.png

逐顶点

Shader "Unlit/Chapter6DiffuseVertextLevel"
{
    Properties
    {
        _Diffuse("Diffuse", Color) = (1, 1, 1, 1)
    }
        SubShader
    {

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

            CGPROGRAM
            #pragma vertex vert 
            #pragma fragment frag
            #include "Lighting.cginc"
            fixed4 _Diffuse;
            struct a2v {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };
            struct v2f {
                float4 pos : SV_POSITION;
                fixed3 color : COLOR;
            };

            v2f vert(a2v v) {
                v2f obj;
                // Transform
                obj.pos = UnityObjectToClipPos(v.vertex);

                // Get ambient term
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

                // Transform the normal fram obj space to world space
                fixed3 worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));

                // Get the light diraction in world space
                fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);

                // Compute diffuse term
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLight));
                obj.color = ambient + diffuse;
                return obj;
            }

            fixed4 frag(v2f i) : SV_Target{
                return fixed4(i.color, 1.0);
            }
            ENDCG
        }
    }
    Fallback "Diffuse"

}

最终效果


image.png
image.png

可以看到逐顶点着色在边缘有锯齿感,逐像素着色可以解决这个问题

逐像素着色

Shader "Unlit/Chapter6DiffusePixelLevel"
{
    Properties
    {
        _Diffuse("Diffuse", Color) = (1, 1, 1, 1)
    }
        SubShader
    {

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

            CGPROGRAM
            #pragma vertex vert 
            #pragma fragment frag
            #include "Lighting.cginc"
            fixed4 _Diffuse;

            struct a2v {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };
            struct v2f {
                float4 pos : SV_POSITION;
                float3 worldNormal: TEXCOORD0;
            };

            v2f vert(a2v v) {
                v2f obj;
                // Transform
                obj.pos = UnityObjectToClipPos(v.vertex);

                obj.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);
               
                return obj;
            }

            fixed4 frag(v2f i) : SV_Target{
                // Get ambient term
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

                // Transform the normal fram obj space to world space
                fixed3 worldNormal = normalize(i.worldNormal);

                // Get the light diraction in world space
                fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);

                // Compute diffuse term
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLight));
                fixed3 color = ambient + diffuse;
                return fixed4(color, 1.0);
            }
            ENDCG
        }
    }
        Fallback "Diffuse"

}
image.png

可以看到边缘锯齿感好很多了,但是阴影是全黑的,不太行

半兰伯特模型

经验公式

fixed4 frag(v2f i): SV_Target{
                ...
                fixed halfLamber = dot(worldNormal, worldLight) * 0.5 + 0.5;
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * halfLamber;
                fixed3 color = ambient + diffuse;
}

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 《人生感悟与劝勉22---中国古代养生格言集锦荟萃8》 冬冬 集文/摄影 ...
    冬冬_79d4阅读 903评论 0 1
  • 今日体验,天网做直播,天网做视频,天网做传播。 核心,地网做服务,地网做体验! 用,这个时代,用点心,就会很了不起。
    王海博阅读 780评论 0 0
  • 中原焦点团队网初35期,熊英分享第76天,同理他人使得我们敢于呈现自己的脆弱,平息潜在的暴力,让乏味的生活变得有趣...
    熊英_0939阅读 754评论 0 0
  • - 很多中层还在个人领导力的形成过程中,缺乏自信心,需要别人给她更多的 “领导地位” - 自私才是人的通性,善意绝...
    佳佳的日常练笔阅读 988评论 0 3
  • 今天的时代是一个知识付费的时代,暑假到来了,本来应该休息的同学却变得异常的忙碌,早晨路上有匆匆忙忙赶去辅导班学习的...
    红日初升泽被天下阅读 676评论 0 1