Hi,
why does the box never intersect the Camera Frustum? It shouldstop moving if it intersects, but it never stops.
You see the box poping up in the RenderToTexture quad, too.
What am i missing?
Regards,
snare
[java]
import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.bounding.BoundingBox;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.control.CameraControl;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Quad;
import com.jme3.texture.FrameBuffer;
import com.jme3.texture.Image.Format;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture2D;
/**
- This test renders a scene to a texture, then displays the texture on a cube.
*/
public class TextExtraCulling extends SimpleApplication {
private ViewPort offView;
public static void main(String[] args) {
TextExtraCulling app = new TextExtraCulling();
app.start();
}
private Camera offCamera;
private Geometry box;
private float sideX = 1;
private float sideZ = 1;
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(50f);
offCamera = new Camera(512, 512);
offCamera.setFrustumPerspective(45f, 1f, 1f, 1000f);
offCamera.setLocation(new Vector3f(0f, 0f, -5f));
offCamera.lookAt(new Vector3f(0f, 0f, 0f), Vector3f.UNIT_Y);
cam.setLocation(new Vector3f(3, 3, 3));
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
//setup main scene
Geometry quad3 = new Geometry(“box”, new Quad(128, 128));
offView = renderManager.createPreView(“Offscreen View”, offCamera);
offView.setClearFlags(true, true, true);
offView.setBackgroundColor(ColorRGBA.DarkGray);
// create offscreen framebuffer
FrameBuffer offBuffer = new FrameBuffer(512, 512, 1);
//setup framebuffer’s texture
Texture2D offTex = new Texture2D(512, 512, Format.RGBA8);
offTex.setMinFilter(Texture.MinFilter.Trilinear);
offTex.setMagFilter(Texture.MagFilter.Bilinear);
//setup framebuffer to use texture
offBuffer.setDepthBuffer(Format.Depth);
offBuffer.setColorTexture(offTex);
//set viewport to render to offscreen framebuffer
offView.setOutputFrameBuffer(offBuffer);
// attach the scene to the viewport to be rendered
offView.attachScene(rootNode);
Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat.setTexture(“ColorMap”, offTex);
quad3.setMaterial(mat);
quad3.setLocalTranslation(30, 0, 0);
guiNode.attachChild(quad3);
box = new Geometry(“box”, new Box(Vector3f.ZERO, 1, 1, 1));
box.setModelBound(new BoundingBox());
box.setLocalTranslation(0, 0, 0f);
mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat.setColor(“Color”, new ColorRGBA(ColorRGBA.Cyan));
box.setMaterial(mat);
rootNode.attachChild(box);
attachDebugShape(offCamera, rootNode, assetManager);
}
@Override
public void simpleUpdate(float tpf) {
offCamera.setPlaneState(0);
Camera.FrustumIntersect inter = offCamera.contains(box.getWorldBound());
Material mat = box.getMaterial();
boolean intersect = false;
switch (inter) {
case Inside:
mat.setColor(“Color”, new ColorRGBA(ColorRGBA.Green));
break;
case Intersects:
mat.setColor(“Color”, new ColorRGBA(ColorRGBA.Cyan));
break;
case Outside:
mat.setColor(“Color”, new ColorRGBA(ColorRGBA.Red));
break;
}
if (!intersect) {
Vector3f cur = box.getLocalTranslation();
if (sideZ != -1 && cur.z > 10) {
sideZ = -1;
}
if (sideZ != 1 && cur.z < -10) {
sideZ = 1;
}
if (sideX != -1 && cur.x > 10) {
sideX = -1;
}
if (sideX != 1 && cur.x < -10) {
sideX = 1;
}
box.setLocalTranslation(cur.x + 1 * tpf * sideX, cur.y, cur.z + 1 * tpf * sideZ);
}
}
public void attachDebugShape(Camera cam, Node nodeToAttach, AssetManager manager) {
Node followerNode = new Node(“CameraFollower”);
followerNode.addControl(new CameraControl(cam, CameraControl.ControlDirection.CameraToSpatial));
{
Geometry quad = new Geometry(“box”, new Box(Vector3f.ZERO.clone(), 0.1f, 4f, 0.1f));
Material mat = new Material(manager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat.setColor(“Color”, new ColorRGBA(ColorRGBA.Orange));
quad.setMaterial(mat);
quad.setLocalTranslation(0, 0, 0);
followerNode.attachChild(quad);
}
{
Geometry quad = new Geometry(“box”, new Box(Vector3f.ZERO.clone(), 0.2f, 0.2f, 0.2f));
Material mat = new Material(manager, “Common/MatDefs/Misc/Unshaded.j3md”);
mat.setColor(“Color”, new ColorRGBA(ColorRGBA.Cyan));
quad.setMaterial(mat);
quad.setLocalTranslation(0, 0, 5f);
followerNode.attachChild(quad);
}
nodeToAttach.attachChild(followerNode);
}
}
[/java]