Home Technical Talk

Transparency with ColladaMax and CgFX?

I'm trying to figure out how to add a transparency control to a CgFX shader. Ultimately it's for use with the ColladaMax exporter.

What I'd like to do is wire up the alpha of a bitmap to control the transparency of my mesh surface. But I can't figure out how to do it.

I'm using mental mill... maybe that's the problem? There don't seem to be any transparency nodes, except for compositors, but I don't want to make the surface blend with another input, I want it to blend with the rest of the scene.

Help!

Replies

  • Eric Chadwick
    Ah, found the answer.
    http://forum.mentalimages.com/showthread.php?t=160

    Nutshell... you can set it up in mental mill but you can't see it, have to edit the file after export.
  • Slum
    Offline / Send Message
    Slum polycounter lvl 18
    Haha, wow, I actually found this topic from google, searching for HLSL transparency in mental mill..

    Small world.
  • Eric Chadwick
    Heh.

    So I guess you're using Maya then? Still having some trouble with getting mental mill CgFX files to load up in the ColladaEffect material in Max, getting only errors so far.
  • ElysiumGX
    Offline / Send Message
    ElysiumGX polycounter lvl 18
    That thread includes the fix for HLSL as well. I've sorta been looking for that. Thanks.
  • Ben Cloward
    Offline / Send Message
    Ben Cloward polycounter lvl 18
    I might be able to help you, Eric. Can you post the errors and/or the shader that you're using? If you don't feel like posting it publicly you can PM me or email me.
  • Eric Chadwick
    Thanks Ben. Well I've switched to the demo of Shader FX now, much easier to use with Max than mental mill I must say. Seems pretty solid, you guys did a nice job with it.

    Still poring through the help files. Is there a way to set the transparency blending mode? The cellphone hardware we're using for this project supports Lerp, Multiply, or Additive for transparency. Would I create a radio-button list (or dropdown) of the three methods, so one could be chosen? Or should I instead make three different shaders?

    The shaders we need for this project are for fixed-function hardware, pretty simple. So the FX files are really just WYSIWYG for the artists, ultimately they'll be converted to hardware-specific code for runtime.

    An example...

    Color = DiffuseBitmap.rgb * Light.rgb * Tint.rgb,
    Opacity = DiffuseBitmap.alpha * Light.alpha * Tint.alpha,
    Then [lerp/mult/add] with behind.

    I have the first two lines figured out, just not the latter.
  • CrazyButcher
    Offline / Send Message
    CrazyButcher polycounter lvl 20
    inside the "pass" definition for max,
    <font class="small">Code:</font><hr /><pre>
    // lerp
    AlphaBlendEnable = TRUE;
    DestBlend = InvSrcAlpha;
    SrcBlend = SrcAlpha;

    // add
    AlphaBlendEnable = TRUE;
    DestBlend = One;
    SrcBlend = One;

    // mul
    AlphaBlendEnable = TRUE;
    DestBlend = Zero;
    SrcBlend = DestColor;
    </pre><hr />

    with DestBlend and SrcBlend you define factors for the "blending" equation, which normally is:

    Output = (DestBlend * DestColor) + (SrcBlend * SrcColor)

    SrcXXX = Source pixel = pixel processing output
    DestXXX = Destination pixel = pixel that is "overwritten"

    DestBlend/SrcBlend can be:

    InvAlpha = (1-Alpha)
    Alpha
    InvColor = 1-Color
    Color
    One
    Zero

    ---
    Sometimes hardware supports other "blending" equations, like subtractive and "min/max" blends, but mostly only this simple additive base formula exists. Be aware that blending is after pixel processing, ie you cannot access pixels you overwrite in pixelshader or whatever. Workaround for this limitation involves rendering to texture. But yeah not wanting to take this too much off-topic hehe.
  • Ben Cloward
    Offline / Send Message
    Ben Cloward polycounter lvl 18
    Ah - cool to hear that ShaderFX is helpful!

    You can set the blend mode by selecting the standard material node and then choosing the blend mode you want from the drop-down box on the right that's labeled "Blend Mode."

    If you're making a custom FX shader for your artists to use, I'd recommend using three separate techniques - one for each blend mode you want to support. That will give you a drop-down box in the max material editor (it's at that bottom of the panel) that will allow them to choose.

    You can create a shader with three or more techniques by using the Techniques node. Here's a demo movie on how that works:

    http://www.lumonix.net/ShaderFX_technique.wmv

    In FX files, the transparency blending is specified in the render states toward the bottom of the shader - not in the pixel shader code. Try creating a simple ShaderFX shader with different blend modes set and take a look at the changes it makes to the render state code to see what I mean.
  • Eric Chadwick
    Hey that's pretty cool. Not needed for this project but maybe later, how would I go about adding different Blend Modes, for example an Overlay or a Darken or a Subtract? I guess I'd write a Custom Code node?
  • Ben Cloward
    Offline / Send Message
    Ben Cloward polycounter lvl 18
    This depends on the type of blending that you're trying to get. If you're blending the current object with other objects in the scene, you have to define those in the render states of the shader. Currently, the custom code node supports adding code the the header, the vertex shader, the pixel shader, and the input and output structs, but not the render states - so in that case, you'd need to go in and hand-edit the FX file after you exported it.

    If you're doing blending within the shader itself - blending one texture with another, etc, you can use the color blend node. This node allows you to combine two inputs using any of the blend modes available in Photoshop.
  • Eric Chadwick
    OK, thanks. We're using blend modes to combine individual ingredients within a surface, as well as to blend the resulting surface with whatever's behind it.

    For example we have one that optionally inverts the dest, multiplies it by an RGB value, multiplies the src with another RGB, then blends the dest and src using one of a list of modes (add, lerp, overlay, etc.).
  • CrazyButcher
    Offline / Send Message
    CrazyButcher polycounter lvl 20
    overlay (< 127 darken, > 127 brighten) would be

    // add
    AlphaBlendEnable = TRUE;
    DestBlend = SrcColor;
    SrcBlend = DestColor;

    which is the same as
    SrcColor * DestColor *2
  • Eric Chadwick
    Thanks for all the code Christoph, much appreciated!
Sign In or Register to comment.