/** * 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; }
/** * 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 masterTexture The master texture for determining the location, it has to exist in tha master map. */ public void addTexture(Texture texture, String mapName, Texture masterTexture) { String sourceTextureName = textureName(masterTexture); if (sourceTextureName == null) { throw new IllegalStateException("Supplied master map texture has no asset key name!"); } else { addTexture(texture, mapName, sourceTextureName); } }
List<Geometry> geometries = new ArrayList<Geometry>(); GeometryBatchFactory.gatherGeoms(spat, geometries); TextureAtlas atlas = createAtlas(spat, atlasSize); if (atlas == null) { return null; Mesh mesh = new Mesh(); GeometryBatchFactory.mergeGeometries(geometries, mesh); applyAtlasCoords(geometries, mesh, atlas); mesh.updateCounts(); mesh.updateBound(); Texture diffuseMap = atlas.getAtlasTexture("DiffuseMap"); Texture normalMap = atlas.getAtlasTexture("NormalMap"); Texture specularMap = atlas.getAtlasTexture("SpecularMap"); if (diffuseMap != null) { mat.setTexture("DiffuseMap", diffuseMap);
/** * 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 master map used for the location. */ public void addTexture(Texture texture, String mapName, String sourceTextureName) { if (texture == null) { throw new IllegalStateException("Texture cannot be null!"); } String name = textureName(texture); if (texture.getImage() != null && name != null) { addImage(texture.getImage(), name, mapName, sourceTextureName); } else { throw new IllegalStateException("Texture has no asset key name!"); } }
private int addCubeGeometry() { Mesh cubeMesh = new BitCubeBox(1f, 1f, 1f); TextureAtlas atlas = new TextureAtlas(400, 1200); MatFactory matFact = getRenderRegCli().getOpticMaterialFacade(null, null); AssetManager assetMgr = matFact.getAssetManager(); Texture zeroTexture = assetMgr.loadTexture("textures/robosteps/Zero.png"); Texture oneTexture = assetMgr.loadTexture("textures/robosteps/One.png"); Texture blankTexture = assetMgr.loadTexture("textures/robosteps/BlankGray.png"); atlas.addTexture(zeroTexture, "ColorMap"); atlas.addTexture(oneTexture, "ColorMap"); atlas.addTexture(blankTexture, "ColorMap"); Material cubeMaterial = matFact.makeMatWithOptTexture("Common/MatDefs/Misc/Unshaded.j3md", "ColorMap", atlas.getAtlasTexture("ColorMap")); return addGeometry(cubeMesh, cubeMaterial, null, new Quaternion()); }
/** * Add a geometries DiffuseMap (or ColorMap), NormalMap and SpecularMap to the atlas. * @param geometry * @return false if the atlas is full. */ public boolean addGeometry(Geometry geometry) { Texture diffuse = getMaterialTexture(geometry, "DiffuseMap"); Texture normal = getMaterialTexture(geometry, "NormalMap"); Texture specular = getMaterialTexture(geometry, "SpecularMap"); if (diffuse == null) { diffuse = getMaterialTexture(geometry, "ColorMap"); } if (diffuse != null && diffuse.getKey() != null) { String keyName = diffuse.getKey().toString(); if (!addTexture(diffuse, "DiffuseMap")) { return false; } else { if (normal != null && normal.getKey() != null) { addTexture(normal, "NormalMap", keyName); } if (specular != null && specular.getKey() != null) { addTexture(specular, "SpecularMap", keyName); } } return true; } return true; }
/** * Applies the texture coordinates to the given geometry * if its DiffuseMap or ColorMap exists in the atlas. * @param geom The geometry to change the texture coordinate buffer on. * @return true if texture has been found and coords have been changed, false otherwise. */ public boolean applyCoords(Geometry geom) { return applyCoords(geom, 0, geom.getMesh()); }
newImage = convertImageToAwt(source); if (newImage != null) { source = newImage;
/** * 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 map. * @return false if the atlas is full. */ public boolean addTexture(Texture texture, String mapName) { if (texture == null) { throw new IllegalStateException("Texture cannot be null!"); } String name = textureName(texture); if (texture.getImage() != null && name != null) { return addImage(texture.getImage(), name, mapName, null); } else { throw new IllegalStateException("Texture has no asset key name!"); } }
/** * Add a geometries DiffuseMap (or ColorMap), NormalMap and SpecularMap to the atlas. * @param geometry * @return false if the atlas is full. */ public boolean addGeometry(Geometry geometry) { Texture diffuse = getMaterialTexture(geometry, "DiffuseMap"); Texture normal = getMaterialTexture(geometry, "NormalMap"); Texture specular = getMaterialTexture(geometry, "SpecularMap"); if (diffuse == null) { diffuse = getMaterialTexture(geometry, "ColorMap"); } if (diffuse != null && diffuse.getKey() != null) { String keyName = diffuse.getKey().toString(); if (!addTexture(diffuse, "DiffuseMap")) { return false; } else { if (normal != null && normal.getKey() != null) { addTexture(normal, "NormalMap", keyName); } if (specular != null && specular.getKey() != null) { addTexture(specular, "SpecularMap", keyName); } } return true; } return true; }
private static void applyAtlasCoords(List<Geometry> geometries, Mesh outMesh, TextureAtlas atlas) { int globalVertIndex = 0; for (Geometry geom : geometries) { Mesh inMesh = geom.getMesh(); geom.computeWorldMatrix(); int geomVertCount = inMesh.getVertexCount(); VertexBuffer inBuf = inMesh.getBuffer(Type.TexCoord); VertexBuffer outBuf = outMesh.getBuffer(Type.TexCoord); if (inBuf == null || outBuf == null) { continue; } atlas.applyCoords(geom, globalVertIndex, outMesh); globalVertIndex += geomVertCount; } }
newImage = convertImageToAwt(source); if (newImage != null) { source = newImage;
List<Geometry> geometries = new ArrayList<Geometry>(); GeometryBatchFactory.gatherGeoms(spat, geometries); TextureAtlas atlas = createAtlas(spat, atlasSize); if (atlas == null) { return null; Mesh mesh = new Mesh(); GeometryBatchFactory.mergeGeometries(geometries, mesh); applyAtlasCoords(geometries, mesh, atlas); mesh.updateCounts(); mesh.updateBound(); Texture diffuseMap = atlas.getAtlasTexture("DiffuseMap"); Texture normalMap = atlas.getAtlasTexture("NormalMap"); Texture specularMap = atlas.getAtlasTexture("SpecularMap"); if (diffuseMap != null) { mat.setTexture("DiffuseMap", diffuseMap);
/** * 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 map. * @return false if the atlas is full. */ public boolean addTexture(Texture texture, String mapName) { if (texture == null) { throw new IllegalStateException("Texture cannot be null!"); } String name = textureName(texture); if (texture.getImage() != null && name != null) { return addImage(texture.getImage(), name, mapName, null); } else { throw new IllegalStateException("Texture has no asset key name!"); } }
/** * 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 masterTexture The master texture for determining the location, it has to exist in tha master map. */ public void addTexture(Texture texture, String mapName, Texture masterTexture) { String sourceTextureName = textureName(masterTexture); if (sourceTextureName == null) { throw new IllegalStateException("Supplied master map texture has no asset key name!"); } else { addTexture(texture, mapName, sourceTextureName); } }
/** * 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; }
/** * Applies the texture coordinates to the given geometry * if its DiffuseMap or ColorMap exists in the atlas. * @param geom The geometry to change the texture coordinate buffer on. * @return true if texture has been found and coords have been changed, false otherwise. */ public boolean applyCoords(Geometry geom) { return applyCoords(geom, 0, geom.getMesh()); }
/** * 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 master map used for the location. */ public void addTexture(Texture texture, String mapName, String sourceTextureName) { if (texture == null) { throw new IllegalStateException("Texture cannot be null!"); } String name = textureName(texture); if (texture.getImage() != null && name != null) { addImage(texture.getImage(), name, mapName, sourceTextureName); } else { throw new IllegalStateException("Texture has no asset key name!"); } }