JME2(JavaMonkeyEngine2):选择物体并且在缩略图中显示. 鼠标右键按下旋转视角(二)

2014-11-24 00:38:45 · 作者: · 浏览: 1
lse);


monitorNode.setRenderState(buf);


tRenderer.setBackgroundColor(new ColorRGBA(0f, 0f, 0f, 1f));
fakeTex = new Texture2D();
fakeTex.setRenderToTextureType(Texture.RenderToTextureType.RGBA);
tRenderer.setupTexture(fakeTex);
TextureState screen = display.getRenderer().createTextureState();
screen.setTexture(fakeTex);
screen.setEnabled(true);
quad.setRenderState(screen);


monitorNode.setLightCombineMode(Spatial.LightCombineMode.Off);
rootNode.attachChild(monitorNode);
}
//我们将用Texture2D的方式将框框放到界面里 那将是一个Quad 四边形 .


//接下来是我们最核心的方法
protected void simpleUpdate(){ //我们首先来判断 是否是左键点击 这里的isButtonDown(0) 中的 0 代表的就是左键 if
(MouseInput.get().isButtonDown(0)) { Vector2f screenPos = new
Vector2f(); screenPos.set(am.getHotSpotPosition().x,
am.getHotSpotPosition().y); Vector3f worldCoords =
display.getWorldCoordinates(screenPos, 0); Vector3f worldCoords2 =
display.getWorldCoordinates(screenPos, 1); Ray mouseRay = new
Ray(worldCoords, worldCoords2.subtractLocal(
worldCoords).normalizeLocal()); pr.clear();
//为了让 BoundingPickResults 可以点击 objects内的所有模型
objects.findPick(mouseRay, pr);
try {
//我们会通过pickResults拿到我们点击当前的第1个Node.并且拿到Node的Nmae.接着我们判断并且将点击的Box 复制给缩略图里的Box..这样。他就进去了 String name =
pr.getPickData(0).getTargetMesh().getName(); if
(name.equalsIgnoreCase("box1")) { shadowBox = textureBox; } else if
(name.equalsIgnoreCase("box2")) { shadowBox = whiteBox; } } catch
(IndexOutOfBoundsException ex) { ex.printStackTrace(); } }
}


接下来我们是鼠标的处理...由于要求是鼠标右键按下时并且拖动鼠标 才可以旋转视角 OK
那么我们可以在MouseLook的performAction里 增加一些条件 来实现这个效果

public void performAction(InputActionEvent evt) {
float time = 0.01f * speed;
//首先看鼠标是否右键按下 如果按下 .setCursorVisible(false); 让光标隐藏掉并且移动视角
if(MouseInput.get().isButtonDown(1)){
MouseInput.get().setCursorVisible(false);
if(!buttonPressRequired || MouseInput.get().isButtonDown(mouseButtonForRequired)) {
if (mouse.getLocalTranslation().x > 0) {
event.setTime(time * mouse.getLocalTranslation().x);
rotateRight.performAction(event);
} else if (mouse.getLocalTranslation().x < 0) {
event.setTime(time * mouse.getLocalTranslation().x * -1);
rotateLeft.performAction(event);
}
if (mouse.getLocalTranslation().y > 0) {
event.setTime(time * mouse.getLocalTranslation().y);
lookUp.performAction(event);
} else if (mouse.getLocalTranslation().y < 0) {
event.setTime(time * mouse.getLocalTranslation().y * -1);
lookDown.performAction(event);
}
}
}
//当鼠标右键抬起后 我们将在光标显示出来.
if(!Mou