use of com.jme3.texture.Texture in project jmonkeyengine by jMonkeyEngine.
the class TextureAtlas method createAtlas.
/**
* Create a texture atlas for the given root node, containing DiffuseMap, NormalMap and SpecularMap.
* @param root The rootNode to create the atlas for.
* @param atlasSize The size of the atlas (width and height).
* @return Null if the atlas cannot be created because not all textures fit.
*/
public static TextureAtlas createAtlas(Spatial root, int atlasSize) {
List<Geometry> geometries = new ArrayList<Geometry>();
GeometryBatchFactory.gatherGeoms(root, geometries);
TextureAtlas atlas = new TextureAtlas(atlasSize, atlasSize);
for (Geometry geometry : geometries) {
if (!atlas.addGeometry(geometry)) {
logger.log(Level.WARNING, "Texture atlas size too small, cannot add all textures");
return null;
}
}
return atlas;
}
use of com.jme3.texture.Texture in project jmonkeyengine by jMonkeyEngine.
the class TextureAtlas method getAtlasTexture.
/**
* Creates a new atlas texture for the given map name.
* @param mapName
* @return the atlas texture
*/
public Texture getAtlasTexture(String mapName) {
if (images == null) {
return null;
}
byte[] image = images.get(mapName);
if (image != null) {
//TODO check if color space shouldn't be sRGB
Texture2D tex = new Texture2D(new Image(format, atlasWidth, atlasHeight, BufferUtils.createByteBuffer(image), null, ColorSpace.Linear));
tex.setMagFilter(Texture.MagFilter.Bilinear);
tex.setMinFilter(Texture.MinFilter.BilinearNearestMipMap);
tex.setWrap(Texture.WrapMode.EdgeClamp);
return tex;
}
return null;
}
use of com.jme3.texture.Texture in project jmonkeyengine by jMonkeyEngine.
the class TextureAtlas method drawImage.
private void drawImage(Image source, int x, int y, String mapName) {
if (images == null) {
images = new HashMap<String, byte[]>();
}
byte[] image = images.get(mapName);
//Texture Atlas should linearize the data if the source image isSRGB
if (image == null) {
image = new byte[atlasWidth * atlasHeight * 4];
images.put(mapName, image);
}
//TODO: all buffers?
ByteBuffer sourceData = source.getData(0);
int height = source.getHeight();
int width = source.getWidth();
Image newImage = null;
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;
//a
image[i] = sourceData.get(j);
//b
image[i + 1] = sourceData.get(j + 1);
//g
image[i + 2] = sourceData.get(j + 2);
//r
image[i + 3] = sourceData.get(j + 3);
} else if (source.getFormat() == Format.BGR8) {
int j = (xPos + yPos * width) * 3;
//a
image[i] = 1;
//b
image[i + 1] = sourceData.get(j);
//g
image[i + 2] = sourceData.get(j + 1);
//r
image[i + 3] = sourceData.get(j + 2);
} else if (source.getFormat() == Format.RGB8) {
int j = (xPos + yPos * width) * 3;
//a
image[i] = 1;
//b
image[i + 1] = sourceData.get(j + 2);
//g
image[i + 2] = sourceData.get(j + 1);
//r
image[i + 3] = sourceData.get(j);
} else if (source.getFormat() == Format.RGBA8) {
int j = (xPos + yPos * width) * 4;
//a
image[i] = sourceData.get(j + 3);
//b
image[i + 1] = sourceData.get(j + 2);
//g
image[i + 2] = sourceData.get(j + 1);
//r
image[i + 3] = sourceData.get(j);
} else if (source.getFormat() == Format.Luminance8) {
int j = (xPos + yPos * width) * 1;
//a
image[i] = 1;
//b
image[i + 1] = sourceData.get(j);
//g
image[i + 2] = sourceData.get(j);
//r
image[i + 3] = sourceData.get(j);
} else if (source.getFormat() == Format.Luminance8Alpha8) {
int j = (xPos + yPos * width) * 2;
//a
image[i] = sourceData.get(j + 1);
//b
image[i + 1] = sourceData.get(j);
//g
image[i + 2] = sourceData.get(j);
//r
image[i + 3] = sourceData.get(j);
} else {
//ImageToAwt conversion
if (newImage == null) {
newImage = convertImageToAwt(source);
if (newImage != null) {
source = newImage;
sourceData = source.getData(0);
int j = (xPos + yPos * width) * 4;
//a
image[i] = sourceData.get(j);
//b
image[i + 1] = sourceData.get(j + 1);
//g
image[i + 2] = sourceData.get(j + 2);
//r
image[i + 3] = sourceData.get(j + 3);
} else {
throw new UnsupportedOperationException("Cannot draw or convert textures with format " + source.getFormat());
}
} else {
throw new UnsupportedOperationException("Cannot draw textures with format " + source.getFormat());
}
}
}
}
}
use of com.jme3.texture.Texture in project jmonkeyengine by jMonkeyEngine.
the class J3MLoaderTest method setupMockForTexture.
private TextureKey setupMockForTexture(final String paramName, final String path, final boolean flipY, final Texture texture) {
when(materialDef.getMaterialParam(paramName)).thenReturn(new MatParamTexture(VarType.Texture2D, paramName, texture, null));
final TextureKey textureKey = new TextureKey(path, flipY);
textureKey.setGenerateMips(true);
when(assetManager.loadTexture(textureKey)).thenReturn(texture);
return textureKey;
}
use of com.jme3.texture.Texture in project jmonkeyengine by jMonkeyEngine.
the class J3MLoaderTest method oldStyleTextureParameters_shouldBeSupported.
@Test
public void oldStyleTextureParameters_shouldBeSupported() throws Exception {
when(assetInfo.openStream()).thenReturn(J3MLoader.class.getResourceAsStream("/texture-parameters-oldstyle.j3m"));
final Texture textureOldStyle = Mockito.mock(Texture.class);
final Texture textureOldStyleUsingQuotes = Mockito.mock(Texture.class);
final TextureKey textureKeyUsingQuotes = setupMockForTexture("OldStyleUsingQuotes", "old style using quotes/texture.png", true, textureOldStyleUsingQuotes);
final TextureKey textureKeyOldStyle = setupMockForTexture("OldStyle", "old style/texture.png", true, textureOldStyle);
j3MLoader.load(assetInfo);
verify(assetManager).loadTexture(textureKeyUsingQuotes);
verify(assetManager).loadTexture(textureKeyOldStyle);
verify(textureOldStyle).setWrap(Texture.WrapMode.Repeat);
verify(textureOldStyleUsingQuotes).setWrap(Texture.WrapMode.Repeat);
}
Aggregations