Search in sources :

Example 61 with VertexBuffer

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

Example 62 with VertexBuffer

use of com.jme3.scene.VertexBuffer in project chordatlas by twak.

the class SkelFootprint method getColor.

public static ColorRGBA getColor(Geometry geom, Vector3f pt, int index, Tweed tweed) {
    MatParam param = geom.getMaterial().getParam("DiffuseMap");
    ImageRaster ir = ImageRaster.create(tweed.getAssetManager().loadTexture(((Texture2D) param.getValue()).getName()).getImage());
    Mesh mesh = geom.getMesh();
    // geom.getMaterial().getMaterialDef().
    VertexBuffer pb = mesh.getBuffer(Type.Position);
    VertexBuffer tb = mesh.getBuffer(Type.TexCoord);
    IndexBuffer ib = mesh.getIndicesAsList();
    Vector2f uva = new Vector2f(), uvb = new Vector2f(), uvc = new Vector2f();
    Vector3f la = new Vector3f(), lb = new Vector3f(), lc = new Vector3f();
    if (pb != null && pb.getFormat() == Format.Float && pb.getNumComponents() == 3) {
        FloatBuffer fpb = (FloatBuffer) pb.getData();
        FloatBuffer ftb = (FloatBuffer) tb.getData();
        // aquire triangle's vertex indices
        int vertIndex = index * 3;
        int va = ib.get(vertIndex);
        int vb = ib.get(vertIndex + 1);
        int vc = ib.get(vertIndex + 2);
        BufferUtils.populateFromBuffer(la, fpb, va);
        BufferUtils.populateFromBuffer(lb, fpb, vb);
        BufferUtils.populateFromBuffer(lc, fpb, vc);
        BufferUtils.populateFromBuffer(uva, ftb, va);
        BufferUtils.populateFromBuffer(uvb, ftb, vb);
        BufferUtils.populateFromBuffer(uvc, ftb, vc);
        // PaintThing.debug.put(1, new Line ( la.x, la.z, lb.x, lb.z) );
        // PaintThing.debug.put(2, new Line ( lb.x, lb.z, lc.x, lc.z) );
        // PaintThing.debug.put(3, new Line ( lc.x, lc.z, la.x, la.z) );
        float[] bary = barycentric(pt, la, lb, lc);
        int x = (int) ((uva.x * bary[0] + uvb.x * bary[1] + uvc.x * bary[2]) * ir.getWidth()), y = (int) ((uva.y * bary[0] + uvb.y * bary[1] + uvc.y * bary[2]) * ir.getHeight());
        // ir.getHeight() - y -1 );
        ColorRGBA out = ir.getPixel(x, y);
        return out;
    } else {
        throw new UnsupportedOperationException("Position buffer not set or has incompatible format");
    }
}
Also used : IndexBuffer(com.jme3.scene.mesh.IndexBuffer) MatParam(com.jme3.material.MatParam) ColorRGBA(com.jme3.math.ColorRGBA) VertexBuffer(com.jme3.scene.VertexBuffer) Vector2f(com.jme3.math.Vector2f) Vector3f(com.jme3.math.Vector3f) Mesh(com.jme3.scene.Mesh) ImageRaster(com.jme3.texture.image.ImageRaster) FloatBuffer(java.nio.FloatBuffer) MFPoint(org.twak.tweed.gen.FeatureCache.MFPoint)

Example 63 with VertexBuffer

use of com.jme3.scene.VertexBuffer in project TeachingInSimulation by ScOrPiOzzy.

the class JmeUtil method getLineStart.

/**
 * 获取Mesh line的真实的Start点,预设
 * @param line com.jme3.scene.shape.Line
 */
public static Vector3f getLineStart(Line line) {
    VertexBuffer position = line.getBuffer(Type.Position);
    float[] arr = BufferUtils.getFloatArray((FloatBuffer) position.getDataReadOnly());
    return new Vector3f(arr[0], arr[1], arr[2]);
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) Vector3f(com.jme3.math.Vector3f)

Aggregations

VertexBuffer (com.jme3.scene.VertexBuffer)45 FloatBuffer (java.nio.FloatBuffer)43 Vector3f (com.jme3.math.Vector3f)20 ByteBuffer (java.nio.ByteBuffer)12 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)10 ShortBuffer (java.nio.ShortBuffer)9 IntBuffer (java.nio.IntBuffer)8 Mesh (com.jme3.scene.Mesh)7 Geometry (com.jme3.scene.Geometry)6 Buffer (java.nio.Buffer)6 ColorRGBA (com.jme3.math.ColorRGBA)5 Vector2f (com.jme3.math.Vector2f)5 ArrayList (java.util.ArrayList)5 Bone (com.jme3.animation.Bone)4 Material (com.jme3.material.Material)4 Matrix4f (com.jme3.math.Matrix4f)4 Type (com.jme3.scene.VertexBuffer.Type)3 Texture (com.jme3.texture.Texture)3 TempVars (com.jme3.util.TempVars)3 HashMap (java.util.HashMap)3