use of com.jme3.asset.AssetKey in project jmonkeyengine by jMonkeyEngine.
the class Context method buildSourcesRec.
private void buildSourcesRec(BufferedReader reader, StringBuilder builder, AssetManager assetManager) throws IOException {
String ln;
while ((ln = reader.readLine()) != null) {
if (ln.trim().startsWith("#import ")) {
ln = ln.trim().substring(8).trim();
if (ln.startsWith("\"")) {
ln = ln.substring(1);
}
if (ln.endsWith("\"")) {
ln = ln.substring(0, ln.length() - 1);
}
AssetInfo info = assetManager.locateAsset(new AssetKey<String>(ln));
if (info == null) {
throw new AssetNotFoundException("Unable to load source file \"" + ln + "\"");
}
try (BufferedReader r = new BufferedReader(new InputStreamReader(info.openStream()))) {
builder.append("//-- begin import ").append(ln).append(" --\n");
buildSourcesRec(r, builder, assetManager);
builder.append("//-- end import ").append(ln).append(" --\n");
}
} else {
builder.append(ln).append('\n');
}
}
}
use of com.jme3.asset.AssetKey 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;
}
use of com.jme3.asset.AssetKey in project jmonkeyengine by jMonkeyEngine.
the class TextureProcessor method postProcess.
@Override
public Object postProcess(AssetKey key, Object obj) {
TextureKey texKey = (TextureKey) key;
Image img = (Image) obj;
if (img == null) {
return null;
}
Texture tex;
if (texKey.getTextureTypeHint() == Texture.Type.CubeMap) {
if (texKey.isFlipY()) {
// also flip -y and +y image in cubemap
ByteBuffer pos_y = img.getData(2);
img.setData(2, img.getData(3));
img.setData(3, pos_y);
}
tex = new TextureCubeMap();
} else if (texKey.getTextureTypeHint() == Texture.Type.ThreeDimensional) {
tex = new Texture3D();
} else {
tex = new Texture2D();
}
// or generate them if requested by user
if (img.hasMipmaps() || texKey.isGenerateMips()) {
tex.setMinFilter(Texture.MinFilter.Trilinear);
}
tex.setAnisotropicFilter(texKey.getAnisotropy());
tex.setName(texKey.getName());
tex.setImage(img);
return tex;
}
use of com.jme3.asset.AssetKey in project jmonkeyengine by jMonkeyEngine.
the class J3MLoader method loadFromRoot.
private void loadFromRoot(List<Statement> roots) throws IOException {
if (roots.size() == 2) {
Statement exception = roots.get(0);
String line = exception.getLine();
if (line.startsWith("Exception")) {
throw new AssetLoadException(line.substring("Exception ".length()));
} else {
throw new IOException("In multiroot material, expected first statement to be 'Exception'");
}
} else if (roots.size() != 1) {
throw new IOException("Too many roots in J3M/J3MD file");
}
boolean extending = false;
Statement materialStat = roots.get(0);
String materialName = materialStat.getLine();
if (materialName.startsWith("MaterialDef")) {
materialName = materialName.substring("MaterialDef ".length()).trim();
extending = false;
} else if (materialName.startsWith("Material")) {
materialName = materialName.substring("Material ".length()).trim();
extending = true;
} else {
throw new IOException("Specified file is not a Material file");
}
String[] split = materialName.split(":", 2);
if (materialName.equals("")) {
throw new MatParseException("Material name cannot be empty", materialStat);
}
if (split.length == 2) {
if (!extending) {
throw new MatParseException("Must use 'Material' when extending.", materialStat);
}
String extendedMat = split[1].trim();
MaterialDef def = (MaterialDef) assetManager.loadAsset(new AssetKey(extendedMat));
if (def == null) {
throw new MatParseException("Extended material " + extendedMat + " cannot be found.", materialStat);
}
material = new Material(def);
material.setKey(key);
material.setName(split[0].trim());
// material.setAssetName(fileName);
} else if (split.length == 1) {
if (extending) {
throw new MatParseException("Expected ':', got '{'", materialStat);
}
materialDef = new MaterialDef(assetManager, materialName);
// NOTE: pass file name for defs so they can be loaded later
materialDef.setAssetName(key.getName());
} else {
throw new MatParseException("Cannot use colon in material name/path", materialStat);
}
for (Statement statement : materialStat.getContents()) {
split = statement.getLine().split("[ \\{]");
String statType = split[0];
if (extending) {
if (statType.equals("MaterialParameters")) {
readExtendingMaterialParams(statement.getContents());
} else if (statType.equals("AdditionalRenderState")) {
readAdditionalRenderState(statement.getContents());
} else if (statType.equals("Transparent")) {
readTransparentStatement(statement.getLine());
}
} else {
if (statType.equals("Technique")) {
readTechnique(statement);
} else if (statType.equals("MaterialParameters")) {
readMaterialParams(statement.getContents());
} else {
throw new MatParseException("Expected material statement, got '" + statType + "'", statement);
}
}
}
}
use of com.jme3.asset.AssetKey in project jmonkeyengine by jMonkeyEngine.
the class TestAssetLinkNode method simpleInitApp.
@Override
public void simpleInitApp() {
AssetLinkNode loaderNode = new AssetLinkNode();
loaderNode.addLinkedChild(new ModelKey("Models/MonkeyHead/MonkeyHead.mesh.xml"));
//save and load the loaderNode
try {
//export to byte array
ByteArrayOutputStream bout = new ByteArrayOutputStream();
BinaryExporter.getInstance().save(loaderNode, bout);
//import from byte array, automatically loads the monkeyhead from file
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
BinaryImporter imp = BinaryImporter.getInstance();
imp.setAssetManager(assetManager);
Node newLoaderNode = (Node) imp.load(bin);
//attach to rootNode
rootNode.attachChild(newLoaderNode);
} catch (IOException ex) {
Logger.getLogger(TestAssetLinkNode.class.getName()).log(Level.SEVERE, null, ex);
}
rootNode.attachChild(loaderNode);
lightMdl = new Geometry("Light", new Sphere(10, 10, 0.1f));
lightMdl.setMaterial((Material) assetManager.loadAsset(new AssetKey("Common/Materials/RedColor.j3m")));
rootNode.attachChild(lightMdl);
// flourescent main light
pl = new PointLight();
pl.setColor(new ColorRGBA(0.88f, 0.92f, 0.95f, 1.0f));
rootNode.addLight(pl);
// sunset light
DirectionalLight dl = new DirectionalLight();
dl.setDirection(new Vector3f(-0.1f, -0.7f, 1).normalizeLocal());
dl.setColor(new ColorRGBA(0.44f, 0.30f, 0.20f, 1.0f));
rootNode.addLight(dl);
// skylight
dl = new DirectionalLight();
dl.setDirection(new Vector3f(-0.6f, -1, -0.6f).normalizeLocal());
dl.setColor(new ColorRGBA(0.10f, 0.22f, 0.44f, 1.0f));
rootNode.addLight(dl);
// white ambient light
dl = new DirectionalLight();
dl.setDirection(new Vector3f(1, -0.5f, -0.1f).normalizeLocal());
dl.setColor(new ColorRGBA(0.50f, 0.40f, 0.50f, 1.0f));
rootNode.addLight(dl);
}
Aggregations