Unity Shader 6 Surface Shaders

Unity Shader 6 Surface Shaders 就是 顶点Shader和片段Sahder的封装。

  1. Surface Shaders无pass 通道
  2. Tags 描述渲染类型
  3. LOD 层次细节
  4. #CGPROGRAM 表示CG代码块开始
  5. #ENDCG 表示结束CG代码
  6. #pragma surface surf Standard fullforwardshadows
    surface 表示按照 surface 方式编写
  7. #pragma target 3.0
    使用 shader model 3.0

默认代码

Shader "Custom/NewSurfaceShader"
{
    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
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        //使用硬件 shader 3.0 值越高越好,默认是 2.0 Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex; //纹理必须是sample开头的关键字

        struct Input
        {
            float2 uv_MainTex;//必须以 uv_开头 MainTex表示声明的 sample2D
        };

        half _Glossiness;//光泽度
        half _Metallic;//金属
        fixed4 _Color;//颜色

        // 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)
        /**
        struct SurfaceOutputStandard
        {
            fixed3 Albedo;      // 基础(漫反射或镜面反射)颜色 base (diffuse or specular) color
            fixed3 Normal;      // 切线空间法线(如果写入)tangent space normal, if written
            half3 Emission;
            half Metallic;      // 金属的 0=non-metal, 1=metal
            half Smoothness;    // 平滑度 0=rough, 1=smooth
            half Occlusion;     // 剔除 occlusion (default 1)
            fixed Alpha;        // 透明 alpha for transparencies
        };
        */
        //定义一个方法 inout 引用传递 SurfaceOutputStandard 基于物理的光照模型
        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"
}


posted @ 2022-11-22 08:24  fishpro  阅读(43)  评论(0编辑  收藏  举报