How to make an object blink?

I have a character in my game that I would like to blink (on and off at a particular frequency) so that the user can see him easier on the screen when something gets highlighted. What is the best way to make this happen?

Wait some period of time, set the cull hint on the spatial to always. Wait some moreā€¦ set the cull hint to inherit. Repeat.

Do it in a control.

If any of these things sounds too terribly foreign then also do some more JME tutorials. (Specifically about controls).

Do you have a link for me regarding controls, or a code snippet? Iā€™m a newby :smile:

Here you go:
http://wiki.jmonkeyengine.org/doku.php

Here is a simple example without Control or AppState - just copy-paste the code.
As pspeed said: better use Control for this (or AppState if you want global control).
As both pspeed and jmaasing said: read the wiki and go through the tutorials.
Or buy one of the three jME3.0 books that are currently available, some are quite good.

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial.CullHint;
import com.jme3.scene.shape.Box;

/**
 * Simple test with two blinking techniques...
 * 
 * @author Ogli
 */
public class SimpleTest extends SimpleApplication {

    public int counter = 0;
    
    public static void main(String[] args) {
        SimpleTest app = new SimpleTest();
        app.start();
    }
    
    public Geometry blinkingBox1;
    public Geometry blinkingBox2;
    public Material blinkingMaterial;
    public Material otherMaterial;
    public ColorRGBA normalColor = ColorRGBA.Blue;
    public ColorRGBA blinkColor = ColorRGBA.White;
    
    @Override
    public void simpleInitApp() 
    {
        //create the blinking box
        Box b = new Box(1, 1, 1);
        blinkingBox1 = new Geometry("Box", b);
        blinkingMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        blinkingMaterial.setColor("Color", normalColor);
        blinkingBox1.setMaterial(blinkingMaterial);
        rootNode.attachChild(blinkingBox1);
        
        //create the other blinking box
        Box b2 = new Box(1.3f,2.1f,1.3f);
        blinkingBox2 = new Geometry("Box 2", b2);
        otherMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        otherMaterial.setColor("Color", ColorRGBA.LightGray);
        blinkingBox2.setMaterial(otherMaterial);
        Node box2Node = new Node();
        box2Node.attachChild(blinkingBox2);
        rootNode.attachChild(box2Node);
        box2Node.move(5, 0, 0);        
        
        //make camera a little faster than usual
        flyCam.setMoveSpeed(100f);
    }

    public float secondsPassed = 0f;
    public float blinkOnTime = 0.5f;
    public float blinkOffTime = 0.5f;
    public boolean blinkOn = true;
    
    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
        secondsPassed += tpf;
        float maxTime = blinkOn ? blinkOnTime : blinkOffTime;
        if(secondsPassed > maxTime)
        {
            secondsPassed = 0;
            if(blinkOn)
            {
                blinkOn = false;
                
                //blinking box 1
                blinkingMaterial.setColor("Color", normalColor);
                
                //blinking box 2
                blinkingBox2.setCullHint(CullHint.Inherit);
            }
            else
            {
                blinkOn = true;
                
                //blinking box 1
                blinkingMaterial.setColor("Color", blinkColor);
                
                //blinking box 2
                blinkingBox2.setCullHint(CullHint.Always);
            }
        }
    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }
}
1 Like

P.S.
You might have noticed that multiple ways are possible to achieve the ā€˜blinkingā€™ effect.
Those are just two very simple effects - one could also write a ā€œpulsating color shaderā€ in GLSL.

And other effects are possible too:

  • Render a decal on the ground (for example a ring) :low_brightness: and let it have
    varying size or color - just one idea.
  • Or maybe a 3d-arrow :arrow_down: or exclamation mark :exclamation: above the head (like in MMOs) would do the trick - just another idea.
1 Like

Since this thread is similar to a problem I have, Iā€™m going to write it here. If I should create a thread, no problems.

How do I make an object fade away, to become transparent over time? Is there any filter or control created for that? If not, how do I do it then?

And btw, can someone give me an example of a wait(int seconds) method for me to create? I have been using timers (fields that increment with the tpf value), but for some cases it is more friendly to have a method like that.

What do you mean wait()ā€¦ like, that line wouldnā€™t complete until the time had expired? So youā€™d block the whole rendering thread waiting?

As to the other, itā€™s a completely different topic.

Different topic, yes.
Many answers in the docs and :chimpanzee_google: is your friendā€¦

I was able to write a simple fade-over-time in a few minutes.
Here are some important lines:

blinkingMaterial.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
blinkingBox1.setMaterial(blinkingMaterial);
blinkingBox1.setQueueBucket(Bucket.Translucent);
public float fading = 1.0f;
fading *= 0.9f;
blinkingMaterial.setColor("Color", new ColorRGBA(0,0,1,fading));

The other code is very similar to the blinking demo.
You can change some things and play around.

And as before, there are multiple ways to reach your destination.
This is just very simple stuff, other solutions are better.

For the waiting-function,
from time to time I emulate low framerates (fps near 1 or 2 or soā€¦)
just by having this simple code block in some standard place:

try
{
    Thread.sleep(400);
}
catch(Exception ex)
{

}

Simple but effectiveā€¦ :chimpanzee_smile:

@Ogli and @pspeed thanks for the replys. I now have an ideia how to make object fade away.

The mthod wait() i wanted was some sort of method that would wait a certain time WITHOUT stop rendering the game. I used Thread.sleep(), but it has that problem.

So waiting without waiting. The would be a trick.

No, I think I know what you mean, Evilblow.

You want to stop all game-logic but keep rendering.
Iā€™ve planned that for my game too, for when the user enters the main menu - in the background I want to keep rendering the game scene, but the game is paused completely.
I wrote a PauseOtherControlsControl that even stops the in-engine AnimationControl etc.
But since Iā€™m now working on something completely different, I didnā€™t continue working on that thing.

Maybe one of the core devs has a better solution for that problem.
And I hope that I get your intentions right, Evilblow.

P.S.
The background should also have a rotating camera (which is the only object that gets some kind of tpf update). So rendering to a texture is not the solution.
P.P.S.
Further, some people will want this feature to show steps of their 3d algorithm from a different point of view - e.g. their LOD terrain rendering seen from above the camera. Itā€™s used in scientific 3d graphics stuff quite often in order to demonstrate how the algorithms work. People can then ā€œfreezeā€ their LOD algorithm and use a fly camera to navigate around the area.