fenomas Posted July 13, 2015 Share Posted July 13, 2015 Hi, Possibly dumb question, but is it possible to change the blend mode for a given mesh or material? Or would this require a custom material/shader? Are there samples/tutorials I should check? Quote Link to comment Share on other sites More sharing options...
jahow Posted July 13, 2015 Share Posted July 13, 2015 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. fenomas 1 Quote Link to comment Share on other sites More sharing options...
fenomas Posted July 13, 2015 Author Share Posted July 13, 2015 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. Quote Link to comment Share on other sites More sharing options...
jahow Posted July 13, 2015 Share Posted July 13, 2015 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 fenomas 1 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted July 14, 2015 Share Posted July 14, 2015 I agree this should be an option on the material. And it would be pretty easy to do (just need to wrap webgl constants into babylonjs constants). I can add it to my todo list Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted July 14, 2015 Share Posted July 14, 2015 https://github.com/BabylonJS/Babylon.js/issues/606 jahow 1 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted July 14, 2015 Share Posted July 14, 2015 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 fenomas and pathogen 2 Quote Link to comment Share on other sites More sharing options...
jahow Posted July 14, 2015 Share Posted July 14, 2015 Great, thanks! Quote Link to comment Share on other sites More sharing options...
fenomas Posted July 15, 2015 Author Share Posted July 15, 2015 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? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted July 15, 2015 Share Posted July 15, 2015 ADD will add (!!) vertex color and destination colorCOMBINE will use vertex alpha to blend vertex color with destination Quote Link to comment Share on other sites More sharing options...
fenomas Posted July 17, 2015 Author Share Posted July 17, 2015 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. Quote Link to comment Share on other sites More sharing options...
jahow Posted July 17, 2015 Share Posted July 17, 2015 There's a little fix to add in the code for that, I'll add it since I'm working on new blend modes. fenomas 1 Quote Link to comment Share on other sites More sharing options...
jahow Posted July 17, 2015 Share Posted July 17, 2015 Done! https://github.com/BabylonJS/Babylon.js/pull/619 ADD is now modulated by the source alpha value. jerome and fenomas 2 Quote Link to comment Share on other sites More sharing options...
fenomas Posted July 17, 2015 Author Share Posted July 17, 2015 Wowza! That's crazy fast. I will try these out (I'm using them on mesh-based particle systems) if/once they're merged. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted July 17, 2015 Share Posted July 17, 2015 PR merged. To save backward compat I also added a ONEONE mode Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.