[SOLVED] Disable depthTest and depthWrite on entire mesh (Fix gun clipping)

Hi,
I’ve made a simple gun which clips through the wall.


So in order to fix it I created a new material, disabled depthTest and depthWrite, assigned the material to the meshInstances of the gun, created a new layer and moved the gun to that layer which partly fixed the issue. It no longer clips through the walls.

But now the problem is that the gun is made up of multiple materials and the rendering order of these materials is messed up now.

How the gun should look:
IMG_20210913_160408
How it looks after fixing the clipping:
IMG_20210913_160520

I couldn’t figure out a way to fix this issue. What would be the best way to solve this?

Thanks!

Hi @Gamer_Wael,

Instead of messing with depth test/write, another way is to create a new layer and assign only your gun to it.

Order the layer to come after your World, and any other object layer you may have. And at the end add it to your active camera.

This way it will render on top of everything.

1 Like

Hey @Leonidas thanks for the quick reply!

I tried to implement your suggestion, but the behaviour of the gun doesn’t change, is there something wrong with the way I did it?

var weaponLayer = new pc.Layer("weaponLayer");
app.scene.layers.insert(weaponLayer, 10);		
Gun.model.layers = [weaponLayer.id];

var layers = Camera.camera.layers;
layers.push(weaponLayer.id);
Camera.camera.layers = layers;

use weaponLayer.clearDepthBuffer = true;

see Layer | PlayCanvas API Reference

3 Likes

I’m not sure I understand how to use that. By adding that like to the above snippet, It still doesn’t change anything.

this is what the rendering should look like with this:

  1. renders the world - this writes world z values to depth buffer
  2. starts rendering your weapon later … clear existing depth buffer
  3. render the weapon mesh, and it should not be clipped against the world as the depth buffer is empty

to check what it does, try settings this to true anywhere in a script at start up
app.scene.layers.logRenderActions = true;
this will log the order of rendering into browser console.

and then check the results - it should render in the order as I described and clear depth buffer. Post the section of the log.

2 Likes

Or you can use the two camera method: Keep object from going through the floor

2 Likes

It works now! Thanks for the explanation.

I updated my local version of the engine and then it started working.

1 Like