zoukankan      html  css  js  c++  java
  • Unity3D ShaderLab 自定义光照模型

    接着上一篇BasicMyDiffuse的代码来说,这次要说明的就是自定义的光照模型,Ctrl+D>BasicMyDiffuse。

    1.>//#pragma surface surf Lambert::这个是默认的光照模型

    2.>#pragma surface surf CusDiffuse //自己定义的光照模型CusDiffuse;

    3.>在SubShader 中加入光照模型函数:

    inline float4 LightingCusDiffuse(SurfaceOutput s,fixed3 lightDir,fixed atten){
    
    float difLight = max(0,dot(s.Normal,lightDir));
    
    float4 col;
    
    //使用(difLight*atten*2)试试区别;;
    
    col.rgb = s.Albedo * _LightColor0.rgb*(difLight*atten*1.5);
    
    col.a=s.Alpha;
    
    return col;
    
    }

    第二步,告诉着色器使用CusDiffuse光照模型来计算。第二篇文章使用的是默认的Lambert光照模型。

    第三步,编写自己的光照模型函数。函数名格式Lighting+光照模型名称,也就是我们的LightingCusDiffuse;

    为了完成漫反射的效果,我们将SurfaceOutput 结构体提供给我们的数据做乘法运算,s.Albedo来自于surf函数,_LightColor0.rgb来自于unity,通过乘法获得col的rgb值。

    通过在Unity中的Shader切换,我们可以看出我们自己的光照模型和上一篇BasicMyDiffuse使用Lambert光照模型的效果区别。若把参数调整为2,那么效果区别就很难看出。

     

    read:1 这里用到的点乘函数dot(arg1,arg2)是cg语言内置的数学函数,用来比较两个向量在空间的方向;dot函数会检查两个向量是平行还是垂直,

    任意2个向量都可以通过dot函数获得-1~1的夹角范围,-1表示平行向量被离视角的方向,1表示平行但朝向你的方向,0表示和你垂直的方向向量。

    read:2 max函数属于cg标准库的函数,我们可以通过他限制点乘的计算结果,max(arg1,arg2);通过max函数保证漫反射计算记过永远介于0和点乘最大值之间。

    如果是小于0的数值,着色器可能会生成极度黑色的区域,并且影响后续的计算值。

    以下是本次修改后的全部内容。

    code start ---------------------------------------------------------------

    Shader "91YGame/BasicMyCusDiffuse" {
        Properties {
            _EmissiveColor("Emissive Color",Color) = (1,1,1,1)
            _AmbientColor("Ambient Color",Color)=(1,1,1,1)
            _MySliderValue("Slider Value",Range(0,10))=1.3
        }
        SubShader {
            Tags { "RenderType"="Opaque" }
            LOD 200
            
            CGPROGRAM
            //#pragma surface surf Lambert
            #pragma surface surf CusDiffuse
    
            float4 _EmissiveColor;
            float4 _AmbientColor;
            float _MySliderValue;
    
            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;
            }
    
            inline float4 LightingCusDiffuse(SurfaceOutput s,fixed3 lightDir,fixed atten){
                float difLight = max(0,dot(s.Normal,lightDir));
                float4 col;
                col.rgb = s.Albedo * _LightColor0.rgb*(difLight*atten*1.5);
                col.a=s.Alpha;
                return col;
            }
            ENDCG
        }
        FallBack "Diffuse"
    }


    code end ---------------------------------------------------------------

     

  • 相关阅读:
    hdu 2612 Find a way(BFS)
    Anaconda(miniconda)安装及使用--转
    分布式定时任务调度系统技术选型--转
    hbase windows安装
    js函数前加分号和感叹号是什么意思?有什么用?
    纯JS实现房贷利率报表对比
    横向排列两个多个div盒子的方法(CSS浮动清除float-clear/inline)/办法
    DIV设置浮动float以后下一个DIV要换行的方法
    DIV横向排列_CSS如何让多个div盒子并排同行显示
    查看.Net Framework版本号
  • 原文地址:https://www.cnblogs.com/2Yous/p/4204749.html
Copyright © 2011-2022 走看看