【Unity Shaders and Effects Cookbook】Diffuse Shading

1. 基础的shader 写法

Shader "CookbookShaders/BasicDiffuse" 
{
    Properties     
    {
           _MainTex(“Base(RGB)”,2D) = “white”{}
    }
    
    SubShader 
    {
        Tags { "RenderType"="Opaque" }
        LOD 200
        
        CGPROGRAM
        #pragma surface surf Lambert
         sampler2D _MainTex;
         struct Input
        {
             float2 uv_MainTex;
         };
        
        void surf (Input IN, inout SurfaceOutput o) 
        {
            float4 c;
            c =  tex2D((_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        
        ENDCG
    } 
    
    FallBack "Diffuse"
}



Adding Properties to a Surface Shader

1 Properties 块创建UI模块,从而在UI 中控制输入Shader中的变量,就像D3D 中的set


比如 EmissiveColor ("Emissive Color", Color) = (1,1,1,1) 表示颜色,默认值(1,1,1,1)

2 接下来在Subshader中CGPROGRAM下面声明 properties 中的变量

        float4 _EmissiveColor;
        float4 _AmbientColor;
        float _MySliderValue;

3 把放射光(Emissivecolor)+ 环境光(Ambientcolor)  相加,用pow 函数处理,指数来自UI的滑动slider

        void surf (Input IN, inout SurfaceOutput o)
        {
            float4 c;
            c =  pow((_EmissiveColor + _AmbientColor), _MySliderValue);
           
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }

创建一个自定义的漫反射光照模型

1. 修改#pragma,告诉Shader 使用BasicDiffu 光照模型

      # pragma surface surf BasicDiffuse

2. 在SubShder中 添加下面的代码

  inline float4 LightingBasicDiffuse(Surfaceoutput s, fixed3 lightDir, fixed atten)
{
   float difLight = max(0, dot(s.Normal, lightDir));
  float4  col;
   col.rgb = s.Albedo * _LightColor0.rgb * (difLight * atten * 2);
   col.a = s.Alpha;
    return col; 
}

   其中,创建一个新的光照模型函数,有三种光照模型可以使用,如下

   half4 LightingName(SurfaceOutput s, half3 lightDir, half atten) {}  不要使用viewDir

   half4 LightingName(SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) 需要使用view

   half4 LightingName(SurfanceOutput s, half4 light){}

 

  dot 函数判断两个空间向量的方向是平行(1)还是垂直(-1)

创建 Half Lambert 光照模型

Half Lambert 是通过阈值调整低光区的表面光照(as a way of getting the lighting to show the surface

of an object in low-light areas)。Half Lambert光照模型是Valve公司在制作”半条命“游戏时发明的,用来给在比较暗的区域显示物体。总体来说,该光照模型提高了物体表面的漫反射光。

        inline float4 LightingBasicDiffuse (SurfaceOutput s, fixed3 lightDir, fixed atten)
        {
            float difLight = dot (s.Normal, lightDir);
            float hLambert = difLight * 0.5 + 0.5; 
            
            float4 col;
            col.rgb = s.Albedo * _LightColor0.rgb * (hLambert * atten * 2);
            col.a = s.Alpha;
            return col;
        }

     如上,Half Lambert 技术,通过 乘以0.5 ,加上0.5 , 实现把范围调整到0.5 ~ 1.0


Shader "CookbookShaders/Chapter1/HalfLambertDiffuse" 
{
    Properties     
    {
        _EmissiveColor ("Emissive Color", Color) = (1,1,1,1)
        _AmbientColor  ("Ambient Color", Color) = (1,1,1,1)
        _MySliderValue ("This is a Slider", Range(0,10)) = 2.5
    }
    
    SubShader 
    {
        Tags { "RenderType"="Opaque" }
        LOD 200
        
        CGPROGRAM
        #pragma surface surf BasicDiffuse
        #pragma target 3.0
        float4 _EmissiveColor;
        float4 _AmbientColor;
        float _MySliderValue;
        
        inline float4 LightingBasicDiffuse (SurfaceOutput s, fixed3 lightDir, fixed atten)
        {
            float difLight = dot (s.Normal, lightDir);
            float hLambert = difLight * 0.5 + 0.5; 
            
            float4 col;
            col.rgb = s.Albedo * _LightColor0.rgb * (hLambert * atten * 2);
            col.a = s.Alpha;
            return col;
        }
        struct Input 
        {
            float2 uv_MainTex;
        };
        void surf (Input IN, inout SurfaceOutput o) 
        {
            float4 c;
            c =  pow((_EmissiveColor + _AmbientColor), _MySliderValue);
            
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        
        ENDCG
    } 
    
    FallBack "Diffuse"
}





Book Description: Since their introduction to Unity, Shaders have been notoriously difficult to understand and implement in games: complex mathematics have always stood in the way of creating your own Shaders and attaining that level of realism you crave. With Shaders, you can transform your game into a highly polished, refined product with Unity’s post-processing effects. Unity Shaders and Effects Cookbook is the first of its kind to bring you the secrets of creating Shaders for Unity3D―guiding you through the process of understanding vectors, how lighting is constructed with them, and also how textures are used to create complex effects without the heavy math. We’ll start with essential lighting and finishing up by creating stunning screen Effects just like those in high quality 3D and mobile games. You’ll discover techniques including normal mapping, image-based lighting, and how to animate your models inside a Shader. We’ll explore the secrets behind some of the most powerful techniques, such as physically based rendering! With Unity Shaders and Effects Cookbook, what seems like a dark art today will be second nature by tomorrow. What You Will Learn Understand physically based rendering to fit the aesthetic of your game Enter the world of post-processing effects to make your game look visually stunning Add life to your materials, complementing Shader programming with interactive scripts Design efficient Shaders for mobile platforms without sacrificing their realism Use state-of-the-art techniques such as volumetric explosions and fur shading Build your knowledge by understanding how Shader models have evolved and how you can create your own Discover what goes into the structure of Shaders and why lighting works the way it does Master the math and algorithms behind the most used lighting models
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不负初心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值