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

Fur Shader

Discussion in 'Made With Unity' started by Jonathan Czeck, Apr 13, 2007.

  1. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    I made a 20 pass two directional light + ambient fur shader for a game I'm working on. I'm sharing it in hopes that someone will help optimize it, since this is my first Cg program. :)

    It should run on anything that supports vertex programs. It runs really slowly on a GMA 950 as it is vertex bound, but taking out a light and making it 10 passes can make it acceptable for that card. Not much else to do about it. :(

    The crummy thing is that it doesn't use Unity's lighting system, so I have a script that passes two directional lights of your choice to the shader that you stick on the furry object. I don't know how to make it use Unity's lighting system.

    It uses shells and not fins, but you can fade out the shells along the edges with one of the parameters if you want to add some fins with another material / triangle list.

    Cheers,
    -Jon
     

    Attached Files:

  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    If only all the soldiers in Global Conflicts Palestine looked like this. :D

    -Jon
     

    Attached Files:

  3. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Your Cg is quite good in fact! Two things I changed:

    I changed the way you do extrusion. I just add normal to the object space position, and then transform that by regular MVP matrix. This produces different result than extrusion you did (which was some sort of extrusion in post-perspective space), but it saves a couple of instructions; and possibly some shader constants because less parameters are used.

    Then I removed support for texture scale&offset. So that's a loss of flexibility, but a save of matrix multiplication in the vertex shader.

    So now the vertex shader is 28 instructions, down from 34. That could make some difference on GMA950; hard to guess how much exactly.

    I've also moved some more code into the helper .cginc file, so the shader file itself is now shorter.

    Other interesting things to try (no for GMA950 though... I think): do a proper anisotropic lighting on the fur. Now it treats fur normals the same as surface normals; whereas in real fur the normals point to all directions orthogonal to surface normal.
     

    Attached Files:

  4. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Yeah, I'm not sure what was going on with that. I tried something like you've done first but couldn't get it working for some reason. Thanks!
    Yeah, don't need this for a real character.
    It did! In a test scene it went from 17 fps to 25 fps. Now with a 10 pass 1 light version it is even more acceptable. Of course it still runs hundreds of FPS even on a lowly Radeon 9600...
    Ahh, clever. Thanks
    Yeah, there seems to be a ton more to be done with this, but I think it may be good/slow enough for our purposes for now. Nicholas pointed me to these cool slides with way better fur. He also helped idea wise with the shader, so thanks Nicholas. :)
    http://ati.amd.com/developer/SIGGRAPH02/CustomizableFur_Isidoro.pdf

    -Jon
     
  5. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Jon-Aras,
    You guys are fantastic-seriously. Is there anything you cant do?

    This takes things to a new level. I cant believe how quickly you can whip stuff up like this. Mega cred 2 u guys.
    AC
     
  6. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    The soldier looks nice. :O)

    One question: Do the shells result into real polys in the end like if you just would clone a modell, enlarge it and reduce the alpha value of the texture step by step?
     
  7. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
  8. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Yes. In essence this draws the model 20 times, extruded more and more each time. So it's almost the same as creating 20 shells manually (the vertex shader also does some fading out at the edges).
     
    larsbertram1 likes this.
  9. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    Taumel: That would be rather cool as a sun with a wreath of flame.
     
  10. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    @Aras

    Is a shader version theoretically any faster because for instance it spares this or that mathematical transformation compared to a grouped object where i just rotate the parent?

    The sw3d example is overall under 50 lines of code and also runs on really old shaderless hardware. A shaderversion for sure is more flexible and the better way to go if you make it more complex.

    By the way is there a good tutorial around (a crash course) for working with shaders in unity? I only did four really simple things and i guess there's much more to explore...
     
  11. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    @Marble
    I'm not sure, i guess there are better ways to fake a sun. I think i would more go for a particel solution then...
     
  12. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Does the shaderless version simply scale the object up? That only works for very simple geometry as you want to extrude each vertex along the normal. For a sphere that's fine, but not for anthing real.

    You can use the mesh interface to do pretty much the same in Unity at loadtime. Performance should be about the same.

    The shader one is faster once you begin skinning the objects.
     
  13. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    Yep, it only clones (same ressource) and scales a sphere but it would be enough for a marble game or even a fur-pacman... ;O)
     
  14. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    I managed to shave off two more instructions from the vertex shader. Replace FurVertexPass in my previous package's Fur/Support/FurHelpers.cginc with this:
    Code (csharp):
    1. void FurVertexPass(float multiplier, appdata_base v, float furLength,
    2.     out float4 pos, out float fog, out float4 color, out float4 uv)
    3. {
    4.     // extrude position
    5.     float4 wpos = v.vertex;
    6.     wpos.xyz += v.normal * furLength * multiplier;
    7.     pos = mul(glstate.matrix.mvp, wpos);    
    8.  
    9.     // fog
    10.     fog = pos.w;
    11.    
    12.     // UV (pass through to save instr - loses tiling/offset though)
    13.     uv = v.texcoord;
    14.    
    15.     // edge fade out
    16.     float alpha = 1 - (multiplier * multiplier);
    17.     // transform normal into eye space and use Z component
    18.     // to control the fade
    19.     float3 eyeNormal = mul((float3x3)glstate.matrix.modelview[0],
    20.         v.normal);
    21.     alpha += saturate(eyeNormal.z) - _EdgeFade;
    22.    
    23.     // lighting
    24.     float3 normalWorld = mul ((float3x3)_Object2World, v.normal);
    25.    
    26.     float light0 = clamp(dot(normalWorld, _LightDirection0.xyz),
    27.         _MyLightColor0.w, 1);
    28.     float light1 = clamp(dot(normalWorld, _LightDirection1.xyz),
    29.         _MyLightColor1.w, 1);
    30.  
    31.     color = float4((light0 * _MyLightColor0).xyz +
    32.         (light1 * _MyLightColor1).xyz,alpha);
    33. }
    I changed the part that calculates the alpha - instead of calculating view direction (which involves a costly normalize), I just transform normal into view space and use it's Z component to do the fade.
     
  15. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    Works...

    Any idea on a shader crash course?
     
  16. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Here's how I learnt it:

    I started out by playing with texture combiners - like trying to do nice glass with fresnel falloff (which can be made to run on anything).

    Next I would make them run on more modern combiners. IIRC, you have a Mac mini with a radeon9000 GFX card. That means moving the shaders to the ATIFS1 assembly language (which is simpler than the name implies). There are a bunch of examples at the bottom of the ATI_TEXT_FRAGMENT_SHADER spec.

    ATI has a lot of Radeon85000-based articles online from ShaderX1 - they are made for DirectX, so the format is bit different from the OpenGL one used in Unity. Moving these over gets you going - basically you get in the habit of "thinking like a pixel".

    Ignore lighting at first - it's quite complex, a major hassle not very fun. Just focus on doing fun effects (gems, ligtning, water, etc).
     
  17. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    @Nicholas
    Okay, thanks

    @Aras
    Could you alter your one so that the tiling is back?
     
  18. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Just insert matrix multiplication where UV is computed in the vertex shader; just like it was in the first Jon's package.
     
  19. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    Which then try to overwrite yours in unity...couldn't you cut&paste the relevant lines here and say where to insert?
     
  20. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    ditto! cool as... fur!
     
  21. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    You have to move each vertex out along the normal, not scale the whole thing up as they've said.

    Also if you have a skinned character, the skinning transformations would only happen once with the shader version I hope which I'd think would save some CPU.

    Also the shader version has the Edge Fade property and I'm working on hacking in fake better fur lighting in the vertex shader.

    -Jon
     
  22. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Fine...
    Code (csharp):
    1. uv = v.texcoord;
    to:
    Code (csharp):
    1. uv = mul( glstate.matrix.texture[0], v.texcoord );
    -Jon

    p.s. Man oh man this shader takes so long to compile.
     
  23. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    *back to the computer...*

    Ahhh do you also have such a good weather these days? :O)

    Thanks Jon but it doesn't work here. Do i have to rebuild or do anything special beside of altering the line and saving the script?

    The interesting thing about the tiling is that you can come up with pretty nice furs if you use photoshop and imagesynth...
     
  24. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Change the .cginc file and then also save the .shader file. We don't track Cg includes right now, so you have to save the shader to force it to recompile.
     
  25. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Thanks! I guess it could be used for ass fur.

    -Jon
     
  26. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    Okay thanks that worked...
     
  27. AGhost

    AGhost

    Joined:
    Apr 13, 2006
    Posts:
    90
    Is this written for specific video cards? because it isn't working on my Radeon 7500 (eMac) or my Radeon X300 (PC).

    AGhost
     
  28. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    This needs vertex shaders. Radeon 7500 does not have them; and Radeon X300 does have them, but it might be that in your case you have old drivers or something (we explicitly disable vertex shaders on some ATI drivers... otherwise they would crash).
     
  29. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Ok, looks like this shaders gone from summer coat to winter coat( a few changes). Perhaps someone with the latest working version could post it here or on the wiki?
    Cheers
    AC
     
  30. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    a) Download Aras version.
    b) Open the files ".cginc" and ".shader".
    c) Replace "uv = v.texcoord;" with "uv = mul( glstate.matrix.texture[0], v.texcoord );" in .cginc.
    d) Save both and be patient for a few seconds.

    Et voila...
     
  31. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    >>Thanks! I guess it could be used for ass fur.

    lol. didn't mean that! my ... was more of a pause. but i guess, sure, it could. nice one either way!
     
  32. slippyd

    slippyd

    Joined:
    Jun 11, 2005
    Posts:
    129
    Since everyone seems to like the idea of a good fur shader, how about you guys post it to the wiki once everyone agrees it's optimized/flexible enough?
     
  33. dingosmoov

    dingosmoov

    Joined:
    Jul 12, 2005
    Posts:
    559
    Can someone post a web player so we can see it in action?
     
  34. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    411
    Here is some ass fur...

    The shader has the nice effect of anti-aliasing fuzzy creatures, even if they have relatively short fur. (in the case of the bear below)

    Given unlimited power, I would use a shader like this on many objects to soften edges on objects, or on anything that AA didn't fully correct. 8)

    It would be awesome if we could get this running on a variety of hardware
     

    Attached Files:

  35. slippyd

    slippyd

    Joined:
    Jun 11, 2005
    Posts:
    129
    Wait a second, is there a Unity 2 beta out? The GUI in those screenshots is different than the 1.6.1 GUI (or did they just update the GUI in 1.6.2 without mentioning it in the release notes?)
     
  36. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Hey, I didn't notice the GUI thing. Wierd. But I like it. :) But to be on topic, the fur shader = <3, but it looks a little odd around tight corners. but still, good work.
     
  37. oPless_

    oPless_

    Joined:
    Feb 18, 2007
    Posts:
    20

    Looks almost windows xp-ish to me.

    What gives?
     
  38. slippyd

    slippyd

    Joined:
    Jun 11, 2005
    Posts:
    129
  39. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The result of some skinning app like Shapeshifter, maybe?

    --Eric
     
  40. slippyd

    slippyd

    Joined:
    Jun 11, 2005
    Posts:
    129
    Doubt it. What's the story, Brian?
     
  41. Brian-Kehrer

    Brian-Kehrer

    Joined:
    Nov 7, 2006
    Posts:
    411
    what GUI?

    I don't see any GUI...

    Cough. cough...

    8)

    Do you like the shader?
    Skinned in maya using good ole' Pelting Tools--so it's relatively clean uv layout.

    Obviously the left bear is without any fur shader.
     
  42. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    There hasn't been any work on this shader in some time, but it appears to still be the only publicly available fur shader for Unity. Where does the lighting limitation stem from? It would be really nice to be able to use Unity's lighting.
     
  43. blackant

    blackant

    Joined:
    Jun 18, 2009
    Posts:
    529
    thanks for your job, this is marvellous !
     
  44. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You'd do other things as well, though, right? Fix global warming and create world peace, maybe? ;-)
     
  45. Caiuse

    Caiuse

    Joined:
    Jan 6, 2010
    Posts:
    30
    Okay i understand this is an old post, sorry for resserecting it, but I'm having a problems with the shader, Im trying to use the shader on a object that is scaled by 50000, but for some reason is does not show at all, is there a way of making the shader scale too so the fur effect actually shows?
     
  46. FreelandCJV

    FreelandCJV

    Joined:
    Nov 12, 2009
    Posts:
    221
    Hey Aras/tamuel (sorry if I spelled your name wrong... :oops: )

    I'm having trouble with your shaders.
    When I download them to my Mac, I get something like this on the web page that opens:

    ‹
     
  47. DaveA

    DaveA

    Joined:
    Apr 15, 2009
    Posts:
    310
    Any chance this can be updated for Unity 3.0? iPhone? Android?
     
  48. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    Bump-ity bump, I would like to see this working for unity 3 too!
     
  49. Esprite

    Esprite

    Joined:
    May 20, 2010
    Posts:
    1
    Another bump, could use this as well :p
     
  50. thellama

    thellama

    Joined:
    Mar 25, 2010
    Posts:
    360
    Aye, a 3.0 version would be awesome.