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

Reconciling different shader writing styles

Discussion in 'Shaders' started by omatase, Nov 29, 2014.

  1. omatase

    omatase

    Joined:
    Jul 31, 2014
    Posts:
    159
    I'm trying to put an outline around my sprites using a shader. I found one that sort-of works already and am trying to modify it to fix a problem with it. I've read all of the documentation in the manual and watched several tutorials on writing shaders and would like help understanding why the syntax in the shader I'm modifying doesn't match the syntax of the shader the guy is writing in the tutorials I'm watching. I assume it's just two ways to do the same thing. Here's the shader I'm modifying:

    Code (CSharp):
    1. Shader "Outlined/Silhouetted Diffuse" {
    2.     Properties {
    3.         _EmisColor ("Emissive Color", Color) = (.2,.2,.2,0)
    4.         _MainTex ("Particle Texture", 2D) = "white" {}
    5.     }
    6.  
    7.     SubShader {
    8.            
    9.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    10.         Blend SrcAlpha OneMinusSrcAlpha
    11.         Cull Off
    12.         ZWrite Off
    13.         Fog { Color (0,0,0,0) }
    14.         Lighting Off
    15.         Material { Emission [_EmisColor] }
    16.         ColorMaterial AmbientAndDiffuse
    17.  
    18.         Pass {
    19.             AlphaTest Equal 1
    20.             SetTexture [_MainTex]
    21.             {  
    22.                 combine texture * primary
    23.             }
    24.         }
    25.  
    26.         Pass {
    27.             ZTest Less
    28.             AlphaTest NotEqual 1
    29.  
    30.             SetTexture [_MainTex]
    31.             {  
    32.                 combine texture * primary
    33.             }
    34.             SetTexture [_MainTex]
    35.             {
    36.                 constantColor [_EmisColor]
    37.                 combine previous * constant
    38.             }
    39.         }
    40.  
    41.     }
    42.  
    43.     Fallback "Diffuse"
    44. }
    And here is one of the shader samples from the video tutorials:

    Code (CSharp):
    1. Shader "_Shaders/JustColor"
    2. {
    3.     Properties
    4.     {}
    5.    
    6.     SubShader
    7.     {
    8.         Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
    9.         Blend SrcAlpha OneMinusSrcAlpha
    10.        
    11.         Pass
    12.         {
    13.             CGPROGRAM
    14.                 #pragma exclude_renderers ps3 xbox360
    15.                 #pragma fragmentoption ARB_precision_hint_fastest
    16.                 #pragma vertex vertex
    17.                 #pragma fragment fragment
    18.                 #include "UnityCG.cginc"
    19.  
    20.                 // uniforms
    21.                 uniform fixed4 _Color;
    22.                
    23.                 struct vertexInput
    24.                 {
    25.                     float4 vertex : POSITION;
    26.                 };
    27.                
    28.                 struct fragmentInput
    29.                 {
    30.                     float4 pos : SV_POSITION;
    31.                     float4 color : COLOR0;
    32.                 };
    33.                
    34.                 fragmentInput vert( vertexInput i )
    35.                 {
    36.                     fragmentInput o;
    37.                     o.pos = mul( UNITY_MATRIX_MVP, i.vertex );
    38.                     o.color = _Color;
    39.                    
    40.                     return o;
    41.                 }
    42.                
    43.                 half4 frag( fragmentInput i ) : COLOR
    44.                 {
    45.                     return i.color;
    46.                 }
    47.             ENDCG
    48.         }
    49.     }
    50. }
    The big difference is, in the video tutorials version the guy doesn't ever use SetTexture in any of his passes. Instead he's using a CGPROGRAM block, setting up a vertex and fragment input struct. Then defining his own vert and frag methods.

    The shader I'm modifying on the other hand uses SetTexture and combine to change the color output.

    I understand the video tutorial workflow pretty well I think. I just could use some context when applying what I learned from that to the shader I'm modifying.

    Appreciate the help!
     
  2. omatase

    omatase

    Joined:
    Jul 31, 2014
    Posts:
    159
    Nevermind, in my continued effort to figure out and get the shader I have been working on correct I came across information that helps me understand what's happening.

    It looks like SetTexture is a layered way of writing a shader that is intended for support for older graphics cards. Exactly how old your card has to be to only support this approach is something I'm not sure of but it sounds like they have to be pretty outdated to not support the "fragment programs" approach.
     
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,415
    SetTexture is part of the fixed-function pipeline, whereas GCPROGRAM is the programmable pipeline.