Git Product home page Git Product logo

repack's Introduction

repack

A library and tool for creating texture atlases. Repack can be used as a library to create texture/image atlas at runtime and it also have a tool to generate the atlases before hand.

Installing repack

Simply do this haxelib install repack or you can clone this repository and manually use it.

Using repack as a library

Here is an example of using repack to create texture/image atlas at runtime.

package;

import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.Lib;
import flash.display.Sprite;
import org.repack.Packer;

using Std;
using Lambda;

class Main {
	public static function main() {
		var stage  = Lib.current.stage;
		var sizes  = [ 16, 32, 64 ];
		var packer = new Packer<Void>(stage.stageWidth, stage.stageHeight);
		var boxes  = [];

		for (i in 0...80) {
			boxes.push({
				width  : rnd(sizes),
				height : rnd(sizes)
			});
		}

		// Always sort your inputs for the best result
		boxes.sort(function(a, b) { 
			return (b.width * b.height) - (a.width * a.height); 
		});

		for (b in boxes) {
			// in real life, there is possibility that this will fail
			// but for our example, we ignore it
			packer.add(b.width, b.height);
		}

		stage.scaleMode = StageScaleMode.NO_SCALE;
		stage.align     = StageAlign.TOP_LEFT;

		var canvas   = cast(stage.addChild(new Sprite()), Sprite);
		var graphics = canvas.graphics;

		// draw each packed rectangle that we manage to fit in the packer
		graphics.clear();
		for (pack in packer) {
			var rect  = pack.rect;
			var color = col();

			graphics.lineStyle(1, color, 1.0);
			graphics.beginFill(color, 0.75);
			graphics.drawRect(rect.x, rect.y, rect.width - 1, rect.height - 1);
		}
		graphics.endFill();
	}

	private static inline function col():Int {
		var r = ((Math.random() * 256).int() + 255) >> 1;
		var g = ((Math.random() * 256).int() + 255) >> 1;
		var b = ((Math.random() * 256).int() + 255) >> 1;
		return r << 16 | g << 8 | b;
	}

	private static inline function rnd(v:Array<Int>):Int {
		return v[(Math.random() * v.length).int()];
	}
}

You can tweak the packing method, rotation method, and how you sort your data to achive optimal result.

Using repack as a haxelib runnable

There is also runnable on repack haxelib repo that you can use to generate atlases before hand, for example

haxelib run repack -input asset -output atlas\output.png -method tl -rotate normal -sort max:min:w:h

You can find out about the options by using haxelib run repack -help

repack's People

Contributors

aaulia avatar

Watchers

paling avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.