Jump to content

Blend modes


fenomas
 Share

Recommended Posts

Hi fenomas,

 

You can have a mesh displayed additively with this:

mesh.registerBeforeRender(function () { engine.setAlphaMode(BABYLON.Engine.ALPHA_ADD); } );

This is the only way to do it currently. Using a ShaderMaterial won't help you changing the blend mode, since you won't have access to the background color in the fragment shader.

Link to comment
Share on other sites

Ah, thanks! Works like a charm.. makes you wonder if it shouldn't just be an option on material.

 

Incidentally  it looks like I also have to add a registerAfterRender to set the mode back again, right? If I don't other transparent objects in my scene seem to be affected. Setting it back to ALPHA_COMBINE seems to fix this, though I'm not sure if that's bulletproof.

Link to comment
Share on other sites

Yeah... this is not handled incredibly well by the engine. You could also remove the registered function (don't know how, though). I think the setting is reset to ALPHA_COMBINE at the beginning of the render loop.

 

A setting on the mesh would be easy to add for sure! Not sure what DK had in mind for this though. Also, it could be nice to have more choices than just COMBINE and ADD. This is a summary of what's possible using blend modes: http://i.imgur.com/yMX2wZJ.jpg

Link to comment
Share on other sites

Done!

 

https://github.com/BabylonJS/Babylon.js/commit/a6103f47cdcb8d561c6070120157a046d7cb3c76:

* Added material.alphaMode (can be set to BABYLON.Engine.ALPHA_ADD or  BABYLON.Engine.ALPHA_COMBINE)

 

If you want to add more mode, feel free to submit a PR for this:

https://github.com/BabylonJS/Babylon.js/blob/master/src/babylon.engine.ts#L1432

Link to comment
Share on other sites

Haha, glorious! Popped this into my scene and it works like a charm.

 

It would also be great if someone wants to add more blend modes. Personally I don't know enough to know which ones would be useful.

 

 

Follow up question: for a mesh with vertex colors and an ambient texture, rendered in ALPHA_ADD, it seems that vertex colors are used but vertex alpha is not. Vertex alpha seems to work normally with ALPHA_COMBINE so I'd thought it would work with ADD too. Is this unavoidable, or fixable, or am I missing something?

Link to comment
Share on other sites

ADD will add (!!) vertex color and destination color

 

No need for snark. :(  Firstly, to be precise ADD presumably doesn't add the vertex color, but rather some complicated combination of vertex color, emissive texture color, texture alpha, other material colors, etc, etc. I had supposed that vertex alpha would fit into that combination somewhere, before the blend mode occurs, and then "material.alpha" would be used for the final blend operation. I guess that's not the case, maybe it's the other way around?

 

Either way, in other contexts (photoshop, svg, etc), blend modes normally operate on alpha-multiplied color values. So if vertex alpha is the alpha used for the COMBINE blend operation, it would probably make sense to premultiply it before the ADD blend operation. If that's possible, I mean.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...