I’m writing a custom alpha cutout shader because I want to have a material texture for the visible parts, and programmatically generate the alpha map at runtime. The current code is below. The attached screenshot shows the shader on a simple plane with a circle alpha map. I can’t figure out where the noise in the alpha map is coming from. The built-in Unlit/Transparent Cutout shader works as expected, with no noise.
Thanks!
Shader "Custom/StandardAlphaCutout" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
[NoScaleOffset] _MainTex ("Base (RGB)", 2D) = "white" {}
[Normal] [NoScaleOffset] _Normal ("Normal (RGB)", 2D) = "bump" {}
_NormalScale ("Normal scale", Vector) = (1,1,1,0)
[NoScaleOffset] _MetallicGlossMap("Metallic (R) Smoothness (A)", 2D) = "white" {}
_Metallic ("Metallic", Range(0,1)) = 0.0
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Cutoff ("Base Alpha cutoff", Range (0,.9)) = .5
[Toggle(InvertAlpha)] _InvertAlpha("Invert alpha map?", Int) = 0
_AlphaTex ("Alpha Map (RGB)", 2D) = "white" {}
}
SubShader {
Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
//LOD 200
Cull Off
CGPROGRAM
#pragma shader_feature METALLICGLOSSMAP
#pragma shader_feature InvertAlpha
// Physically based Standard lighting model
#pragma surface surf Standard alphatest:_Cutoff
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
sampler2D _MetallicGlossMap;
sampler2D _AlphaTex;
sampler2D _Normal;
struct Input {
float2 uv_MainTex;
float2 uv_AlphaTex;
float2 uv_MetallicGlossMap;
float2 uv_Normal;
};
fixed4 _NormalScale;
fixed _Glossiness;
fixed _Metallic;
fixed4 _Color;
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;
fixed3 normal = UnpackNormal ( tex2D ( _Normal, IN.uv_Normal * _NormalScale.xy ) );
normal.xy *= _NormalScale.z;
o.Normal = normalize ( normal );
#if InvertAlpha
o.Alpha = 1 - tex2D ( _AlphaTex, IN.uv_AlphaTex ).a;
#else
o.Alpha = tex2D ( _AlphaTex, IN.uv_AlphaTex ).a;
#endif
#if METALLICGLOSSMAP
fixed4 mg = tex2D ( _MetallicGlossMap, IN.uv_MetallicGlossMap );
o.Metallic = mg.r;
o.Smoothness = mg.a;
#else
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
#endif
}
ENDCG
}
FallBack "Transparent/Cutout/Diffuse"
CustomEditor "StandardAlphaCutoutGUI"
}