From 1aabed9bc522e086fa757a5015838f09b6d62e98 Mon Sep 17 00:00:00 2001 From: "nor..67" Date: Sat, 14 Jan 2012 23:00:06 +0000 Subject: [PATCH] - 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 --- .../test/jme3test/tools/TestTextureAtlas.java | 4 +- .../optimize/GeometryBatchFactory.java | 38 ++++----- .../jme3tools/optimize/TextureAtlas.java | 84 +++++++++++++++---- 3 files changed, 89 insertions(+), 37 deletions(-) diff --git a/engine/src/test/jme3test/tools/TestTextureAtlas.java b/engine/src/test/jme3test/tools/TestTextureAtlas.java index 130b314a7..40bb20495 100644 --- a/engine/src/test/jme3test/tools/TestTextureAtlas.java +++ b/engine/src/test/jme3test/tools/TestTextureAtlas.java @@ -58,12 +58,12 @@ public class TestTextureAtlas extends SimpleApplication { Geometry geom = GeometryBatchFactory.makeAtlasBatch(scene, assetManager, 4096); AmbientLight al = new AmbientLight(); - scene.addLight(al); + rootNode.addLight(al); DirectionalLight sun = new DirectionalLight(); sun.setDirection(new Vector3f(0.69077975f, -0.6277887f, -0.35875428f).normalizeLocal()); sun.setColor(ColorRGBA.White.clone().multLocal(2)); - scene.addLight(sun); + rootNode.addLight(sun); rootNode.attachChild(geom); } diff --git a/engine/src/tools/jme3tools/optimize/GeometryBatchFactory.java b/engine/src/tools/jme3tools/optimize/GeometryBatchFactory.java index 72d72a7c4..d8c395aa4 100644 --- a/engine/src/tools/jme3tools/optimize/GeometryBatchFactory.java +++ b/engine/src/tools/jme3tools/optimize/GeometryBatchFactory.java @@ -209,8 +209,8 @@ public class GeometryBatchFactory { tex = getMaterialTexture(geom, "ColorMap"); } - if (tex != null && tex.getKey() != null) { - TextureAtlasTile tile = atlas.getAtlasTile(tex.getKey().getName()); + if (tex != null) { + TextureAtlasTile tile = atlas.getAtlasTile(tex); if (tile != null) { FloatBuffer inPos = (FloatBuffer) inBuf.getData(); FloatBuffer outPos = (FloatBuffer) outBuf.getData(); @@ -356,25 +356,21 @@ public class GeometryBatchFactory { mesh.updateCounts(); mesh.updateBound(); geom.setMesh(mesh); -// geom.setMesh(new Box(1,1,1)); - -// Material mat = new Material(mgr, "Common/MatDefs/Light/Lighting.j3md"); -// Texture diffuseMap = atlas.getAtlasTexture("DiffuseMap"); -// Texture normalMap = atlas.getAtlasTexture("NormalMap"); -// Texture specularMap = atlas.getAtlasTexture("SpecularMap"); -// if (diffuseMap != null) { -// mat.setTexture("DiffuseMap", diffuseMap); -// } -// if (normalMap != null) { -// mat.setTexture("NormalMap", normalMap); -// } -// if (specularMap != null) { -// mat.setTexture("SpecularMap", specularMap); -// } -// mat.setFloat("Shininess", 16.0f); - - Material mat = new Material(mgr, "Common/MatDefs/Misc/Unshaded.j3md"); - mat.setTexture("ColorMap", atlas.getAtlasTexture("DiffuseMap")); + + Material mat = new Material(mgr, "Common/MatDefs/Light/Lighting.j3md"); + Texture diffuseMap = atlas.getAtlasTexture("DiffuseMap"); + Texture normalMap = atlas.getAtlasTexture("NormalMap"); + Texture specularMap = atlas.getAtlasTexture("SpecularMap"); + if (diffuseMap != null) { + mat.setTexture("DiffuseMap", diffuseMap); + } + if (normalMap != null) { + mat.setTexture("NormalMap", normalMap); + } + if (specularMap != null) { + mat.setTexture("SpecularMap", specularMap); + } + mat.setFloat("Shininess", 16.0f); geom.setMaterial(mat); return geom; diff --git a/engine/src/tools/jme3tools/optimize/TextureAtlas.java b/engine/src/tools/jme3tools/optimize/TextureAtlas.java index c2e691f1c..7fd37b846 100644 --- a/engine/src/tools/jme3tools/optimize/TextureAtlas.java +++ b/engine/src/tools/jme3tools/optimize/TextureAtlas.java @@ -70,32 +70,60 @@ public class TextureAtlas { /** * 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. + * @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 */ 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 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 */ 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); + String name = textureName(texture); + if (texture.getImage() != null && name != null) { + return addImage(texture.getImage(), name, mapName, sourceTextureName); } else { 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) { if (rootMapName == null) { rootMapName = mapName; @@ -169,10 +197,33 @@ public class TextureAtlas { } } - public TextureAtlasTile getAtlasTile(String assetName) { + /** + * Get the TextureAtlasTile for the given Texture + * @param texture The texture to retrieve the TextureAtlasTile for. + * @return + */ + public TextureAtlasTile getAtlasTile(Texture texture) { + String sourceTextureName = textureName(texture); + if (sourceTextureName != null) { + return getAtlasTile(sourceTextureName); + } + return null; + } + + /** + * Get the TextureAtlasTile for the given Texture + * @param assetName The texture to retrieve the TextureAtlasTile for. + * @return + */ + private TextureAtlasTile getAtlasTile(String assetName) { return locationMap.get(assetName); } + /** + * Gets a new atlas texture for the given map name. + * @param mapName + * @return + */ public Texture getAtlasTexture(String mapName) { if (images == null) { return null; @@ -260,6 +311,11 @@ public class TextureAtlas { this.height = height; } + /** + * Get the transformed texture location for a given input location + * @param previousLocation + * @return + */ public Vector2f getLocation(Vector2f previousLocation) { float x = (float) getX() / (float) atlasWidth; float y = (float) getY() / (float) atlasHeight; @@ -267,15 +323,15 @@ public class TextureAtlas { 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)); } + /** + * 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) { Vector2f tex = new Vector2f();