Home Technical Talk

lcUberShader.cgfx - A Maya Viewport Shader for Fun and Learning

polycounter lvl 10
Offline / Send Message
chronic polycounter lvl 10
He saw what he had created and knew . . . it was a MONSTER - dum dum dum
Goals:
1. Create a useful Maya viewport shader with many functions and techniques to showcase a wide range of surface properties
2. Write a Shader with a compartmentalized structure that is easy to read and dissect for people learning to code.

This shader is very large and complex - and not performance optimized by design to allow for better readability - performance is not necessarily an issue when targeting the Maya viewport anyways.

I really recomend that if you use this shader you use it in conjunction with this MEL script I also wrote: http://boards.polycount.net/showthread.php?t=70032

Download:
lcUberShader_1.2.cgfx
lcUberShader_1.1.cgfx
lcUberShader_1.0.cgfx
lcBasicShaderComponents.cgfx - for learning - a basic stub
BasicTextures.zip - (i dont quite remember where these all came from, but thank you)

Known Issues:
* Although i provide 8-bit alpha support, Maya has transparency sorting issues when in this mode.
* Because there are so many attributes the Attribute Editor can sometimes stall momentarily when updating / opening / switching techniques
* Only has an input for 1 environment cubemap (serves for both Reflection and Ambient) - the shader assumes this is a properly filtered cubemap made with ATI's CubeMapGen Tool

Please use this as inspiration for writing your own shaders, the code is very easy to take apart, rearrange and unfold. I have tried to comment the code in key places of interest. I also included a more basic shader with the same coding structure as a starting point for people interested in learing.

lcUberShader_sphereLineup.jpg
lcUberShader_attributes.jpg

Replies

  • kodde
    Options
    Offline / Send Message
    kodde polycounter lvl 19
    Woaw, did you make this overnight or what? :)

    Seems to be some sweet features. Ill be back with a bunch of questions later. ^^
  • pior
    Options
    Offline / Send Message
    pior grand marshal polycounter
  • killingpeople
    Options
    Offline / Send Message
    killingpeople polycounter lvl 18
    Awesome, thanks!
  • Malus
    Options
    Offline / Send Message
    Malus polycounter lvl 17
    Sweet.

    Had a play and there's seems to be some update issues with the lighting.

    Should I be able to move them around and see the lighting direction change?
  • kodde
    Options
    Offline / Send Message
    kodde polycounter lvl 19
    Malus> Moving an object that defines a direction shouldn't matter? Only if you rotate it.
  • Malus
    Options
    Offline / Send Message
    Malus polycounter lvl 17
    Sorry, rotate. Bad symantics my end. :P

    It seems like its not happening at home so I might have gimped something at work. :)
  • chronic
    Options
    Offline / Send Message
    chronic polycounter lvl 10
    @Kodde
    This is the 4 week culmination of all my shader knowledge - ask all the questions you like, hopefully I'll be able to answer some.

    @Malus
    The shader expects directional lights to be linked into its light slots - i have noticed that cgfx shaders can momentarily lag sometimes when moving/rotating lights. I really recomend you interact with the shader using my lcRTShaderTools.mel script as it automates many things.
  • airbrush
    Options
    Offline / Send Message
    airbrush polycounter lvl 13
    Nice work...thx
  • chronic
    Options
    Offline / Send Message
    chronic polycounter lvl 10
    Additional Note:
    Don't scale your lights or any group your lights are a part of, scale / inherited scale increases the magnitude of the light vector and gives incorrect results.

    normalizing the light vector would fix this issue but I'm not sure its a good idea to do that - if anyone wants to chime in with ideas/comments please feel free.
  • r4ptur3
    Options
    Offline / Send Message
    r4ptur3 polycounter lvl 10
  • funshark
    Options
    Offline / Send Message
    funshark polycounter lvl 16
    Tested on maya2008 and got this :

    // Error: CG ERROR : The compile returned an error. //
    // Error: (879) : warning C7011: implicit cast from "float4" to "float3"
    (662) : warning C7011: implicit cast from "float3" to "float"
    (662) : warning C7011: implicit cast from "float3" to "float"
    (862) : error C5051: profile does not support conditional returns
    (893) : error C5051: profile does not support conditional returns
    //

    Also, I can only choose Toon_Ramp, Lit_Sphere and World_Normals
    Any idea?

    ( WinXP, Quadro 3450 w/ last drivers )
  • chronic
    Options
    Offline / Send Message
    chronic polycounter lvl 10
    @funshark
    for whatever reason your system is being extremely picky with code semantics at compile. Probably something to do with the Quadro and its fancier OpenGL drivers. I can defiantly fix those errors, although its a bit hard to test since no system I have gives those.
    The reason you can still choose some of the Techniques is because those are much simpler and don't use any of the functions returning errors.

    No conditional returns! sheesh how am i supposed to get anything done around here.
  • funshark
    Options
    Offline / Send Message
    funshark polycounter lvl 16
    Quadro has fancy OpenGL drivers? :D heh it supposed to have the best ones nah? eheh

    What about the Cg Core runtime library ( cg.dll ) ?
    I remembered that I have updated it in the past and the cgfx shader I have been testing worked better...

    I will try...

    Actually, my version is 1.50.0605.238
  • chronic
    Options
    Offline / Send Message
    chronic polycounter lvl 10
    @funshark
    after some more thought I'm thinking its probably some slight differences in cgc.exe (Cg Compiler) between Maya versions - I only have 2009 for testing

    Anyway - upadate to v1.1 download link in first post - probably will still give you at least one blocking compile error (crosses fingers)
  • kodde
    Options
    Offline / Send Message
    kodde polycounter lvl 19
    Chronic> Some cool stuff. I really liked the anisotropic specular, gotta look into that. Overall it's inspiring to see other's shaders. Making me even more stoked about venturing into shader writing by hand.

    Oh btw, I just saw the Maya 2011 press release today. Wouldn't it be awesome if they also ovehauled the UI capabilities of CGFX shaders? *crossing fingers*
  • chronic
    Options
    Offline / Send Message
    chronic polycounter lvl 10
    The anisotropic specular is one of my favorite parts, I was really excited when i finally figured out how to get it to react to the normal map.
  • Hazardous
    Options
    Offline / Send Message
    Hazardous polycounter lvl 12
    Dude could this be the Maya answer to xoliul's max shader !?!?! I shall see.... I SHALL SEE RIGHT NOW!!!!!
  • chronic
    Options
    Offline / Send Message
    chronic polycounter lvl 10
    @Hazerdous
    Thats quite a compliment, thanks - Xoliul's shader is really finely tuned (and 3dsmax's shadow support is a killer feature). I see this shader more as a technical exploration and as a learning tool, which is why I've separated all the core shading functions out into compact sections to help other people learn the nuts and bolts.
  • Malus
    Options
    Offline / Send Message
    Malus polycounter lvl 17
    I've been playing around with this at work and home and it's a really nice shader, thanks Chronic. :)
  • chronic
    Options
    Offline / Send Message
    chronic polycounter lvl 10
    @Malus
    Thanks!

    @Kodde
    If Autodesk overhauls the CGFX integration int 2011 to be more stable that would be awesome - if they extended it to allow access to the already existing shared shadow buffer that would be amazing. I have heard of some people re-writing the cgfxShader.mll plug-in for in house use, if i could even just get my hands on that, ohhhh boy.
  • funshark
    Options
    Offline / Send Message
    funshark polycounter lvl 16
    1.1 tested ( in maya 2008 again ) :

    // Error: CG ERROR : Invalid parameter handle. //
    // Error: (132) : error C8001: Unknown state 'MaxAnisotropy'
    (314) : error C8001: Unknown state 'MaxAnisotropy'
    (662) : warning C7011: implicit cast from "float3" to "float"
    (1066) : warning C7011: implicit cast from "float4" to "float3"
    (1067) : warning C7011: implicit cast from "float4" to "float3"
    (1068) : warning C7011: implicit cast from "float4" to "float3"
    //


    Works fine in maya 2009 :o

    Nice shader! ;)
  • funshark
    Options
    Offline / Send Message
    funshark polycounter lvl 16
    Hey it seems I can't use the hemispherical ambient. :o
    About the Toon shader, would it be possible to multiply the diffuse map with the shading ( toon ramp ) ?

    edit : ah! and as you can see, there is a wireframe issue with the toon shader ( maybe something I need to uncheck? )

    vampire_shader.jpg
  • chronic
    Options
    Offline / Send Message
    chronic polycounter lvl 10
    Update 1.2
    Fixed some more typecasting issues
    Toon and LitSphere update - now they multiply by the diffuse texture - you can modulate the amount of diffuse texture by changing the diffuse color value (0-1 B&W only, no tinting). The multiplication operation of the toon ramp and diffuse texture looks best with a greyscale toon ramp with harsh transitions between colors.

    download link in first post

    @funshark
    * I fixed some more of those typecasting errors - the 'MaxAnisotropy' error is unusual - you can comment out those flags on the diffuseMapSampler and normalMapSampler - but they work in other versions of Maya and I won't remove them by default.
    * Ambient Hemispherical Lighting is multiplied by the Ambient Color Attr - make sure its something other than black - the effect can be subtle - its most pronounced when the sky color is very bright and the ground color is very dark or black.
    * I can't reproduce the 'Wireframe' issue you seem to be having so i don't know what to change to fix it. - do you have Anti-Aliasing forced to ON in your driver settings? Thats my best guess.
  • andorahms
    Options
    Offline / Send Message
    OMG!!

    Pass that Chronic!!

    This Shader makes my D H!!
  • funshark
    Options
    Offline / Send Message
    funshark polycounter lvl 16
    You're fast ;)

    Everything is nice now !
    The wireframe issue seems to be something related to Maya... I didn't reproduce this time.

    Good job!
  • rob_beddall
    Options
    Offline / Send Message
    rob_beddall polycounter lvl 18
    Hey,

    I have downloaded the shader but I can't get it to work at all and I'm in need of a little help.
    Firstly, I am using Maya 2011. win xp. ATI card. (dunno if that makes a difference)
    I can create a cgfx shader, and point it to the cgfx file. but anything I do after that has no effect it seems. I tell it I want to use a diffuse texture. and then point it to a .tga file but nothing happens, the shader just stays the default pink colour. I have tried linking lights but still nothing. I am in hardware textured mode. Is there something I am missing here?
    I haven't had much luck so far with cgfx shaders, so any help would really be appreciated as this shader looks like it could be ace :)

    cheers
  • rob_beddall
    Options
    Offline / Send Message
    rob_beddall polycounter lvl 18
    ...forgot to mention. All the techniques are greyed out... so I'm guessing something has gone wrong somewhere... :(
  • kodde
    Options
    Offline / Send Message
    kodde polycounter lvl 19
    I'm seeing a common factor here when it comes to Maya CGFX problems... ATI cards.
  • rob_beddall
    Options
    Offline / Send Message
    rob_beddall polycounter lvl 18
    oooh... that could be a problem then. I have had cgfx shaders working at work, but there we have Nvidia cards. :( I have also tried your shader Kodde and the same happens with that. both techniques are greyed out and nothing I do has any effect on the default pink shader.

    I'll try the shader out at work tomorrow to find out if it is the card.
  • chronic
    Options
    Offline / Send Message
    chronic polycounter lvl 10
    if techniques are greyed out, then its probably the card. The shader is really heavy so it might max out the instruction limits on some older or low spec cards. Greyed out is usualy an indicator that 'this technique did not compile successfully'.

    I have noticed some strangeness in maya 2011 with the cgfx plugin, havent had much time to experiment - hopefully we are not moving backwards in terms of support.
  • rob_beddall
    Options
    Offline / Send Message
    rob_beddall polycounter lvl 18
    I tried the shader at work and it now works, So I guess ATI cards really don't like cgfx shaders.
    I think my card would be good enough to process the shader. But even with the Nvidia card there still seems to be some error. No matter which technique you select, it keeps going back to the bottom technique. That's in Maya 2011.

    cheers
  • Malus
    Options
    Offline / Send Message
    Malus polycounter lvl 17
    I've noticed a strange bug in 2009 and 2010.
    If you change the name of the shader after linking to the cgfx file a lot of the options don't work, for example tick boxes don't tick.

    Rename it back to the original and it all works again.

    Change the name before linking ot the cgfx file and its all fine.

    Happened on a couple of PC's at work (win7, Nvidia cards)
  • chronic
    Options
    Offline / Send Message
    chronic polycounter lvl 10
    i have noticed problems chaning the name of cgfx shaders too, seems to happen less often if i deselect everything and select and rename the shader through the hypershade.

    maya 2011 seems to have a problem with the interface radio buttons and selecting techniques, this is probably related to the QT ui switch and/or the cgfx shader's AE template scripts - my work around has been to add a dummy technique to the end of the list, that way you can switch between all techniques even if the last one always appears selected.
  • bugworld
    Options
    Offline / Send Message
    ATI card no luck here +1.(x1400, its old I know...)
    The shader looks really good, but I have to stick to your HLSL shader for the moment. And I'm having trouble getting that one's environment reflection to work, dont know if its me or not but I can't seem to find any other related option. Basically theres no environment reflection, even after cubemaps applied and option checked.
    Everything else works really well tho. Thanks again.
  • haiddasalami
    Options
    Offline / Send Message
    haiddasalami polycounter lvl 14
    Just wanted to let you know but the cgfx shader worked for me and I was running ATi 3870. Use your shader as a reference with Ben's awesome DVD's :)
  • chronic
    Options
    Offline / Send Message
    chronic polycounter lvl 10
    For the interested - I've been having a ton of fun experimenting with new shaders after reading John Hable's Siggraph 2010 presentation on Uncharted 2 - I have a skin shader and a hair shader using techniques touched on in the presentation. check them out.

    FYI - they might be a little raw - code is fresh off the grill and not thoroughly tested.

    hairShader.jpg
    ssscompare.jpg
  • arshlevon
    Options
    Offline / Send Message
    arshlevon polycounter lvl 18
    These look really nice!
    also had a problem getting them to work on an ati card, will try on my laptop when i get a chance.
  • haiddasalami
    Options
    Offline / Send Message
    haiddasalami polycounter lvl 14
    Nice work Chronic...gonna have to dig through that. Understood most of it but probably gonna have to read through it again. Was interested in that valve flowing water presentation...
  • Ballo
    Options
    Offline / Send Message
    Ballo polycounter lvl 9
    Really cool, thanks, I'm gonna test it tonight at home.
  • CopperHaze
    Options
    Offline / Send Message
    Yo Chronic! I tried your shader last night and it is the BOMB son. Nice SSS! It that dank ass shhhhh Thanks Yo
  • andorahms
  • chronic
    Options
    Offline / Send Message
    chronic polycounter lvl 10
    All my shaders got an update, hopefully I wont have to do that again for a while.

    Blog Post: http://blog.leocov.com/2010/09/maya-realtime-shaders-big-update.html
    Download Link: http://blog.leocov.com/p/downloads.html

    shaderCompare_A.jpg
    hairShaderComparison.jpg
    litSphereImage.jpg

    Have Fun!
  • kodde
    Options
    Offline / Send Message
    kodde polycounter lvl 19
    Nice Leo!
    Liking the results of the hair shader a lot. Also had a look at gamma correcting the shaders I'm working with. Gonna look into tone mapping as well.
  • CheeseOnToast
    Options
    Offline / Send Message
    CheeseOnToast greentooth
    Lovely hair shader, but I'm having a problem with the skin shader (Maya 2009, Win XP32, Geforce 9600GT). The SSS edge colour and radius are missing for me. Any ideas?
  • chronic
    Options
    Offline / Send Message
    chronic polycounter lvl 10
    I took those 2 things out in the latest version of the skin shader - its gone through alot of changes so older posts on my blog are not necessarily reliable information. - in the latest version you cant change the color of the 'sss effect' - the coloring blue/red is a side effect of the blended normal technique im using.
  • CheeseOnToast
    Options
    Offline / Send Message
    CheeseOnToast greentooth
    Thanks for clearing that up. It was looking good anyway, I just wondered where those extra controls had got to.
  • mton1109
    Options
    Offline / Send Message
    Thanks for sharing the shader. I used your stubs to implement a toon shader based on the recent Guilty Gear Xrd GDC talk. Check it out at:

    uploads%2F2015-06-12%2F23%2F1984%2FFJpOHw_t5sLrlWeG-yuec0rbHyk%2Fs3ul29%2FToon_xrd_Documentation.jpg

    http://miketon.posthaven.com/tag/shaders

    It's available on github at :

    https://github.com/miketon/Maya_Tools/tree/master/Shaders/Toon_xrd

    I really appreciate the dirLight hookup; previously when I was building cgfx shaders years ago, I used to grab it directly from the openGL context...which wasn't great form :) .

    -Mike
Sign In or Register to comment.