public Node(int x, int y, int width, int height) { location = new TextureAtlasTile(x, y, width, height); child = new Node[2]; child[0] = null; child[1] = null; occupied = false; }
/** * Get the <code>TextureAtlasTile</code> for the given Texture * @param texture The texture to retrieve the <code>TextureAtlasTile</code> for. * @return the atlas tile */ public TextureAtlasTile getAtlasTile(Texture texture) { String sourceTextureName = textureName(texture); if (sourceTextureName != null) { return getAtlasTile(sourceTextureName); } return null; }
private boolean assertValidMesh() { // Allows to find bugs in collapsing. for (Vertex vertex : collapseCostSet) { assertValidVertex(vertex); } return true; }
/** * 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; }
/** * Optimizes a scene by combining Geometry with the same material. * All Geometries found in the scene are detached from their parent and * a new Node containing the optimized Geometries is attached. * @param scene The scene to optimize * @return The newly created optimized geometries attached to a node */ public static Spatial optimize(Node scene) { return optimize(scene, false); }
/** * Get the transformed texture coordinate for a given input location. * @param previousLocation The old texture coordinate. * @return The new texture coordinate inside the atlas. */ public Vector2f getLocation(Vector2f previousLocation) { float x = (float) getX() / (float) atlasWidth; float y = (float) getY() / (float) atlasHeight; float w = (float) getWidth() / (float) atlasWidth; float h = (float) getHeight() / (float) atlasHeight; Vector2f location = new Vector2f(x, y); float prevX = previousLocation.x; float prevY = previousLocation.y; location.addLocal(prevX * w, prevY * h); return location; }
private void replaceVertexID(Triangle triangle, int oldID, int newID, Vertex dst) { dst.triangles.add(triangle); // NOTE: triangle is not removed from src. This is implementation specific optimization. // Its up to the compiler to unroll everything. for (int i = 0; i < 3; i++) { if (triangle.vertexId[i] == oldID) { for (int n = 0; n < 3; n++) { if (i != n) { // This is implementation specific optimization to remove following line. //removeEdge(triangle.vertex[i], new Edge(triangle.vertex[n])); removeEdge(triangle.vertex[n], new Edge(triangle.vertex[i])); addEdge(triangle.vertex[n], new Edge(dst)); addEdge(dst, new Edge(triangle.vertex[n])); } } triangle.vertex[i] = dst; triangle.vertexId[i] = newID; return; } } // assert (false); }
/** * 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); } }
private void removeTriangleFromEdges(Triangle triangle, Vertex skip) { // skip is needed if we are iterating on the vertex's edges or triangles. for (int i = 0; i < 3; i++) { if (triangle.vertex[i] != skip) { triangle.vertex[i].triangles.remove(triangle); } } for (int i = 0; i < 3; i++) { for (int n = 0; n < 3; n++) { if (i != n) { removeEdge(triangle.vertex[i], new Edge(triangle.vertex[n])); } } } }
private boolean assertValidVertex(Vertex v) { // Allows to find bugs in collapsing. // System.out.println("Asserting " + v.index); for (Triangle t : v.triangles) { for (int i = 0; i < 3; i++) { // System.out.println("check " + t.vertex[i].index); //assert (collapseCostSet.contains(t.vertex[i])); assert (find(collapseCostSet, t.vertex[i])); assert (t.vertex[i].edges.contains(new Edge(t.vertex[i].collapseTo))); for (int n = 0; n < 3; n++) { if (i != n) { int id = t.vertex[i].edges.indexOf(new Edge(t.vertex[n])); Edge ed = t.vertex[i].edges.get(id); //assert (ed.collapseCost != UNINITIALIZED_COLLAPSE_COST); } else { assert (!t.vertex[i].edges.contains(new Edge(t.vertex[n]))); } } } } return true; }
public static List<Geometry> makeBatches(Collection<Geometry> geometries) { return makeBatches(geometries, false); }
/** * Construct a LodGenerator for the given mesh * * @param mesh the mesh to consider to generate de Lods. */ public LodGenerator(Mesh mesh) { this.mesh = mesh; build(); }
private void addEdge(Vertex v, Edge edge) { // assert (edge.destination != v); for (Edge ed : v.edges) { if (ed.equals(edge)) { ed.refCount++; return; } } v.edges.add(edge); edge.refCount = 1; }
private Triangle getDuplicate(Triangle triangle) { // duplicate triangle detection (where all vertices has the same position) for (Triangle tri : triangle.vertex[0].triangles) { if (isDuplicateTriangle(triangle, tri)) { return tri; } } return null; }
@Override public String toString() { String out = "Triangle{\n"; for (int i = 0; i < 3; i++) { out += vertexId[i] + " : " + vertex[i].toString() + "\n"; } out += '}'; return out; } }
private boolean checkCosts() { for (Vertex vertex : vertexList) { boolean test = find(collapseCostSet, vertex); if (!test) { System.out.println("vertex " + vertex.index + " not present in collapse costs"); return false; } } return true; }
private void computeVertexCollapseCost(Vertex vertex) { vertex.collapseCost = UNINITIALIZED_COLLAPSE_COST; // assert (!vertex.edges.isEmpty()); for (Edge edge : vertex.edges) { edge.collapseCost = computeEdgeCollapseCost(vertex, edge); // assert (edge.collapseCost != UNINITIALIZED_COLLAPSE_COST); if (vertex.collapseCost > edge.collapseCost) { vertex.collapseCost = edge.collapseCost; vertex.collapseTo = edge.destination; } } // assert (vertex.collapseCost != UNINITIALIZED_COLLAPSE_COST); collapseCostSet.add(vertex); }
/** * 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()); }
/** * Construct a LodGenerator for the given geometry * * @param geom the geometry to consider to generate de Lods. */ public LodGenerator(Geometry geom) { mesh = geom.getMesh(); build(); }
private void removeEdge(Vertex v, Edge edge) { Edge ed = null; for (Edge edge1 : v.edges) { if (edge1.equals(edge)) { ed = edge1; break; } } if (ed.refCount == 1) { v.edges.remove(ed); } else { ed.refCount--; } }