Skip to content

Commit

Permalink
added 'auto-size' option to field agent
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimaryFeather committed Dec 6, 2016
1 parent 8f95a78 commit d16c613
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
18 changes: 13 additions & 5 deletions util/field_agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ This allows them to be scaled to almost any size while staying crisp and sharp.
This tool is actually a short [Ruby][2] script that calls [ImageMagick][3] to create the distance field.
So you need both Ruby and ImageMagick installed on your computer.

### MacOS
### macOS

MacOS already comes with Ruby, so you only need to install ImageMagick.
For that, my recommendation is to first install [Homebrew][4], a fantastic package manager for macOS.
Once ready, you can install ImageMagick (like many other tools and libraries) with the following terminal command:

brew install imagemagick

You also optionally make the script executable, which allows you to call it directly (without passing it to Ruby).
You can also optionally make the script executable, which will allow you to call it directly (without passing it to Ruby).

cd /path/to/starling/util/field_agent
chmod u+x field_agent.rb
Expand All @@ -31,6 +31,8 @@ Once ready, install both Ruby and ImageMagick via the command line (`cmd.exe`).
choco install ruby
choco install imagemagick

It also works great with [Cygwin][6].

## Usage

As input, the "Field Agent" requires a pure black and white bitmap.
Expand Down Expand Up @@ -69,10 +71,15 @@ Of course, you can customize that:

Note that the value is given in pixels of the _output_ image.

### Rendering
### More options

Run the script without any arguments to see a list of all supported options.
For example, it also supports trimming the texture to the optimal size or inverting its colors.

## Using the Texture in Starling

Inside Starling, load the distance field texture as usual, then assign it to an _Image_ or any other kind of _Mesh_.
To switch to distance field rendering, simply assign the appropriate [style][6].
To switch to distance field rendering, simply assign the appropriate [style][7].

var image:Image = new Image(distanceFieldTexture);
image.style = new DistanceFieldStyle();
Expand All @@ -84,4 +91,5 @@ That's it!
[3]: http://www.imagemagick.org
[4]: http://brew.sh
[5]: https://chocolatey.org
[6]: http://doc.starling-framework.org/current/starling/styles/DistanceFieldStyle.html
[6]: https://cygwin.com
[7]: http://doc.starling-framework.org/current/starling/styles/DistanceFieldStyle.html
15 changes: 11 additions & 4 deletions util/field_agent/field_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
input_file = nil
output_file = nil

def windows?
(/mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
def windows_prompt?
`echo \\(`.strip != "("
end

def log(message)
Expand All @@ -29,7 +29,7 @@ def fail(message)

# crude workaround so that the call to 'convert' works on Windows, too.
def system_xp(command)
if windows?
if windows_prompt?
command.gsub!("'", '"')
command.gsub!("%", "%%")
command.gsub!("\\(", "(")
Expand All @@ -43,7 +43,8 @@ def system_xp(command)
options.spread = 8
options.quality = 1
options.scale = 1
options.convert_path = windows? ? 'magick' : 'convert'
options.auto_size = false
options.convert_path = windows_prompt? ? 'magick' : 'convert'

option_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename(__FILE__)} input-file output-file [options]"
Expand All @@ -66,6 +67,10 @@ def system_xp(command)
options.invert = true
end

opts.on('-a', '--auto-size', 'Extend or trim image to the size required by the DF.') do
options.auto_size = true
end

opts.on('-c', '--convert-path PATH', String, 'Name or path of the ImageMagick "convert" tool.') do |path|
options.convert_path = path
end
Expand Down Expand Up @@ -104,9 +109,11 @@ def system_xp(command)
command << "-negate " if options.invert
command << "-background white -alpha remove "
command << "-filter Jinc -resize #{options.quality * 100}\% -threshold 30% " unless options.quality == 1
command << "-bordercolor white -border #{spread}x#{spread} " if options.auto_size
command << "\\( +clone -negate -morphology Distance Euclidean:4,'#{spread}!' -level 50%,-50% \\) "
command << "-morphology Distance Euclidean:4,'#{spread}!' "
command << "-compose Plus -composite "
command << "-trim " if options.auto_size
command << "-resize #{options.scale / options.quality * 100}\% " unless options.scale * options.quality == 1
command << "-negate -alpha copy -channel RGB -fx '#fff' "
command << "PNG32:#{output_file}"
Expand Down

0 comments on commit d16c613

Please sign in to comment.