【问题标题】:Is there a way to render certain colors of a vertex-colored mesh as transparent in Unity?有没有办法在 Unity 中将顶点色网格的某些颜色渲染为透明?
【发布时间】:2019-06-18 14:59:51
【问题描述】:

我有一个 .fbx 3D 网格,我已将它作为 Unity 中的游戏对象导入。它有顶点颜色。我试图让网格的某些部分呈现为透明,理想情况下,一旦用户选择了一个选项。

作为参考,这是我正在使用的网格的屏幕截图。

https://imgur.com/a/FY8Z38r

我在 Unity 中编写了一个着色器,它附加到这个游戏对象的材质上,它允许显示网格的顶点颜色。

Shader "Custom/VertexColor" { 
    // Where it will appear inside of the Shader Dropdown Menu of the Material / Name of the shader

    SubShader{
        Tags { "RenderType" = "Opaque" }
        LOD 200


        CGPROGRAM
        #pragma surface surf Lambert vertex:vert
        #pragma target 3.0

        struct Input {
            float4 vertColor;
        };

        float _CutoutThresh;

        void vert(inout appdata_full v, out Input o) {
            UNITY_INITIALIZE_OUTPUT(Input, o);
            o.vertColor = v.color;
        }

        void surf(Input IN, inout SurfaceOutput o) {

            #include "UnityCG.cginc

            o.Albedo = IN.vertColor.rgb;
            clip(IN.vertColor.r = 0.5); // Discards any pixel whose interpolated vertex color in the red channel is less than 0.5
        }
        ENDCG
    }
    FallBack "Diffuse"
}

具体来说,这里的这一行: 剪辑(IN.vertColor.g = 0.5); // 丢弃绿色通道中插值顶点颜色小于 0.5 的任何像素

我希望这条线丢弃所有非绿色像素,但我的游戏对象看起来仍然一样。

【问题讨论】:

    标签: unity3d colors shader mesh vertex-shader


    【解决方案1】:

    如果值小于零,HLSL 的裁剪函数会丢弃一个像素。

    您正在寻找的内容类似于:

    clip( IN.vertColor.g < 0.5f ? -1:1 );
    

    【讨论】:

    • 感谢您的帮助,它成功了!但是,我想澄清一下如何阅读这段代码——没有太多关于这方面的文档。我知道它正在比较插值的顶点颜色,但是 imgur.com/a/S3vAJuA再次感谢您的帮助
    • 这就是所谓的条件运算符或内联 if 并且在许多语言中都有。之前的部分?是条件,所以在这种情况下,它是“顶点颜色的绿色值小于 0.5”。后面的部分?是根据条件使用的值。 : 之前的部分是条件为真时使用的部分,之后的部分是条件为假时使用的部分。您可以在 inline ifs here 上找到更多信息。因此,如果您希望对蓝色部分执行相同操作,则类似于:clip( IN.vertColor.b &gt; 0.5f ? -1:1 );
    猜你喜欢
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2019-09-12
    相关资源
    最近更新 更多