-
-
Save emilio-rst/baae894fa62cb08bb306 to your computer and use it in GitHub Desktop.
Revisions
-
markusfisch revised this gist
Jul 1, 2014 . 2 changed files with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -187,7 +187,7 @@ atlas_create() # prepare source files local SRC for SRC do local COPY="$TMPDIR/${SRC##*/}" @@ -222,4 +222,4 @@ readonly BORDER=${BORDER:-0} if [ "$BASH_SOURCE" == "$0" ] then atlas_create "$@" fi This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,7 @@ atlas_patch() local PATCH="s^{/\*${NAME}\*/[\"xywh:0-9,]*}^{/*${NAME}*/${FRAME%,}^g" local TMP=".${0##*/}-$$.tmp" local SRC for SRC do sed -e "$PATCH" < $SRC > $TMP && mv $TMP $SRC done -
markusfisch revised this gist
Jun 25, 2014 . 2 changed files with 8 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -219,4 +219,7 @@ atlas_create() readonly BORDER=${BORDER:-0} if [ "$BASH_SOURCE" == "$0" ] then atlas_create "$@" fi This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -35,4 +35,7 @@ atlas_patch() done } if [ "$BASH_SOURCE" == "$0" ] then atlas_patch "$@" fi -
markusfisch revised this gist
May 7, 2014 . 2 changed files with 149 additions and 115 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -45,22 +45,15 @@ Just put them before invocation, e.g.: $ ATLAS=atlas.jpg ./mkatlas img/* #### MAX_SIZE Maximum width/height of atlas. Default is 2048. #### BORDER Padding around sprites. Default is 0. #### ATLAS File name (and image format) of atlas image. [1]: http://en.wikipedia.org/wiki/Bash_(Unix_shell) [2]: http://en.wikipedia.org/wiki/Texture_atlas [3]: http://www.imagemagick.org/ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,17 +1,35 @@ #!/usr/bin/env bash # Compose images into atlas atlas_cache_compose() { [ -f $CACHE/args ] && convert \ -size "`< $CACHE/width`x`< $CACHE/height`" \ xc:transparent \ `< $CACHE/args` \ -strip \ -trim \ -bordercolor none -border $BORDER \ "${ATLAS:-atlas.png}" } # Print JSON and build arguments for convert atlas_cache_summarize() { echo "var atlas={" find $CACHE -type f -name image | while read do local X Y W H FILE read X Y W H FILE < $REPLY echo "$FILE -geometry +$X+$Y -composite" >> $CACHE/args local NAME=${FILE##*/} echo "${NAME%.*}:{x:$X,y:$Y,w:$W,h:$H}," done echo "};" } # Insert image, packing algorithm from @@ -20,162 +38,185 @@ atlas_cut_vertical() # @param 1 - image width # @param 2 - image height # @param 3 - image file atlas_cache_insert() { [ -f $CACHE/candidates ] || return 1 local INDEX TARGET while read INDEX TARGET do break done <<< "`sort $CACHE/candidates`" [ -f $TARGET/rect ] || return 1 local X Y W H read X Y W H < $TARGET/rect if (( W < $1 )) || (( H < $2 )) then return 1 fi mkdir $TARGET/child0 $TARGET/child1 || return $? local RW=$(( W-$1 )) local RH=$(( H-$2 )) if (( RW > RH )) then # +-------+---+ # | image | | # +-------+ | # | | r | # | b | | # | | | # +-------+---+ echo $(( X+$1 )) $Y $RW $H > $TARGET/child0/rect echo $X $(( Y+$2 )) $1 $RH > $TARGET/child1/rect else # +-------+---+ # | image | r | # +-------+---+ # | | # | b | # | | # +-----------+ echo $(( X+$1 )) $Y $RW $2 > $TARGET/child0/rect echo $X $(( Y+$2 )) $W $RH > $TARGET/child1/rect fi local RIGHT=$(( X+$1 )) (( $RIGHT > `< $CACHE/width` )) && echo $RIGHT > $CACHE/width local BOTTOM=$(( Y+$2 )) (( $BOTTOM > `< $CACHE/height` )) && echo $BOTTOM > $CACHE/height echo $X $Y $1 $2 $3 > $TARGET/image } # Find possible candidates and give them a sort index # # @param 1 - image width # @param 2 - image height # @param 3 - image file atlas_cache_find_nodes() { if [ -d "$NODE/child0" ] then NODE=$NODE/child0 atlas_cache_find_nodes $1 $2 $3 NODE=$NODE/child1 atlas_cache_find_nodes $1 $2 $3 return fi [ -f $NODE/rect ] || return 1 local X Y W H read X Y W H < $NODE/rect if (( W < $1 )) || (( H < $2 )) then return fi local MAX_WIDTH=`< $CACHE/width` local MAX_HEIGHT=`< $CACHE/height` local RIGHT=$(( X+$1 )) local BOTTOM=$(( Y+$2 )) (( RIGHT > MAX_WIDTH )) && MAX_WIDTH=$RIGHT (( BOTTOM > MAX_HEIGHT )) && MAX_HEIGHT=$BOTTOM printf '%08d %s\n' \ $(( MAX_WIDTH+MAX_HEIGHT )) \ $NODE >> $CACHE/candidates } # Sort files and insert them into the atlas atlas_cache_compile() { local NODE=$CACHE local W H FILE while read W H FILE do [ "$FILE" ] || continue printf '%08d %d %d %s\n' $(( W+H )) $W $H $FILE done | sort -r | while read MAX W H FILE do rm -f $CACHE/candidates if ! atlas_cache_find_nodes $W $H $FILE || ! atlas_cache_insert $W $H $FILE then echo 'error: cannot insert' $FILE >&2 return 1 fi done } # Read 'width height file' from standard input and create atlas atlas_create_from_list() { local CACHE CACHE=`mktemp -d ${0##*/}.XXXXXXXXXX` || return $? local MAX_SIZE=${MAX_SIZE:-2048} echo 0 0 $MAX_SIZE $MAX_SIZE > $CACHE/rect echo 0 > $CACHE/width echo 0 > $CACHE/height atlas_cache_compile && atlas_cache_summarize && atlas_cache_compose rm -rf $CACHE } # Create texture atlas from given image files # # @param ... - image files atlas_create() { local INKSCAPE=${INKSCAPE:-`which inkscape`} local TMPDIR TMPDIR=`mktemp -d ${0##*/}.XXXXXXXXXX` || return $? # prepare source files local SRC for SRC in "$@" do local COPY="$TMPDIR/${SRC##*/}" case ${SRC##*.} in svg) COPY="${COPY%.*}.png" # use inkscape if available [ "$INKSCAPE" ] && $INKSCAPE "$SRC" \ -z -e "$COPY" &>/dev/null && SRC=${COPY} ;; esac convert \ -background none \ "$SRC" \ -strip \ -bordercolor none -border 3x3 \ -trim \ -bordercolor none -border ${BORDER} \ "$COPY" done identify -format '%w %h %d/%f\n' "$TMPDIR/*" | atlas_create_from_list rm -rf $TMPDIR } readonly BORDER=${BORDER:-0} [ "$0" == "$BASH_SOURCE" ] && atlas_create "$@" -
markusfisch revised this gist
Apr 25, 2014 . 1 changed file with 9 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -140,10 +140,12 @@ atlas_from_list() # @param ... - image files atlas_from_files() { local INKSCAPE=${INKSCAPE:-`which inkscape`} local TRIMDIR TRIMDIR=`mktemp -d ${0##*/}.XXXXXXXXXX` || return 1 # trim/raster input files first local FILE for FILE in "$@" do @@ -152,6 +154,12 @@ atlas_from_files() case ${TRIM##*.} in svg) TRIM="${TRIM%.*}.png" # use inkscape if available [ "$INKSCAPE" ] && $INKSCAPE "$FILE" \ -z -e "$TRIM" &>/dev/null && FILE=${TRIM} ;; esac -
markusfisch revised this gist
Apr 24, 2014 . 1 changed file with 18 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -140,22 +140,32 @@ atlas_from_list() # @param ... - image files atlas_from_files() { local TRIMDIR TRIMDIR=`mktemp -d ${0##*/}.XXXXXXXXXX` || return 1 # trim input files first local FILE for FILE in "$@" do local TRIM="$TRIMDIR/${FILE##*/}" case ${TRIM##*.} in svg) TRIM="${TRIM%.*}.png" ;; esac convert \ -background none \ "$FILE" \ -strip \ -trim \ -bordercolor none -border $BORDER \ "$TRIM" done identify -format '%w %h %d/%f\n' "$TRIMDIR/*" | atlas_from_list rm -rf $TRIMDIR } readonly BORDER=${BORDER:-0} -
markusfisch revised this gist
Feb 4, 2014 . 2 changed files with 10 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -57,10 +57,10 @@ Padding around sprites. Default is 0. #### ATLAS File name (and image format) of atlas image. #### PREFER_LARGER_CELLS Prefer larger cells for lay out if set. Depending on your sprite set, that may make a smaller atlas. Just try. [1]: http://en.wikipedia.org/wiki/Bash_(Unix_shell) [2]: http://en.wikipedia.org/wiki/Texture_atlas [3]: http://www.imagemagick.org/ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -52,18 +52,18 @@ atlas_insert() if (( H > W )) then if (( PREFER_LARGER_CELLS )) then atlas_cut_horizontal $1 $2 else atlas_cut_vertical $1 $2 fi else if (( PREFER_LARGER_CELLS )) then atlas_cut_vertical $1 $2 else atlas_cut_horizontal $1 $2 fi fi -
markusfisch revised this gist
Jan 16, 2014 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -67,7 +67,12 @@ atlas_insert() fi fi echo \ $(( X+BORDER )) \ $(( Y+BORDER )) \ $(( $1-BORDER*2 )) \ $(( $2-BORDER*2 )) \ $3 > $NODE/image return 0 } -
markusfisch revised this gist
Jan 9, 2014 . 2 changed files with 3 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -43,7 +43,7 @@ Switches There are a few variables for fine tuning. Just put them before invocation, e.g.: $ ATLAS=atlas.jpg ./mkatlas img/* #### WIDTH Maximum width of atlas. Default is 2048. @@ -52,9 +52,7 @@ Maximum width of atlas. Default is 2048. Maximum height of atlas. Default is 2048. #### BORDER Padding around sprites. Default is 0. #### ATLAS File name (and image format) of atlas image. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -153,6 +153,6 @@ atlas_from_files() rm -rf $TRIMMED } readonly BORDER=${BORDER:-0} [ "$0" == "$BASH_SOURCE" ] && atlas_from_files "$@" -
markusfisch revised this gist
Oct 2, 2013 . 2 changed files with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ #!/usr/bin/env bash # Horizontal cut atlas_cut_horizontal() This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ #!/usr/bin/env bash # Patch given JavaScript files with JSON read from stdin # -
markusfisch revised this gist
Sep 10, 2013 . 1 changed file with 1 addition and 69 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,6 @@ mkatlas ======= [BASH][1] script to build a [texture atlas][2] for small/medium web sites/games. Requires [ImageMagick][3]. @@ -69,68 +65,4 @@ atlas. Just try. [1]: http://en.wikipedia.org/wiki/Bash_(Unix_shell) [2]: http://en.wikipedia.org/wiki/Texture_atlas [3]: http://www.imagemagick.org/ -
markusfisch revised this gist
Sep 9, 2013 . 1 changed file with 68 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,10 @@ mkatlas ======= [BASH][1] script to build a [texture atlas][2] for small/medium web sites/games. Requires [ImageMagickmkatlas ======= [BASH][1] script to build a [texture atlas][2] for small/medium web sites/games. Requires [ImageMagick][3]. @@ -40,6 +44,70 @@ This file may be patched with _patchatlas_ like this: Switches -------- There are a few variables for fine tuning. Just put them before invocation, e.g.: $ BORDER=0 ATLAS=atlas.jpg ./mkatlas img/* #### WIDTH Maximum width of atlas. Default is 2048. #### HEIGHT Maximum height of atlas. Default is 2048. #### BORDER Padding around sprites. If you intend to scale the atlas, it's wise to have a padding because sampling will make sprites "bleed" out of their frame. Default is 2. #### ATLAS File name (and image format) of atlas image. #### PREFER_SMALLER Prefer smaller region for lay out if set. Sometimes this gives a smaller atlas. Just try. [1]: http://en.wikipedia.org/wiki/Bash_(Unix_shell) [2]: http://en.wikipedia.org/wiki/Texture_atlas [3]: http://www.imagemagick.org/ ][3]. Usage ----- ### Basic Just run $ ./mkatlas img/* which will create _atlas.png_ and outputs the corresponding sprite regions in JSON format: var atlas={ first:{x:298,y:0,w:35,h:22}, second:{x:254,y:0,w:44,h:33}, ... } ### Advanced Alternatively you may use just markers in your JavaScript to insert the frame data in place. This way you wouldn't have to have a extra _atlas_ object and wouldn't need to do any additional mapping. For example: var obj = { name: "Jim", frame: {/*first*/x:0,y:0,w:0,h:0}, live: 100 }; This file may be patched with _patchatlas_ like this: $ ./mkatlas img/* | ./patchatlas index.html Switches -------- There are a few variables for fine tuning. Just put them before invocation, e.g.: -
markusfisch revised this gist
Sep 9, 2013 . 2 changed files with 30 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,6 +37,32 @@ This file may be patched with _patchatlas_ like this: $ ./mkatlas img/* | ./patchatlas index.html Switches -------- There are a few variables for fine tuning. Just put them before invocation, e.g.: $ BORDER=0 ATLAS=atlas.jpg ./mkatlas img/* WIDTH Maximum width of atlas. Default is 2048. HEIGHT Maximum height of atlas. Default is 2048. BORDER Padding around sprites. If you intend to scale the atlas, it's wise to have a padding because sampling will make sprites "bleed" out of their frame. Default is 2. ATLAS File name (and image format) of atlas image. PREFER_SMALLER Prefer smaller region for lay out if set. Sometimes this gives a smaller atlas. Just try. [1]: http://en.wikipedia.org/wiki/Bash_(Unix_shell) [2]: http://en.wikipedia.org/wiki/Texture_atlas [3]: http://www.imagemagick.org/ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -124,7 +124,7 @@ atlas_from_list() # compose images into atlas convert -size "${WIDTH}x${HEIGHT}" xc:transparent \ `< $CACHE/args` \ -strip -trim -bordercolor none -border $BORDER \ "${ATLAS:-atlas.png}" rm -rf $CACHE @@ -145,12 +145,14 @@ atlas_from_files() convert "$F" \ -strip \ -trim \ -bordercolor none -border $BORDER \ "$TRIMMED/${F##*/}" done identify -format '%w %h %d/%f\n' "$TRIMMED/*" | atlas_from_list rm -rf $TRIMMED } readonly BORDER=${BORDER:-2} [ "$0" == "$BASH_SOURCE" ] && atlas_from_files "$@" -
markusfisch revised this gist
Sep 8, 2013 . 1 changed file with 7 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -124,7 +124,8 @@ atlas_from_list() # compose images into atlas convert -size "${WIDTH}x${HEIGHT}" xc:transparent \ `< $CACHE/args` \ -strip -trim -bordercolor none -border 2 \ "${ATLAS:-atlas.png}" rm -rf $CACHE } @@ -141,7 +142,11 @@ atlas_from_files() local F for F in "$@" do convert "$F" \ -strip \ -trim \ -bordercolor none -border 2 \ "$TRIMMED/${F##*/}" done identify -format '%w %h %d/%f\n' "$TRIMMED/*" | atlas_from_list -
markusfisch revised this gist
Sep 4, 2013 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,6 +37,6 @@ This file may be patched with _patchatlas_ like this: $ ./mkatlas img/* | ./patchatlas index.html [1]: http://en.wikipedia.org/wiki/Bash_(Unix_shell) [2]: http://en.wikipedia.org/wiki/Texture_atlas [3]: http://www.imagemagick.org/ -
markusfisch created this gist
Sep 4, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ mkatlas ======= [BASH][1] script to build a [texture atlas][2] for small/medium web sites/games. Requires [ImageMagick][3]. Usage ----- ### Basic Just run $ ./mkatlas img/* which will create _atlas.png_ and outputs the corresponding sprite regions in JSON format: var atlas={ first:{x:298,y:0,w:35,h:22}, second:{x:254,y:0,w:44,h:33}, ... } ### Advanced Alternatively you may use just markers in your JavaScript to insert the frame data in place. This way you wouldn't have to have a extra _atlas_ object and wouldn't need to do any additional mapping. For example: var obj = { name: "Jim", frame: {/*first*/x:0,y:0,w:0,h:0}, live: 100 }; This file may be patched with _patchatlas_ like this: $ ./mkatlas img/* | ./patchatlas index.html 1: http://en.wikipedia.org/wiki/Bash_(Unix_shell) 2: http://en.wikipedia.org/wiki/Texture_atlas 3: http://www.imagemagick.org/ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,151 @@ #!/bin/bash # Horizontal cut atlas_cut_horizontal() { echo $(( X+$1 )) $Y $W $2 > $NODE/child0/rect echo $X $(( Y+$2 )) $NW $H > $NODE/child1/rect } # Vertical cut atlas_cut_vertical() { echo $X $(( Y+$2 )) $1 $H > $NODE/child0/rect echo $(( X+$1 )) $Y $W $NH > $NODE/child1/rect } # Insert image, packing algorithm from # http://www.blackpawn.com/texts/lightmaps/default.html # # @param 1 - image width # @param 2 - image height # @param 3 - image file atlas_insert() { if [ -d "$NODE/child0" ] then local N=$NODE NODE=$N/child0 atlas_insert $1 $2 $3 && return 0 NODE=$N/child1 atlas_insert $1 $2 $3 && return 0 return 1 fi [ -f $NODE/rect ] || return 1 local X Y NW NH read X Y NW NH < $NODE/rect if (( $1 > NW )) || (( $2 > NH )) then return 1 fi local W=$(( NW-$1 )) local H=$(( NH-$2 )) mkdir $NODE/child0 $NODE/child1 if (( H > W )) then if (( PREFER_SMALLER )) then atlas_cut_vertical $1 $2 else atlas_cut_horizontal $1 $2 fi else if (( PREFER_SMALLER )) then atlas_cut_horizontal $1 $2 else atlas_cut_vertical $1 $2 fi fi echo $X $Y $1 $2 $3 > $NODE/image return 0 } # Create texture atlas from a listing in the format "width height filename" atlas_from_list() { local CACHE CACHE=`mktemp -d ${0##*/}.XXXXXXXXXX` || return 1 local WIDTH=${WIDTH:-2048} local HEIGHT=${HEIGHT:-2048} echo 0 0 $WIDTH $HEIGHT > $CACHE/rect # sort files and insert them into the atlas local MAX W H FILE while read W H FILE do [ "$FILE" ] || continue if (( W > H )) then MAX=$W else MAX=$H fi printf '%08d %d %d %s\n' $MAX $W $H $FILE done | sort -r | while read MAX W H FILE do local NODE=$CACHE atlas_insert $W $H $FILE || { echo 'error: canvas too small' >&2 break } done # print JSON and build arguments for convert echo "var atlas={" find $CACHE -type f -name image | while read do local X Y W H FILE read X Y W H FILE < $REPLY echo "$FILE -geometry +$X+$Y -composite" >> $CACHE/args local NAME=${FILE##*/} echo "${NAME%.*}:{x:$X,y:$Y,w:$W,h:$H}," done echo "};" # compose images into atlas convert -size "${WIDTH}x${HEIGHT}" xc:transparent \ `< $CACHE/args` \ -strip -trim "${ATLAS:-atlas.png}" rm -rf $CACHE } # Create texture atlas # # @param ... - image files atlas_from_files() { local TRIMMED TRIMMED=`mktemp -d ${0##*/}.XXXXXXXXXX` || return 1 # trim input files first local F for F in "$@" do convert "$F" -strip -trim "$TRIMMED/${F##*/}" done identify -format '%w %h %d/%f\n' "$TRIMMED/*" | atlas_from_list rm -rf $TRIMMED } [ "$0" == "$BASH_SOURCE" ] && atlas_from_files "$@" This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ #!/bin/bash # Patch given JavaScript files with JSON read from stdin # # @param ... - JavaScript files to patch atlas_patch() { (( $# < 1 )) && { echo "usage: ${0##*/} FILE..." return 1 } while read do case $REPLY in *:{*) ;; *) continue ;; esac local NAME=${REPLY%%:\{*} [ "$NAME" ] || continue local FRAME=${REPLY#*\{} local PATCH="s^{/\*${NAME}\*/[\"xywh:0-9,]*}^{/*${NAME}*/${FRAME%,}^g" local TMP=".${0##*/}-$$.tmp" local SRC for SRC in "$@" do sed -e "$PATCH" < $SRC > $TMP && mv $TMP $SRC done done } [ "$0" == "$BASH_SOURCE" ] && atlas_patch "$@"