Search in sources :

Example 1 with LodGenerator

use of jme3tools.optimize.LodGenerator in project jmonkeyengine by jMonkeyEngine.

the class TestLodGeneration method makeLod.

private void makeLod(final LodGenerator.TriangleReductionMethod method, final float value, final int ll) {
    exec.execute(new Runnable() {

        public void run() {
            for (final Geometry geometry : listGeoms) {
                LodGenerator lODGenerator = new LodGenerator(geometry);
                final VertexBuffer[] lods = lODGenerator.computeLods(method, value);
                enqueue(new Callable<Void>() {

                    public Void call() throws Exception {
                        geometry.getMesh().setLodLevels(lods);
                        lodLevel = 0;
                        if (geometry.getMesh().getNumLodLevels() > ll) {
                            lodLevel = ll;
                        }
                        geometry.setLodLevel(lodLevel);
                        hudText.setText(computeNbTri() + " tris");
                        return null;
                    }
                });
            }
        }
    });
}
Also used : Geometry(com.jme3.scene.Geometry) LodGenerator(jme3tools.optimize.LodGenerator) Callable(java.util.concurrent.Callable)

Example 2 with LodGenerator

use of jme3tools.optimize.LodGenerator in project jmonkeyengine by jMonkeyEngine.

the class TestLodGeneration method simpleInitApp.

public void simpleInitApp() {
    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
    rootNode.addLight(dl);
    AmbientLight al = new AmbientLight();
    al.setColor(ColorRGBA.White.mult(0.6f));
    rootNode.addLight(al);
    // model = (Node) assetManager.loadModel("Models/Sinbad/Sinbad.mesh.xml");
    model = (Node) assetManager.loadModel("Models/Jaime/Jaime.j3o");
    BoundingBox b = ((BoundingBox) model.getWorldBound());
    model.setLocalScale(1.2f / (b.getYExtent() * 2));
    //  model.setLocalTranslation(0,-(b.getCenter().y - b.getYExtent())* model.getLocalScale().y, 0);
    for (Spatial spatial : model.getChildren()) {
        if (spatial instanceof Geometry) {
            listGeoms.add((Geometry) spatial);
        }
    }
    ChaseCamera chaseCam = new ChaseCamera(cam, inputManager);
    model.addControl(chaseCam);
    chaseCam.setLookAtOffset(b.getCenter());
    chaseCam.setDefaultDistance(5);
    chaseCam.setMinVerticalRotation(-FastMath.HALF_PI + 0.01f);
    chaseCam.setZoomSensitivity(0.5f);
    //           ch = model.getControl(AnimControl.class).createChannel();
    //          ch.setAnim("Wave");
    SkeletonControl c = model.getControl(SkeletonControl.class);
    if (c != null) {
        c.setEnabled(false);
    }
    reductionvalue = 0.80f;
    lodLevel = 1;
    for (final Geometry geometry : listGeoms) {
        LodGenerator lodGenerator = new LodGenerator(geometry);
        lodGenerator.bakeLods(LodGenerator.TriangleReductionMethod.PROPORTIONAL, reductionvalue);
        geometry.setLodLevel(lodLevel);
    }
    rootNode.attachChild(model);
    flyCam.setEnabled(false);
    guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
    hudText = new BitmapText(guiFont, false);
    hudText.setSize(guiFont.getCharSet().getRenderedSize());
    hudText.setText(computeNbTri() + " tris");
    hudText.setLocalTranslation(cam.getWidth() / 2, hudText.getLineHeight(), 0);
    guiNode.attachChild(hudText);
    inputManager.addListener(new ActionListener() {

        public void onAction(String name, boolean isPressed, float tpf) {
            if (isPressed) {
                if (name.equals("plus")) {
                    //                        lodLevel++;
                    //                        for (Geometry geometry : listGeoms) {
                    //                            if (geometry.getMesh().getNumLodLevels() <= lodLevel) {
                    //                                lodLevel = 0;
                    //                            }
                    //                            geometry.setLodLevel(lodLevel);
                    //                        }
                    //                        jaimeText.setText(computeNbTri() + " tris");
                    reductionvalue += 0.05f;
                    updateLod();
                }
                if (name.equals("minus")) {
                    //                        lodLevel--;
                    //                        for (Geometry geometry : listGeoms) {
                    //                            if (lodLevel < 0) {
                    //                                lodLevel = geometry.getMesh().getNumLodLevels() - 1;
                    //                            }
                    //                            geometry.setLodLevel(lodLevel);
                    //                        }
                    //                        jaimeText.setText(computeNbTri() + " tris");
                    reductionvalue -= 0.05f;
                    updateLod();
                }
                if (name.equals("wireFrame")) {
                    wireFrame = !wireFrame;
                    for (Geometry geometry : listGeoms) {
                        geometry.getMaterial().getAdditionalRenderState().setWireframe(wireFrame);
                    }
                }
            }
        }

        private void updateLod() {
            reductionvalue = FastMath.clamp(reductionvalue, 0.0f, 1.0f);
            makeLod(LodGenerator.TriangleReductionMethod.PROPORTIONAL, reductionvalue, 1);
        }
    }, "plus", "minus", "wireFrame");
    inputManager.addMapping("plus", new KeyTrigger(KeyInput.KEY_ADD));
    inputManager.addMapping("minus", new KeyTrigger(KeyInput.KEY_SUBTRACT));
    inputManager.addMapping("wireFrame", new KeyTrigger(KeyInput.KEY_SPACE));
}
Also used : KeyTrigger(com.jme3.input.controls.KeyTrigger) ChaseCamera(com.jme3.input.ChaseCamera) SkeletonControl(com.jme3.animation.SkeletonControl) Geometry(com.jme3.scene.Geometry) LodGenerator(jme3tools.optimize.LodGenerator) BitmapText(com.jme3.font.BitmapText) ActionListener(com.jme3.input.controls.ActionListener) Spatial(com.jme3.scene.Spatial) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) BoundingBox(com.jme3.bounding.BoundingBox) AmbientLight(com.jme3.light.AmbientLight)

Example 3 with LodGenerator

use of jme3tools.optimize.LodGenerator in project chordatlas by twak.

the class LineGen3d method calculate.

@Override
public void calculate() {
    for (Spatial s : gNode.getChildren()) s.removeFromParent();
    {
        Geometry geom;
        Mesh m = new Mesh();
        m.setMode(Mesh.Mode.Lines);
        List<Float> coords = new ArrayList();
        List<Integer> inds = new ArrayList();
        for (Line3d l : getLines()) {
            inds.add(inds.size());
            inds.add(inds.size());
            coords.add((float) l.start.x);
            coords.add((float) l.start.y);
            coords.add((float) l.start.z);
            coords.add((float) l.end.x);
            coords.add((float) l.end.y);
            coords.add((float) l.end.z);
        }
        m.setBuffer(VertexBuffer.Type.Position, 3, Arrayz.toFloatArray(coords));
        m.setBuffer(VertexBuffer.Type.Index, 2, Arrayz.toIntArray(inds));
        geom = new Geometry(filename, m);
        geom.setCullHint(CullHint.Never);
        Material lineMaterial = new Material(tweed.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
        lineMaterial.setColor("Color", new ColorRGBA(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, 1f));
        geom.setMaterial(lineMaterial);
        geom.setLocalTranslation(0, 0, 0);
        gNode.attachChild(geom);
    }
    // int c = 0;
    VertexBuffer emptyVB = new VertexBuffer(Type.Index);
    emptyVB.setupData(Usage.Static, 3, Format.UnsignedShort, BufferUtils.createShortBuffer(0));
    {
        Geometry geom;
        Random randy = new Random();
        for (Map.Entry<Loop<Point3d>, Integer> e : getFaces().entrySet()) {
            Loop<Point3d> p = e.getKey();
            final int callbackI = e.getValue();
            Mesh m = Jme3z.fromLoop(p);
            // m.setMode( Mesh.Mode.Triangles );
            // 
            // List<Integer> inds = new ArrayList<>();
            // List<Float> pos = new ArrayList<>();
            // List<Float> norms = new ArrayList<>();
            // 
            // Loopz.triangulate( p, true, inds, pos, norms );
            // 
            // m.set setVe( Type.Index, Arrayz.toIntArray(inds));
            geom = new Geometry(filename, m);
            geom.setCullHint(CullHint.Never);
            // ColorRGBA col = Jme3z.toJme( Rainbow.getColour( c++ ) );
            ColorRGBA col = new ColorRGBA(color.getRed() * randy.nextFloat() / 500f + 0.1f, color.getGreen() * randy.nextFloat() / 500f + 0.1f, color.getBlue() * randy.nextFloat() / 500f + 0.1f, 1f);
            Material mat = new Material(tweed.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
            mat.setColor("Diffuse", col);
            mat.setColor("Ambient", col);
            mat.setBoolean("UseMaterialColors", true);
            geom.setUserData(ClickMe.class.getSimpleName(), new Object[] { new ClickMe() {

                @Override
                public void clicked(Object data) {
                    polyClicked(callbackI);
                }
            } });
            geom.setUserData(Gen.class.getSimpleName(), new Object[] { this });
            geom.setMaterial(mat);
            geom.setLocalTranslation(0, 0, 0);
            if (TweedSettings.settings.LOD) {
                LodGenerator lod = new LodGenerator(geom);
                lod.bakeLods(LodGenerator.TriangleReductionMethod.COLLAPSE_COST, 10, 100);
                GISLodControl lc = new GISLodControl();
                lc.setTrisPerPixel(0.000001f);
                geom.addControl(lc);
            }
            gNode.attachChild(geom);
        }
    }
    gNode.updateModelBound();
    super.calculate();
}
Also used : Loop(org.twak.utils.collections.Loop) ClickMe(org.twak.tweed.ClickMe) VertexBuffer(com.jme3.scene.VertexBuffer) ArrayList(java.util.ArrayList) Mesh(com.jme3.scene.Mesh) Material(com.jme3.material.Material) Line3d(org.twak.utils.geom.Line3d) Geometry(com.jme3.scene.Geometry) LodGenerator(jme3tools.optimize.LodGenerator) ColorRGBA(com.jme3.math.ColorRGBA) Random(java.util.Random) Spatial(com.jme3.scene.Spatial) Point3d(javax.vecmath.Point3d) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Geometry (com.jme3.scene.Geometry)3 LodGenerator (jme3tools.optimize.LodGenerator)3 Spatial (com.jme3.scene.Spatial)2 SkeletonControl (com.jme3.animation.SkeletonControl)1 BoundingBox (com.jme3.bounding.BoundingBox)1 BitmapText (com.jme3.font.BitmapText)1 ChaseCamera (com.jme3.input.ChaseCamera)1 ActionListener (com.jme3.input.controls.ActionListener)1 KeyTrigger (com.jme3.input.controls.KeyTrigger)1 AmbientLight (com.jme3.light.AmbientLight)1 DirectionalLight (com.jme3.light.DirectionalLight)1 Material (com.jme3.material.Material)1 ColorRGBA (com.jme3.math.ColorRGBA)1 Vector3f (com.jme3.math.Vector3f)1 Mesh (com.jme3.scene.Mesh)1 VertexBuffer (com.jme3.scene.VertexBuffer)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Random (java.util.Random)1 Callable (java.util.concurrent.Callable)1