首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Unity Alpha漫反射遮罩明暗器

Unity Alpha漫反射遮罩明暗器
EN

Stack Overflow用户
提问于 2015-11-27 23:36:21
回答 2查看 9.4K关注 0票数 2

我在Unity中工作,使用Unity 5.2.2的最新版本,我在问,是否有可能创建一个可以淡出蒙版的alpha蒙版着色器。现在我有这个着色器

代码语言:javascript
复制
Shader "MaskedTexture"
{
   Properties
   {
      _MainTex ("Base (RGB)", 2D) = "white" {}
      _Mask ("Culling Mask", 2D) = "white" {}
      _Cutoff ("Alpha cutoff", Range (0,1)) = 0.1
   }
  SubShader
  {
      Tags {"Queue"="Transparent"}
      Lighting Off
      ZWrite Off
      Blend SrcAlpha OneMinusSrcAlpha
      AlphaTest GEqual [_Cutoff]
  Pass
  {
     SetTexture [_Mask] {combine texture}
     SetTexture [_MainTex] {combine texture, previous}
  }
  }
}

我需要添加一个颜色属性还是什么?我想要的就是淡出这个物体,这样它就不再可见了。我不能简单地禁用它,它实际上必须平滑地淡出。

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-30 16:46:55

我想通了。

代码语言:javascript
复制
Shader "MaskTexture"
{
   Properties
   {
     _Color ("Main Color", Color) = (1, 1, 1, 1)
     _MainTex ("Base (RGB) Transparency (A)", 2D) = "" { }
      _Mask ("Culling Mask", 2D) = "white" {}
   }
   SubShader
   {
      Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
      Lighting Off
      ZWrite Off
      Blend SrcAlpha OneMinusSrcAlpha
      AlphaTest GEqual [_Cutoff]
      Pass
      {

        SetTexture [_Mask] {combine texture}

        SetTexture [_MainTex] { combine texture, previous }

        SetTexture [_MainTex] {
            constantColor [_Color]
            combine previous * constant
        }
      }
   }
}
票数 3
EN

Stack Overflow用户

发布于 2015-11-28 00:29:42

我不知道你做蒙版的方式,但我通常会使用像素着色器。我找到了这篇博文,我认为它对你有帮助。http://bensilvis.com/unity3d-unlit-alpha-mask-shader/

我使用了代码并为alpha添加了一个额外的值来调整蒙版的数量。

代码语言:javascript
复制
// - no lightmap support
// - no per-material color

Shader "AlphaMask" {
Properties {
_alphaValue ("Alpha Value", Range (0, 1)) = 1
_MainTex ("Base (RGB)", 2D) = "white" {}
_AlphaTex ("Alpha mask (R)", 2D) = "white" {}

}

SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 100

ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha

Pass {  
    CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag

        #include "UnityCG.cginc"

        struct appdata_t {
            float4 vertex : POSITION;
            float2 texcoord : TEXCOORD0;
        };

        struct v2f {
            float4 vertex : SV_POSITION;
            half2 texcoord : TEXCOORD0;
        };

        sampler2D _MainTex;
        sampler2D _AlphaTex;
        float _alphaValue;

        float4 _MainTex_ST;

        v2f vert (appdata_t v)
        {
            v2f o;
            o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
            o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
            return o;
        }

        fixed4 frag (v2f i) : SV_Target
        {
            fixed4 col = tex2D(_MainTex, i.texcoord);
            fixed4 col2 = tex2D(_AlphaTex, i.texcoord);

            //Uncomment for only alpha of mask
            //float alpha = col2.r+_alphaValue;
            //return fixed4(col.r, col.g, col.b,alpha);
             return fixed4(col.r, col.g, col.b,_alphaValue);
        }
    ENDCG
}
}
}

希望这就是你要找的

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33960669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档