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

X-Ray Shader

Discussion in 'Shaders' started by metervara, Dec 5, 2006.

  1. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
  2. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Tasty! Thanks for sharing this with everyone, it might come in handy for me right now actually. It's a really nice effect.
     
  3. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Very cool, thanks!
     
  4. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Yay for Patrik! Was the process of writing the shader painful or not? :)
     
  5. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    nice one!
     
  6. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    That looks really nice, thanks!
     
  7. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    Glad you all liked it.

    Not painful at all, very rewarding to see it work actually. It has definitely given me the taste for more :) Hope to post some more shaders in the future as I learn more about ShaderLab Cg.

    /P
     
  8. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    That's really nice!

    As a bonus, it does other interesting things if you give it a texture which isn't a gradient. ;)
     
  9. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Yeah, playing around with the gradient (or putting in entirely weird textures) gets you some neat effects like pseudo reflections, anisotropic looks, and phosphorescent exoskeletons (like deep ocean creatures). Really cool. It's inspiring to see shaders like this that respond in such interesting ways depending on it's orientation to the camera.
     
  10. norby

    norby

    Joined:
    Jul 10, 2006
    Posts:
    277
    Realy nice.
    i am not a coder, but would it
    be possible to use some of the code
    to create a dirt shader.

    Norby
     
  11. norby

    norby

    Joined:
    Jul 10, 2006
    Posts:
    277
    This is what i mean

    Norby
     

    Attached Files:

  12. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Dirt shader I guess is the same as "ambient occlusion". Which is (I think) impossible to do in shaders alone, because for each point you have to actually compute how much it is occluded.

    Most people just compute AO as some kind of offline process, bake it into the texture (or vertex colors) and just use that. Computing AO is not much different than computing lightmaps with a lot of shadow casting lights placed all around your scene.
     
  13. norby

    norby

    Joined:
    Jul 10, 2006
    Posts:
    277
    Thanks Aras for clarifying this for me.

    Norby
     
  14. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    I've got an interesting issue with this shader. I'm using Radeon 9200, and if I use the shader in a project and build it, I don't see the xray effect. However, if I run a build that someone else has compiled, I do see the effect. Any solutions/workarounds from the shader author or OTEE?

    Does this shader require pixel shaders 2.0 on the video card?
     
  15. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Are you setting up a good ramp texture?

    The shader only requires vertex shaders (R9200 can do that), and no fragment programs at all.
     
  16. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    The ramp texture should be fine, as it is displaying a-ok for my team members. this is what I'm seeing:
     

    Attached Files:

  17. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    I dont see _any thing_ when using this shader im on a 9200 as well.
     
  18. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    This is totally strange... When I view the BotBuilder beta this shader problem gets fixed. The BotBuilder beta is here: http://starmanta.googlepages.com/BotBuilder.html

    This has happened to me twice now, and I think it is a bug in Unity...

    I open Unity and I see a screwed-up sphere as pictured above. I leave the project open, view the BotBuilder beta, switch back to my project, and my sphere is rendered correctly.
     
  19. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Sam, that is seriously screwy! Can anyone else verify this odd behavior on their own computer (and if so what GFX card). We want to use this shader for our 3DU entry, but not if it doesn't work on a number of people's computers.
     
  20. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    Not sure if it makes any difference, but you can remove the line 'Lighting On', it doesn't do anything in this shader anyway.

    Otherwise I don't have a clue, I'm still learning about shaders myself.

    Hope it helps.

    /Patrik
     
  21. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Sorry to dig this up from the past, but I wanted to save some people some grief:

    It also doesn't work on a GMA 950.

    -Jon
     
  22. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    I got it working on the GMA 950 by putting in a fragment program. What's the deal with that?

    Code (csharp):
    1. Shader "XRay Effect" {
    2.     Properties {
    3.         _Color ("Tint (RGB)", Color) = (1,1,1,1)
    4.         _RampTex ("Facing Ratio Ramp (RGB)", 2D) = "white" {}
    5.     }
    6.     Category {
    7.         ZWrite Off
    8.         Tags {"Queue"="Transparent"}
    9.         Blend One One
    10.         SubShader {
    11.             Pass {
    12.        
    13.             CGPROGRAM
    14.             // profiles arbfp1
    15.             // vertex vert
    16.             // fragment frag
    17.             // fragmentoption ARB_fog_exp2
    18.  
    19.             #include "UnityCG.cginc"
    20.  
    21.             sampler2D _RampTex : register(s0);
    22.            
    23.             struct v2f {
    24.                 V2F_POS_FOG;
    25.                 float2 uv : TEXCOORD0;
    26.             };
    27.        
    28.             v2f vert (appdata_base v) {
    29.                 v2f o;
    30.                 PositionFog( v.vertex, o.pos, o.fog );
    31.  
    32.                 float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
    33.  
    34.                 o.uv.x = dot(viewDir,v.normal);
    35.                 o.uv.y = 0.5;
    36.  
    37.                 return o;
    38.             }
    39.        
    40.             half4 frag (v2f i) : COLOR
    41.             {
    42.                 return tex2D(_RampTex, i.uv);
    43.             }
    44.            
    45.             ENDCG
    46.  
    47.             SetTexture [_RampTex] {}
    48.             }
    49.         }
    50.     }
    51. }
    52.  
    -Jon
     
  23. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    I don't have a GMA950 right here to test... What if you replace "combine texture * constant" in the original shader with just "combine texture" (you'd lose the tint color though)? If that works, that must be a bug in GMA950 drivers in handling of constant color (there does exist a similar bug on OS X 10.4 Radeon X1600 drivers). Could be fixed by outputing the color in the vertex program and using "combine texture * primary" in the combiner...
     
  24. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    I gave it a shot but it didn't make a difference. I also tested a simple Display Normals shader with and without using a fragment program, and that worked just fine. So it doesn't *need* a separate fragment program. (Good)

    And even taking the base XRay shader, taking out the combiner stuff, and changing the vertex program and setting the UVs to a constant doesn't seem to work. If I change o.uv.x, I should see the color of the object change. It remains the same.
    Code (csharp):
    1.             v2f vert (appdata_base v) {
    2.                 v2f o;
    3.                 PositionFog( v.vertex, o.pos, o.fog );
    4.  
    5. //              float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
    6.  
    7.                 o.uv.x = 0.7;  // change me
    8.                 o.uv.y = 0.5;
    9.  
    10.                 return o;
    11.             }
    12.  
    I give up. :D

    -Jon
     
  25. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Another wild guess: change 'uv' in the output structure from float2 to float4, and output 0 to z and 1 to w.
     
  26. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    That worked. :) Why did it work?

    -Jon
     
  27. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    By default, OpenGL uses projective texturing - that means that it divides UV.xy with UV.w - hence you need to make sure that w is 1 (unless you're doing funky projective texturing, of course ;-))
     
  28. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    I'm reading the vertex program spec right now, and it says in issue (16):
    So the moral is: if you're using vertex programs but not fragment programs, always output full valid float4 for the texture coordinates. One more caveat to learn when writing shaders (and it's different in Direct3D for example, where D3D does not use projective texturing by default... it only uses it when you tell it to).
     
  29. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    I'll put that moral on a tshirt. Thanks guys. I'll update the wiki soon.

    *curls up with the spec and some hot cocoa for some good reading*

    -Jon
     
  30. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    411
    Im getting a subshader error on this-- I'm a total shaderlab n00b.

    ShaderLab error: Shader 'XRay': no subshaders can run on this graphics card

    I think I must have not done something correctly...

    Is there a latest version that works?


    Mac Pro
    NVIDIA GeForce 7300 GT...
     
  31. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Do you get any other errors before that? How does the shader look like after compiling (in Unity's inspector)?
     
  32. FancyPants

    FancyPants

    Joined:
    Jun 30, 2005
    Posts:
    38
    Quick question: Is there a way to have the shader "reveal" a regular texture, instead of using alpha blending with the highlight color? In other words, have it fade to the edges with a regular-old texture map, instead of a constant color?

    I don't know much about shaders, and I'm trying to collect examples like this to piece things together..

    Thanks,
    -Thomas
     
  33. MikeG

    MikeG

    Joined:
    Dec 16, 2008
    Posts:
    8
    Just playing around with Metervara's xray shader, I made a minor change (from "Blend One One" to "Blend One MinusSrcAlpha"). I use it with a white to black ramp for the rgb AND the alpha. This sort of gives it a reverse xray look - darker at the edges instead of lighter. You can use it against a white background. You can also combine it with another material to darken (and tint) the darks.

    Model building is my area, not shader writing; however, it seems to work.
    You can try it out to see what you think.

    Code (csharp):
    1. Shader "XRay" {
    2.  
    3.     Properties {
    4.         _Color ("Tint (RGB)", Color) = (1,1,1,1)
    5.         _RampTex ("Facing Ratio Ramp (RGB)", 2D) = "white" {}
    6.     }
    7.     SubShader {
    8.         ZWrite Off
    9.         //Cull Off
    10.         Tags { "Queue" = "Transparent" }
    11.         Blend One OneMinusSrcAlpha
    12.         Pass {
    13.            
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #include "UnityCG.cginc"
    17.  
    18.             struct v2f {
    19.                 V2F_POS_FOG;
    20.                 float4 uv : TEXCOORD0;
    21.             };
    22.            
    23.             v2f vert (appdata_base v) {
    24.                 v2f o;
    25.                 PositionFog( v.vertex, o.pos, o.fog );
    26.                 float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
    27.                 o.uv = float4( dot(viewDir,v.normal), 0.5, 0.0, 1.0 );
    28.                 return o;
    29.             }
    30.             ENDCG
    31.             SetTexture [_RampTex] {constantColor[_Color] combine texture * constant}
    32.         }
    33.     }
    34.     Fallback Off
    35. }
     

    Attached Files:

  34. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Oooh, nice!
     
  35. outofinspiration

    outofinspiration

    Joined:
    Oct 3, 2011
    Posts:
    3
    Thank you! looks and works excellent in my unity 3.4! :)
     
  36. kurylo3d

    kurylo3d

    Joined:
    Nov 7, 2009
    Posts:
    1,123
    can anyone make one that works on a tegra 2 tablet?
     
  37. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    This is a great shader - but it appears to break in PC builds with Unity 4.5...everything goes pink. Works great on the Mac, but breaks on the PC. Anyone got an idea?