private static float calcScreenArea(BoundingBox bound, float distance, float screenWidth) { // Calc as if we are a BoundingSphere for now... float radiusSquare = bound.getXExtent() * bound.getXExtent() + bound.getYExtent() * bound.getYExtent() + bound.getZExtent() * bound.getZExtent(); return ((radiusSquare * screenWidth * screenWidth) / (distance * distance * 4)) * FastMath.PI; } }
/** * Compute the Zfar in the model vieuw to adjust the Zfar distance for the splits calculation */ public static float computeZFar(GeometryList occ, GeometryList recv, Camera cam) { Matrix4f mat = cam.getViewMatrix(); BoundingBox bbOcc = ShadowUtil.computeUnionBound(occ, mat); BoundingBox bbRecv = ShadowUtil.computeUnionBound(recv, mat); return min(max(bbOcc.getZExtent() - bbOcc.getCenter().z, bbRecv.getZExtent() - bbRecv.getCenter().z), cam.getFrustumFar()); } }
/** * Flat projection for 2D textures. * * @param mesh * mesh that is to be projected * @param bb * the bounding box for projecting * @return UV coordinates after the projection */ public static float[] flatProjection(float[] positions, BoundingBox bb) { Vector3f min = bb.getMin(null); float[] ext = new float[] { bb.getXExtent() * 2.0f, bb.getZExtent() * 2.0f }; float[] uvCoordinates = new float[positions.length / 3 * 2]; for (int i = 0, j = 0; i < positions.length; i += 3, j += 2) { uvCoordinates[j] = (positions[i] - min.x) / ext[0]; // skip the Y-coordinate uvCoordinates[j + 1] = (positions[i + 2] - min.z) / ext[1]; } return uvCoordinates; }
/** * This method returns the bounding sphere of the given geometries. * * @param geometries * the list of geometries * @return bounding sphere of the given geometries */ /* package */static BoundingSphere getBoundingSphere(Geometry... geometries) { BoundingSphere result = null; for (Geometry geometry : geometries) { geometry.updateModelBound(); BoundingVolume bv = geometry.getModelBound(); if (bv instanceof BoundingBox) { BoundingBox bb = (BoundingBox) bv; float r = Math.max(bb.getXExtent(), bb.getYExtent()); r = Math.max(r, bb.getZExtent()); return new BoundingSphere(r, bb.getCenter()); } else if (bv instanceof BoundingSphere) { return (BoundingSphere) bv; } else { throw new IllegalStateException("Unknown bounding volume type: " + bv.getClass().getName()); } } return result; }
/** * This method converts the given point into 3D UV coordinates. * * @param boundingBox * the bounding box of the mesh * @param point * the point to be transformed * @param uvs * the result UV coordinates */ private void toTextureUV(BoundingBox boundingBox, Vector3f point, float[] uvs) { uvs[0] = (point.x - boundingBox.getCenter().x) / (boundingBox.getXExtent() == 0 ? 1 : boundingBox.getXExtent()); uvs[1] = (point.y - boundingBox.getCenter().y) / (boundingBox.getYExtent() == 0 ? 1 : boundingBox.getYExtent()); uvs[2] = (point.z - boundingBox.getCenter().z) / (boundingBox.getZExtent() == 0 ? 1 : boundingBox.getZExtent()); // UVS cannot go outside <0, 1> range, but since we are generating texture for triangle envelope it might happen that // some points of the envelope will exceet the bounding box of the mesh thus generating uvs outside the range for (int i = 0; i < 3; ++i) { uvs[i] = FastMath.clamp(uvs[i], 0, 1); } }
/** * Create a geometry suitable for visualizing the specified bounding box. * * @param bbox the bounding box (not null) * @return a new Geometry instance in world space */ public static Geometry makeGeometry(BoundingBox bbox) { float xExtent = bbox.getXExtent(); float yExtent = bbox.getYExtent(); float zExtent = bbox.getZExtent(); WireBox mesh = new WireBox(xExtent, yExtent, zExtent); Geometry result = new Geometry("bounding box", mesh); Vector3f center = bbox.getCenter(); result.setLocalTranslation(center); return result; } }
/** * used by attachBoundChildren() */ private void attachBoundingBox(BoundingBox bb, Node parent) { WireBox wb = new WireBox(bb.getXExtent(), bb.getYExtent(), bb.getZExtent()); Geometry g = new Geometry(); g.setMesh(wb); g.setLocalTranslation(bb.getCenter()); parent.attachChild(g); }
Vector3f min = bb.getMin(null); float[] uvCoordsResults = new float[4];// used for coordinates swapping float[] ext = new float[] { bb.getXExtent() * 2, bb.getYExtent() * 2, bb.getZExtent() * 2 }; for (int i = 0; i < ext.length; ++i) { if (ext[i] == 0) {
Vector3f topLeft = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x-bbox.getXExtent(), 0, bbox.getCenter().z-bbox.getZExtent())); Vector3f topRight = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x+bbox.getXExtent(), 0, bbox.getCenter().z-bbox.getZExtent())); Vector3f bottomLeft = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x-bbox.getXExtent(), 0, bbox.getCenter().z+bbox.getZExtent())); Vector3f bottomRight = worldCoordinateToLocal(new Vector3f(bbox.getCenter().x+bbox.getXExtent(), 0, bbox.getCenter().z+bbox.getZExtent()));
box.getCenter().z - box.getZExtent()}; box.getCenter().z + box.getZExtent()};
casterBB.setXExtent(casterBB.getXExtent() + 2.0f); casterBB.setYExtent(casterBB.getYExtent() + 2.0f); casterBB.setZExtent(casterBB.getZExtent() + 2.0f);
occBB.setZExtent(occBB.getZExtent() + 50); occBB.setCenter(occBB.getCenter().addLocal(0, 0, 25)); if (splitBB.intersects(occBB)) { occBB.setZExtent(occBB.getZExtent() - 50); occBB.setCenter(occBB.getCenter().subtractLocal(0, 0, 25)); casterBB.mergeLocal(occBox); occBB.setZExtent(occBB.getZExtent() + 50); occBB.setCenter(occBB.getCenter().addLocal(0, 0, 25)); intersects = splitBB.intersects(occBB);
@Override public boolean intersectsBox(BoundingBox box, TempVars vars) { Vector3f axis1 = getScaledAxis(0, vars.vect1); Vector3f axis2 = getScaledAxis(1, vars.vect2); Vector3f axis3 = getScaledAxis(2, vars.vect3); Vector3f tn = vars.vect4; Plane p = vars.plane; Vector3f c = box.getCenter(); p.setNormal(0, 0, -1); p.setConstant(-(c.z + box.getZExtent())); if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; p.setNormal(0, 0, 1); p.setConstant(c.z - box.getZExtent()); if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; p.setNormal(0, -1, 0); p.setConstant(-(c.y + box.getYExtent())); if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; p.setNormal(0, 1, 0); p.setConstant(c.y - box.getYExtent()); if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; p.setNormal(-1, 0, 0); p.setConstant(-(c.x + box.getXExtent())); if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; p.setNormal(1, 0, 0); p.setConstant(c.x - box.getXExtent()); if (!insidePlane(p, axis1, axis2, axis3, tn)) return false; return true; }
radVect.set(box.getXExtent(), box.getYExtent(), box.getZExtent()); float otherRadiusSquared = radVect.lengthSquared(); float otherRadius = FastMath.sqrt(otherRadiusSquared);
Vector3f z = new Vector3f(0, 0, 1); Vector3f min = bb.getMin(null); float[] ext = new float[] { bb.getXExtent() * 2.0f, bb.getYExtent() * 2.0f, bb.getZExtent() * 2.0f };
public void updateBoindPoints(Vector3f[] points) { BoundingBox bb = (BoundingBox) batch.getWorldBound(); float xe = bb.getXExtent(); float ye = bb.getYExtent(); float ze = bb.getZExtent(); float x = bb.getCenter().x; float y = bb.getCenter().y; float z = bb.getCenter().z; points[0].set(new Vector3f(x - xe, y - ye, z - ze)); points[1].set(new Vector3f(x - xe, y + ye, z - ze)); points[2].set(new Vector3f(x + xe, y + ye, z - ze)); points[3].set(new Vector3f(x + xe, y - ye, z - ze)); points[4].set(new Vector3f(x + xe, y - ye, z + ze)); points[5].set(new Vector3f(x - xe, y - ye, z + ze)); points[6].set(new Vector3f(x - xe, y + ye, z + ze)); points[7].set(new Vector3f(x + xe, y + ye, z + ze)); } }
public void makeBoxWire(BoundingBox box) { Vector3f[] points = new Vector3f[8]; for (int i = 0; i < 8; i++) { points[i] = new Vector3f(); } points[0].set(-1, -1, 1); points[1].set(-1, 1, 1); points[2].set(1, 1, 1); points[3].set(1, -1, 1); points[4].set(-1, -1, -1); points[5].set(-1, 1, -1); points[6].set(1, 1, -1); points[7].set(1, -1, -1); WireFrustum frustumShape = new WireFrustum(points); aabbGeom = new Geometry("box", frustumShape); aabbGeom.setMaterial(new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md")); aabbGeom.getMaterial().getAdditionalRenderState().setWireframe(true); aabbGeom.setLocalTranslation(box.getCenter()); aabbGeom.setLocalScale(box.getXExtent(), box.getYExtent(), box.getZExtent()); rootNode.attachChild(aabbGeom); }
this.guiBounding.setZExtent(cam.guiBounding.getZExtent()); this.guiBounding.setCenter(cam.guiBounding.getCenter()); this.guiBounding.setCheckPlane(cam.guiBounding.getCheckPlane());
BoundingBox bb = (BoundingBox) sphere.getBound(); Vector3f min = bb.getMin(null); float[] ext = new float[]{bb.getXExtent() * 2, bb.getYExtent() * 2, bb.getZExtent() * 2};
float depth = boundingBox.getZExtent() * 2;