Java Code Examples for com.jme3.bounding.BoundingBox#getMin()

The following examples show how to use com.jme3.bounding.BoundingBox#getMin() . 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: BIHTree.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void setMinMax(BoundingBox bbox, boolean doMin, int axis, float value) {
    Vector3f min = bbox.getMin(null);
    Vector3f max = bbox.getMax(null);

    if (doMin) {
        min.set(axis, value);
    } else {
        max.set(axis, value);
    }

    bbox.setMinMax(min, max);
}
 
Example 2
Source File: ParticleEmitter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Instantly emits available particles, up to num.
 */
public void emitParticles(int num) {
    // Force world transform to update
    this.getWorldTransform();

    TempVars vars = TempVars.get();

    BoundingBox bbox = (BoundingBox) this.getMesh().getBound();

    Vector3f min = vars.vect1;
    Vector3f max = vars.vect2;

    bbox.getMin(min);
    bbox.getMax(max);

    if (!Vector3f.isValidVector(min)) {
        min.set(Vector3f.POSITIVE_INFINITY);
    }
    if (!Vector3f.isValidVector(max)) {
        max.set(Vector3f.NEGATIVE_INFINITY);
    }

    for(int i=0;i<num;i++) {
        if( emitParticle(min, max) == null ) break;
    }

    bbox.setMinMax(min, max);
    this.setBoundRefresh();

    vars.release();
}
 
Example 3
Source File: MyParticleEmitter.java    From OpenRTS with MIT License 5 votes vote down vote up
/**
 * Instantly emits all the particles possible to be emitted. Any particles
 * which are currently inactive will be spawned immediately.
 */
@Override
public void emitAllParticles() {
    // Force world transform to update
    this.getWorldTransform();

    TempVars vars = TempVars.get();

    BoundingBox bbox = (BoundingBox) this.getMesh().getBound();

    Vector3f min = vars.vect1;
    Vector3f max = vars.vect2;

    bbox.getMin(min);
    bbox.getMax(max);

    if (!Vector3f.isValidVector(min)) {
        min.set(Vector3f.POSITIVE_INFINITY);
    }
    if (!Vector3f.isValidVector(max)) {
        max.set(Vector3f.NEGATIVE_INFINITY);
    }

    while (emitParticle(min, max) != null);

    bbox.setMinMax(min, max);
    this.setBoundRefresh();

    vars.release();
}
 
Example 4
Source File: Octnode.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void expandBoxToContainTri(BoundingBox bbox, OCTTriangle t){
    Vector3f min = bbox.getMin(null);
    Vector3f max = bbox.getMax(null);
    BoundingBox.checkMinMax(min, max, t.get1());
    BoundingBox.checkMinMax(min, max, t.get2());
    BoundingBox.checkMinMax(min, max, t.get3());
    bbox.setMinMax(min, max);
}
 
Example 5
Source File: UVProjectionGenerator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: BIHTree.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void setMinMax(BoundingBox bbox, boolean doMin, int axis, float value) {
    Vector3f min = bbox.getMin(null);
    Vector3f max = bbox.getMax(null);

    if (doMin) {
        min.set(axis, value);
    } else {
        max.set(axis, value);
    }

    bbox.setMinMax(min, max);
}
 
Example 7
Source File: ParticleEmitter.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Instantly emits all the particles possible to be emitted. Any particles
 * which are currently inactive will be spawned immediately.
 */
public void emitAllParticles() {
    // Force world transform to update
    this.getWorldTransform();

    TempVars vars = TempVars.get();

    BoundingBox bbox = (BoundingBox) this.getMesh().getBound();

    Vector3f min = vars.vect1;
    Vector3f max = vars.vect2;

    bbox.getMin(min);
    bbox.getMax(max);

    if (!Vector3f.isValidVector(min)) {
        min.set(Vector3f.POSITIVE_INFINITY);
    }
    if (!Vector3f.isValidVector(max)) {
        max.set(Vector3f.NEGATIVE_INFINITY);
    }

    while (emitParticle(min, max) != null);

    bbox.setMinMax(min, max);
    this.setBoundRefresh();

    vars.release();
}
 
Example 8
Source File: ShadowUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
     * Updates the shadow camera to properly contain the given points (which
     * contain the eye camera frustum corners)
     *
     * @param shadowCam
     * @param points
     */
    public static void updateShadowCamera(Camera shadowCam, Vector3f[] points) {
        boolean ortho = shadowCam.isParallelProjection();
        shadowCam.setProjectionMatrix(null);

        if (ortho) {
            shadowCam.setFrustum(-1, 1, -1, 1, 1, -1);
        } else {
            shadowCam.setFrustumPerspective(45, 1, 1, 150);
        }

        Matrix4f viewProjMatrix = shadowCam.getViewProjectionMatrix();
        Matrix4f projMatrix = shadowCam.getProjectionMatrix();

        BoundingBox splitBB = computeBoundForPoints(points, viewProjMatrix);

        TempVars vars = TempVars.get();

        Vector3f splitMin = splitBB.getMin(vars.vect1);
        Vector3f splitMax = splitBB.getMax(vars.vect2);

//        splitMin.z = 0;

        // Create the crop matrix.
        float scaleX, scaleY, scaleZ;
        float offsetX, offsetY, offsetZ;

        scaleX = 2.0f / (splitMax.x - splitMin.x);
        scaleY = 2.0f / (splitMax.y - splitMin.y);
        offsetX = -0.5f * (splitMax.x + splitMin.x) * scaleX;
        offsetY = -0.5f * (splitMax.y + splitMin.y) * scaleY;
        scaleZ = 1.0f / (splitMax.z - splitMin.z);
        offsetZ = -splitMin.z * scaleZ;

        Matrix4f cropMatrix = vars.tempMat4;
        cropMatrix.set(scaleX, 0f, 0f, offsetX,
                0f, scaleY, 0f, offsetY,
                0f, 0f, scaleZ, offsetZ,
                0f, 0f, 0f, 1f);


        Matrix4f result = new Matrix4f();
        result.set(cropMatrix);
        result.multLocal(projMatrix);

        vars.release();
        shadowCam.setProjectionMatrix(result);
    }
 
Example 9
Source File: ShadowUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
     * Updates the shadow camera to properly contain the given
     * points (which contain the eye camera frustum corners)
     *
     * @param occluders
     * @param lightCam
     * @param points
     */
    public static void updateShadowCamera(Camera shadowCam, Vector3f[] points) {
        boolean ortho = shadowCam.isParallelProjection();
        shadowCam.setProjectionMatrix(null);

        if (ortho) {
            shadowCam.setFrustum(-1, 1, -1, 1, 1, -1);
        } else {
            shadowCam.setFrustumPerspective(45, 1, 1, 150);
        }

        Matrix4f viewProjMatrix = shadowCam.getViewProjectionMatrix();
        Matrix4f projMatrix = shadowCam.getProjectionMatrix();

        BoundingBox splitBB = computeBoundForPoints(points, viewProjMatrix);

        Vector3f splitMin = splitBB.getMin(null);
        Vector3f splitMax = splitBB.getMax(null);

//        splitMin.z = 0;

        // Create the crop matrix.
        float scaleX, scaleY, scaleZ;
        float offsetX, offsetY, offsetZ;

        scaleX = 2.0f / (splitMax.x - splitMin.x);
        scaleY = 2.0f / (splitMax.y - splitMin.y);
        offsetX = -0.5f * (splitMax.x + splitMin.x) * scaleX;
        offsetY = -0.5f * (splitMax.y + splitMin.y) * scaleY;
        scaleZ = 1.0f / (splitMax.z - splitMin.z);
        offsetZ = -splitMin.z * scaleZ;

        Matrix4f cropMatrix = new Matrix4f(scaleX, 0f, 0f, offsetX,
                0f, scaleY, 0f, offsetY,
                0f, 0f, scaleZ, offsetZ,
                0f, 0f, 0f, 1f);


        Matrix4f result = new Matrix4f();
        result.set(cropMatrix);
        result.multLocal(projMatrix);

        shadowCam.setProjectionMatrix(result);
    }
 
Example 10
Source File: UVProjectionGenerator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Cube projection for 2D textures.
 * 
 * @param positions
 *            points to be projected
 * @param bb
 *            the bounding box for projecting
 * @return UV coordinates after the projection
 */
public static float[] cubeProjection(float[] positions, BoundingBox bb) {
    Triangle triangle = new Triangle();
    Vector3f x = new Vector3f(1, 0, 0);
    Vector3f y = new Vector3f(0, 1, 0);
    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 };

    float[] uvCoordinates = new float[positions.length / 3 * 2];
    float borderAngle = (float) Math.sqrt(2.0f) / 2.0f;
    for (int i = 0, pointIndex = 0; i < positions.length; i += 9) {
        triangle.set(0, positions[i], positions[i + 1], positions[i + 2]);
        triangle.set(1, positions[i + 3], positions[i + 4], positions[i + 5]);
        triangle.set(2, positions[i + 6], positions[i + 7], positions[i + 8]);
        Vector3f n = triangle.getNormal();
        float dotNX = Math.abs(n.dot(x));
        float dorNY = Math.abs(n.dot(y));
        float dotNZ = Math.abs(n.dot(z));
        if (dotNX > borderAngle) {
            if (dotNZ < borderAngle) {// discard X-coordinate
                uvCoordinates[pointIndex++] = (triangle.get1().y - min.y) / ext[1];
                uvCoordinates[pointIndex++] = (triangle.get1().z - min.z) / ext[2];
                uvCoordinates[pointIndex++] = (triangle.get2().y - min.y) / ext[1];
                uvCoordinates[pointIndex++] = (triangle.get2().z - min.z) / ext[2];
                uvCoordinates[pointIndex++] = (triangle.get3().y - min.y) / ext[1];
                uvCoordinates[pointIndex++] = (triangle.get3().z - min.z) / ext[2];
            } else {// discard Z-coordinate
                uvCoordinates[pointIndex++] = (triangle.get1().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get1().y - min.y) / ext[1];
                uvCoordinates[pointIndex++] = (triangle.get2().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get2().y - min.y) / ext[1];
                uvCoordinates[pointIndex++] = (triangle.get3().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get3().y - min.y) / ext[1];
            }
        } else {
            if (dorNY > borderAngle) {// discard Y-coordinate
                uvCoordinates[pointIndex++] = (triangle.get1().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get1().z - min.z) / ext[2];
                uvCoordinates[pointIndex++] = (triangle.get2().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get2().z - min.z) / ext[2];
                uvCoordinates[pointIndex++] = (triangle.get3().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get3().z - min.z) / ext[2];
            } else {// discard Z-coordinate
                uvCoordinates[pointIndex++] = (triangle.get1().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get1().y - min.y) / ext[1];
                uvCoordinates[pointIndex++] = (triangle.get2().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get2().y - min.y) / ext[1];
                uvCoordinates[pointIndex++] = (triangle.get3().x - min.x) / ext[0];
                uvCoordinates[pointIndex++] = (triangle.get3().y - min.y) / ext[1];
            }
        }
        triangle.setNormal(null);// clear the previous normal vector
    }
    return uvCoordinates;
}
 
Example 11
Source File: UVCoordinatesGenerator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Generates a UV coordinates for 3D texture.
 * 
 * @param mesh
 *            the mesh we generate UV's for
 * @param texco
 *            UV coordinates type
 * @param coordinatesSwappingIndexes
 *            coordinates swapping indexes
 * @param geometries
 *            the geometris the given mesh belongs to (required to compute
 *            bounding box)
 * @return UV coordinates for the given mesh
 */
public static List<Vector3f> generateUVCoordinatesFor3DTexture(Mesh mesh, UVCoordinatesType texco, int[] coordinatesSwappingIndexes, List<Geometry> geometries) {
    List<Vector3f> result = new ArrayList<Vector3f>();
    BoundingBox bb = UVCoordinatesGenerator.getBoundingBox(geometries);
    float[] inputData = null;// positions, normals, reflection vectors, etc.

    switch (texco) {
        case TEXCO_ORCO:
            inputData = BufferUtils.getFloatArray(mesh.getFloatBuffer(VertexBuffer.Type.Position));
            break;
        case TEXCO_UV:
            Vector2f[] data = new Vector2f[] { new Vector2f(0, 1), new Vector2f(0, 0), new Vector2f(1, 0) };
            for (int i = 0; i < mesh.getVertexCount(); ++i) {
                Vector2f uv = data[i % 3];
                result.add(new Vector3f(uv.x, uv.y, 0));
            }
            break;
        case TEXCO_NORM:
            inputData = BufferUtils.getFloatArray(mesh.getFloatBuffer(VertexBuffer.Type.Normal));
            break;
        case TEXCO_REFL:
        case TEXCO_GLOB:
        case TEXCO_TANGENT:
        case TEXCO_STRESS:
        case TEXCO_LAVECTOR:
        case TEXCO_OBJECT:
        case TEXCO_OSA:
        case TEXCO_PARTICLE_OR_STRAND:
        case TEXCO_SPEED:
        case TEXCO_STICKY:
        case TEXCO_VIEW:
        case TEXCO_WINDOW:
            LOGGER.warning("Texture coordinates type not currently supported: " + texco);
            break;
        default:
            throw new IllegalStateException("Unknown texture coordinates value: " + texco);
    }

    if (inputData != null) {// make calculations
        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) {
                ext[i] = 1;
            }
        }
        // now transform the coordinates so that they are in the range of
        // <0; 1>
        for (int i = 0; i < inputData.length; i += 3) {
            uvCoordsResults[1] = (inputData[i] - min.x) / ext[0];
            uvCoordsResults[2] = (inputData[i + 1] - min.y) / ext[1];
            uvCoordsResults[3] = (inputData[i + 2] - min.z) / ext[2];
            result.add(new Vector3f(uvCoordsResults[coordinatesSwappingIndexes[0]], uvCoordsResults[coordinatesSwappingIndexes[1]], uvCoordsResults[coordinatesSwappingIndexes[2]]));
        }
    }
    return result;
}
 
Example 12
Source File: TriangulatedTexture.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Constructor that creates an image element from the 3D texture
 * (generated texture). It computes a flat smallest rectangle that can
 * hold a (3D) triangle defined by the given UV coordinates. Then it
 * defines the image pixels for points in 3D space that define the
 * calculated rectangle.
 * 
 * @param faceIndex
 *            the face index this image refers to
 * @param boundingBox
 *            the bounding box of the mesh
 * @param texture
 *            the texture that allows to compute a pixel value in 3D
 *            space
 * @param uv
 *            the UV coordinates of the mesh
 * @param blenderContext
 *            the blender context
 */
public TriangleTextureElement(int faceIndex, BoundingBox boundingBox, GeneratedTexture texture, Vector3f[] uv, int[] uvIndices, BlenderContext blenderContext) {
    this.faceIndex = faceIndex;

    // compute the face vertices from the UV coordinates
    float width = boundingBox.getXExtent() * 2;
    float height = boundingBox.getYExtent() * 2;
    float depth = boundingBox.getZExtent() * 2;

    Vector3f min = boundingBox.getMin(null);
    Vector3f v1 = min.add(uv[uvIndices[0]].x * width, uv[uvIndices[0]].y * height, uv[uvIndices[0]].z * depth);
    Vector3f v2 = min.add(uv[uvIndices[1]].x * width, uv[uvIndices[1]].y * height, uv[uvIndices[1]].z * depth);
    Vector3f v3 = min.add(uv[uvIndices[2]].x * width, uv[uvIndices[2]].y * height, uv[uvIndices[2]].z * depth);

    // get the rectangle envelope for the triangle
    RectangleEnvelope envelope = this.getTriangleEnvelope(v1, v2, v3);

    // create the result image
    Format imageFormat = texture.getImage().getFormat();
    int imageWidth = (int) (envelope.width * blenderContext.getBlenderKey().getGeneratedTexturePPU());
    if (imageWidth == 0) {
        imageWidth = 1;
    }
    int imageHeight = (int) (envelope.height * blenderContext.getBlenderKey().getGeneratedTexturePPU());
    if (imageHeight == 0) {
        imageHeight = 1;
    }
    ByteBuffer data = BufferUtils.createByteBuffer(imageWidth * imageHeight * (imageFormat.getBitsPerPixel() >> 3));
    image = new Image(texture.getImage().getFormat(), imageWidth, imageHeight, data);

    // computing the pixels
    PixelInputOutput pixelWriter = PixelIOFactory.getPixelIO(imageFormat);
    TexturePixel pixel = new TexturePixel();
    float[] uvs = new float[3];
    Vector3f point = new Vector3f(envelope.min);
    Vector3f vecY = new Vector3f();
    Vector3f wDelta = new Vector3f(envelope.w).multLocal(1.0f / imageWidth);
    Vector3f hDelta = new Vector3f(envelope.h).multLocal(1.0f / imageHeight);
    for (int x = 0; x < imageWidth; ++x) {
        for (int y = 0; y < imageHeight; ++y) {
            this.toTextureUV(boundingBox, point, uvs);
            texture.getPixel(pixel, uvs[0], uvs[1], uvs[2]);
            pixelWriter.write(image, 0, pixel, x, y);
            point.addLocal(hDelta);
        }

        vecY.addLocal(wDelta);
        point.set(envelope.min).addLocal(vecY);
    }

    // preparing UV coordinates for the flatted texture
    this.uv = new Vector2f[3];
    this.uv[0] = new Vector2f(FastMath.clamp(v1.subtract(envelope.min).length(), 0, Float.MAX_VALUE) / envelope.height, 0);
    Vector3f heightDropPoint = v2.subtract(envelope.w);// w is directed from the base to v2
    this.uv[1] = new Vector2f(1, heightDropPoint.subtractLocal(envelope.min).length() / envelope.height);
    this.uv[2] = new Vector2f(0, 1);
}