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();
}
Aggregations