Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need help adding alpha input to this shader

Discussion in 'Shaders' started by The-Rizzler, Jan 6, 2015.

  1. The-Rizzler

    The-Rizzler

    Joined:
    Nov 20, 2012
    Posts:
    19
    So I've been using a BumpSpecProbed shader I found to add normals to my lightprobe-using objects and make them look like they actually belong in the scene, but now i'm in a pinch since I need some gratings to have see-through holes - what would I add so I can use an input to add a tiling alpha map?
    Here's the shader code so you don't need to download it yourself: http://pastebin.com/YUkjw8ZU

    side note: I'm getting warnings about this shader not being a surface shader or something, so I was thinking, hasn't someone come up with a better way to display normals on a light probed object since 2012? I haven't researched extensively but either this is a fundamental problem or I'm missing something
     
    Last edited: Jan 6, 2015
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    You can just post the shader code between code tags on the forum. So, here is the mentioned shader:
    Code (csharp):
    1.  
    2. // Upgrade NOTE: replaced 'PositionFog()' with multiply of UNITY_MATRIX_MVP by position
    3. // Upgrade NOTE: replaced 'V2F_POS_FOG' with 'float4 pos : SV_POSITION'
    4.  
    5. Shader "Transparent/Bumped Trans Gloss" {
    6. Properties {
    7.    _Color ("Main Color", Color) = (1,1,1,1)
    8.    _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
    9.    _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    10.    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    11.    _BumpMap ("Bumpmap", 2D) = "bump" {}
    12.    _GlossMap ("Glossmap (A)", 2D) = "black" {}
    13. }
    14.  
    15. Category {
    16.    Tags {Queue=Transparent}
    17.    Alphatest Greater 0
    18.    Fog { Color [_AddFog] }
    19.    ZWrite Off
    20.    ColorMask RGB
    21.  
    22.    // ------------------------------------------------------------------
    23.    // ARB fragment program
    24.  
    25.    #warning Upgrade NOTE: SubShader commented out; uses Unity 2.x per-pixel lighting. You should rewrite shader into a Surface Shader.
    26. SubShader {
    27.      UsePass "Transparent/Specular/BASE"
    28.    
    29.      // Pixel lights
    30.      Pass {
    31.        Blend SrcAlpha One
    32.        Name "PPL"
    33.        Tags { "LightMode" = "Pixel" }
    34. CGPROGRAM
    35. // Upgrade NOTE: excluded shader from DX11 and Xbox360; has structs without semantics (struct v2f members uvK,uv2,viewDirT,lightDirT)
    36. #pragma exclude_renderers d3d11 xbox360
    37. #pragma vertex vert
    38. #pragma fragment frag
    39. #pragma multi_compile_builtin_noshadows
    40. #pragma fragmentoption ARB_fog_exp2
    41. #pragma fragmentoption ARB_precision_hint_fastest
    42.  
    43. #include "UnityCG.cginc"
    44. #include "AutoLight.cginc"
    45.  
    46. struct v2f {
    47.    float4 pos : SV_POSITION;
    48.    LIGHTING_COORDS
    49.    float3   uvK; // xy = UV, z = specular K
    50.    float2   uv2;
    51.    float3   viewDirT;
    52.    float3   lightDirT;
    53. };
    54.  
    55. uniform float4 _MainTex_ST, _BumpMap_ST;
    56. uniform float _Shininess;
    57.  
    58.  
    59. v2f vert (appdata_tan v)
    60. {
    61.    v2f o;
    62.    o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    63.    o.uvK.xy = TRANSFORM_TEX(v.texcoord,_MainTex);
    64.    o.uvK.z = _Shininess * 128;
    65.    o.uv2 = TRANSFORM_TEX(v.texcoord,_BumpMap);
    66.  
    67.    TANGENT_SPACE_ROTATION;
    68.    o.lightDirT = mul( rotation, ObjSpaceLightDir( v.vertex ) );
    69.    o.viewDirT = mul( rotation, ObjSpaceViewDir( v.vertex ) );
    70.  
    71.    TRANSFER_VERTEX_TO_FRAGMENT(o);
    72.    return o;
    73. }
    74.  
    75. uniform sampler2D _GlossMap;
    76. uniform sampler2D _BumpMap;
    77. uniform sampler2D _MainTex;
    78. uniform float4 _Color;
    79.  
    80. float4 frag (v2f i) : COLOR
    81. {  
    82.    float4 texcol = tex2D( _MainTex, i.uvK.xy );
    83.  
    84.    // get normal from the normal map
    85.    float3 normal = tex2D(_BumpMap, i.uv2).xyz * 2 - 1;
    86.  
    87.    float gloss = tex2D(_GlossMap, i.uv2).xyz;
    88.  
    89.    half4 c;
    90.    c.a = texcol.a * _Color.a;
    91.  
    92.    texcol.a = gloss;
    93.    c.rgb = SpecularLight( i.lightDirT, i.viewDirT, normal, texcol, i.uvK.z, LIGHT_ATTENUATION(i) ).rgb;
    94.  
    95.    return c;
    96. }
    97. ENDCG
    98.      }
    99.    }
    100. }
    101.  
    102. FallBack "Transparent/Bumped Specular"
    103.  
    104. }
    105.  
    I'm not sure what you are trying to achieve here, but this shader seems to be very old. It would be best to select one of the standard shaders in the current Unity as a base and then work from that. So, which shader is the starting point and what do you want to add?
     
    Last edited: Jan 7, 2015
  3. The-Rizzler

    The-Rizzler

    Joined:
    Nov 20, 2012
    Posts:
    19
    The closest shader is probably the Transparent bumped specular shader
    Using that, I get this;
    http://i.imgur.com/uGtbpZ0.png (unlit)
    http://imgur.com/Xyx6B6k,uGtbpZ0#0 (lit)
    The problem is in the lit mode, the normals don't display, which is usual lightprobed object behaviour apparently
    The BumpSpecProbed shader I linked above solves this and is able to show normals on non-static objects that use lightprobes, however it doesn't support alphas, which I need for this grating texture

    Also I'm using directional lightmaps mode