Olgo Posted April 10, 2015 Share Posted April 10, 2015 Hello everybodies, I'm a (french) new member of BJS community (so sorry for my English) . One of my projects requires a custom shader that allow me to use an object as mask, ie every rendering behind my object must be discarded (but my object must stay invisible).To do so, my idea is to disable the color buffer in the fragment shader with a function like glDrawBuffer(GL_NONE but maintain it in the depth buffer with a function like gl_FragDepth = gl_FragCoord.z I have two problems with this technique : - Writing the depth buffer directly in a fragment shader is extremely not recommended- This functions aren't compatible with webgl (which use opengl es 2.0, i guess) Is there a better / simplest way to do this trick ? (I'm a newbie in shader programming) hopefully,Loïc. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted April 11, 2015 Share Posted April 11, 2015 Hey Loic, why not just keep using the default material on your object and disable color write on the beforeRender and reset it after? Something like:mesh.registerBeforeRender(function(){ engine.setColorWrite(false);});mesh.registerAfterRender(function(){ engine.setColorWrite(true); }); Quote Link to comment Share on other sites More sharing options...
Olgo Posted April 11, 2015 Author Share Posted April 11, 2015 It work ! This is exactly what i need, thank you Deltakosh. So, this couple of functions allow us to define several actions before and after the render of the mesh, is that ? Quote Link to comment Share on other sites More sharing options...
Temechon Posted April 11, 2015 Share Posted April 11, 2015 It's exactly that. You can find the same methods on the scene object, if you want to iterate on your meshes. Quote Link to comment Share on other sites More sharing options...
Olgo Posted April 11, 2015 Author Share Posted April 11, 2015 Ok, i have a new problem. Deltakosh, when i test you're solution in the playground it works fine, but in my project i'm it don't. In fact, the "mask object" need to be rendered first to work. This playground illustrate it : http://www.babylonjs-playground.com/#134UKN If you define "sphere" as mask object it works, however if you define "sphere2" it don't. How can i choose the render order ? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted April 13, 2015 Share Posted April 13, 2015 you can play with mesh.renderingGroupId Quote Link to comment Share on other sites More sharing options...
Olgo Posted April 13, 2015 Author Share Posted April 13, 2015 Sorry, it doesn't seems to be what i need. mesh.renderingGroupId control the objects order into depth buffer, but not depending on their 3D positions. I made a new playground to have a better example of my problem :http://www.babylonjs-playground.com/#134UKN#3 In this example, we see that the mask (because of the creation order) is ok at some viewing angles and not at others. But if we create the mask object before every other objects, all is ok. In my project case, the objects are provided by a blender export in .babylonjs, so i can't control the creation order. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted April 13, 2015 Share Posted April 13, 2015 So this should be mesh.zOffset Quote Link to comment Share on other sites More sharing options...
Olgo Posted April 14, 2015 Author Share Posted April 14, 2015 Yeah maybe I don't see this class in BJS documentation, and it don't work in the playground. How it works ? Quote Link to comment Share on other sites More sharing options...
Vousk-prod. Posted April 14, 2015 Share Posted April 14, 2015 There is a zOffset property in material class. Related to depthCulling. But modifiyng the values doesn't (apparently ) change anything...http://www.babylonjs-playground.com/#134UKN#4 What this param is for ? and how to use it ? Quote Link to comment Share on other sites More sharing options...
jahow Posted April 14, 2015 Share Posted April 14, 2015 zOffset is the implementation in BabylonJS of a WebGL function called "Polygon Offset". Basically it's useful when you need to render polygons that are at the exact same place, but you still need some of them to be rendered above the others (typically for decals & stuff like that). Interesting thread about it. I don't think this will really help you in that case though... Now I tried to put the mask mesh manually in the first place in the meshes array, and it works: http://www.babylonjs-playground.com/#134UKN#6I guess you could do that after having imported your meshes, by using the mesh id. It's not a really "clean" way to do it, but without reordering meshes I think you'll have a hard time achieving what you're looking for. zOffset and Rendering Groups won't help you, and since your mask mesh will not have any pixel rendered on screen, anything that would have been drawn before behind it would need to be "erased". Quote Link to comment Share on other sites More sharing options...
Olgo Posted April 15, 2015 Author Share Posted April 15, 2015 Problem solved ! Thank you Jahow, this solution is the right one. Thank you guys, you saved me a lot of time. Quote Link to comment Share on other sites More sharing options...
jessepmason Posted October 2, 2015 Share Posted October 2, 2015 I have been playing with this: mesh.registerBeforeRender(function(){engine.setColorWrite(false);});mesh.registerAfterRender(function(){engine.setColorWrite(true); }); but is there by chance a way to toggle back on and off the mesh during rendertime? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted October 2, 2015 Share Posted October 2, 2015 Hello, what do you mean by back on and off? 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.