use of com.jme3.asset.TextureKey in project jmonkeyengine by jMonkeyEngine.
the class TestBrickWall method initMaterial.
public void initMaterial() {
mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
TextureKey key = new TextureKey("Textures/Terrain/BrickWall/BrickWall.jpg");
key.setGenerateMips(true);
Texture tex = assetManager.loadTexture(key);
mat.setTexture("ColorMap", tex);
mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
key2.setGenerateMips(true);
Texture tex2 = assetManager.loadTexture(key2);
mat2.setTexture("ColorMap", tex2);
mat3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
TextureKey key3 = new TextureKey("Textures/Terrain/Pond/Pond.jpg");
key3.setGenerateMips(true);
Texture tex3 = assetManager.loadTexture(key3);
tex3.setWrap(WrapMode.Repeat);
mat3.setTexture("ColorMap", tex3);
}
use of com.jme3.asset.TextureKey in project jmonkeyengine by jMonkeyEngine.
the class PhysicsTestHelper method createBallShooter.
/**
* creates the necessary inputlistener and action to shoot balls from teh camera
* @param app
* @param rootNode
* @param space
*/
public static void createBallShooter(final Application app, final Node rootNode, final PhysicsSpace space) {
ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
Sphere bullet = new Sphere(32, 32, 0.4f, true, false);
bullet.setTextureMode(TextureMode.Projected);
Material mat2 = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
key2.setGenerateMips(true);
Texture tex2 = app.getAssetManager().loadTexture(key2);
mat2.setTexture("ColorMap", tex2);
if (name.equals("shoot") && !keyPressed) {
Geometry bulletg = new Geometry("bullet", bullet);
bulletg.setMaterial(mat2);
bulletg.setShadowMode(ShadowMode.CastAndReceive);
bulletg.setLocalTranslation(app.getCamera().getLocation());
RigidBodyControl bulletControl = new RigidBodyControl(10);
bulletg.addControl(bulletControl);
bulletControl.setLinearVelocity(app.getCamera().getDirection().mult(25));
bulletg.addControl(bulletControl);
rootNode.attachChild(bulletg);
space.add(bulletControl);
}
}
};
app.getInputManager().addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
app.getInputManager().addListener(actionListener, "shoot");
}
use of com.jme3.asset.TextureKey in project jmonkeyengine by jMonkeyEngine.
the class AndroidBufferImageLoader method load.
public Object load(AssetInfo assetInfo) throws IOException {
Bitmap bitmap = null;
Image.Format format;
InputStream in = null;
int bpp;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferQualityOverSpeed = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inTempStorage = tempData;
options.inScaled = false;
options.inDither = false;
options.inInputShareable = true;
options.inPurgeable = true;
options.inSampleSize = 1;
try {
in = assetInfo.openStream();
bitmap = BitmapFactory.decodeStream(in, null, options);
if (bitmap == null) {
throw new IOException("Failed to load image: " + assetInfo.getKey().getName());
}
} finally {
if (in != null) {
in.close();
}
}
switch(bitmap.getConfig()) {
case ALPHA_8:
format = Image.Format.Alpha8;
bpp = 1;
break;
case ARGB_8888:
format = Image.Format.RGBA8;
bpp = 4;
break;
case RGB_565:
format = Image.Format.RGB565;
bpp = 2;
break;
default:
throw new UnsupportedOperationException("Unrecognized Android bitmap format: " + bitmap.getConfig());
}
TextureKey texKey = (TextureKey) assetInfo.getKey();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
ByteBuffer data = BufferUtils.createByteBuffer(bitmap.getWidth() * bitmap.getHeight() * bpp);
if (format == Image.Format.RGBA8) {
int[] pixelData = new int[width * height];
bitmap.getPixels(pixelData, 0, width, 0, 0, width, height);
if (texKey.isFlipY()) {
int[] sln = new int[width];
int y2;
for (int y1 = 0; y1 < height / 2; y1++) {
y2 = height - y1 - 1;
convertARGBtoABGR(pixelData, y1 * width, sln, 0, width);
convertARGBtoABGR(pixelData, y2 * width, pixelData, y1 * width, width);
System.arraycopy(sln, 0, pixelData, y2 * width, width);
}
} else {
convertARGBtoABGR(pixelData, 0, pixelData, 0, pixelData.length);
}
data.asIntBuffer().put(pixelData);
} else {
if (texKey.isFlipY()) {
// Flip the image, then delete the old one.
Matrix flipMat = new Matrix();
flipMat.preScale(1.0f, -1.0f);
Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), flipMat, false);
bitmap.recycle();
bitmap = newBitmap;
if (bitmap == null) {
throw new IOException("Failed to flip image: " + texKey);
}
}
bitmap.copyPixelsToBuffer(data);
}
data.flip();
bitmap.recycle();
Image image = new Image(format, width, height, data, ColorSpace.sRGB);
return image;
}
use of com.jme3.asset.TextureKey in project jmonkeyengine by jMonkeyEngine.
the class AndroidNativeImageLoader method load.
public Image load(AssetInfo info) throws IOException {
boolean flip = ((TextureKey) info.getKey()).isFlipY();
InputStream in = null;
try {
in = info.openStream();
return load(info.openStream(), flip, tmpArray);
} finally {
if (in != null) {
in.close();
}
}
}
use of com.jme3.asset.TextureKey in project jmonkeyengine by jMonkeyEngine.
the class BitmapFontLoader method load.
private BitmapFont load(AssetManager assetManager, String folder, InputStream in) throws IOException {
MaterialDef spriteMat = (MaterialDef) assetManager.loadAsset(new AssetKey("Common/MatDefs/Misc/Unshaded.j3md"));
BitmapCharacterSet charSet = new BitmapCharacterSet();
Material[] matPages = null;
BitmapFont font = new BitmapFont();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String regex = "[\\s=]+";
font.setCharSet(charSet);
String line;
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(regex);
if (tokens[0].equals("info")) {
// Get rendered size
for (int i = 1; i < tokens.length; i++) {
if (tokens[i].equals("size")) {
charSet.setRenderedSize(Integer.parseInt(tokens[i + 1]));
}
}
} else if (tokens[0].equals("common")) {
// Fill out BitmapCharacterSet fields
for (int i = 1; i < tokens.length; i++) {
String token = tokens[i];
if (token.equals("lineHeight")) {
charSet.setLineHeight(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("base")) {
charSet.setBase(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("scaleW")) {
charSet.setWidth(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("scaleH")) {
charSet.setHeight(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("pages")) {
// number of texture pages
matPages = new Material[Integer.parseInt(tokens[i + 1])];
font.setPages(matPages);
}
}
} else if (tokens[0].equals("page")) {
int index = -1;
Texture tex = null;
for (int i = 1; i < tokens.length; i++) {
String token = tokens[i];
if (token.equals("id")) {
index = Integer.parseInt(tokens[i + 1]);
} else if (token.equals("file")) {
String file = tokens[i + 1];
if (file.startsWith("\"")) {
file = file.substring(1, file.length() - 1);
}
TextureKey key = new TextureKey(folder + file, true);
key.setGenerateMips(false);
tex = assetManager.loadTexture(key);
tex.setMagFilter(Texture.MagFilter.Bilinear);
tex.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
}
}
// set page
if (index >= 0 && tex != null) {
Material mat = new Material(spriteMat);
mat.setTexture("ColorMap", tex);
mat.setBoolean("VertexColor", true);
mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
matPages[index] = mat;
}
} else if (tokens[0].equals("char")) {
// New BitmapCharacter
BitmapCharacter ch = null;
for (int i = 1; i < tokens.length; i++) {
String token = tokens[i];
if (token.equals("id")) {
int index = Integer.parseInt(tokens[i + 1]);
ch = new BitmapCharacter();
charSet.addCharacter(index, ch);
} else if (token.equals("x")) {
ch.setX(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("y")) {
ch.setY(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("width")) {
ch.setWidth(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("height")) {
ch.setHeight(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("xoffset")) {
ch.setXOffset(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("yoffset")) {
ch.setYOffset(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("xadvance")) {
ch.setXAdvance(Integer.parseInt(tokens[i + 1]));
} else if (token.equals("page")) {
ch.setPage(Integer.parseInt(tokens[i + 1]));
}
}
} else if (tokens[0].equals("kerning")) {
// Build kerning list
int index = 0;
int second = 0;
int amount = 0;
for (int i = 1; i < tokens.length; i++) {
if (tokens[i].equals("first")) {
index = Integer.parseInt(tokens[i + 1]);
} else if (tokens[i].equals("second")) {
second = Integer.parseInt(tokens[i + 1]);
} else if (tokens[i].equals("amount")) {
amount = Integer.parseInt(tokens[i + 1]);
}
}
BitmapCharacter ch = charSet.getCharacter(index);
ch.addKerning(second, amount);
}
}
return font;
}
Aggregations