Unity Shader ToggleDrawer [Toggl

作者: 云木unity | 来源:发表于2017-11-29 14:33 被阅读117次

    ToggleDrawer

    把一个类型的属性显示为一个开关,它的值要么是0要么是1。

    当选中它时,Unity还会设置一个名为大写属性名_ON(可以自定义名字)的shader feature

    #pragma shader_feature _SELECTED_ON

    我们可以在shader里用过#if、#ifdef或者#if defined关键词来判断它当前是否被开启。

    shader文件:

    Properties
    {
        ...
        [Toggle]_selected("selected", Int) = 0
        ...
    }
    ...
    SubShader
    {
        ...
        #pragma shader_feature _SELECTED_ON
        ...
        #ifdef _SELECTED_ON
            ...
        #else
            ...
        #endif
        ...
    }
    
    ToggleDrawer.png

    设置属性

    1. 设置接口 EnableKeyword/DisableKeyword

    At runtime, the appropriate shader variant is picked up from the Material keywords (Material.EnableKeyword and DisableKeyword) or global shader keywords (Shader.EnableKeyword and DisableKeyword).

    2. keyword的两种方式

    Toggle displays a float as a toggle. The property value will be 0 or 1, depending on the toggle state.

    When it is on, a shader keyword with the uppercase property name +"_ON" will be set, or an explicitly specified shader keyword.

    1. uppercase property name +"_ON"
    // Will set "_INVERT_ON" shader keyword when set
    [Toggle] _Invert ("Invert?", Float) = 0
    
    1. an explicitly specified shader keyword
    // Will set "ENABLE_FANCY" shader keyword when set.
    [Toggle(ENABLE_FANCY)] _Fancy ("Fancy?", Float) = 0
    

    我们使用的而是第一种方式.

    设置代码如下:

    self._fogMaterial = self.fogObj:GetComponent("Renderer").material

    1. 选中
      self._fogMaterial:EnableKeyword("_SELECTED_ON")

    2. 禁用
      self._fogMaterial:DisableKeyword("_SELECTED_ON")

    遇到的问题

    问题: 打包后,真机运行时EnableKeyword失效了。
    原因: Unity在Build时自动把没有被任何用到的材质使用的variant裁切了(Shader build time stripping

    While building the game, Unity can detect that some of the internal shader variants are not used by the game, and skip them from build data. Build-time stripping is done for:

    生成游戏包时,Unity可以检测到某些内在着色器变体没有被使用,然后stripping(裁切、忽略)它们。生成时去除可用于:

    • Individual shader features, for shaders that use #pragma shader_feature. If none of the used materials use a particular variant, then it is not included into the build. See internal shader variants documentation. Out of built-in shaders, the Standard shader uses this.

    • 个别着色器特性,使用 #pragma shader_feature的着色器。如果一个变体没有被任何用到的材质使用,那么生成时就不把它打进去。参考内在着色器文档 ,内置着色器中的标准着色器使用这种方式。

    方案很多:

    1. Shader加入到Edit->Project Settings->Graphics->Always Included Shaders这个列表里

    加入AlwaysIncludedShaders的shader是开始游戏的时候就全部编译了吗?

    答案是不会,加到always include 的shader,会将shader的所有变体打包到游戏,用到的时候才会加载用到的变体到内存!

    相关文档:https://docs.unity3d.com/Manual/OptimizingShaderLoadTime.html

    Under all default settings, Unity loads the object into memory, but does not create the until they are actually needed.
    This means that shader variants that are included into the game build can still potentially be used, but there’s no memory or load time cost paid until they are needed. For example, shaders always include a variant to handle point lights with shadows, but if you never end up using a point light with shadows in your game, then there’s no point in loading this particular variant.

    1. 新建一个材质,将被裁切的variant选中,这样被材质使用到的variant不会stripping ( If none of the used materials use a particular variant, then it is not included into the build)

    2. 修改Shader -- multi_compile

    #pragma shader_feature 改为 #pragma multi_compile

    旧版:#pragma shader_feature _SELECTED_ON
    新版:#pragma multi_compile __ _SELECTED_ON

    Individual shader features, for shaders that use #pragma shader_feature. shader_feature才会Shader build time stripping

    我采用方案3.

    参考链接

    Making multiple shader program variants : https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html

    MaterialPropertyDrawer : https://docs.unity3d.com/ScriptReference/MaterialPropertyDrawer.html

    Optimizing Shader Load Time

    相关文章

      网友评论

        本文标题:Unity Shader ToggleDrawer [Toggl

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