Disabling depth write hides object

I’m trying to draw 2 objects, the first with depth writing on, and the 2nd with depth writing off in JME3, but it seems that the objects are being drawn out of order or something.



The following code demonstrates my problem with two cubes, the red one in behind and the green one in front, but with depth writing disabled. The green shape shouldn’t be blocked from drawing by the red shape.

[java]package mygame;



import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.renderer.RenderManager;

import com.jme3.scene.Geometry;

import com.jme3.scene.Spatial;

import com.jme3.scene.shape.Box;



/**

  • test
  • @author normenhansen

    */

    public class Main extends SimpleApplication {



    public static void main(String[] args) {

    Main app = new Main();

    app.setShowSettings(false);

    app.start();

    }



    @Override

    public void simpleInitApp() {

    Box box = new Box(Vector3f.ZERO, 2f,2f,0.5f);

    Spatial a = new Geometry("BigBoxBox", box );

    Material c1 = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");

    c1.setColor("Color", ColorRGBA.Red);

    a.move(0.0f,0.0f,-1.2f);

    a.setMaterial(c1);

    rootNode.attachChild(a);



    //and a smaller green one

    Spatial b = new Geometry("SmallBox", box );

    b.scale(1.0f,3.0f,1.0f);

    Material c2 = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");

    c2.getAdditionalRenderState().setDepthWrite(false);

    c2.setColor("Color", ColorRGBA.Green);

    b.move(0.0f,0.0f,0.5f);

    b.scale(0.5f);

    b.setMaterial(c2);

    rootNode.attachChild(b);

    }



    @Override

    public void simpleUpdate(float tpf) {

    //TODO: add update code

    }



    @Override

    public void simpleRender(RenderManager rm) {

    //TODO: add render code

    }

    }

    [/java]





    Is spatial a being drawn after spatial b somehow? Is there any way to force ordering?

As I recall from when I looked at that code, the default for the opaque bucket is to sort from front to back. I could check in a way to change this sorting as I have uncommitted code to change the way the transparent bucket is sorted… but it may not help you in the long run unless you come up with a strategy to sort based on something pretty specific to your use-case. I guess it could work.



What is it that you are ultimately trying to acheive? Just wondering…

Set the queue bucket of the 2nd object to transparent, this will make it draw last (after all non-transparent objects and also after all objects that are behind it)

I’m actually working on a custom processor that I want to draw shapes on top of the scene, without writing to the depth buffer.



This was my attempt to narrow it down to a simple demonstration of my problem, but unfortunately it appears that it is indeed just the front-back sorting causing the issues in this case (the problem went away in my example when I swapped to the transparent bucket).



In the code I’m actually having issues with, I’m drawing additional shapes in the postFrame SceneProcessor callback with forced RenderStates, using renderManager.renderGeometry(). As far as I can tell, this should just draw such objects in the order that I call renderGeometry(), however as soon as I disable depth writing, the other objects appear in front (ones that are presumable drawn through the normal opaque bucket rootNode rendering).



Much digging later:

It turns out that drawing in postQueue isn’t what I wanted :stuck_out_tongue: I’m not drawing in postFrame and things are working as I had expected.