About Me

My photo
Game Developer, Composer & Gamer.
All my tutorials are categorised into 3 different sections, "The Lounge" for beginner, "Rec Zone" for intermediate and "Pro Series" for advanced tutorials.

Monday, September 6, 2010

Pro Series #1: How To Make Decals From Non Power of 2 Images

This tutorial is divided into 8 parts;
  • Introduction
  • Part I: Setting Up
  • Part II: Creating & Using Photoshop Actions
  • Part III: Creating a txt list using Windows batch files
  • Part IV: Creating an Extract Alpha batch using ImageMagick & txt list from Part III
  • Part V: Creating an Embed Alpha batch using ImageMagick & txt list from Part III
  • Part VI: Summary & Cleanup
  • Credits
-----------------------------------------------------------------------------
------------------------------------
--Introduction--
------------------------------------

For the following tutorial, you will need several things that make this quicker and easier than doing some things manually.
  • ImageConverter Plus (i have v7.1)
  • DirectX SDK (i have Aug2008)
  • Adobe Photoshop (i have CS4)
  • ImageMagick (i have v6.5.3-Q16)
  • Notepad++ (i have v5.5)
  • Knowledge of Photoshop actions
  • Knowledge of creating and running batch files
  • Knowledge of using "Text paint mode" in Notepad++
Althought i have explained the above 3 methods it in simplest terms possible for newbies

------------------------------------
--PART I--
--Setting Up--
------------------------------------



1. Make sure you make a backup of the source images because they may be needed later!


2. Convert desired images to PNG using ImageConverter Plus ( Preferably in a new folder, e.g "PNG\" )

3. Make a new folder called "4 Textures" or something similar

4. Convert source images to PNG into the "4 Textures" folder using DirectX SDK Converter tool !! with CONSTRAIN PROPORTIONS CHECKED !!
This will convert the source images to a power of 2 texture size, this will stretch the images but do not worry, we wont be using them apart from their dimensions.

5. Navigate to the "4 Textures" folder, and choose the Details view, then right click the title bar (Where it says Name | Type | Size) and choose "Dimensions".
If it is not in the list, click "More" then check the Dimensions box and then OK.

6. Click the Dimensions title in the title-bar to sort the images by their dimensions.

7. Create folders for each different dimensions (e.g 1024x1024, 512x1024, 2048x2048 etc etc)

8. Move all the images into their respective dimensions folders.

9. Now this step may take some time, depending on how many images you are processing.
Go back to your Original PNG images that were converted using ImageConverter Plus, and move/replace them into the new dimensions folders.
For my first time i had around 223 images, took me about 15mins to do this step.

------------------------------------
--PART II--
--Creating & Using Photoshop Actions--

------------------------------------
 
10. In Photoshop, create an Action for the following:

10a. New Action

10b. Right click "Background" layer and chose "Layer From Background" (Make sure you are recording the action first)

10c. Go to Image menu > Canvas Size > choose your desired dimensions as per the folders you have just created.
Note: For each different dimensions, you will need to create a new action, i have 5 common canvas size actions, 1024x2048, 1024x1024, 1024x512, 512x1024 & 512x512
All 5 actions begin with the "Set Background" action, which is vital to this tutorial.

11. Run the actions by choosing File > Automate > Batch and choose the action you want to run
Note: Easiest way is to highlight the action in the Actions tab and then Automate, it will automatically select that action
The Automations settings i used were;
Set: & Action: should be already set (see above Note)
Source: Folder
[Choose] One of your dimensions folder (e.g 512x512)
Uncheck all 4 boxes
Destination: Save & Close
Uncheck 'Override Action "Save As" Commands'
Errors: Stop for errors

------------------------------------
--PART III--
--Creating a txt list using Windows batch files--
------------------------------------


12. After running all the action sets, you should now have PNG images in your dimensions folders that are power of 2 but not stretched. Check this to make sure you have done everything right.

13. Now you will need to create a list of your new images for further processing;
Create a new text document in the root folder ("4 Textures") of all your dimensions folders, for example;
C:\
TexTemp\
4 Textures\
512x512\
1024x1024\
create the text file here

13a. rename the text file "dir.bat". Windows should ask if you want to change the file extension, click yes.

13b. Right click the .bat file and click Edit

13c. In notepad, paste the following and save/close notepad;
winbatch code:
@ECHO OFF
DIR /s /b /a:-d /o:n *.png > files.txt
PAUSE

13d. Now run this batch file, it should create a new text file called "files.txt" which contains a raw list of your images.

------------------------------------
--PART IV--
--Creating an Extract Alpha batch using ImageMagick & txt list from Part III--

------------------------------------
 
13e. Create another new text document in the root folder ("4 Textures"), rename it to "extract_png_alpha.bat". Windows should ask if you want to change the file extension, click yes.

13f. Right click the .bat file and click Edit

13g. In notepad, paste the following;
winbatch code:
@ECHO OFF
SET IMDIR=C:\Program Files\ImageMagick-6.5.3-Q16
SET APP=convert.exe
SET OPTS=-alpha extract

REM Usage:
REM "%IMDIR%\%APP%" "input.png" %OPTS% "output_alpha.png"

!YOUR CODE HERE!

ECHO DONE!
PAUSE
EXIT

13h. Now where i have put !YOUR CODE HERE! you will need to put your batch code based on the usage above.
For example;
winbatch code:
"%IMDIR%\%APP%" "512x512\poster.png" %OPTS% "512x512\poster_alpha.png"
Note: This is when the "files.txt" list comes in handy.

14. Save and close the unfinished "extract_png_alpha.bat"

15. Open "extract_png_alpha.bat" and "files.txt" in Notepad++
Note: For the purposes of this tutorial DO NOT save "files.txt" after editing in Notepad++ (It will be needed again in Part V)

16. Highlight the first line from C:\ to 4 Textures
For example;
C:\TexTemp\4 Textures\
(note the backslash at the end, that is important)


16a. Now with part of that line highlighted, press Ctrl + H

16b. "Find What" should be auto filled in, "Replace With" this:
winbatch code:
"%IMDIR%\%APP%" "
(note the space & quote at the end, that is important)
 
16c. Click [Replace All]
(The reason for this step is ImageMagick doesn't understand Full paths for some reason, only relative paths to where the batch is being run from [this is why we created it in the "4 Textures" folder])

Now you should have all of the files in "files.txt" beginning with something like this;
winbatch code:
"%IMDIR%\%APP%" "512x512\my_texture.png
16d. Now highlight the png in any line, and press Ctrl + H again, this time;
"Find What" png
"Replace With" png"

NOTE THE QUOTE AT THE END OF PNG!


Now you list should have;
winbatch code:
"%IMDIR%\%APP%" "512x512\my_texture.png"
With the quotes on the end of the files

17. Select All from the "files.txt" and paste in a new document (click the New icon top left in notepad++)

18. Highlight "%IMDIR%\APP%" (With the quotes) and press Ctrl + H.

18a. "Find What" "%IMDIR%\%APP%"
"Replace With" %OPTS%

Notice there are no quotes in %OPTS%
Now you list should have;
winbatch code:
%OPTS% "512x512\my_texture.png"
18b. Now highlight the .png in any line, and press Ctrl + H yet again, this time;
"Find What" .png"
"Replace With" _alpha.png"
NOTE THE QUOTE AT THE END OF PNG!
Now you list should have;
winbatch code:
"%OPTS%" "512x512\my_texture_alpha.png"

19. Now this next part is a bit of a secret usage of Notepad++ that has saved me countless hours over the years:

I call it "Text Paint" because highlighting doesn't follow conventional Line by Line rules, instead it acts like a paint brush!


19a. In the new document you just edited, scroll to the bottom of the list.

19b. HOLD ALT AND THEN CLICK AND DRAG FROM THE VERY LEFT OF THE BOTTOM LINE ALL THE WAY UP TO THE TOP RIGHT

(NOTE THAT SOME LINES ARE LONGER THAN OTHERS, YOU _MUST_ LEAVE ENOUGH SPACE ON THE RIGHT FOR ALL LINES TO BE SELECTED)


TIP: Visually find the longest line and click at the end of it, note the "Col:" number in the bottom details bar.

19c. Now press Ctrl + C (Depending on the amount of lines in the file your computer might lag)

19d. Now go back to your files.txt file (while still in Notepad++)

19dd. Select the end of the FIRST LINE and hold space until you reach sufficient space for the modded list to be pasted

(Alternatively hold space until you reach the "Col:" number of the longest line - but it is best to add 1 or 2 spaces after this column for safety)

19e. Now immediately press Ctrl + V (paste) and the modded list should be pasted like paint down the right side (Depending on the amount of lines in the file your computer might lag again)

Note: However, after doing this, Notepad++ creates many many spaces between the pasted list and the original on the left, but i'll cover this in the next step.

20. Highlight any double space in the list, (" ") and press Ctrl + H, and Replace with a single space (" ")

20a. Press [Replace All] (Depending on the amount of lines in the file your computer might lag again)

20b. You will notice this hasn't fixed it completely, keep pressing [Replace All] Until the amount of replacements reaches zero.

21. Now you list should look something like this;

winbatch code:
"%IMDIR%\%APP%" "512x512\my_texture.png" %OPTS% "512x512\my_texture_alpha.png"

22. Now copy the list into the "extract_png_alpha.bat" where i have (You can remove the text)


23. Now save the batch file & if you are satisfied with it, close the files.txt file (DONT SAVE THIS LIST)

24. I would also usually add a PAUSE after the first file in the "extract_png_alpha.bat" to see if it has worked.
For example;
winbatch code:
"%IMDIR%\%APP%" "512x512\my_texture.png" %OPTS% "512x512\my_texture_alpha.png"
PAUSE
"%IMDIR%\%APP%" "512x512\my_texture2.png" %OPTS% "512x512\my_texture2_alpha.png"
"%IMDIR%\%APP%" "512x512\my_texture3.png" %OPTS% "512x512\my_texture3_alpha.png"
"%IMDIR%\%APP%" "512x512\my_texture4.png" %OPTS% "512x512\my_texture4_alpha.png"

Note: the code above is word wrapped on ModDB (each line has a different colored background here, white and grey).

25. Now save & run the batch file.

Remember that we put the PAUSE after the first file, so no matter what the size of the image is it should display "Press Any Key To Continue..." almost immediately

DONT press any key until you have checked it has worked.

Note: Unfortunately ImageMagick doesn't display anything while processing the images so if my list is very large, i normally put a few ECHO's somewhere in the list,
like for each third of the list i'll put ECHO 33% Complete, ECHO 66% Complete, etc etc. Doesn't have to be exact just a rough guess


------------------------------------
--PART V--
--Creating an Embed Alpha batch using--
--ImageMagick & txt list from Part III--
------------------------------------


This part is similar to Part IV, this time we will need to use both the extract_alpha batch and the original files.txt.

26. Make a copy of "extract_png_alpha.bat" and rename it to "embed_alpha.bat"

27. Open "embed_alpha.bat" and "files.txt" in Notepad++

27a. In "embed_alpha.bat" highlight and replace this;

"Find What" SET OPTS=-alpha extract
"Replace With" SET OPT1=-alpha off

27b. Add the following line directly under the SET OPT1=-alpha off

SET OPT2=-compose copyopacity -composite

So the top of your "embed_alpha.bat" should now look like this;
winbatch code:
@ECHO OFF
SET IMDIR=C:\Program Files\ImageMagick-6.5.3-Q16
SET APP=convert.exe
SET OPT1=-alpha off
SET OPT2=-compose copyopacity -composite

27c. Highlight %OPTS% in any line and replace with %OPT1%
28. Now go to the files.txt list
28a. Once again highlight C:\TexTemp\4 Textures\ and press Ctrl + H, This time replace with: %OPT2% "
Note: the space and the quote after %OPT2%
- this is important and i always get caught with it


28b. Now go to the end of any line (still in the files.txt list) and replace .png with .tga"
Note: the " after .tga


28c. Now just like in step 19b, Hold Alt and drag from the very bottom left dragging to the top right to highlight the list

(Remembering about seeking the longest line and taking note of the Col: number)

28d. Press copy, then go back to the "embed_alpha.bat" file and once again seek out the longest line,
select the first line of the list (not of the whole batch file) and hold space until you reach the Col: number (or 1 or 2 spaces after)

28e. Press Paste (Depending on the amount of lines in the file your computer might lag)

28f. Repeat step 20:

20. Highlight any double space in the list, ("  ") and press Ctrl + H, and Replace with a single space (" ")
20a. Press [Replace All] (Depending on the amount of lines in the file your computer might lag again)
20b. You will notice this hasn't fixed it completely, keep pressing [Replace All] Until the amount of replacements reaches zero.

29. Now you list should look something like this;
winbatch code:
"%IMDIR%\%APP%" "512x512\my_texture.png" %OPT1% "512x512\my_texture_alpha.png" %OPT2% "512x512\my_texture.tga"
Note: the code above is ONE line

30. Now save the batch file & if you are satisfied with it, close the files.txt file

31. Again, add a PAUSE after the first file in the "embed_alpha.bat" to see if it has worked.

(The output is now TGA so you will need to load the file into Photoshop or something similar [I use Paint.NET to preview because it loads faster than Photoshop and also parses alphas])

32. Now save & run the batch file.

(Repeated from step 25)
Remember that we put the PAUSE after the first file, so no matter what the size of the image is it should display "Press Any Key To Continue..." almost immediately

DONT press any key until you have checked it has worked.


Note: Unfortunately ImageMagick doesn't display anything while processing the images so if my list is very large, i normally put a few ECHO's somewhere in the list,
like for each third of the list i'll put ECHO 33% Complete, ECHO 66% Complete, etc etc. Doesn't have to be exact just a rough guess

------------------------------------
--PART VI--
--Summary & Cleanup--
------------------------------------


Ok so now you should have a folder with both tga files, _alpha png's and the canvas sized pngs which with alot of files can get messy, i can also help in that deptartment :)

Create 2 new text files;
copy_tga.bat
copy_png_n_alphas.bat


1a. Open them in Notepad++

1b. In "copy_tga.bat" paste the following;

winbatch code:
@ECHO OFF
REM Frequently changed
SET TYP=TGA
SET EXT1=*.tga

REM Perma
SET TTL=Copy Utility [%TYP%]
SET DIR=..\%TYP%_TEMP
SET CPR=© 2008-2010 Jenkins. All Rights Reserved.
SET CPR2=(xer2k8@gmail.com - steam username: jenkins_08)
TITLE %TTL%

REM Make the Target directory
MKDIR %DIR%

REM Do the (x)copy
XCOPY "%EXT1%" %DIR% /S

REM Delete source files (CAUTION! - READ BELOW)
DEL /s %EXT1%

REM Move Target dir back to Source dir (MUST COME AFTER DEL COMMAND !!!)
MOVE "%DIR%" "%~dp0"

REM Done
ECHO ------------------------
ECHO %TTL% %CPR%
ECHO %CPR2%
ECHO ------------------------
ECHO Grab Completed at %TIME% - %DATE%
PAUSE
EXIT

1c. In "copy_png_n_alphas.bat" paste the following;
winbatch code:
@ECHO OFF
REM Frequently changed
SET TYP1=PNG_ALPHA
SET TYP1=PNG
SET EXT1=*_alpha.png
SET EXT2=*.png

REM Perma
SET TTL=Copy Utility [PNG & PNG Alphas]
SET DIR1=..\%TYP1%_TEMP
SET DIR2=..\%TYP2%_TEMP
SET CPR=© 2008-2010 Jenkins. All Rights Reserved.
SET CPR2=(xer2k8@gmail.com - steam username: jenkins_08)
TITLE %TTL%

REM Make the Target directory
MKDIR %DIR1%
MKDIR %DIR2%

REM Do the (x)copy
XCOPY "%EXT1%" %DIR1% /S
XCOPY "%EXT2%" %DIR2% /S

REM Delete source files (CAUTION! - READ BELOW)
DEL /s %EXT1%
DEL /s %EXT2%

REM Move Target dir back to Source dir (MUST COME AFTER DEL COMMAND !!!)
MOVE "%DIR1%" "%~dp0"
MOVE "%DIR2%" "%~dp0"

REM Done
ECHO ------------------------
ECHO %TTL% %CPR%
ECHO %CPR2%
ECHO ------------------------
ECHO Grab Completed at %TIME% - %DATE%
PAUSE
EXIT

2. Save & Close both files, then run them.

These 2 batch files will "grab" all of the images from all subdirectories using the EXT1/EXT2 mask set in the batch file.
So for the copy_tga.bat file, it will only grab all of the .tga files and wont touch anything else.
It will also "Restore folder structure" in the new temp folder that is created.
This file also deletes all the files that are "grabbed". If you don't want this to happen, put REM infront of the lines starting with DEL (e.g REM DEL /s %EXT1%)
It will then move the temp folder back into the current folder. Once again if you don't want this to happen, put REM infront of the lines starting with MOVE (e.g REM MOVE "
%DIR1%" "%~dp0")

Congratulations!

You have now completed this tutorial.


------------------------------------
--Credits--
------------------------------------

Thanks to the users at imagemagick forum for the extract alpha stuff
which has intern helped me to create this tutorial

I pretty much figured the rest out for myself lol.

This tutorial took me 2 and a half hours to write, so it should take you at least half that time to actually use (if your experienced enough).

Also lots of this tutorial can be used for many other things.

No comments:

Post a Comment