[SOLVED] Problem with lighting up models

Hey, I have an enemy class

class Enemy extends Node {

        AnimComposer animComposer;
        long lastAttack = System.currentTimeMillis(); //Time of last attack
        long lastHit    = System.currentTimeMillis(); //Time of last hit taken
        int  health = 5;
        Spatial model;
        Ray ray;
        boolean isYellow = false; //Enemy flashes yellow when it attacks
        boolean isCyan  = false; //Enemy flashes green when it is hit
        String action = "walk";
        int idleTimer = 0;

        //Enemy Constructor
        public Enemy(Vector3f pos) {
            setLocalTranslation(pos);
            createModel();
            Spatial probeHolder = assetManager.loadModel("/assets/Models/Sky_Cloudy.j3o");
            LightProbe probe = (LightProbe)probeHolder.getLocalLightList().get(0);
            probe.setPosition(Vector3f.ZERO);
            probeHolder.removeLight(probe);
            addLight(probe);
            setName("enemy"+enemyCounter);
            enemyCounter++;
            animComposer.setCurrentAction("walking");
        }

        //Creates the enemies model
        private void createModel() {
            model = assetManager.loadModel("/assets/Models/lowpoly_enemy.j3o");
            model.scale(0.3f);
            model.setLocalTranslation(getLocalTranslation());
            attachChild(model);
            ray = new Ray();
            animComposer = getAnimComposer((Node) model);
            setCullHint(CullHint.Never);
        }

        //Enemy attack method
        public void attack() {

            //If cooldown of 500 milliseconds attack player and print health
            if (System.currentTimeMillis() - lastAttack > 500) {
                lastAttack = System.currentTimeMillis();
                isYellow   = true;
                player.health--;
                System.out.println(getName());
                animComposer.setCurrentAction("attacking");
                action = "attack";
            }

        }

        //After 200 milliseconds return the yellow head to red
        public void endYellow() {
            if (System.currentTimeMillis() - lastAttack > 200) {
                isYellow = false;
                //((Geometry)model.getChild("Sphere")).getMaterial().setColor("Color", ColorRGBA.Red);
            }
        }

        public void endCyan() {
            if (System.currentTimeMillis() - lastHit > 200) {
                isCyan = false;
                //((Geometry)model.getChild("Sphere")).getMaterial().setColor("Color", ColorRGBA.Red);
            }
        }

        //Runs when the enemy is attacked
        public void makeCyan() {
            lastHit = System.currentTimeMillis();
            isCyan = true;
            //((Geometry)model.getChild("Sphere")).getMaterial().setColor("Color", ColorRGBA.Cyan);
        }

        private void update(float tpf) {

            if (health <= 0) {
                enemies.remove(this);
                removeFromParent();
            }

            if(getName().equals("enemy6")) {
                enemies.remove(this);
                removeFromParent();
            }

            Vector3f enemyLocation   = model.getWorldTranslation();
            Vector3f playerDirection = cam.getLocation().subtract(enemyLocation);
            Vector3f random = new Vector3f(new Vector3f(getRandomNumber(-3, 3), 0, getRandomNumber(-3, 3)));

            //If the enemy is close then attack the player
            float distance = enemyLocation.distance(cameraNode.getLocalTranslation());
            if (distance < 5) {
                attack();
            } else {
                if(action == "attack"){
                    animComposer.setCurrentAction("walking");
                    action = "walk";
                }
            }

            Vector3f relative = cam.getLocation().subtract(model.getWorldTranslation());
            relative.y = 0;
            relative.normalizeLocal();
            Quaternion rotation = new Quaternion().lookAt(relative, Vector3f.UNIT_Y);
            model.setLocalRotation(rotation);
            if(moveCheck(playerDirection, model.getWorldTranslation())) {
                if (distance < 20) {
                    move(playerDirection.mult(.25f).mult(tpf));
                }
            } else if(moveCheck(random, enemyLocation)) {
                move(random.mult(.25f).mult(tpf));
            }

            if(distance >= 20)if(moveCheck(random, enemyLocation))move(random);

            if(distance <= 40){
                idleTimer++;
                if(idleTimer >= 500){
                    skeleton_idle.setLocalTranslation(getLocalTranslation());
                    skeleton_idle.playInstance();
                    idleTimer = 0;
                }
            }

            //If e is yellow or cyan run make red
           /* if (isYellow) {
                endYellow();
            }
            else if (isCyan) {
                endCyan();
            }*/
        }

    }

now, I had an issue, my .j3o model came from a .gltf and it wasn’t lighting up. pspeed told me to add a lightprobe. I did and it worked fine but some of the enemies that were spawned were still black.

Do you add your light to every model or do you add it to the scene?

LightProbe = Light. Add it where you add the light.

I add the light from the lightprobe using addLight in the enemy constructor. I then spawn in the enemies using this method

     private void createEnemy(Vector3f pos) {
        Enemy e = new Enemy(pos);
        enemies.add(e);
        rootNode.attachChild(e);
    }

where enemies is an arraylist of all enemies and the createEnemy method is called in this method:

public void loadLevel(BufferedImage img){
        int w = img.getWidth();
        int h = img.getHeight();

        for(int xx = 0; xx < w; xx++){
            for(int zz = 0; zz < h; zz++){
                int pixel = img.getRGB(xx, zz);

                int red = (pixel >> 16) & 0xff;
                int green = (pixel >> 8) & 0xff;
                int blue = (pixel) & 0xff;
                
               if(red == 0 && blue == 0 && green == 255){
                    createEnemy(new Vector3f(xx, 1, zz));
                }
             }
          }

       }

Do not add the light probe to the objects.

Add it to the scene.

Presumably that’s where you add your regular light.

Add your light probe where you add your regular light. And just to save myself having to repeat that again: Add your light probe where you add your regular light.

So you need to Add your light probe where you add your regular light.

so to the rootNode?

Where do you add your regular light?

to the rootnode

If you add your light to the root node then that’s where you should add your light probe in this case because the light probe in this case should be added where you add your light which is the root node so add your light probe to the root node.

…which is where you added your regular light.

In conclusion:
Add your light probe where you add your regular light.

Pop quiz:
Where should you add your light probe?

yay it worked thanks! now how do I mark this as solved?

1 Like

Edit the title and put “[Solved]” in front.

2 Likes