zoukankan      html  css  js  c++  java
  • Unity #pragma multi_compile说明

    https://blog.csdn.net/ak47007tiger/article/details/100007655

    multi_compile

    • 作用
      • 根据编译选项产生shader变体
      • 避免分支语句导致的性能下降
      • 主要用于在代码中选择shader变体
    • Unity在打包时会把所有multi_compile产生的shader变体都打进包中

    示例shader

    Shader "DC/Shader/ShaderLab/MultiCompile"
    {
        Properties
        {
            _MainTex ("Texture", 2D) = "white" {}
            [KeywordEnum(R,G,B)] _CL("ColorSelect", Float) = 0
        }
        SubShader
        {
            Tags { "RenderType"="Opaque" }
            LOD 100
    
            Pass
            {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                
                #pragma multi_compile _CL_R _CL_G _CL_B
                //使用 __ 减少一个编译选项,编译选项最多256个
                #pragma multi_compile __ DB_ON
    
                #include "UnityCG.cginc"
    
                struct appdata
                {
                    float4 vertex : POSITION;
                    float2 uv : TEXCOORD0;
                };
    
                struct v2f
                {
                    float2 uv : TEXCOORD0;
                    float4 vertex : SV_POSITION;
                };
    
                sampler2D _MainTex;
                float4 _MainTex_ST;
    
                v2f vert (appdata v)
                {
                    v2f o;
                    o.vertex = UnityObjectToClipPos(v.vertex);
                    o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                    UNITY_TRANSFER_FOG(o,o.vertex);
                    return o;
                }
    
                fixed4 frag (v2f i) : SV_Target
                {
                    #if DB_ON
                        return fixed4(1,1,0,1);
                    #elif _CL_R
                        return fixed4(1,0,0,1);
                    #elif _CL_G
                        return fixed4(0,1,0,1);
                    #elif _CL_B
                        return fixed4(0,0,1,1);
                    #else
                        fixed4 col = tex2D(_MainTex, i.uv);
                        return col;
                    #endif
                }
                ENDCG
            }
        }
    }
    

      示例脚本

    using UnityEngine;
    
    namespace DC
    {
      public class MultiCompile : MonoBehaviour
      {
        public Material mat;
    
        public void OnEnable()
        {
          mat.EnableKeyword("DB_ON");
    //      Shader.EnableKeyword("ON");
        }
    
        public void OnDisable()
        {
          mat.DisableKeyword("DB_ON");
    //      Shader.DisableKeyword("DB_ON");
        }
      }
    }
    

      

  • 相关阅读:
    【JAVA编码专题】JAVA字符编码系列一:Unicode,GBK,GB2312,UTF-8概念基础
    读取Webpage表中的内容
    各种排序算法的分析及java实现
    运行一个Hadoop Job所需要指定的属性
    Hbase常见异常
    Gora官方文档之二:Gora对Map-Reduce的支持
    Linux 系统挂载数据盘
    Gora快速入门
    Gora官方范例
    在Eclipse中运行Nutch2.3
  • 原文地址:https://www.cnblogs.com/nafio/p/12805688.html
Copyright © 2011-2022 走看看