Java Code Examples for com.jme3.renderer.Camera#getScreenCoordinates()
The following examples show how to use
com.jme3.renderer.Camera#getScreenCoordinates() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultDraggable.java From Lemur with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Creates a 3D draggable that will move the specified spatial relative to * its current translation in a plane relative to the current viewport camera. */ public DefaultDraggable( ViewPort view, Spatial spatial, Vector2f start ) { Camera cam = view.getCamera(); Vector3f origin = spatial.getWorldTranslation(); Vector3f screenPos = cam.getScreenCoordinates(origin); Vector2f xScreen = new Vector2f(screenPos.x + 1, screenPos.y); Vector2f yScreen = new Vector2f(screenPos.x, screenPos.y + 1); // Find the world location for one pixel right and one pixel up // in the plane of our object. Vector3f xWorld = cam.getWorldCoordinates(xScreen, screenPos.z); Vector3f yWorld = cam.getWorldCoordinates(yScreen, screenPos.z); this.start = start.clone(); this.spatial = spatial; this.origin = origin.clone(); this.xAxis = xWorld.subtractLocal(origin); this.yAxis = yWorld.subtractLocal(origin); this.currentLocation = start.clone(); }
Example 2
Source File: GuiGlobals.java From Lemur with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Deprecated public Vector3f getScreenCoordinates( Spatial relativeTo, Vector3f pos ) { ViewPort vp = getCollisionViewPort(relativeTo); if( vp == null ) { throw new RuntimeException("Could not find viewport for:" + relativeTo); } // Calculate the world position relative to the spatial pos = relativeTo.localToWorld(pos, null); Camera cam = vp.getCamera(); if( cam.isParallelProjection() ) { return pos.clone(); } return cam.getScreenCoordinates(pos); }
Example 3
Source File: AbstractSceneFileEditor.java From jmonkeybuilder with Apache License 2.0 | 5 votes |
@FxThread private boolean isVisibleOnEditor(@NotNull final Spatial spatial) { final MA editor3DPart = getEditor3DPart(); final Camera camera = editor3DPart.getCamera(); final Vector3f position = spatial.getWorldTranslation(); final Vector3f coordinates = camera.getScreenCoordinates(position, new Vector3f()); boolean invisible = coordinates.getZ() < 0F || coordinates.getZ() > 1F; invisible = invisible || !isInside(coordinates.getX(), camera.getHeight() - coordinates.getY(), Event.class); return !invisible; }
Example 4
Source File: AbstractCursorEvent.java From Lemur with BSD 3-Clause "New" or "Revised" License | 5 votes |
public Vector3f getRelativeViewCoordinates( Spatial relativeTo, Vector3f pos ) { // Calculate the world position relative to the spatial pos = relativeTo.localToWorld(pos, null); Camera cam = view.getCamera(); if( cam.isParallelProjection() ) { return pos.clone(); } return cam.getScreenCoordinates(pos); }
Example 5
Source File: RotationToolControl.java From jmonkeybuilder with Apache License 2.0 | 4 votes |
@Override @JmeThread public void processTransform() { final LocalObjects local = LocalObjects.get(); final EditorTransformSupport editorControl = getEditorControl(); final InputManager inputManager = EditorUtil.getInputManager(); final Camera camera = editorControl.getCamera(); final Transform transform = notNull(editorControl.getTransformCenter()); // cursor position and selected position vectors final Vector2f cursorPos = inputManager.getCursorPosition(); final Vector3f transformOnScreen = camera.getScreenCoordinates(transform.getTranslation()); final Vector2f selectedCoords = local.nextVector(transformOnScreen.getX(), transformOnScreen.getY()); //set new deltaVector if it's not set if (Float.isNaN(editorControl.getTransformDeltaX())) { editorControl.setTransformDeltaX(selectedCoords.getX() - cursorPos.getX()); editorControl.setTransformDeltaY(selectedCoords.getY() - cursorPos.getY()); } // Picked vector final TransformationMode transformationMode = editorControl.getTransformationMode(); final Vector3f pickedVector = transformationMode.getPickedVector(transform, editorControl.getPickedAxis(), camera); final Vector3f deltaVector = local.nextVector(editorControl.getTransformDeltaX(), editorControl.getTransformDeltaY(), 0F); // rotate according to angle final Vector2f cursorDirection = selectedCoords.subtractLocal(cursorPos).normalizeLocal(); float angle = cursorDirection.angleBetween(local.nextVector(deltaVector.getX(), deltaVector.getY())); angle = FastMath.RAD_TO_DEG * angle * FastMath.DEG_TO_RAD; final Node parentNode = getParentNode(); final Node childNode = getChildNode(); transformationMode.prepareToRotate(parentNode, childNode, transform, camera); final Quaternion rotation = parentNode.getLocalRotation(); final Vector3f axisToRotate = rotation.mult(pickedVector, local.nextVector()); float angleCheck = axisToRotate.angleBetween(camera.getDirection(local.nextVector())); if (angleCheck > FastMath.HALF_PI) { angle = -angle; } final Quaternion difference = local.nextRotation().fromAngleAxis(angle, pickedVector); final Quaternion newRotation = rotation.mult(difference, local.nextRotation()); parentNode.setLocalRotation(newRotation); final Spatial toTransform = notNull(editorControl.getToTransform()); toTransform.setLocalRotation(childNode.getWorldRotation()); editorControl.notifyTransformed(toTransform); }
Example 6
Source File: ScaleToolControl.java From jmonkeybuilder with Apache License 2.0 | 4 votes |
@JmeThread @Override public void processTransform() { final EditorTransformSupport editorControl = getEditorControl(); final LocalObjects local = LocalObjects.get(); final Camera camera = editorControl.getCamera(); final InputManager inputManager = EditorUtil.getInputManager(); final Transform transform = notNull(editorControl.getTransformCenter()); // cursor position and selected position vectors final Vector2f cursorPos = inputManager.getCursorPosition(); final Vector3f transformOnScreen = camera.getScreenCoordinates(transform.getTranslation(), local.nextVector()); final Vector2f selectedCoords = local.nextVector(transformOnScreen.getX(), transformOnScreen.getY()); // set new deltaVector if it's not set (scale tool stores position of a cursor) if (Float.isNaN(editorControl.getTransformDeltaX())) { editorControl.setTransformDeltaX(cursorPos.getX()); editorControl.setTransformDeltaY(cursorPos.getY()); } final Node parentNode = getParentNode(); final Node childNode = getChildNode(); // Picked vector final Spatial toTransform = notNull(editorControl.getToTransform()); final TransformationMode transformationMode = editorControl.getTransformationMode(); transformationMode.prepareToScale(parentNode, childNode, transform, camera); // scale according to distance final Vector3f deltaVector = local.nextVector(editorControl.getTransformDeltaX(), editorControl.getTransformDeltaY(), 0F); final Vector2f delta2d = local.nextVector(deltaVector.getX(), deltaVector.getY()); final Vector3f baseScale = local.nextVector(transform.getScale()); // default scale final Vector3f pickedVector = local.nextVector(transformationMode.getScaleAxis(transform, editorControl.getPickedAxis(), camera)); pickedVector.setX(abs(pickedVector.getX())); pickedVector.setY(abs(pickedVector.getY())); pickedVector.setZ(abs(pickedVector.getZ())); if (Config.DEV_TRANSFORMS_DEBUG) { System.out.println("Base scale " + baseScale + ", pickedVector " + pickedVector); } // scale object float disCursor = cursorPos.distance(selectedCoords); float disDelta = delta2d.distance(selectedCoords); float scaleValue = (float) (cursorPos.distance(delta2d) * 0.01f * Math.sqrt(baseScale.length())); if (disCursor > disDelta) { baseScale.addLocal(pickedVector.mult(scaleValue, local.nextVector())); } else { scaleValue = Math.min(scaleValue, 0.999f); // remove negateve values baseScale.subtractLocal(pickedVector.mult(scaleValue, local.nextVector())); } parentNode.setLocalScale(baseScale); if (Config.DEV_TRANSFORMS_DEBUG) { System.out.println("New scale " + baseScale + ", result world " + childNode.getWorldScale()); } parentNode.setLocalScale(baseScale); toTransform.setLocalScale(childNode.getWorldScale()); editorControl.notifyTransformed(toTransform); }
Example 7
Source File: MathUtils.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * Returns the shortest distance between a Ray and a segment. * The segment is defined by a start position and an end position in world space * The distance returned will be in world space (world units). * If the camera parameter is not null the distance will be returned in screen space (pixels) * * @param ray The ray * @param segStart The start position of the segment in world space * @param segEnd The end position of the segment in world space * @param camera The renderer camera if the distance is required in screen space. Null if the distance is required in world space * @return the shortest distance between the ray and the segment or -1 if no solution is found. */ public static float raySegmentShortestDistance(Ray ray, Vector3f segStart, Vector3f segEnd, Camera camera) { // Algorithm is ported from the C algorithm of // Paul Bourke at http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline3d/ TempVars vars = TempVars.get(); Vector3f resultSegmentPoint1 = vars.vect1; Vector3f resultSegmentPoint2 = vars.vect2; Vector3f p1 = segStart; Vector3f p2 = segEnd; Vector3f p3 = ray.origin; Vector3f p4 = vars.vect3.set(ray.getDirection()).multLocal(Math.min(ray.getLimit(), 1000)).addLocal(ray.getOrigin()); Vector3f p13 = vars.vect4.set(p1).subtractLocal(p3); Vector3f p43 = vars.vect5.set(p4).subtractLocal(p3); if (p43.lengthSquared() < 0.0001) { vars.release(); return -1; } Vector3f p21 = vars.vect6.set(p2).subtractLocal(p1); if (p21.lengthSquared() < 0.0001) { vars.release(); return -1; } double d1343 = p13.x * (double) p43.x + (double) p13.y * p43.y + (double) p13.z * p43.z; double d4321 = p43.x * (double) p21.x + (double) p43.y * p21.y + (double) p43.z * p21.z; double d1321 = p13.x * (double) p21.x + (double) p13.y * p21.y + (double) p13.z * p21.z; double d4343 = p43.x * (double) p43.x + (double) p43.y * p43.y + (double) p43.z * p43.z; double d2121 = p21.x * (double) p21.x + (double) p21.y * p21.y + (double) p21.z * p21.z; double denom = d2121 * d4343 - d4321 * d4321; if (Math.abs(denom) < 0.0001) { vars.release(); return -1; } double numer = d1343 * d4321 - d1321 * d4343; double mua = numer / denom; double mub = (d1343 + d4321 * (mua)) / d4343; resultSegmentPoint1.x = (float) (p1.x + mua * p21.x); resultSegmentPoint1.y = (float) (p1.y + mua * p21.y); resultSegmentPoint1.z = (float) (p1.z + mua * p21.z); resultSegmentPoint2.x = (float) (p3.x + mub * p43.x); resultSegmentPoint2.y = (float) (p3.y + mub * p43.y); resultSegmentPoint2.z = (float) (p3.z + mub * p43.z); //check if result 1 is in the segment section. float startToPoint = vars.vect3.set(resultSegmentPoint1).subtractLocal(segStart).lengthSquared(); float endToPoint = vars.vect3.set(resultSegmentPoint1).subtractLocal(segEnd).lengthSquared(); float segLength = vars.vect3.set(segEnd).subtractLocal(segStart).lengthSquared(); if (startToPoint > segLength || endToPoint > segLength) { vars.release(); return -1; } if (camera != null) { //camera is not null let's convert the points in screen space camera.getScreenCoordinates(resultSegmentPoint1, resultSegmentPoint1); camera.getScreenCoordinates(resultSegmentPoint2, resultSegmentPoint2); } float length = resultSegmentPoint1.subtractLocal(resultSegmentPoint2).length(); vars.release(); return length; }