Search in sources :

Example 11 with Geometry

use of com.jme3.scene.Geometry in project jmonkeyengine by jMonkeyEngine.

the class TextureAtlas method createAtlas.

/**
     * Create a texture atlas for the given root node, containing DiffuseMap, NormalMap and SpecularMap.
     * @param root The rootNode to create the atlas for.
     * @param atlasSize The size of the atlas (width and height).
     * @return Null if the atlas cannot be created because not all textures fit.
     */
public static TextureAtlas createAtlas(Spatial root, int atlasSize) {
    List<Geometry> geometries = new ArrayList<Geometry>();
    GeometryBatchFactory.gatherGeoms(root, geometries);
    TextureAtlas atlas = new TextureAtlas(atlasSize, atlasSize);
    for (Geometry geometry : geometries) {
        if (!atlas.addGeometry(geometry)) {
            logger.log(Level.WARNING, "Texture atlas size too small, cannot add all textures");
            return null;
        }
    }
    return atlas;
}
Also used : Geometry(com.jme3.scene.Geometry) ArrayList(java.util.ArrayList)

Example 12 with Geometry

use of com.jme3.scene.Geometry in project jmonkeyengine by jMonkeyEngine.

the class LightFilterTest method testSpotFiltering.

@Test
public void testSpotFiltering() {
    SpotLight sl = new SpotLight(Vector3f.ZERO, Vector3f.UNIT_Z);
    sl.setSpotRange(0);
    geom.addLight(sl);
    // Infinite spot lights are only filtered
    checkFilteredLights(1);
    // if the geometry is outside the infinite cone.
    TempVars vars = TempVars.get();
    try {
        // The spot is not touching the near plane of the camera yet, 
        // should still be culled.
        sl.setSpotRange(1f - FastMath.ZERO_TOLERANCE);
        assert !sl.intersectsFrustum(cam, vars);
        // should be culled from the geometry's PoV
        checkFilteredLights(0);
        // Now it touches the near plane.
        sl.setSpotRange(1f);
        // still culled from the geometry's PoV
        checkFilteredLights(0);
        assert sl.intersectsFrustum(cam, vars);
    } finally {
        vars.release();
    }
    // make it barely reach the geometry
    sl.setSpotRange(9f);
    checkFilteredLights(0);
    // make it reach the geometry (touching its bound)
    sl.setSpotRange(9f + FastMath.ZERO_TOLERANCE);
    checkFilteredLights(1);
    // rotate the cone a bit so it no longer faces the geom
    sl.setDirection(new Vector3f(0.316f, 0, 0.948f).normalizeLocal());
    checkFilteredLights(0);
    // extent the range much farther
    sl.setSpotRange(20);
    checkFilteredLights(0);
    // Create box of size X=10 (double the extent)
    // now, the spot will touch the box.
    geom.setMesh(new Box(5, 1, 1));
    checkFilteredLights(1);
    // ==================================
    // Tests for bounding sphere, with a radius of 1f (in the box geom)
    sl.setPosition(Vector3f.ZERO);
    sl.setDirection(Vector3f.UNIT_Z);
    geom.setLocalTranslation(Vector3f.ZERO);
    geom.setModelBound(new BoundingSphere(1f, Vector3f.ZERO));
    // Infinit spot lights are only filtered
    // if the geometry is outside the infinite cone.
    sl.setSpotRange(0);
    checkFilteredLights(1);
    //the geommetry is outside the infinit cone (cone direction going away from the geom)
    sl.setPosition(Vector3f.UNIT_Z.mult(1 + FastMath.ZERO_TOLERANCE));
    checkFilteredLights(0);
    //place the spote ligth in the corner of the box geom, (in order to test bounding sphere)
    sl.setDirection(new Vector3f(1, 1, 0).normalizeLocal());
    geom.setLocalTranslation(0, 0, 10);
    sl.setPosition(sl.getDirection().mult(-2f).add(geom.getLocalTranslation()));
    // make it barely reach the sphere, incorect with a box
    sl.setSpotRange(1f - FastMath.ZERO_TOLERANCE);
    checkFilteredLights(0);
    // make it reach the sphere
    sl.setSpotRange(1f + FastMath.ZERO_TOLERANCE);
    checkFilteredLights(1);
    // extent the range
    sl.setPosition(Vector3f.ZERO);
    sl.setDirection(Vector3f.UNIT_Z);
    sl.setSpotRange(20);
    checkFilteredLights(1);
    // rotate the cone a bit so it no longer faces the geom
    sl.setDirection(new Vector3f(0, 0.3f, 0.7f).normalizeLocal());
    checkFilteredLights(0);
    // Create sphere of size X=10 (double the radius)
    // now, the spot will touch the sphere.
    geom.setModelBound(new BoundingSphere(5f, Vector3f.ZERO));
    checkFilteredLights(1);
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) Vector3f(com.jme3.math.Vector3f) Box(com.jme3.scene.shape.Box) TempVars(com.jme3.util.TempVars) Test(org.junit.Test)

Example 13 with Geometry

use of com.jme3.scene.Geometry in project jmonkeyengine by jMonkeyEngine.

the class LightSortTest method testSceneGraphSort.

@Test
public void testSceneGraphSort() {
    Node n = new Node("node");
    Geometry g = new Geometry("geom", new Mesh());
    SpotLight spot = new SpotLight(Vector3f.ZERO, Vector3f.UNIT_X);
    PointLight point = new PointLight(Vector3f.UNIT_X);
    DirectionalLight directional = new DirectionalLight(Vector3f.UNIT_X);
    AmbientLight ambient = new AmbientLight();
    // Some lights are on the node
    n.addLight(spot);
    n.addLight(point);
    // .. and some on the geometry.
    g.addLight(directional);
    g.addLight(ambient);
    n.attachChild(g);
    n.updateGeometricState();
    LightList list = g.getWorldLightList();
    // check the sorting (when geom is at 0,0,0)
    assert list.get(0) instanceof AmbientLight;
    assert list.get(1) instanceof DirectionalLight;
    assert list.get(2) instanceof SpotLight;
    assert list.get(3) instanceof PointLight;
    // move the geometry closer to the point light
    g.setLocalTranslation(Vector3f.UNIT_X);
    n.updateGeometricState();
    assert list.get(0) instanceof AmbientLight;
    assert list.get(1) instanceof DirectionalLight;
    assert list.get(2) instanceof PointLight;
    assert list.get(3) instanceof SpotLight;
// now move the point light away from the geometry
// and the spot light closer
// XXX: doesn't work! jME can't detect that the light moved!
//        point.setPosition(Vector3f.ZERO);
//        spot.setPosition(Vector3f.UNIT_X);
//        n.updateGeometricState();
//        
//        assert list.get(0) instanceof AmbientLight;
//        assert list.get(1) instanceof DirectionalLight;
//        assert list.get(2) instanceof SpotLight;
//        assert list.get(3) instanceof PointLight;
}
Also used : Geometry(com.jme3.scene.Geometry) Node(com.jme3.scene.Node) Mesh(com.jme3.scene.Mesh) Test(org.junit.Test)

Example 14 with Geometry

use of com.jme3.scene.Geometry in project jmonkeyengine by jMonkeyEngine.

the class SceneLoader method loadObjects.

private void loadObjects(FbxElement element) throws IOException {
    FbxObject obj = null;
    for (FbxElement e : element.children) {
        switch(e.id) {
            case "Geometry":
                FbxMesh mesh = new FbxMesh(this, e);
                obj = mesh;
                if (mesh.geometries != null)
                    geomMap.put(mesh.id, mesh);
                break;
            case "Material":
                obj = new FbxMaterial(this, e);
                break;
            case "Model":
                FbxNode node = new FbxNode(this, e);
                obj = node;
                modelMap.put(node.id, node);
                if (node.isLimb())
                    limbMap.put(node.id, node);
                break;
            case "Pose":
                FbxBindPose pose = new FbxBindPose(this, e);
                obj = pose;
                bindMap.put(pose.id, pose);
                break;
            case "Texture":
                obj = new FbxTexture(this, e);
                break;
            case "Video":
                obj = new FbxImage(this, e);
                break;
            case "Deformer":
                obj = loadDeformer(e);
                break;
            case "AnimationLayer":
                FbxObject layer = new FbxObject(this, e);
                obj = layer;
                alayerMap.put(layer.id, layer);
                break;
            case "AnimationCurve":
                obj = new FbxAnimCurve(this, e);
                break;
            case "AnimationCurveNode":
                obj = new FbxAnimNode(this, e);
                break;
            default:
                obj = null;
        }
        if (obj != null)
            allObjects.put(obj.id, obj);
    }
}
Also used : FbxElement(com.jme3.scene.plugins.fbx.file.FbxElement) FbxObject(com.jme3.scene.plugins.fbx.objects.FbxObject) FbxNode(com.jme3.scene.plugins.fbx.objects.FbxNode) FbxAnimCurve(com.jme3.scene.plugins.fbx.objects.FbxAnimCurve) FbxTexture(com.jme3.scene.plugins.fbx.objects.FbxTexture) FbxMesh(com.jme3.scene.plugins.fbx.objects.FbxMesh) FbxBindPose(com.jme3.scene.plugins.fbx.objects.FbxBindPose) FbxAnimNode(com.jme3.scene.plugins.fbx.objects.FbxAnimNode) FbxMaterial(com.jme3.scene.plugins.fbx.objects.FbxMaterial) FbxImage(com.jme3.scene.plugins.fbx.objects.FbxImage)

Example 15 with Geometry

use of com.jme3.scene.Geometry 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)

Aggregations

Geometry (com.jme3.scene.Geometry)246 Material (com.jme3.material.Material)175 Vector3f (com.jme3.math.Vector3f)116 Box (com.jme3.scene.shape.Box)92 DirectionalLight (com.jme3.light.DirectionalLight)56 Node (com.jme3.scene.Node)54 Sphere (com.jme3.scene.shape.Sphere)49 Spatial (com.jme3.scene.Spatial)41 Quaternion (com.jme3.math.Quaternion)39 Quad (com.jme3.scene.shape.Quad)33 Texture (com.jme3.texture.Texture)30 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)26 Mesh (com.jme3.scene.Mesh)25 KeyTrigger (com.jme3.input.controls.KeyTrigger)24 AmbientLight (com.jme3.light.AmbientLight)24 ColorRGBA (com.jme3.math.ColorRGBA)21 FilterPostProcessor (com.jme3.post.FilterPostProcessor)21 PointLight (com.jme3.light.PointLight)20 Vector2f (com.jme3.math.Vector2f)17 FloatBuffer (java.nio.FloatBuffer)17