Search in sources :

Example 1 with Cancellable

use of org.twak.utils.ui.Cancellable in project chordatlas by twak.

the class TexGen method setTexture.

protected void setTexture(Geometry g, Material mat, int[] texSize, BufferedImage srcPano, File writeTo) {
    if (!autoProject)
        return;
    if (cancel != null)
        cancel.cancel();
    cancel = new Cancellable();
    new Thread() {

        public void run() {
            VertexBuffer vb = g.getMesh().getBuffer(VertexBuffer.Type.TexCoord);
            VertexBuffer ib = g.getMesh().getBuffer(VertexBuffer.Type.Index);
            VertexBuffer pb = g.getMesh().getBuffer(VertexBuffer.Type.Position);
            float[][] pts = new float[3][3], uvs = new float[3][2];
            BufferedImage target = new BufferedImage(texSize[0], texSize[1], BufferedImage.TYPE_3BYTE_BGR);
            // if (false) {
            for (int i = 0; i < ib.getNumElements(); i++) {
                assert ib.getNumComponents() == 3;
                for (int c = 0; c < ib.getNumComponents(); c++) {
                    int ind = ((Number) ib.getElementComponent(i, c)).intValue();
                    for (int x = 0; x < 3; x++) pts[c][x] = ((Number) pb.getElementComponent(ind, x)).floatValue();
                    for (int u = 0; u < 2; u++) uvs[c][u] = ((Number) vb.getElementComponent(ind, u)).floatValue() * texSize[u];
                }
                cast(pts, uvs, srcPano, target, cancel, texSize);
                if (cancel.cancelled)
                    return;
            }
            try {
                System.out.println("writing " + writeTo);
                ImageIO.write(target, "png", writeTo);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (mat != null)
                tweed.enqueue(new Callable<Spatial>() {

                    public Spatial call() throws Exception {
                        System.out.println("calculations complete");
                        TextureKey key = new TextureKey("Desktop/foo.png", false);
                        tweed.getAssetManager().deleteFromCache(key);
                        Texture texture = tweed.getAssetManager().loadTexture(key);
                        mat.setTexture("DiffuseMap", texture);
                        return null;
                    }
                });
        }
    }.start();
}
Also used : TextureKey(com.jme3.asset.TextureKey) VertexBuffer(com.jme3.scene.VertexBuffer) Spatial(com.jme3.scene.Spatial) Cancellable(org.twak.utils.ui.Cancellable) IOException(java.io.IOException) Texture(com.jme3.texture.Texture) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException)

Aggregations

TextureKey (com.jme3.asset.TextureKey)1 Spatial (com.jme3.scene.Spatial)1 VertexBuffer (com.jme3.scene.VertexBuffer)1 Texture (com.jme3.texture.Texture)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 Cancellable (org.twak.utils.ui.Cancellable)1