How to set a background Texture?

I want to put a Texture as my Background Image Behind all of my other Objects.

I want this texture to fill the screen but be last in the render que

How do I do this?

Thanks for any help

@gbluntzer said:
I want to put a Texture as my Background Image Behind all of my other Objects.
I want this texture to fill the screen but be last in the render que
How do I do this?
Thanks for any help


Perhaps try a full screen quad added to the sky bucket? If you need the image to move depending on point of view... use a sky box.
1 Like

If you search for background image in the google custom search in the upper right, a bunch of hits come back with various techniques. Maybe some of that is helpful.

Thanks for the suggestion @t0neg0d

I ended up doing this. If anyone wants to Code Review it and let me know it there was a better way.

Thanks



Edit:Made a slight code change to Set the Camera back and still get a full screen.



[java]package com.scriptblocks.jmetest;



import java.util.logging.Level;

import java.util.logging.Logger;



import com.jme3.app.FlyCamAppState;

import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Vector3f;

import com.jme3.renderer.queue.RenderQueue.Bucket;

import com.jme3.scene.Geometry;

import com.jme3.scene.Spatial.CullHint;

import com.jme3.scene.shape.Box;

import com.jme3.scene.shape.Quad;

import com.jme3.texture.Texture;



/**

  • Simple Test of Setting the Background to a Texture
  • I want it to Fill the entire viewport

    /

    public class HelloBackgroundTexture extends SimpleApplication{

    Geometry backgroundGeom;



    public static void main(String[] args) {

    Logger.getLogger("com.jme3").setLevel(Level.SEVERE); //Set the logging to SEVERE to get rid of all the excess logs

    HelloBackgroundTexture app = new HelloBackgroundTexture();

    app.start();



    }



    @Override

    public void simpleInitApp() {

    stateManager.getState(FlyCamAppState.class).setEnabled(false); //Turn off the Fly Cam Controller

    initBox();



    //Make the Background

    Material backgroundMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    Texture backgroundTex = assetManager.loadTexture("Interface/Logo/Monkey.jpg");

    backgroundMat.setTexture("ColorMap", backgroundTex);

    float w = this.getContext().getSettings().getWidth();

    float h = this.getContext().getSettings().getHeight();

    float ratio = w/h;



    cam.setLocation(Vector3f.ZERO.add(new Vector3f(0.0f, 0.0f,100f)));//Move the Camera back

    float camZ = cam.getLocation().getZ()-15; //No Idea why I need to subtract 15

    float width = camZ
    ratio;

    float height = camZ;



    Quad fsq = new Quad(width, height);

    backgroundGeom = new Geometry("Background", fsq);

    backgroundGeom.setQueueBucket(Bucket.Sky);

    backgroundGeom.setCullHint(CullHint.Never);

    backgroundGeom.setMaterial(backgroundMat);

    backgroundGeom.setLocalTranslation(-(width / 2), -(height/ 2), 0); //Need to Divide by two because the quad origin is bottom left

    rootNode.attachChild(backgroundGeom);

    }



    /**
  • This just makes a box so I can see if the background is rendered last

    */

    private void initBox(){

    //Make a box

    Box b = new Box(Vector3f.ZERO, 0.1f, 0.1f, 0.1f); // create cube shape at the origin

    Geometry geom = new Geometry(“Box”, b); // create cube geometry from the shape

    Material mat = new Material(assetManager,

    “Common/MatDefs/Misc/Unshaded.j3md”); // create a simple material

    mat.setColor(“Color”, ColorRGBA.Blue); // set color of material to blue

    geom.setMaterial(mat); // set the cube’s material

    rootNode.attachChild(geom);

    }

    }[/java]
@gbluntzer said:
Thanks for the suggestion @t0neg0d
I ended up doing this. If anyone wants to Code Review it and let me know it there was a better way.
Thanks

[java]package com.scriptblocks.jmetest;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme3.app.FlyCamAppState;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial.CullHint;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Quad;
import com.jme3.texture.Texture;

/**
* Simple Test of Setting the Background to a Texture
* I want it to Fill the entire viewport
*/
public class HelloBackgroundTexture extends SimpleApplication{
Geometry backgroundGeom;

public static void main(String[] args) {
Logger.getLogger("com.jme3").setLevel(Level.SEVERE); //Set the logging to SEVERE to get rid of all the excess logs
HelloBackgroundTexture app = new HelloBackgroundTexture();
app.start();

}

@Override
public void simpleInitApp() {
stateManager.getState(FlyCamAppState.class).setEnabled(false); //Turn off the Fly Cam Controller
initBox();

//Make the Background
Material backgroundMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
Texture backgroundTex = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
backgroundMat.setTexture("ColorMap", backgroundTex);
float w = this.getContext().getSettings().getWidth();
float h = this.getContext().getSettings().getHeight();
float ratio = w/h;
cam.setLocation(Vector3f.ZERO.add(new Vector3f(0.0f, 0.0f,1.1f)));//Move the Camera back just a bit.

float width = 1*ratio;
float height = 1;

Quad fsq = new Quad(width, height);
backgroundGeom = new Geometry("Background", fsq);
backgroundGeom.setQueueBucket(Bucket.Sky);
backgroundGeom.setCullHint(CullHint.Never);
backgroundGeom.setMaterial(backgroundMat);
backgroundGeom.setLocalTranslation(-(width / 2), -(height/ 2), 0); //Need to Divide by two because the quad origin is bottom left
rootNode.attachChild(backgroundGeom);
}

/**
* This just makes a box so I can see if the background is rendered last
*/
private void initBox(){
//Make a box
Box b = new Box(Vector3f.ZERO, 0.1f, 0.1f, 0.1f); // create cube shape at the origin
Geometry geom = new Geometry("Box", b); // create cube geometry from the shape
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
mat.setColor("Color", ColorRGBA.Blue); // set color of material to blue
geom.setMaterial(mat); // set the cube's material
rootNode.attachChild(geom);
}
}[/java]


Is this for rendering static scenes? Or do you need the quad to move with the camera? I've never actually done what you're doing here... but it looks good to me!

The camera will be frozen.

I think I need to make the code a bit more dynamic so that I can set the initial camera location further back.



I am going to use the Kinect Texture output as the background Image once I figure this out.



Thanks for the help.