- add normal and specular (lighting) support to TextureAtlas generation

- add more TextureAtlas javadoc

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9032 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
nor..67 13 years ago
parent 58a08eb570
commit 1aabed9bc5
  1. 4
      engine/src/test/jme3test/tools/TestTextureAtlas.java
  2. 38
      engine/src/tools/jme3tools/optimize/GeometryBatchFactory.java
  3. 84
      engine/src/tools/jme3tools/optimize/TextureAtlas.java

@ -58,12 +58,12 @@ public class TestTextureAtlas extends SimpleApplication {
Geometry geom = GeometryBatchFactory.makeAtlasBatch(scene, assetManager, 4096); Geometry geom = GeometryBatchFactory.makeAtlasBatch(scene, assetManager, 4096);
AmbientLight al = new AmbientLight(); AmbientLight al = new AmbientLight();
scene.addLight(al); rootNode.addLight(al);
DirectionalLight sun = new DirectionalLight(); DirectionalLight sun = new DirectionalLight();
sun.setDirection(new Vector3f(0.69077975f, -0.6277887f, -0.35875428f).normalizeLocal()); sun.setDirection(new Vector3f(0.69077975f, -0.6277887f, -0.35875428f).normalizeLocal());
sun.setColor(ColorRGBA.White.clone().multLocal(2)); sun.setColor(ColorRGBA.White.clone().multLocal(2));
scene.addLight(sun); rootNode.addLight(sun);
rootNode.attachChild(geom); rootNode.attachChild(geom);
} }

@ -209,8 +209,8 @@ public class GeometryBatchFactory {
tex = getMaterialTexture(geom, "ColorMap"); tex = getMaterialTexture(geom, "ColorMap");
} }
if (tex != null && tex.getKey() != null) { if (tex != null) {
TextureAtlasTile tile = atlas.getAtlasTile(tex.getKey().getName()); TextureAtlasTile tile = atlas.getAtlasTile(tex);
if (tile != null) { if (tile != null) {
FloatBuffer inPos = (FloatBuffer) inBuf.getData(); FloatBuffer inPos = (FloatBuffer) inBuf.getData();
FloatBuffer outPos = (FloatBuffer) outBuf.getData(); FloatBuffer outPos = (FloatBuffer) outBuf.getData();
@ -356,25 +356,21 @@ public class GeometryBatchFactory {
mesh.updateCounts(); mesh.updateCounts();
mesh.updateBound(); mesh.updateBound();
geom.setMesh(mesh); geom.setMesh(mesh);
// geom.setMesh(new Box(1,1,1));
Material mat = new Material(mgr, "Common/MatDefs/Light/Lighting.j3md");
// Material mat = new Material(mgr, "Common/MatDefs/Light/Lighting.j3md"); Texture diffuseMap = atlas.getAtlasTexture("DiffuseMap");
// Texture diffuseMap = atlas.getAtlasTexture("DiffuseMap"); Texture normalMap = atlas.getAtlasTexture("NormalMap");
// Texture normalMap = atlas.getAtlasTexture("NormalMap"); Texture specularMap = atlas.getAtlasTexture("SpecularMap");
// Texture specularMap = atlas.getAtlasTexture("SpecularMap"); if (diffuseMap != null) {
// if (diffuseMap != null) { mat.setTexture("DiffuseMap", diffuseMap);
// mat.setTexture("DiffuseMap", diffuseMap); }
// } if (normalMap != null) {
// if (normalMap != null) { mat.setTexture("NormalMap", normalMap);
// mat.setTexture("NormalMap", normalMap); }
// } if (specularMap != null) {
// if (specularMap != null) { mat.setTexture("SpecularMap", specularMap);
// mat.setTexture("SpecularMap", specularMap); }
// } mat.setFloat("Shininess", 16.0f);
// mat.setFloat("Shininess", 16.0f);
Material mat = new Material(mgr, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setTexture("ColorMap", atlas.getAtlasTexture("DiffuseMap"));
geom.setMaterial(mat); geom.setMaterial(mat);
return geom; return geom;

@ -70,32 +70,60 @@ public class TextureAtlas {
/** /**
* Add a texture for a specific map name * Add a texture for a specific map name
* @param texture A texture to add to the atlas * @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. * @param mapName A freely chosen map name that can be later retrieved as a Texture. The first map name supplied will be the master map.
* @return * @return
*/ */
public boolean addTexture(Texture texture, String mapName) { public boolean addTexture(Texture texture, String mapName) {
return addTexture(texture, mapName, null); return addTexture(texture, mapName, (String) null);
} }
/** /**
* Add a texture for a specific map name at the location of another existing texture. * Add a texture for a specific map name at the location of another existing texture (on the master map).
* @param texture A texture to add to the atlas. * @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 mapName A freely chosen map name that can be later retrieved as a Texture.
* @param sourceTextureName Name of the original texture location. * @param sourceTexture The base texture for determining the location.
* @return
*/
public boolean addTexture(Texture texture, String mapName, Texture sourceTexture) {
String sourceTextureName = textureName(sourceTexture);
if (sourceTextureName == null) {
return false;
} else {
return addTexture(texture, mapName, sourceTextureName);
}
}
/**
* Add a texture for a specific map name at the location of another existing texture (on the master map).
* @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 base texture for the location.
* @return * @return
*/ */
public boolean addTexture(Texture texture, String mapName, String sourceTextureName) { public boolean addTexture(Texture texture, String mapName, String sourceTextureName) {
if (texture == null) { if (texture == null) {
return false; return false;
} }
AssetKey key = texture.getKey(); String name = textureName(texture);
if (texture.getImage() != null && key != null && key.getName() != null) { if (texture.getImage() != null && name != null) {
return addImage(texture.getImage(), key.getName(), mapName, sourceTextureName); return addImage(texture.getImage(), name, mapName, sourceTextureName);
} else { } else {
return false; return false;
} }
} }
private String textureName(Texture texture) {
if (texture == null) {
return null;
}
AssetKey key = texture.getKey();
if (key != null) {
return key.getName();
} else {
return null;
}
}
private boolean addImage(Image image, String name, String mapName, String sourceTextureName) { private boolean addImage(Image image, String name, String mapName, String sourceTextureName) {
if (rootMapName == null) { if (rootMapName == null) {
rootMapName = mapName; rootMapName = mapName;
@ -169,10 +197,33 @@ public class TextureAtlas {
} }
} }
public TextureAtlasTile getAtlasTile(String assetName) { /**
* Get the <code>TextureAtlasTile</code> for the given Texture
* @param texture The texture to retrieve the <code>TextureAtlasTile</code> for.
* @return
*/
public TextureAtlasTile getAtlasTile(Texture texture) {
String sourceTextureName = textureName(texture);
if (sourceTextureName != null) {
return getAtlasTile(sourceTextureName);
}
return null;
}
/**
* Get the <code>TextureAtlasTile</code> for the given Texture
* @param assetName The texture to retrieve the <code>TextureAtlasTile</code> for.
* @return
*/
private TextureAtlasTile getAtlasTile(String assetName) {
return locationMap.get(assetName); return locationMap.get(assetName);
} }
/**
* Gets a new atlas texture for the given map name.
* @param mapName
* @return
*/
public Texture getAtlasTexture(String mapName) { public Texture getAtlasTexture(String mapName) {
if (images == null) { if (images == null) {
return null; return null;
@ -260,6 +311,11 @@ public class TextureAtlas {
this.height = height; this.height = height;
} }
/**
* Get the transformed texture location for a given input location
* @param previousLocation
* @return
*/
public Vector2f getLocation(Vector2f previousLocation) { public Vector2f getLocation(Vector2f previousLocation) {
float x = (float) getX() / (float) atlasWidth; float x = (float) getX() / (float) atlasWidth;
float y = (float) getY() / (float) atlasHeight; float y = (float) getY() / (float) atlasHeight;
@ -267,15 +323,15 @@ public class TextureAtlas {
float h = (float) getHeight() / (float) atlasHeight; float h = (float) getHeight() / (float) atlasHeight;
Vector2f location = new Vector2f(x, y); Vector2f location = new Vector2f(x, y);
Vector2f scale = new Vector2f(w, h); 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)); return location.addLocal(previousLocation.multLocal(scale));
} }
/**
* Transforms a whole texture coordinates buffer
* @param inBuf The input texture buffer
* @param offset The offset in the output buffer
* @param outBuf The output buffer
*/
public void transformTextureCoords(FloatBuffer inBuf, int offset, FloatBuffer outBuf) { public void transformTextureCoords(FloatBuffer inBuf, int offset, FloatBuffer outBuf) {
Vector2f tex = new Vector2f(); Vector2f tex = new Vector2f();

Loading…
Cancel
Save