美文网首页
[Shader] Surface Shader 01

[Shader] Surface Shader 01

作者: 周末的游戏之旅 | 来源:发表于2019-08-27 14:13 被阅读0次
    // https://docs.unity3d.com/2018.3/Documentation/Manual/SL-SurfaceShaders.html
    Shader "Sbin/sf1"
    {
        Properties
        {
            // 颜色值   
            _Color ("Color", Color) = (1,1,1,1)
            // 主纹理
            _MainTex ("Albedo (RGB)", 2D) = "white" {}
            //一个浮点值 被用于计算高光的光泽度
            _Glossiness ("Smoothness", Range(0,1)) = 0.5
            // 也是一个浮点值 被用于与计算这个材质所要表现的金属光泽
            _Metallic ("Metallic", Range(0,1)) = 0.0
        }
        // 子Shader
        SubShader
        {
            // 在 Surface Shader 中不需要编写pass通道
            // Surface Shader 就是对顶点着色器和片元着色器的一种包装,为我们生成代码
            // 因此pass通道由Surface Shader帮我们生成,如果添加了pass通道,编译会报错
    
            //描述一个渲染类型 这里描述的是一个不透明的物体
            Tags { "RenderType"="Opaque" }
            //层级细节,这里暂时忽略它
            LOD 200
    
            // 这是一个代码块,表示从这一句开始一直到ENDCG使用了CG语法
            CGPROGRAM
            // Physically based Standard lighting model, and enable shadows on all light types
            // (基于物理的标准照明模型,并在所有灯光类型上启用阴影)
            #pragma surface surf Standard fullforwardshadows
            // pragma  是一个编译指令 有固定的格式
            // surface 表示这个shader按照surface shader的方式编写
            // surf 这里是一个函数名称,可以随意起名,对应下面的 void surf 函数
            // Standard 这里是光照模型,Standard实际上也是一个函数
            //  这个函数在Editor\Data\CGInclude\UnityPBSLighting.cginc中
            // fullforwardshadows 这里是描述的选项,可以在官方API查看详细信息和其他选项
    
            // Use shader model 3.0 target, to get nicer looking lighting
            // (使用着色器模型3.0目标,以获得更好看的照明)
            #pragma target 3.0
            // Shader model 是硬件的一个用于着色处理的基础的指令范围和能力
            // Shader model 值越高代表可以使用更高级的硬件功能,如果不写这句话默认是2.0
    
            // 这是一个输入结构体,描述的是uv的坐标,实际上就是纹理坐标
            struct Input
            {
                float2 uv_MainTex;
                // 例如,在properties中声明了_MainTex,就必须在这里声明以 uv 开头加_MainTex的属性。
                // 格式是不能错的,必须以 uv 开头并加上属性的名称,否则会得不到纹理采样。
            };
            
            //声明一个纹理的变量,类型是sampler2D
            sampler2D _MainTex;
            // 这里要跟properties中声明的纹理变量名一致,Unity会自动对应这两个变量。
    
            // 下面是对properties中三个参数的具体声明
            half _Glossiness;
            half _Metallic;
            fixed4 _Color;
    
            /*通过上面的属性声明可知:
            * 除了Input结构体外,还必须要声明对应propertis中的属性。
            * 还要注意的是:
            * properties中的类型名和CG中的不一致,对应如下
            * 2D == sampler2D
            * color == fixed4 (4阶的向量)
            * float == half (基本的浮点值)
            */
    
            // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
            // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
            // #pragma instancing_options assumeuniformscaling
            UNITY_INSTANCING_BUFFER_START(Props)
                // put more per-instance properties here
            UNITY_INSTANCING_BUFFER_END(Props)
    
            void surf (Input IN, inout SurfaceOutputStandard o)
            {
                // Albedo comes from a texture tinted by color
                fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    
                o.Albedo = c.rgb;
                // Metallic and smoothness come from slider variables
                o.Metallic = _Metallic;
                o.Smoothness = _Glossiness;
                o.Alpha = c.a;
            }
            ENDCG
        }
        FallBack "Diffuse"
    }
    

    相关文章

      网友评论

          本文标题:[Shader] Surface Shader 01

          本文链接:https://www.haomeiwen.com/subject/fileectx.html