Search in sources :

Example 26 with Vector3f

use of com.jme3.math.Vector3f in project jmonkeyengine by jMonkeyEngine.

the class FbxToJmeTrack method toJmeTrackInternal.

private Track toJmeTrackInternal(int boneIndex, Transform inverseBindPose) {
    float duration = animStack.getDuration();
    FbxAnimCurveNode translationCurve = animCurves.get("Lcl Translation");
    FbxAnimCurveNode rotationCurve = animCurves.get("Lcl Rotation");
    FbxAnimCurveNode scalingCurve = animCurves.get("Lcl Scaling");
    long[] fbxTimes = getKeyTimes();
    float[] times = new float[fbxTimes.length];
    // Translations / Rotations must be set on all tracks.
    // (Required for jME3)
    Vector3f[] translations = new Vector3f[fbxTimes.length];
    Quaternion[] rotations = new Quaternion[fbxTimes.length];
    Vector3f[] scales = null;
    if (scalingCurve != null) {
        scales = new Vector3f[fbxTimes.length];
    }
    for (int i = 0; i < fbxTimes.length; i++) {
        long fbxTime = fbxTimes[i];
        float time = (float) (fbxTime * FbxAnimUtil.SECONDS_PER_UNIT);
        if (time > duration) {
            // Expand animation duration to fit the curve.
            duration = time;
            System.out.println("actual duration: " + duration);
        }
        times[i] = time;
        if (translationCurve != null) {
            translations[i] = translationCurve.getVector3Value(fbxTime);
        } else {
            translations[i] = new Vector3f();
        }
        if (rotationCurve != null) {
            rotations[i] = rotationCurve.getQuaternionValue(fbxTime);
            if (i > 0) {
                if (rotations[i - 1].dot(rotations[i]) < 0) {
                    System.out.println("rotation will go the long way, oh noes");
                    rotations[i - 1].negate();
                }
            }
        } else {
            rotations[i] = new Quaternion();
        }
        if (scalingCurve != null) {
            scales[i] = scalingCurve.getVector3Value(fbxTime);
        }
        if (inverseBindPose != null) {
            applyInverse(translations[i], rotations[i], scales != null ? scales[i] : null, inverseBindPose);
        }
    }
    if (boneIndex == -1) {
        return new SpatialTrack(times, translations, rotations, scales);
    } else {
        if (scales != null) {
            return new BoneTrack(boneIndex, times, translations, rotations, scales);
        } else {
            return new BoneTrack(boneIndex, times, translations, rotations);
        }
    }
}
Also used : SpatialTrack(com.jme3.animation.SpatialTrack) BoneTrack(com.jme3.animation.BoneTrack) Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f)

Example 27 with Vector3f

use of com.jme3.math.Vector3f in project jmonkeyengine by jMonkeyEngine.

the class Vector3Serializer method writeObject.

public void writeObject(ByteBuffer buffer, Object object) throws IOException {
    Vector3f vec3 = (Vector3f) object;
    buffer.putFloat(vec3.x);
    buffer.putFloat(vec3.y);
    buffer.putFloat(vec3.z);
}
Also used : Vector3f(com.jme3.math.Vector3f)

Example 28 with Vector3f

use of com.jme3.math.Vector3f in project jmonkeyengine by jMonkeyEngine.

the class ObjectHelper method flipMeshIfRequired.

/**
     * The method flips the mesh if the scale is mirroring it. Mirroring scale has either 1 or all 3 factors negative.
     * If two factors are negative then there is no mirroring because a rotation and translation can be found that will
     * lead to the same transform when all scales are positive.
     * 
     * @param geometry
     *            the geometry that is being flipped if necessary
     * @param scale
     *            the scale vector of the given geometry
     */
private void flipMeshIfRequired(Geometry geometry, Vector3f scale) {
    float s = scale.x * scale.y * scale.z;
    if (s < 0 && geometry.getMesh() != null) {
        // negative s means that the scale is mirroring the object
        FloatBuffer normals = geometry.getMesh().getFloatBuffer(Type.Normal);
        if (normals != null) {
            for (int i = 0; i < normals.limit(); i += 3) {
                if (scale.x < 0) {
                    normals.put(i, -normals.get(i));
                }
                if (scale.y < 0) {
                    normals.put(i + 1, -normals.get(i + 1));
                }
                if (scale.z < 0) {
                    normals.put(i + 2, -normals.get(i + 2));
                }
            }
        }
        if (geometry.getMesh().getMode() == Mode.Triangles) {
            // there is no need to flip the indexes for lines and points
            LOGGER.finer("Flipping index order in triangle mesh.");
            Buffer indexBuffer = geometry.getMesh().getBuffer(Type.Index).getData();
            for (int i = 0; i < indexBuffer.limit(); i += 3) {
                if (indexBuffer instanceof ShortBuffer) {
                    short index = ((ShortBuffer) indexBuffer).get(i + 1);
                    ((ShortBuffer) indexBuffer).put(i + 1, ((ShortBuffer) indexBuffer).get(i + 2));
                    ((ShortBuffer) indexBuffer).put(i + 2, index);
                } else {
                    int index = ((IntBuffer) indexBuffer).get(i + 1);
                    ((IntBuffer) indexBuffer).put(i + 1, ((IntBuffer) indexBuffer).get(i + 2));
                    ((IntBuffer) indexBuffer).put(i + 2, index);
                }
            }
        }
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) IntBuffer(java.nio.IntBuffer) Buffer(java.nio.Buffer) IntBuffer(java.nio.IntBuffer) FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) CullHint(com.jme3.scene.Spatial.CullHint)

Example 29 with Vector3f

use of com.jme3.math.Vector3f in project jmonkeyengine by jMonkeyEngine.

the class GeneratedTexture method triangulate.

/**
     * This method triangulates the texture. In the result we get a set of small
     * flat textures for each face of the given mesh. This can be later merged
     * into one flat texture.
     * 
     * @param mesh
     *            the mesh we create the texture for
     * @param geometriesOMA
     *            the old memory address of the geometries group that the given
     *            mesh belongs to (required for bounding box calculations)
     * @param coordinatesType
     *            the types of UV coordinates
     * @param blenderContext
     *            the blender context
     * @return triangulated texture
     */
public TriangulatedTexture triangulate(Mesh mesh, Long geometriesOMA, UVCoordinatesType coordinatesType, BlenderContext blenderContext) {
    TemporalMesh geometries = (TemporalMesh) blenderContext.getLoadedFeature(geometriesOMA, LoadedDataType.TEMPORAL_MESH);
    int[] coordinatesSwappingIndexes = new int[] { ((Number) mTex.getFieldValue("projx")).intValue(), ((Number) mTex.getFieldValue("projy")).intValue(), ((Number) mTex.getFieldValue("projz")).intValue() };
    List<Vector3f> uvs = UVCoordinatesGenerator.generateUVCoordinatesFor3DTexture(mesh, coordinatesType, coordinatesSwappingIndexes, geometries);
    Vector3f[] uvsArray = uvs.toArray(new Vector3f[uvs.size()]);
    BoundingBox boundingBox = UVCoordinatesGenerator.getBoundingBox(geometries);
    Set<TriangleTextureElement> triangleTextureElements = new TreeSet<TriangleTextureElement>(new Comparator<TriangleTextureElement>() {

        public int compare(TriangleTextureElement o1, TriangleTextureElement o2) {
            return o1.faceIndex - o2.faceIndex;
        }
    });
    int[] indices = new int[3];
    for (int i = 0; i < mesh.getTriangleCount(); ++i) {
        mesh.getTriangle(i, indices);
        triangleTextureElements.add(new TriangleTextureElement(i, boundingBox, this, uvsArray, indices, blenderContext));
    }
    return new TriangulatedTexture(triangleTextureElements, blenderContext);
}
Also used : TriangleTextureElement(com.jme3.scene.plugins.blender.textures.TriangulatedTexture.TriangleTextureElement) TemporalMesh(com.jme3.scene.plugins.blender.meshes.TemporalMesh) TreeSet(java.util.TreeSet) Vector3f(com.jme3.math.Vector3f) BoundingBox(com.jme3.bounding.BoundingBox)

Example 30 with Vector3f

use of com.jme3.math.Vector3f in project jmonkeyengine by jMonkeyEngine.

the class ImageUtils method convertToNormalMapTexture.

/**
     * This method converts the given texture into normal-map texture.
     * 
     * @param source
     *            the source texture
     * @param strengthFactor
     *            the normal strength factor
     * @return normal-map texture
     */
public static Image convertToNormalMapTexture(Image source, float strengthFactor) {
    BufferedImage sourceImage = ImageToAwt.convert(source, false, false, 0);
    BufferedImage heightMap = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
    BufferedImage bumpMap = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
    ColorConvertOp gscale = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
    gscale.filter(sourceImage, heightMap);
    Vector3f S = new Vector3f();
    Vector3f T = new Vector3f();
    Vector3f N = new Vector3f();
    for (int x = 0; x < bumpMap.getWidth(); ++x) {
        for (int y = 0; y < bumpMap.getHeight(); ++y) {
            // generating bump pixel
            S.x = 1;
            S.y = 0;
            S.z = strengthFactor * ImageUtils.getHeight(heightMap, x + 1, y) - strengthFactor * ImageUtils.getHeight(heightMap, x - 1, y);
            T.x = 0;
            T.y = 1;
            T.z = strengthFactor * ImageUtils.getHeight(heightMap, x, y + 1) - strengthFactor * ImageUtils.getHeight(heightMap, x, y - 1);
            float den = (float) Math.sqrt(S.z * S.z + T.z * T.z + 1);
            N.x = -S.z;
            N.y = -T.z;
            N.z = 1;
            N.divideLocal(den);
            // setting thge pixel in the result image
            bumpMap.setRGB(x, y, ImageUtils.vectorToColor(N.x, N.y, N.z));
        }
    }
    return ImageUtils.toJmeImage(bumpMap, source.getFormat());
}
Also used : ColorConvertOp(java.awt.image.ColorConvertOp) Vector3f(com.jme3.math.Vector3f) BufferedImage(java.awt.image.BufferedImage)

Aggregations

Vector3f (com.jme3.math.Vector3f)536 Material (com.jme3.material.Material)126 Geometry (com.jme3.scene.Geometry)118 DirectionalLight (com.jme3.light.DirectionalLight)95 Quaternion (com.jme3.math.Quaternion)94 TempVars (com.jme3.util.TempVars)67 ColorRGBA (com.jme3.math.ColorRGBA)64 Node (com.jme3.scene.Node)63 Spatial (com.jme3.scene.Spatial)57 Box (com.jme3.scene.shape.Box)57 Sphere (com.jme3.scene.shape.Sphere)51 Texture (com.jme3.texture.Texture)41 KeyTrigger (com.jme3.input.controls.KeyTrigger)36 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)35 Vector2f (com.jme3.math.Vector2f)35 FilterPostProcessor (com.jme3.post.FilterPostProcessor)34 FloatBuffer (java.nio.FloatBuffer)34 InputCapsule (com.jme3.export.InputCapsule)33 BoundingBox (com.jme3.bounding.BoundingBox)30 AmbientLight (com.jme3.light.AmbientLight)30