• Unity3D Shader 半兰伯特光照模型


    //效果预览

    //Shader代码

    Shader "Unlit/HalfLambert"
    {
        Properties
        {
            _MainTex ("Texture", 2D) = "white" {}
            _Diffuse ("Diffuse Color", Color) = (0,0,0,0)
        }
        SubShader
        {
            Tags { "RenderType"="Opaque" }
            LOD 100
    
            Pass
            {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                // make fog work
                #pragma multi_compile_fog
                
                #include "UnityCG.cginc"
                #include "Lighting.cginc"
    
                struct a2v  
                {  
                    float4 vertex : POSITION;  
                    float3 normal : NORMAL;  
                    float4 texcoord : TEXCOORD0;  
                };  
    
                struct v2f
                {
                    float2 uv : TEXCOORD0;
                    UNITY_FOG_COORDS(1)
                    float4 vertex : SV_POSITION;
                    fixed3 worldNormal:TEXCOORD1;
                };
    
                sampler2D _MainTex;
                float4 _MainTex_ST;
                fixed4 _Diffuse;
                
                v2f vert (a2v v)
                {
                    v2f o;
                    o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                    o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                    o.worldNormal = mul((float3x3)unity_ObjectToWorld,v.normal);
                    UNITY_TRANSFER_FOG(o,o.vertex);
                    return o;
                }
                
                fixed4 frag (v2f i) : SV_Target
                {
                    fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
                    fixed3 worldNormal = normalize(i.worldNormal);
    
                    fixed3 halLambert = 0.5+0.5*dot(worldLightDir,worldNormal);
    
    
    
                    fixed3 dif = (UNITY_LIGHTMODEL_AMBIENT+halLambert*_LightColor0)*_Diffuse;
    
                    // sample the texture
                    fixed4 col = tex2D(_MainTex, i.uv);
    
                    // apply fog
                    UNITY_APPLY_FOG(i.fogCoord, col);
                    return fixed4(dif * col.rgb, col.a); 
    
                    return col;
                }
                ENDCG
            }
        }
    }
  • 相关阅读:
    [FE] uni-app 安装 uview-ui 的两种方式
    [FE] Canvas 转图片并下载的方式
    [K8s] Pod 与容器设计模式 Sidecar
    [Docker] 使 Volume 独立于容器运行时的方式
    mysql授权用户以指定IP登录的方法
    linux安装mysql客户端
    kali 安装 Google Chrome
    KALI图形界面root 用户登入
    git reset 版本回退命令
    git log 常用命令
  • 原文地址:https://www.cnblogs.com/mrblue/p/7733027.html
Copyright © 2020-2023  润新知