Graphic issue with showing Cubes on Grid

I am so sorry, but can anybody explain to me please, what’s happening here?
I have seriously no clue what could cause this… The cubes are supposed to be on the grid. And well… They should have clean lines and not melt into each other like this…

if you want me to guess:

  • you did something wrong in Your code

if you want me to answer:

  • please provide TestCase or at least code we can see.

set the material to wireframe to check if it’s your mesh causing the issue.

material.getAdditionalRenderState().setWireframe(true);
1 Like

No, it’s not the fault of the mesh. I already tried that… Thanks though for the suggestion!

Then it’s probably z-fighting. The faces of each mesh are occupying the same space. It doesn’t know which one to show so it’s just showing whichever one is closest according to the inaccuracy of some float math somewhere along the line.

2 Likes

I feel like an idiot… I’ve never heard of z-fighting before! But thanks for giving me a new lead! I seriously didn’t know what else I could google for, but now I can research stuff again!

2 Likes

Okay, I’m at my wit’s end!
I tried to do different things, but they are not working.

I know this is a really ugly hard-coded example but it visualizes my problem pretty good. Maybe somebody knows whats happening here.


This is what it looks like for me!

Code:

import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingBox;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.debug.Grid;
import com.jme3.scene.debug.WireBox;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Line;
import com.jme3.system.AppSettings;
import com.jmonkey.engine.extensions.FocusCameraState;

public class Testprojekt extends SimpleApplication {

    public static void main(final String[] args) {

        Testprojekt app = new Testprojekt();
        AppSettings settings = new AppSettings(true);
        settings.setTitle("3D Visualization"); //$NON-NLS-1$
        app.setSettings(settings);
        app.setShowSettings(false);
        settings.setResolution(1280, 720);
        app.start();
    }

    private Line x_axis_line;

    private Line y_axis_line;

    private Line z_axis_line;

    private Geometry g_x;

    private Geometry g_y;

    private Geometry g_z;

    private Material m_x;

    private Material m_y;

    private Material m_z;

    private FocusCameraState focusCameraState;

    private Node cs_node;

    private Node pivotNode;

    @SuppressWarnings("deprecation")
    @Override
    public void simpleInitApp() {

        // Background
        getViewPort().setBackgroundColor(ColorRGBA.LightGray);

        // Mousesetting
        inputManager.setCursorVisible(true);

        // Grid
        int min = 500;
        int max = 500;
        Grid g = new Grid(min, max, 500);
        Geometry grid = new Geometry("Ground", g);//$NON-NLS-1$
        grid.setLocalTranslation(new Vector3f(-min * 250, 0, -max * 250));
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");//$NON-NLS-1$
        mat.setColor("Color", ColorRGBA.Black);//$NON-NLS-1$
        grid.setMaterial(mat);

        // Coordinate system
        cs_node = new Node("coordinate_system");//$NON-NLS-1$

        x_axis_line = new Line(new Vector3f(-1000, 0, 0), new Vector3f(+1000, 0, 0));
        x_axis_line.setLineWidth(3f);
        y_axis_line = new Line(new Vector3f(0, -1000, 0), new Vector3f(0, +1000, 0));
        y_axis_line.setLineWidth(3f);
        z_axis_line = new Line(new Vector3f(0, 0, -1000), new Vector3f(0, 0, +1000));
        z_axis_line.setLineWidth(3f);

        g_x = new Geometry("x_axis", x_axis_line);//$NON-NLS-1$
        g_y = new Geometry("y_axis", y_axis_line);//$NON-NLS-1$
        g_z = new Geometry("z_axis", z_axis_line);//$NON-NLS-1$

        m_x = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");//$NON-NLS-1$
        m_x.setColor("Color", ColorRGBA.Black);//$NON-NLS-1$
        m_y = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");//$NON-NLS-1$
        m_y.setColor("Color", ColorRGBA.Black);//$NON-NLS-1$
        m_z = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");//$NON-NLS-1$
        m_z.setColor("Color", ColorRGBA.Blue);//$NON-NLS-1$

        g_x.setMaterial(m_x);
        g_y.setMaterial(m_y);
        g_z.setMaterial(m_z);

        cs_node.attachChild(g_x);
        cs_node.attachChild(g_y);
        cs_node.attachChild(g_z);

        // Geometries

        Box Ruby = new Box(2000, 500, 700);
        Geometry red = new Geometry("Box", Ruby); //$NON-NLS-1$
        Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); //$NON-NLS-1$
        mat1.setColor("Color", ColorRGBA.Red); //$NON-NLS-1$
        red.setMaterial(mat1);
        red.setLocalTranslation(0, 0, 700f);

        Box Sapphire = new Box(2000, 500, 700);
        Geometry blue = new Geometry("Box", Sapphire); //$NON-NLS-1$
        Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); //$NON-NLS-1$
        mat2.setColor("Color", ColorRGBA.Blue); //$NON-NLS-1$
        blue.setMaterial(mat2);
        blue.setLocalTranslation(0, 0, 2100f);

        Box Jade = new Box(2000, 500, 700);
        Geometry green = new Geometry("Box", Jade); //$NON-NLS-1$
        Material mat3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); //$NON-NLS-1$
        mat3.setColor("Color", ColorRGBA.Green); //$NON-NLS-1$
        green.setMaterial(mat3);
        green.setLocalTranslation(0, 0, 3500f);

        Box Amethyst = new Box(2000, 500, 700);
        Geometry violet = new Geometry("Box", Amethyst); //$NON-NLS-1$
        Material mat4 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); //$NON-NLS-1$
        mat4.setColor("Color", ColorRGBA.Magenta); //$NON-NLS-1$
        violet.setMaterial(mat4);
        violet.setLocalTranslation(0, 0, 4900f);

        Box Amber = new Box(2000, 500, 700);
        Geometry yellow = new Geometry("Box", Amber); //$NON-NLS-1$
        Material mat5 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); //$NON-NLS-1$
        mat5.setColor("Color", ColorRGBA.Yellow); //$NON-NLS-1$
        yellow.setMaterial(mat5);
        yellow.setLocalTranslation(0, 0, 6300f);

        Box Crystal = new Box(2000, 500, 700);
        Geometry pink = new Geometry("Box", Crystal); //$NON-NLS-1$
        Material mat6 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); //$NON-NLS-1$
        mat6.setColor("Color", ColorRGBA.Pink); //$NON-NLS-1$
        pink.setMaterial(mat6);
        pink.setLocalTranslation(0, 0, 7700f);

        // BoundingBox
        Material boundingMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); //$NON-NLS-1$
        boundingMaterial.setColor("Color", ColorRGBA.Black); //$NON-NLS-1$

        Geometry boundingVolume = WireBox.makeGeometry((BoundingBox) red.getWorldBound());
        boundingVolume.setMaterial(boundingMaterial);
        boundingVolume.setLocalTranslation(red.getLocalTranslation());
        boundingVolume.setLocalRotation(red.getLocalRotation());
        boundingVolume.setLocalScale(red.getLocalScale());

        Geometry boundingVolume2 = WireBox.makeGeometry((BoundingBox) blue.getWorldBound());
        boundingVolume2.setMaterial(boundingMaterial);
        boundingVolume2.setLocalTranslation(blue.getLocalTranslation());
        boundingVolume2.setLocalRotation(blue.getLocalRotation());
        boundingVolume2.setLocalScale(blue.getLocalScale());

        Geometry boundingVolume3 = WireBox.makeGeometry((BoundingBox) green.getWorldBound());
        boundingVolume3.setMaterial(boundingMaterial);
        boundingVolume3.setLocalTranslation(green.getLocalTranslation());
        boundingVolume3.setLocalRotation(green.getLocalRotation());
        boundingVolume3.setLocalScale(green.getLocalScale());

        Geometry boundingVolume4 = WireBox.makeGeometry((BoundingBox) violet.getWorldBound());
        boundingVolume4.setMaterial(boundingMaterial);
        boundingVolume4.setLocalTranslation(violet.getLocalTranslation());
        boundingVolume4.setLocalRotation(violet.getLocalRotation());
        boundingVolume4.setLocalScale(violet.getLocalScale());

        Geometry boundingVolume5 = WireBox.makeGeometry((BoundingBox) yellow.getWorldBound());
        boundingVolume5.setMaterial(boundingMaterial);
        boundingVolume5.setLocalTranslation(yellow.getLocalTranslation());
        boundingVolume5.setLocalRotation(yellow.getLocalRotation());
        boundingVolume5.setLocalScale(yellow.getLocalScale());

        Geometry boundingVolume6 = WireBox.makeGeometry((BoundingBox) pink.getWorldBound());
        boundingVolume6.setMaterial(boundingMaterial);
        boundingVolume6.setLocalTranslation(pink.getLocalTranslation());
        boundingVolume6.setLocalRotation(pink.getLocalRotation());
        boundingVolume6.setLocalScale(pink.getLocalScale());

        // Nodes
        rootNode.attachChild(cs_node);

        pivotNode = new Node("pivot");//$NON-NLS-1$
        rootNode.attachChild(pivotNode);
        pivotNode.attachChild(grid);
        pivotNode.attachChild(red);
        pivotNode.attachChild(blue);
        pivotNode.attachChild(green);
        pivotNode.attachChild(violet);
        pivotNode.attachChild(yellow);
        pivotNode.attachChild(pink);
        pivotNode.attachChild(boundingVolume);
        pivotNode.attachChild(boundingVolume2);
        pivotNode.attachChild(boundingVolume3);
        pivotNode.attachChild(boundingVolume4);
        pivotNode.attachChild(boundingVolume5);
        pivotNode.attachChild(boundingVolume6);

        // Disabeling flyCam
        flyCam.setEnabled(false);

        //FocusCam
        focusCameraState = new FocusCameraState();
        stateManager.attach(focusCameraState);

        focusCameraState.setFocusPoint(pink);
        focusCameraState.setOffset(new Vector3f(200, 500, 700));

        //Camera Settings
        focusCameraState.setZoomSpeed(30);
        focusCameraState.setMinZoom(3);
        focusCameraState.setMaxZoom(300000);

        focusCameraState.setRotationSpeed(FastMath.TWO_PI);
        focusCameraState.setInvertY(false);

        cam.setFrustumPerspective(45f, (float) cam.getWidth() / cam.getHeight(), 0.01f, 1000f);
        cam.setFrustumFar(300000);

    }

}

I would really appreciate some help!

I guess this is the question of an origin point of a model. It is a starting point of a model. It is not visible in JME but in Blender it is the yellow dot. When you create a cube like this new Box(5, 5, 5) the origin point is in the very center which is 2.5, 2.5, 2.5. You place cubes like this red.setLocalTranslation(0, 0, 0); which means they will always be lowered by 50%. You should add half of its Y dimention to the translation.

Your test contains external code and it is not compilable. I removed the camera and the whole scene was messed up. Cannot help you with this.

Just a quick note and something easy to test:

Would be fixed by changing this line:

To

yellow.setLocalTranslation(0, 1, 6300f); //move it up just slightly to prevent z-fighting

And then the yellow one might not have the problem and it can be fixed properly after that.

Sorry polinc! Ishould habe been more specific. I know that I need to translate everything in the y direction for the geometries to be on on the grid. I just wanted to express that in one of the pictures it looks like they are underneath the coordinate system. Which is not right.

Also also I already dreaded that the camera could be an issue. This happens with my old camera too though…

    flyCam.setEnabled(false);

    ChaseCamera cam1 = new ChaseCamera(cam, rootNode.rotate(0, 8, 0), inputManager);
    cam1.setHideCursorOnRotate(false);
    cam1.setDefaultDistance(2000);

    cam.setFrustumPerspective(45f, (float) cam.getWidth() / cam.getHeight(), 0.01f, 1000f);

    cam1.setMaxDistance(20000);
    cam1.setRotationSpeed(3);
    cam1.setZoomSensitivity(17); 

    cam.setFrustumFar(200000);
    cam.update();

Maybe this is of help?

Hey Murph9!
It is seriousy not a z fighting problem (probably…). I disabled the grid, I put an offset to the geometries, I used a different camera… But at some point they just start to melt as if the material was not solid… I just don’t know what else the issue could be…

When doing test project keep in mind that this means minimal code that it takes to reproduce the problem.

Maybe you should cut it down to two boxes and remove anything that is not needed to reproduce the issue, like the grid and chaseCam and start from there.

Hey mitm!

You are right! I’m sorry about that. First time for me to actually write in a Forum to ask questions. I’m still learning :slight_smile:

But even if I remove everything this issue still occures. (Even when I put a space between the geometries) And I’m still not sure why…

We need a test case we can run. Your previous test case was not runnable.

In the menu on the left there is the link for transparency.

Transparency for Dummies

import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingBox;
import com.jme3.input.ChaseCamera;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.debug.Grid;
import com.jme3.scene.debug.WireBox;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Line;
import com.jme3.system.AppSettings;

public class Testprojekt extends SimpleApplication {

public static void main(final String[] args) {

    Testprojekt app = new Testprojekt();
    AppSettings settings = new AppSettings(true);
    settings.setTitle("3D Visualization"); //$NON-NLS-1$
    app.setSettings(settings);
    app.setShowSettings(false);
    settings.setResolution(1280, 720);
    app.start();
}

private Line x_axis_line;

private Line y_axis_line;

private Line z_axis_line;

private Geometry g_x;

private Geometry g_y;

private Geometry g_z;

private Material m_x;

private Material m_y;

private Material m_z;

private Node cs_node;

private Node pivotNode;

@SuppressWarnings("deprecation")
@Override
public void simpleInitApp() {

    // Background
    getViewPort().setBackgroundColor(ColorRGBA.LightGray);

    // Mousesetting
    inputManager.setCursorVisible(true);

    // Grid
    int min = 500;
    int max = 500;
    Grid g = new Grid(min, max, 500);
    Geometry grid = new Geometry("Ground", g);//$NON-NLS-1$
    grid.setLocalTranslation(new Vector3f(-min * 250, 0, -max * 250));
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");//$NON-NLS-1$
    mat.setColor("Color", ColorRGBA.Black);//$NON-NLS-1$
    grid.setMaterial(mat);

    // Coordinate system
    cs_node = new Node("coordinate_system");//$NON-NLS-1$

    x_axis_line = new Line(new Vector3f(-1000, 0, 0), new Vector3f(+1000, 0, 0));
    x_axis_line.setLineWidth(3f);
    y_axis_line = new Line(new Vector3f(0, -1000, 0), new Vector3f(0, +1000, 0));
    y_axis_line.setLineWidth(3f);
    z_axis_line = new Line(new Vector3f(0, 0, -1000), new Vector3f(0, 0, +1000));
    z_axis_line.setLineWidth(3f);

    g_x = new Geometry("x_axis", x_axis_line);//$NON-NLS-1$
    g_y = new Geometry("y_axis", y_axis_line);//$NON-NLS-1$
    g_z = new Geometry("z_axis", z_axis_line);//$NON-NLS-1$

    m_x = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");//$NON-NLS-1$
    m_x.setColor("Color", ColorRGBA.Black);//$NON-NLS-1$
    m_y = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");//$NON-NLS-1$
    m_y.setColor("Color", ColorRGBA.Black);//$NON-NLS-1$
    m_z = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");//$NON-NLS-1$
    m_z.setColor("Color", ColorRGBA.Blue);//$NON-NLS-1$

    g_x.setMaterial(m_x);
    g_y.setMaterial(m_y);
    g_z.setMaterial(m_z);

    cs_node.attachChild(g_x);
    cs_node.attachChild(g_y);
    cs_node.attachChild(g_z);

    // Geometries

    Box Ruby = new Box(2000, 500, 700);
    Geometry red = new Geometry("Box", Ruby); //$NON-NLS-1$
    Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); //$NON-NLS-1$
    mat1.setColor("Color", ColorRGBA.Red); //$NON-NLS-1$
    red.setMaterial(mat1);
    red.setLocalTranslation(0, 0, 700f);

    Box Sapphire = new Box(2000, 500, 700);
    Geometry blue = new Geometry("Box", Sapphire); //$NON-NLS-1$
    Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); //$NON-NLS-1$
    mat2.setColor("Color", ColorRGBA.Blue); //$NON-NLS-1$
    blue.setMaterial(mat2);
    blue.setLocalTranslation(0, 0, 2100f);

    // BoundingBox
    Material boundingMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); //$NON-NLS-1$
    boundingMaterial.setColor("Color", ColorRGBA.Black); //$NON-NLS-1$

    Geometry boundingVolume = WireBox.makeGeometry((BoundingBox) red.getWorldBound());
    boundingVolume.setMaterial(boundingMaterial);
    boundingVolume.setLocalTranslation(red.getLocalTranslation());
    boundingVolume.setLocalRotation(red.getLocalRotation());
    boundingVolume.setLocalScale(red.getLocalScale());

    Geometry boundingVolume2 = WireBox.makeGeometry((BoundingBox) blue.getWorldBound());
    boundingVolume2.setMaterial(boundingMaterial);
    boundingVolume2.setLocalTranslation(blue.getLocalTranslation());
    boundingVolume2.setLocalRotation(blue.getLocalRotation());
    boundingVolume2.setLocalScale(blue.getLocalScale());

    // Nodes
    rootNode.attachChild(cs_node);

    pivotNode = new Node("pivot");//$NON-NLS-1$
    rootNode.attachChild(pivotNode);
    pivotNode.attachChild(red);
    pivotNode.attachChild(blue);

    pivotNode.attachChild(boundingVolume);
    pivotNode.attachChild(boundingVolume2);

    // Disabeling flyCam
    flyCam.setEnabled(false);

    //FocusCam
    ChaseCamera cam1 = new ChaseCamera(cam, rootNode.rotate(0, 8, 0), inputManager);
    cam1.setHideCursorOnRotate(false);

    cam1.setDefaultDistance(2000);

    cam.setFrustumPerspective(45f, (float) cam.getWidth() / cam.getHeight(), 0.01f, 1000f);

    cam1.setMaxDistance(20000);
    cam1.setRotationSpeed(3);
    cam1.setZoomSensitivity(17);

    cam.setFrustumFar(200000);
    cam.update();

}

Hope this will work then…

Yup found it. IMO this is the problem. Units. I dont know why you are using such enormous numbers. The initial box example is 1,1,1 afair and it fine. I divided everything by 100 in the test and it works. It is a GL problem not the engine. I guess with such a big distance AA doesn’t work as expected.

Tips for future tests:

  1. Always provide working flycam
  2. Remove anything which isnt a problem
  3. Dont disable splash screen settings.

Yeah, the numbers are quite big… The problem is that I have not much to say about the sizes of the geometries. This project reads in files from an external source and is supposed to show whats written in the file accordingly. One of the worst case examples are these big boxes.

Thanks for helping me out! I think I can work with all the advices you gave me :wink:

you can always scale everything.

he could also subdivide this boxes, so GL would not have issues.