Showing objects after Cullhinting

Hello,

I’m still having problem to get hided object set visible back again.
I’m hiding objects when they are LoD 9 (CullHint.Always), and in other cases using CullHint.Never, but objects are not appearing but they physics exist - block player etc.
Here’s my code:

@Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
             distance = (vp.getCamera().getLocation().distance(spatial.getWorldTranslation()));
             int dist = checkDistance(distance);
        switch(dist) {
            case 10:
            spatial.setCullHint(CullHint.Always);
            break;
            case 9:
            spatial.setCullHint(CullHint.Never);
            break;
            case 8:
            spatial.setCullHint(CullHint.Never);
            break;
            case 7:
            spatial.setCullHint(CullHint.Never);
            break;
            case 6:
            spatial.setCullHint(CullHint.Never);
            break;
            case 5:
            spatial.setCullHint(CullHint.Never);
            break;
            case 4:
            spatial.setCullHint(CullHint.Never);
            break;
            case 3:
            spatial.setCullHint(CullHint.Never);
            break;
            case 2:
            spatial.setCullHint(CullHint.Never);
            break;
            case 1:
            spatial.setCullHint(CullHint.Never);
            break;
            default:
            spatial.setCullHint(CullHint.Never);
            }
       
        if (dist < ((Geometry)spatial).getMesh().getNumLodLevels()) {

               ((Geometry)spatial).setLodLevel(dist);
            
        }
        
        }
  1. controlRender isn’t meant for making changes to the scenegraph (like setting a cull hint), use update
  2. we don’t see all of your code

Hmm, using Update also give no effect. Here’s my LoD Control class.

public class LODControl extends AbstractControl {
    
    private float switchDistance = 10f;
    private float distance;
    private boolean hidden = false;
    
    public LODControl(float switchDistance) {
        this.switchDistance = switchDistance;
    }
    
    public LODControl() {
        
    }
     /**
     * This method will parse the scene passed to it and generate the lod per
     * geometry and add the LodControl which handles it.
     *
     * @param spatial
     */
    public void parseSceneForLOD(Spatial spatial) {
        if (!(spatial instanceof Terrain)) {
            if (spatial instanceof Geometry) {

                Geometry g = (Geometry) spatial;   
                LodGenerator lodGenerator = new LodGenerator(g);
                lodGenerator.bakeLods(LodGenerator.TriangleReductionMethod.PROPORTIONAL,
                0.08f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f);

                LODControl lod = new LODControl(10); //Change this value for distance to change LOD
                g.addControl(lod);
                
            } else if (spatial instanceof Node) {
                Node node = (Node) spatial;
                for (int i = 0; i < node.getQuantity(); i++) {
                    Spatial child = node.getChild(i);
                    parseSceneForLOD(child);
                }
            }
        }
    }
    
    @Override
    protected void controlUpdate(float tpf) {
 
                int dist = checkDistance(distance);
        switch(dist) {
            case 10:
            spatial.setCullHint(CullHint.Always);
            break;
            case 9:
            spatial.setCullHint(CullHint.Never);
            break;
            case 8:
            spatial.setCullHint(CullHint.Never);
            break;
            case 7:
            spatial.setCullHint(CullHint.Never);
            break;
            case 6:
            spatial.setCullHint(CullHint.Never);
            break;
            case 5:
            spatial.setCullHint(CullHint.Never);
            break;
            case 4:
            spatial.setCullHint(CullHint.Never);
            break;
            case 3:
            spatial.setCullHint(CullHint.Never);
            break;
            case 2:
            spatial.setCullHint(CullHint.Never);
            break;
            case 1:
            spatial.setCullHint(CullHint.Never);
            break;
            default:
            spatial.setCullHint(CullHint.Never);
            }
       
        if (dist < ((Geometry)spatial).getMesh().getNumLodLevels()) {
                ((Geometry)spatial).setLodLevel(dist);          
        }    
    }
    
    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
       distance = (vp.getCamera().getLocation().distance(spatial.getWorldTranslation()));
        }
    
    private int checkDistance(float distanceFloat) {
        int index;
      if (distanceFloat > 400) {
           index = 10;
       } else if (distanceFloat < 400 && distanceFloat > 350) {
           index = 9;
       } else if (distanceFloat <= 350 && distanceFloat > 340) {
           index = 8;
       } else if (distanceFloat <= 340 && distanceFloat > 280) {
           index = 7;
       } else if (distanceFloat <= 280 && distanceFloat > 230) {
           index = 6;
       } else if (distanceFloat <= 230 && distanceFloat > 200) {
           index = 5;
       } else if (distanceFloat <= 200 && distanceFloat > 140) {
           index = 4;
       } else if (distanceFloat <= 140 && distanceFloat > 75) {
           index = 3;
       } else if (distanceFloat <= 75 && distanceFloat > 50) {
           index = 2;
       } else {
           index = 1;
       }
       return index;
    }
}

The object isn’t rendered when the cullhint is set, hence render is not called. Your distance is never updated after the cullhint is set. Again, don’t use render.

Edit: And btw you never do anything about the physics, it will always stay in the physics space if these geometries are in it.

1 Like

Yay, right. Thanks, already fixed and working fine :smile: . Well, I don’t need to remove physics because if player is far from that objects anyway he can’t collide with then, but when he will come closer it’s fine because I don’t need to recreate physics.