Search Unity

big question for a new shader guy...

Discussion in 'Shaders' started by FancyPants, Sep 28, 2007.

  1. FancyPants

    FancyPants

    Joined:
    Jun 30, 2005
    Posts:
    38
    Hi all,

    I'm trying to figure out how to make a shader that makes an object seem partially translucent, and blurry. I don't want motion blur, because the object could be sitting still, I just want the edges to sort of "fuzz out" to the background.

    Any ideas?

    -T
     
  2. FancyPants

    FancyPants

    Joined:
    Jun 30, 2005
    Posts:
    38
    Okay, here's what I've come up with so far. Its a modification to the x-ray shader. Instead of fading into/out of a standard color, it fades through a texture assigned to the object. It doesn't quite work the way I want, though.

    Here's the code

    Code (csharp):
    1.  
    2. Shader "XRay" {
    3.     Properties {
    4.         _Color ("Tint (RGB)", Color) = (1,1,1,1)
    5.         _RampTex ("Facing Ratio Ramp (RGB)", 2D) = "white" {}
    6.         _MainTex ("Main texture",2D) = "white" {}
    7.     }
    8.     Category {
    9.         ZWrite Off
    10.         Tags {"Queue"="Transparent"}
    11.         Lighting On
    12.         SubShader {
    13.             Pass {
    14.                 Blend One One
    15.                
    16.                 CGPROGRAM
    17.                 // profiles arbfp1
    18.                 // vertex vert
    19.                
    20.                 #include "UnityCG.cginc"
    21.      
    22.                 sampler2D _MainTex : register(s0);
    23.                 struct v2f {
    24.                     V2F_POS_FOG;
    25.                     float4 uv : TEXCOORD0;
    26.                     float2 uv1 :TEXCOORD1;
    27.                 };
    28.                
    29.                 v2f vert (appdata_base v) {
    30.                     v2f o;
    31.                     PositionFog( v.vertex, o.pos, o.fog );
    32.                     o.uv1 = TRANSFORM_UV(0);
    33.  
    34.                     float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
    35.                    
    36.                     o.uv.x = dot(viewDir, v.normal);
    37.                     o.uv.y = 0.5;
    38.                     o.uv.z = 0.0;
    39.                     o.uv.w = 1.0;
    40.                     return o;
    41.                 }
    42.                
    43.                 ENDCG
    44.                
    45.                 SetTexture [_RampTex] {
    46.                     constantColor[_Color] combine texture * constant
    47.                 }
    48.                 SetTexture [_MainTex] {
    49.                     combine texture * previous, previous
    50.                 }
    51.             }
    52.         }
    53.     }
    54. }
    55.  
    Try it out. The problem is, even the parts that are supposed to be "solid" aren't. Any ideas on why, or how to fix that issue?

    Thanks,
    -T

    [/code]
     
  3. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    I did not try your shader, but a first guess: try changing "Blend One One" to "Blend SrcAlpha OneMinusSrcAlpha". The X-ray shader uses additive blending (possibly because the author of the effect wanted it that way), whereas I guess you want regular alpha blending.
     
  4. FancyPants

    FancyPants

    Joined:
    Jun 30, 2005
    Posts:
    38
    That had an interesting effect. It makes it fade out to black where before it was transparent. I'll keep playing around with it (I'm still learning about shader programming), but any more insights that people might have would be very much appreciated.

    -T
     
  5. FancyPants

    FancyPants

    Joined:
    Jun 30, 2005
    Posts:
    38
    Alright, I figured out something that works:

    Code (csharp):
    1.  
    2. Shader "XRay" {
    3.     Properties {
    4.         _Color ("Tint (RGB)", Color) = (1,1,1,1)
    5.         _MainTex ("Main texture",2D) = "white" {}
    6.     }
    7.     Category {
    8.         ZWrite Off
    9.         Tags {"Queue"="Transparent"}
    10.         Lighting Off
    11.         SubShader {
    12.             Pass {
    13.                 // Blend One One
    14.                 Blend SrcAlpha OneMinusSrcAlpha
    15.                
    16.                 CGPROGRAM
    17.                 // profiles arbfp1
    18.                 // vertex vert
    19.                 //fragment frag
    20.                 //fragmentoption ARB_fog_exp2
    21.    
    22.                 #include "UnityCG.cginc"
    23.      
    24.                 sampler2D _MainTex : register(s0);
    25.                 struct v2f {
    26.                     V2F_POS_FOG;
    27.                     float2 uv1 :TEXCOORD1;
    28.                     float3 color : COLOR0;
    29.                     float alpha;
    30.                 };
    31.                
    32.                 v2f vert (appdata_base v) {
    33.                     v2f o;
    34.                     PositionFog( v.vertex, o.pos, o.fog );
    35.                     o.uv1 = TRANSFORM_UV(0);
    36.                    
    37.                     float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
    38.                     o.alpha = dot(viewDir, v.normal) * dot(viewDir,v.normal);
    39.                    
    40.                     return o;
    41.                 }
    42.                
    43.                 half4 frag(v2f i) : COLOR {
    44.                     half4 texcol = tex2D(_MainTex, i.uv1);
    45.                     return half4(texcol.rgb, i.alpha);
    46.                 }
    47.                
    48.                 ENDCG
    49.            
    50.                 SetTexture [_MainTex] {
    51.                     combine texture
    52.                 }          
    53.             }
    54.         }
    55.     }
    56. }
    57.  
    Its frustrating working with a new language/paradigm...

    Anyway, this gives me the fade-to-edges that I was talking about. It's not ideal (ideally, it would just sample that part of the screen, and do a radial blur...), and it uses a fragment program, but it's a start.

    Does anyone have any insights into how to get this same effect without a fragment program?

    Thanks,
    -T
     
  6. FancyPants

    FancyPants

    Joined:
    Jun 30, 2005
    Posts:
    38
    Okay, I'm going all the way with a grab texture, and doing a radial blur on what it gets. I know the radial blur algorithm, no problem, but I have an issue. When I compile my shader, I get a "profile does not support for statements" error. What profile does (that unity knows how to compile to)? Or do I have to un-roll the loop myself?

    -T
     
  7. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Unity compiles to ARBVP/ARBFP profile (ARB vertex and fragment programs), roughly a shader model 2.0 equivalent of Direct3D world. They don't support dynamic branching and Cg might not be able to unroll loops with variable number of iterations.

    Doing radial blur is probably better done as an image effect, by repeatedly rendering texture onto itself, while scaling/rotating it. Similar like Motion Blur image effect does.
     
  8. FancyPants

    FancyPants

    Joined:
    Jun 30, 2005
    Posts:
    38
    Okay, good to know. Thanks! How, though, do I do an image effect that only applies to the area of the screen that a particular object is on, instead of the whole screen?
     
  9. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    That would be tricky... I don't know.

    Maybe what you actually want is something akin to a fur shader? With changing it so that there's more intensity around the edges...

    Sorry if that is already what you're trying to do and I'm just confusing you for no good reason. Don't have time to play with it myself right now, we're trying to get some release out of the door... :roll:
     
  10. FancyPants

    FancyPants

    Joined:
    Jun 30, 2005
    Posts:
    38
    Dude. How did I not find that thread in all my searching?! This is exactly the sort of thing that I'm looking for (or at least a GREAT starting point for it). Thank you so much! Hopefully when I start to figure out more of this stuff, I can start contributing back.

    Thanks again!