Search in sources :

Example 41 with TempVars

use of com.jme3.util.TempVars in project jmonkeyengine by jMonkeyEngine.

the class ObjectHelper method getTransformation.

/**
     * This method calculates local transformation for the object. Parentage is
     * taken under consideration.
     * 
     * @param objectStructure
     *            the object's structure
     * @return objects transformation relative to its parent
     */
public Transform getTransformation(Structure objectStructure, BlenderContext blenderContext) {
    TempVars tempVars = TempVars.get();
    Matrix4f parentInv = tempVars.tempMat4;
    Pointer pParent = (Pointer) objectStructure.getFieldValue("parent");
    if (pParent.isNotNull()) {
        Structure parentObjectStructure = (Structure) blenderContext.getLoadedFeature(pParent.getOldMemoryAddress(), LoadedDataType.STRUCTURE);
        this.getMatrix(parentObjectStructure, "obmat", fixUpAxis, parentInv).invertLocal();
    } else {
        parentInv.loadIdentity();
    }
    Matrix4f globalMatrix = this.getMatrix(objectStructure, "obmat", fixUpAxis, tempVars.tempMat42);
    Matrix4f localMatrix = parentInv.multLocal(globalMatrix);
    this.getSizeSignums(objectStructure, tempVars.vect1);
    localMatrix.toTranslationVector(tempVars.vect2);
    localMatrix.toRotationQuat(tempVars.quat1);
    localMatrix.toScaleVector(tempVars.vect3);
    Transform t = new Transform(tempVars.vect2, tempVars.quat1.normalizeLocal(), tempVars.vect3.multLocal(tempVars.vect1));
    tempVars.release();
    return t;
}
Also used : Matrix4f(com.jme3.math.Matrix4f) Pointer(com.jme3.scene.plugins.blender.file.Pointer) TempVars(com.jme3.util.TempVars) Structure(com.jme3.scene.plugins.blender.file.Structure) Transform(com.jme3.math.Transform)

Example 42 with TempVars

use of com.jme3.util.TempVars in project jmonkeyengine by jMonkeyEngine.

the class GeneratedTexture method generateSkyTexture.

/**
     * Creates a texture for the sky. The result texture has 6 layers.
     * @param size
     *            the size of the texture (width and height are equal)
     * @param horizontalColor
     *            the horizon color
     * @param zenithColor
     *            the zenith color
     * @param blenderContext
     *            the blender context
     * @return the sky texture
     */
public TextureCubeMap generateSkyTexture(int size, ColorRGBA horizontalColor, ColorRGBA zenithColor, BlenderContext blenderContext) {
    Image image = ImageUtils.createEmptyImage(Format.RGB8, size, size, 6);
    PixelInputOutput pixelIO = PixelIOFactory.getPixelIO(image.getFormat());
    TexturePixel pixel = new TexturePixel();
    float delta = 1 / (float) (size - 1);
    float sideV, sideS = 1, forwardU = 1, forwardV, upS;
    TempVars tempVars = TempVars.get();
    CastFunction castFunction = CAST_FUNCTIONS[blenderContext.getBlenderKey().getSkyGeneratedTextureShape().ordinal()];
    float castRadius = blenderContext.getBlenderKey().getSkyGeneratedTextureRadius();
    for (int x = 0; x < size; ++x) {
        sideV = 1;
        forwardV = 1;
        upS = 0;
        for (int y = 0; y < size; ++y) {
            castFunction.cast(tempVars.vect1.set(1, sideV, sideS), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // right
            pixelIO.write(image, NEGATIVE_X, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            castFunction.cast(tempVars.vect1.set(0, sideV, 1 - sideS), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // left
            pixelIO.write(image, POSITIVE_X, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            castFunction.cast(tempVars.vect1.set(forwardU, forwardV, 0), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // front
            pixelIO.write(image, POSITIVE_Z, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            castFunction.cast(tempVars.vect1.set(1 - forwardU, forwardV, 1), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // back
            pixelIO.write(image, NEGATIVE_Z, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            castFunction.cast(tempVars.vect1.set(forwardU, 0, upS), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // top
            pixelIO.write(image, NEGATIVE_Y, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            castFunction.cast(tempVars.vect1.set(forwardU, 1, 1 - upS), castRadius);
            textureGenerator.getPixel(pixel, tempVars.vect1.x, tempVars.vect1.y, tempVars.vect1.z);
            // bottom
            pixelIO.write(image, POSITIVE_Y, ImageUtils.color(pixel, horizontalColor, zenithColor), x, y);
            sideV = FastMath.clamp(sideV - delta, 0, 1);
            forwardV = FastMath.clamp(forwardV - delta, 0, 1);
            upS = FastMath.clamp(upS + delta, 0, 1);
        }
        sideS = FastMath.clamp(sideS - delta, 0, 1);
        forwardU = FastMath.clamp(forwardU - delta, 0, 1);
    }
    tempVars.release();
    return new TextureCubeMap(image);
}
Also used : PixelInputOutput(com.jme3.scene.plugins.blender.textures.io.PixelInputOutput) TextureCubeMap(com.jme3.texture.TextureCubeMap) TempVars(com.jme3.util.TempVars) Image(com.jme3.texture.Image)

Example 43 with TempVars

use of com.jme3.util.TempVars in project jmonkeyengine by jMonkeyEngine.

the class BetterCharacterControl method checkCanUnDuck.

/**
     * This checks if the character can go from ducked to unducked state by
     * doing a ray test.
     */
protected boolean checkCanUnDuck() {
    TempVars vars = TempVars.get();
    Vector3f location = vars.vect1;
    Vector3f rayVector = vars.vect2;
    location.set(localUp).multLocal(FastMath.ZERO_TOLERANCE).addLocal(this.location);
    rayVector.set(localUp).multLocal(height + FastMath.ZERO_TOLERANCE).addLocal(location);
    List<PhysicsRayTestResult> results = space.rayTest(location, rayVector);
    vars.release();
    for (PhysicsRayTestResult physicsRayTestResult : results) {
        if (!physicsRayTestResult.getCollisionObject().equals(rigidBody)) {
            return false;
        }
    }
    return true;
}
Also used : PhysicsRayTestResult(com.jme3.bullet.collision.PhysicsRayTestResult) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 44 with TempVars

use of com.jme3.util.TempVars in project jmonkeyengine by jMonkeyEngine.

the class BetterCharacterControl method prePhysicsTick.

/**
     * Used internally, don't call manually
     *
     * @param space
     * @param tpf
     */
public void prePhysicsTick(PhysicsSpace space, float tpf) {
    checkOnGround();
    if (wantToUnDuck && checkCanUnDuck()) {
        setHeightPercent(1);
        wantToUnDuck = false;
        ducked = false;
    }
    TempVars vars = TempVars.get();
    Vector3f currentVelocity = vars.vect2.set(velocity);
    // dampen existing x/z forces
    float existingLeftVelocity = velocity.dot(localLeft);
    float existingForwardVelocity = velocity.dot(localForward);
    Vector3f counter = vars.vect1;
    existingLeftVelocity = existingLeftVelocity * physicsDamping;
    existingForwardVelocity = existingForwardVelocity * physicsDamping;
    counter.set(-existingLeftVelocity, 0, -existingForwardVelocity);
    localForwardRotation.multLocal(counter);
    velocity.addLocal(counter);
    float designatedVelocity = walkDirection.length();
    if (designatedVelocity > 0) {
        Vector3f localWalkDirection = vars.vect1;
        //normalize walkdirection
        localWalkDirection.set(walkDirection).normalizeLocal();
        //check for the existing velocity in the desired direction
        float existingVelocity = velocity.dot(localWalkDirection);
        //calculate the final velocity in the desired direction
        float finalVelocity = designatedVelocity - existingVelocity;
        localWalkDirection.multLocal(finalVelocity);
        //add resulting vector to existing velocity
        velocity.addLocal(localWalkDirection);
    }
    if (currentVelocity.distance(velocity) > FastMath.ZERO_TOLERANCE)
        rigidBody.setLinearVelocity(velocity);
    if (jump) {
        //TODO: precalculate jump force
        Vector3f rotatedJumpForce = vars.vect1;
        rotatedJumpForce.set(jumpForce);
        rigidBody.applyImpulse(localForwardRotation.multLocal(rotatedJumpForce), Vector3f.ZERO);
        jump = false;
    }
    vars.release();
}
Also used : Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 45 with TempVars

use of com.jme3.util.TempVars in project jmonkeyengine by jMonkeyEngine.

the class BetterCharacterControl method calculateNewForward.

/**
     * This method works similar to Camera.lookAt but where lookAt sets the
     * priority on the direction, this method sets the priority on the up vector
     * so that the result direction vector and rotation is guaranteed to be
     * perpendicular to the up vector.
     *
     * @param rotation The rotation to set the result on or null to create a new
     * Quaternion, this will be set to the new "z-forward" rotation if not null
     * @param direction The direction to base the new look direction on, will be
     * set to the new direction
     * @param worldUpVector The up vector to use, the result direction will be
     * perpendicular to this
     * @return
     */
protected final void calculateNewForward(Quaternion rotation, Vector3f direction, Vector3f worldUpVector) {
    if (direction == null) {
        return;
    }
    TempVars vars = TempVars.get();
    Vector3f newLeft = vars.vect1;
    Vector3f newLeftNegate = vars.vect2;
    newLeft.set(worldUpVector).crossLocal(direction).normalizeLocal();
    if (newLeft.equals(Vector3f.ZERO)) {
        if (direction.x != 0) {
            newLeft.set(direction.y, -direction.x, 0f).normalizeLocal();
        } else {
            newLeft.set(0f, direction.z, -direction.y).normalizeLocal();
        }
        logger.log(Level.INFO, "Zero left for direction {0}, up {1}", new Object[] { direction, worldUpVector });
    }
    newLeftNegate.set(newLeft).negateLocal();
    direction.set(worldUpVector).crossLocal(newLeftNegate).normalizeLocal();
    if (direction.equals(Vector3f.ZERO)) {
        direction.set(Vector3f.UNIT_Z);
        logger.log(Level.INFO, "Zero left for left {0}, up {1}", new Object[] { newLeft, worldUpVector });
    }
    if (rotation != null) {
        rotation.fromAxes(newLeft, worldUpVector, direction);
    }
    vars.release();
}
Also used : Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Aggregations

TempVars (com.jme3.util.TempVars)103 Vector3f (com.jme3.math.Vector3f)50 Quaternion (com.jme3.math.Quaternion)13 Matrix4f (com.jme3.math.Matrix4f)12 BoundingBox (com.jme3.bounding.BoundingBox)10 Bone (com.jme3.animation.Bone)8 Spatial (com.jme3.scene.Spatial)7 CollisionResult (com.jme3.collision.CollisionResult)6 Vector2f (com.jme3.math.Vector2f)6 FloatBuffer (java.nio.FloatBuffer)6 BoundingSphere (com.jme3.bounding.BoundingSphere)5 BoundingVolume (com.jme3.bounding.BoundingVolume)5 Transform (com.jme3.math.Transform)5 DirectionalLight (com.jme3.light.DirectionalLight)4 PointLight (com.jme3.light.PointLight)4 Geometry (com.jme3.scene.Geometry)4 SpotLight (com.jme3.light.SpotLight)3 ColorRGBA (com.jme3.math.ColorRGBA)3 Matrix3f (com.jme3.math.Matrix3f)3 Vector4f (com.jme3.math.Vector4f)3