UnityShader-ShaderLab

ShaderLab 是unity编写shader的语言

SubShader结构

在unity 内置渲染管线中创建一个 SurfaceShader观察结构

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

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        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)

        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"
}

属性(用来调节效果)

属性主要分为三类
数值
颜色和向量(颜色rgb来表示向量)
纹理贴图(为mesh的每一个顶点确定着色方式,normal,ao等等)

属性设置方式
Properties
    {
         _Name("MyProperty",Color) = (1,1,1,1)
    }
主要属性类型
color(颜色是0-255,而shader是0-1,需要归一化)
2D(贴图,本质是uv映射的color,即每一点的颜色不同)
vector(x,y,z,w四个值,没有归一化的限制)
Cube(立方体贴图,如天空盒)
3D
2DArray

大小写不敏感,默认值可填写 "defaulttexture"
 _Name("MyProperty",2DArray) = "defaulttexture"{} 


标签

可以有多个,确定渲染方式以及顺序等等

标签,确定渲染顺序

预定义5种,
Background  1000
Geometry 2000
AlphaTest 2450
Transparent 3000
Overlay 4000
可以自定义渲染顺序:Tags{"MyQueue"=“Geometry +

确定渲染类型

禁用批处理

"DisableBatching"="LODFading" 在Lod效果激活时禁用批处理
"DisableBatching"="True"
"DisableBatching"="False"

问?为什么要禁用批处理,  批处理导致模型丢失模型空间,使用模型空间顶点数据的shader 无法实现效果,所以禁用

禁止阴影投射

ForceNoShadowCasting 不产生阴影

等等(查看unity 文档关于标签)

渲染状态

在pass外设置,影响所有pass,在pass内则单独设置
Cull  Back|Front|Off  剔除方式,默认背面剔除
ZTest 深度测试
ZWrite 写入深度缓存
Blend 渲染图像混合方式
ColorMask 设置颜色通道写入蒙版

Pass

尽可能减少pass,每一个pass都会为这个物体执行一次渲染,所以比较耗费性能(灯光还会增加渲染次数)
但是多Pass可以实现更复杂的效果(也许吧,不过unity,URP下不支持多pass,第二个pass后都会失效,有URP实现多Pass方法么?)

Fallback运行时,所有的Subshader 都不支持,则使用最普通的效果最常用的为 内置的Diffuse

留下评论