Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Pixelated noise on custom cutout shader

Discussion in 'Shaders' started by wastvedt, May 23, 2016.

  1. wastvedt

    wastvedt

    Joined:
    Oct 6, 2012
    Posts:
    25
    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!

    Code (CSharp):
    1. Shader "Custom/StandardAlphaCutout" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         [NoScaleOffset] _MainTex ("Base (RGB)", 2D) = "white" {}
    5.  
    6.         [Normal] [NoScaleOffset] _Normal ("Normal (RGB)", 2D) = "bump" {}
    7.         _NormalScale ("Normal scale", Vector) = (1,1,1,0)
    8.  
    9.         [NoScaleOffset] _MetallicGlossMap("Metallic (R) Smoothness (A)", 2D) = "white" {}
    10.         _Metallic ("Metallic", Range(0,1)) = 0.0
    11.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    12.  
    13.         _Cutoff ("Base Alpha cutoff", Range (0,.9)) = .5
    14.         [Toggle(InvertAlpha)] _InvertAlpha("Invert alpha map?", Int) = 0
    15.         _AlphaTex ("Alpha Map (RGB)", 2D) = "white" {}
    16.     }
    17.     SubShader {
    18.         Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
    19.         //LOD 200
    20.         Cull Off
    21.        
    22.         CGPROGRAM
    23.  
    24.         #pragma shader_feature METALLICGLOSSMAP
    25.  
    26.         #pragma shader_feature InvertAlpha
    27.  
    28.         // Physically based Standard lighting model
    29.         #pragma surface surf Standard alphatest:_Cutoff
    30.  
    31.         // Use shader model 3.0 target, to get nicer looking lighting
    32.         #pragma target 3.0
    33.        
    34.         sampler2D _MainTex;
    35.         sampler2D _MetallicGlossMap;
    36.         sampler2D _AlphaTex;
    37.         sampler2D _Normal;
    38.        
    39.         struct Input {
    40.             float2 uv_MainTex;
    41.             float2 uv_AlphaTex;
    42.             float2 uv_MetallicGlossMap;
    43.             float2 uv_Normal;
    44.         };
    45.        
    46.         fixed4 _NormalScale;
    47.         fixed _Glossiness;
    48.         fixed _Metallic;
    49.         fixed4 _Color;
    50.  
    51.         void surf (Input IN, inout SurfaceOutputStandard o) {
    52.             // Albedo comes from a texture tinted by color
    53.             fixed4 c = tex2D ( _MainTex, IN.uv_MainTex ) * _Color;          
    54.             o.Albedo = c.rgb;
    55.  
    56.             fixed3 normal = UnpackNormal ( tex2D ( _Normal, IN.uv_Normal * _NormalScale.xy ) );
    57.             normal.xy *= _NormalScale.z;
    58.             o.Normal = normalize ( normal );
    59.            
    60.             #if InvertAlpha
    61.             o.Alpha = 1 - tex2D ( _AlphaTex, IN.uv_AlphaTex ).a;
    62.             #else
    63.             o.Alpha = tex2D ( _AlphaTex, IN.uv_AlphaTex ).a;
    64.             #endif
    65.  
    66.             #if METALLICGLOSSMAP
    67.             fixed4 mg = tex2D ( _MetallicGlossMap, IN.uv_MetallicGlossMap );
    68.             o.Metallic = mg.r;
    69.             o.Smoothness = mg.a;
    70.             #else
    71.             // Metallic and smoothness come from slider variables
    72.             o.Metallic = _Metallic;
    73.             o.Smoothness = _Glossiness;
    74.             #endif
    75.  
    76.         }
    77.         ENDCG
    78.     }
    79.     FallBack "Transparent/Cutout/Diffuse"
    80.     CustomEditor "StandardAlphaCutoutGUI"
    81. }
    82.  
     

    Attached Files:

  2. wastvedt

    wastvedt

    Joined:
    Oct 6, 2012
    Posts:
    25
    I'm building to WebGL, now on 5.3.5f1, and there's no noise when running in the browser. The noise is still there in the editor though.