|
|
|
/*
|
|
|
|
* Copyright (c) 2009-2010 jMonkeyEngine
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
package jme3tools.optimize;
|
|
|
|
|
|
|
|
import com.jme3.asset.AssetKey;
|
|
|
|
import com.jme3.math.Vector2f;
|
|
|
|
import com.jme3.texture.Image;
|
|
|
|
import com.jme3.texture.Image.Format;
|
|
|
|
import com.jme3.texture.Texture;
|
|
|
|
import com.jme3.texture.Texture2D;
|
|
|
|
import com.jme3.util.BufferUtils;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.FloatBuffer;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.TreeMap;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author Lukasz Bruun - lukasz.dk, normenhansen
|
|
|
|
*/
|
|
|
|
public class TextureAtlas {
|
|
|
|
|
|
|
|
private static final Logger logger = Logger.getLogger(TextureAtlas.class.getName());
|
|
|
|
private Map<String, byte[]> images;
|
|
|
|
private int atlasWidth, atlasHeight;
|
|
|
|
private Format format = Format.ABGR8;
|
|
|
|
private Node root;
|
|
|
|
private Map<String, TextureAtlasTile> locationMap;
|
|
|
|
private String rootMapName;
|
|
|
|
|
|
|
|
public TextureAtlas(int width, int height) {
|
|
|
|
this.atlasWidth = width;
|
|
|
|
this.atlasHeight = height;
|
|
|
|
root = new Node(0, 0, width, height);
|
|
|
|
locationMap = new TreeMap<String, TextureAtlasTile>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a texture for a specific map name
|
|
|
|
* @param texture A texture to add to the atlas
|
|
|
|
* @param mapName A freely chosen map name that can be later retrieved as a Texture. The first map name supplied will be the master texture.
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public boolean addTexture(Texture texture, String mapName) {
|
|
|
|
return addTexture(texture, mapName, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a texture for a specific map name at the location of another existing texture.
|
|
|
|
* @param texture A texture to add to the atlas.
|
|
|
|
* @param mapName A freely chosen map name that can be later retrieved as a Texture.
|
|
|
|
* @param sourceTextureName Name of the original texture location.
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public boolean addTexture(Texture texture, String mapName, String sourceTextureName) {
|
|
|
|
if (texture == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
AssetKey key = texture.getKey();
|
|
|
|
if (texture.getImage() != null && key != null && key.getName() != null) {
|
|
|
|
return addImage(texture.getImage(), key.getName(), mapName, sourceTextureName);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean addImage(Image image, String name, String mapName, String sourceTextureName) {
|
|
|
|
if (rootMapName == null) {
|
|
|
|
rootMapName = mapName;
|
|
|
|
}
|
|
|
|
if (sourceTextureName == null && !rootMapName.equals(mapName)) {
|
|
|
|
throw new IllegalStateException("Cannot add texture to new map without source texture");
|
|
|
|
}
|
|
|
|
TextureAtlasTile location;
|
|
|
|
if (sourceTextureName == null) {
|
|
|
|
Node node = root.insert(image);
|
|
|
|
if (node == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
location = node.location;
|
|
|
|
} else {
|
|
|
|
location = locationMap.get(sourceTextureName);
|
|
|
|
}
|
|
|
|
if (location == null) {
|
|
|
|
logger.log(Level.WARNING, "Source texture not found");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
locationMap.put(name, location);
|
|
|
|
drawImage(image, location.getX(), location.getY(), mapName);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void drawImage(Image source, int x, int y, String mapName) {
|
|
|
|
//TODO: all buffers?
|
|
|
|
if (images == null) {
|
|
|
|
images = new HashMap<String, byte[]>();
|
|
|
|
}
|
|
|
|
byte[] image = images.get(mapName);
|
|
|
|
if (image == null) {
|
|
|
|
image = new byte[atlasWidth * atlasHeight * 4];
|
|
|
|
images.put(mapName, image);
|
|
|
|
}
|
|
|
|
ByteBuffer sourceData = source.getData(0);
|
|
|
|
int height = source.getHeight();
|
|
|
|
int width = source.getWidth();
|
|
|
|
for (int yPos = 0; yPos < height; yPos++) {
|
|
|
|
for (int xPos = 0; xPos < width; xPos++) {
|
|
|
|
int i = ((xPos + x) + (yPos + y) * atlasWidth) * 4;
|
|
|
|
if (source.getFormat() == Format.ABGR8) {
|
|
|
|
int j = (xPos + yPos * width) * 4;
|
|
|
|
image[i] = sourceData.get(j); //a
|
|
|
|
image[i + 1] = sourceData.get(j + 1); //b
|
|
|
|
image[i + 2] = sourceData.get(j + 2); //g
|
|
|
|
image[i + 3] = sourceData.get(j + 3); //r
|
|
|
|
} else if (source.getFormat() == Format.BGR8) {
|
|
|
|
int j = (xPos + yPos * width) * 3;
|
|
|
|
image[i] = 0; //b
|
|
|
|
image[i + 1] = sourceData.get(j); //b
|
|
|
|
image[i + 2] = sourceData.get(j + 1); //g
|
|
|
|
image[i + 3] = sourceData.get(j + 2); //r
|
|
|
|
} else if (source.getFormat() == Format.RGB8) {
|
|
|
|
int j = (xPos + yPos * width) * 3;
|
|
|
|
image[i] = 0; //b
|
|
|
|
image[i + 1] = sourceData.get(j + 2); //b
|
|
|
|
image[i + 2] = sourceData.get(j + 1); //g
|
|
|
|
image[i + 3] = sourceData.get(j); //r
|
|
|
|
} else if (source.getFormat() == Format.RGBA8) {
|
|
|
|
int j = (xPos + yPos * width) * 4;
|
|
|
|
image[i] = sourceData.get(j + 3); //a
|
|
|
|
image[i + 1] = sourceData.get(j + 2); //b
|
|
|
|
image[i + 2] = sourceData.get(j + 1); //g
|
|
|
|
image[i + 3] = sourceData.get(j); //r
|
|
|
|
} else {
|
|
|
|
logger.log(Level.WARNING, "Could not draw texture with format {0}", source.getFormat());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public TextureAtlasTile getAtlasTile(String assetName) {
|
|
|
|
return locationMap.get(assetName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Texture getAtlasTexture(String mapName) {
|
|
|
|
if (images == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
byte[] image = images.get(mapName);
|
|
|
|
if (image != null) {
|
|
|
|
Texture2D tex = new Texture2D(new Image(format, atlasWidth, atlasHeight, BufferUtils.createByteBuffer(image)));
|
|
|
|
tex.setMagFilter(Texture.MagFilter.Bilinear);
|
|
|
|
tex.setMinFilter(Texture.MinFilter.BilinearNearestMipMap);
|
|
|
|
tex.setWrap(Texture.WrapMode.Clamp);
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private class Node {
|
|
|
|
|
|
|
|
public TextureAtlasTile location;
|
|
|
|
public Node child[];
|
|
|
|
public Image image;
|
|
|
|
|
|
|
|
public Node(int x, int y, int width, int height) {
|
|
|
|
location = new TextureAtlasTile(x, y, width, height);
|
|
|
|
child = new Node[2];
|
|
|
|
child[0] = null;
|
|
|
|
child[1] = null;
|
|
|
|
image = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isLeaf() {
|
|
|
|
return child[0] == null && child[1] == null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Algorithm from http://www.blackpawn.com/texts/lightmaps/
|
|
|
|
public Node insert(Image image) {
|
|
|
|
if (!isLeaf()) {
|
|
|
|
Node newNode = child[0].insert(image);
|
|
|
|
|
|
|
|
if (newNode != null) {
|
|
|
|
return newNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return child[1].insert(image);
|
|
|
|
} else {
|
|
|
|
if (this.image != null) {
|
|
|
|
return null; // occupied
|
|
|
|
}
|
|
|
|
|
|
|
|
if (image.getWidth() > location.getWidth() || image.getHeight() > location.getHeight()) {
|
|
|
|
return null; // does not fit
|
|
|
|
}
|
|
|
|
|
|
|
|
if (image.getWidth() == location.getWidth() && image.getHeight() == location.getHeight()) {
|
|
|
|
this.image = image; // perfect fit
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dw = location.getWidth() - image.getWidth();
|
|
|
|
int dh = location.getHeight() - image.getHeight();
|
|
|
|
|
|
|
|
if (dw > dh) {
|
|
|
|
child[0] = new Node(location.getX(), location.getY(), image.getWidth(), location.getHeight());
|
|
|
|
child[1] = new Node(location.getX() + image.getWidth(), location.getY(), location.getWidth() - image.getWidth(), location.getHeight());
|
|
|
|
} else {
|
|
|
|
child[0] = new Node(location.getX(), location.getY(), location.getWidth(), image.getHeight());
|
|
|
|
child[1] = new Node(location.getX(), location.getY() + image.getHeight(), location.getWidth(), location.getHeight() - image.getHeight());
|
|
|
|
}
|
|
|
|
|
|
|
|
return child[0].insert(image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class TextureAtlasTile {
|
|
|
|
|
|
|
|
private int x;
|
|
|
|
private int y;
|
|
|
|
private int width;
|
|
|
|
private int height;
|
|
|
|
|
|
|
|
public TextureAtlasTile(int x, int y, int width, int height) {
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2f getLocation(Vector2f previousLocation) {
|
|
|
|
float x = (float) getX() / (float) atlasWidth;
|
|
|
|
float y = (float) getY() / (float) atlasHeight;
|
|
|
|
float w = (float) getWidth() / (float) atlasWidth;
|
|
|
|
float h = (float) getHeight() / (float) atlasHeight;
|
|
|
|
Vector2f location = new Vector2f(x, y);
|
|
|
|
Vector2f scale = new Vector2f(w, h);
|
|
|
|
// if (previousLocation.x > 1) {
|
|
|
|
// previousLocation.x = previousLocation.x - (int) previousLocation.x;
|
|
|
|
// }
|
|
|
|
// if (previousLocation.y > 1) {
|
|
|
|
// previousLocation.y = previousLocation.y - (int) previousLocation.y;
|
|
|
|
// }
|
|
|
|
return location.addLocal(previousLocation.multLocal(scale));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void transformTextureCoords(FloatBuffer inBuf, int offset, FloatBuffer outBuf) {
|
|
|
|
Vector2f tex = new Vector2f();
|
|
|
|
|
|
|
|
// offset is given in element units
|
|
|
|
// convert to be in component units
|
|
|
|
offset *= 2;
|
|
|
|
|
|
|
|
for (int i = 0; i < inBuf.capacity() / 2; i++) {
|
|
|
|
tex.x = inBuf.get(i * 2 + 0);
|
|
|
|
tex.y = inBuf.get(i * 2 + 1);
|
|
|
|
Vector2f outLoc = getLocation(tex);
|
|
|
|
outBuf.put(offset + i * 2 + 0, outLoc.x);
|
|
|
|
outBuf.put(offset + i * 2 + 1, outLoc.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getX() {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getY() {
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getWidth() {
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getHeight() {
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|