Pre-viewport + cam.lookAt removing depth

I must be doing something stupid with this but:

I create a PreView
I attach a scene

On the rootNode is a spatial whose material has
setColorWrite(false);

(The idea being that it will be invisible but still hide objects behind it - works as expected)

In the simpleupdate, I update my game stuff, I look at the player, and finally update the stuff I attached to the PreView, since it’s not attached to the root node.

When I looked at the player, it looks at his feet, so I figured I’d raise the Y value…

lookAt.x = player.player.getWorldTranslation().x;
lookAt.y = player.player.getWorldTranslation().y+5;
lookAt.z = player.player.getWorldTranslation().z;
cam.lookAt(lookAt, Vector3f.UNIT_Y);

When I do that +5 bit, depth no longer works
(shown below, +5 on the left, and without the +5 on the right)

The bottom picture has it working though. I added an int dl = 0;
Then in the update after setting the lookAt to the players values…

lookAt.y+=dl;
if(dl<5)dl++;
cam.lookAt(lookAt, Vector3f.UNIT_Y);

So it increases by 1 per update until it gets to 5.

If I went back and did

lookAt.y = player.player.getWorldTranslation().y+1;

It again does not work and depth is gone. Just asking out of curiosity really since that horrible dl bit does fix it, despite my hatred of it - why does me changing the value directly cause depth to stop working?

Seems like a rendering order issue… like they get sorted different at different angles or something. That implies that the player and/or the couch are not writing depth or something.

This is an interesting question…It’s unlike to change the lookAt and rendering order change that bit.

Having played with it more, the general rule seems to be:

I can change lookAt.y to anything I like only if simpleUpdate has run once already. If I change it before 1 initial update then the depth goes, after 1 update has been I can change it to any value and it works as expected.

Well, there is no logical reason for that to be true… so it’s either something in your code or a random runtime thing (like sort order) that happens to trigger magically in one case but not another (similar to the way hashmaps have a different order under some circumstances versus others).

Edit: either way, it should be fixable. One case indicates there’s some ordering error in your code somewhere and the other indicates that it’s actually an issue in scene setup/assumptions that will crop up randomly later if not fixed. I don’t really know enough about what I’m seeing in that scene to comment further.

1 Like

I agree, and it’s not that bad but I’m thinking spaghetti code is a problem since I’ve changed my project so many times. I’m creating a simple test case type thing for the depth since making test cases normally solves my problems.

I remember you and nehon helped me with this before, but I need to ask-

depth = assetManager.loadModel("Models/testboat/2.j3o");
Material mat1 = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
mat1.getAdditionalRenderState().setColorWrite(false);
mat1.getAdditionalRenderState().setDepthTest(true);
mat1.getAdditionalRenderState().setDepthWrite(true);
depth.setMaterial(mat1);
rootNode.attachChild(depth);

Should this not create a model that is invisible but hides those behind it? This is the technique I used before but currently it does not do that.

Yeah, it should.

: / Not working

I created a Box geometry, copy pasted right out of the wiki beginner tutorial and moved it over that object with the code above, didn’t work. Without the ColorWrite(false) line it does exactly what you’d expect, depth is uniformly white and hides the box when I go behind it. With ColorWrite(false) it just doesn’t hide it. I’m totally confused by how I have this working in my broken spaghetti case.

The invisible object has to be rendered first so that subsequent objects can see its’ depth… Consider using a pre-viewport here.

ViewPort vp = renderManager.createPreView("background", cam);
vp.setClearFlags(true, false, true);
vp.attachScene(depth);

And in update

depth.updateLogicalState(tpf);
depth.updateGeometricState();

Is this how you would do it? Still not working as it is

The main viewport is still clearing the depth afterwards, you want to make sure you disable that.

viewPort.setClearFlags(true, false, true);

This just gives me complete black screen, the blue box no longer being drawn (I have made sure the camera is facing it, and have tried moving backwards/forwards) :frowning:

If the main viewport isn’t clearing depth, then the pre-viewport needs to.

1 Like

:chimpanzee_facepalm:

Thank you!

As usual you guys have been ludicrously helpful.

One thing that has cropped up, and is not a problem but just found it interesting:
Again the cam.lookAt() is doing strange things. If I set it to look at the blue cube by default, I get a different on-screen result to that if I set it to look at via a button,

e.g. if(lookAtBoolean)cam.lookAt(…)
with lookAtBoolean triggered by a key press.

I must be going mad. Still it does not matter the programs runs perfectly now, thanks again.

You would have to be more specific… as specific as possible… if we were ever to hope helping.

Note: it still could be a render order issue but we’d have to know how your scene is constructed, etc…

Note2: you could have gotten away without a preview port if you’d carefully controlled your rendering order for example.