Saturday, July 28, 2012

Customizing cgwg's CRT Pixel Shader

This document identifies some of the user-configurable options available in cgwg's GLSL CRT shader and provides some screenshots to explain their effects.

For individuals interested in recapturing the look of retro gaming consoles played on a CRT television like the ones we grew up with, cgwg's CRT shader provides the closest approximation available for modern LCD displays. The pixel shader is purely cosmetic--nothing could impart the technical advantages of a CRT, such as negligible input lag and insanely high contrast, to an LCD--but it can go a long way to displaying old games as we remember them. Since memory and nostalgia are important to the way we perceive these games, each person will have different feelings about what looks "right" to them.

We will be using the latest (as of the time of this writing, 7/28/12) and most complete version of the shader, known as CRT-Geom. This shader allows for simulated screen curvature, rounded corners, simulation of a back-tilted screen (for arcade emulation) and many other goodies. All images are captured at 4x scale, click the thumbnails to embiggen.

This is what the shader looks like with all default parameters, also known as the 'curved' variant (cgwg chose sane values for all of the variables by default):
The user-configurable parameters are primarily located in two places in the shader, starting at line 87 for the first section and line 135 for the second section. You should be able to open and edit the shader in any text editor, but I recommend Notepad++.

The first parameters, 'gamma of simulated CRT' and 'gamma of display monitor' allow the shader to determine how much gamma correction to apply to the image. CRT displays generally had higher gamma ratings than most LCD displays (2.4 for a typical CRT vs 2.2 for a typical LCD), so the shader adds 0.2 to the gamma to compensate. If you calibrate your LCD to a higher or lower gamma setting, you may wish to change the 'gamma of display monitor' to reflect your individual settings. You can also raise or lower the simulated gamma to make a darker, more saturated look:
or a lighter, desaturated look:

Next, we have 'overscan.' Each CRT showed a slightly different amount of the picture, and the cut-off portion was referred to as overscan. You can modify the amount of over- or underscan by changing this variable. Setting it to (0.00,0.00) will assume you want the picture aligned exactly with the borders of the window. Setting it to a value greater than 1.0 (for example, 1.10,1.10) will cut off a large portion of the screen (approximately 10% in this example):
while setting it to a value less than 1.0 will surround the screen with black bars (i.e., underscan):

Increasing the overscan slightly, say, to 1.02,1.02, can cut off unsightly garbage lines at the top of the screen, which is common on NES games.

Next, we have 'aspect ratio,' which controls how much our screen curvature settings will affect the top/bottom and sides of the image. The default setting of 1.0, 0.75 means the curvature will be more pronounced on the top/bottom than on the sides, to compensate for the standard 4:3 aspect ratio. Setting it to 1.0, 1.0 will make the settings affect both sides of the screen equally, which isn't quite right in that the top/bottom actually appears more curved than the sides:
 If for some reason you want only the sides straight, you can set it to 1.0, 0.5:

or 0.5, 1.0 to make only the top/bottom straight:

Next up, we have 'simulated distance from viewer to monitor.' Similar to the 'aspect ratio' setting, this exaggerates the effect of the 'tilt angle' setting that we'll be covering in just a moment. The default value of 2.0 is reasonable. Increasing the value reduces the effect of the tilt angle:
while lower values magnify it:

Next, we have 'radius of curvature,' which determines how curved our simulated TV tube will be. Setting the value to 2.0 reduces the curvature to something more like what I remember.

'Tilt angle' pincushions one side of the screen. To tilt the screen back, use a negative value for the second number. The default for the 'arcade tilt' variant, (0.0,-0.15), is a sane value:
while increasing the second value tilts it back further, which can be disorienting:

After that, we come to 'cornersize,' which determines the radius of the corner arc. I think a setting of 0.03, as used in the default image, looks appropriate. Increasing the value to 0.10 makes for some silly, super-round corners:
while a value of 0.001 makes for super-pointy corners:

For the last parameter in this section, we have 'cornersmooth,' which determines how sharp (sharp as in sharp/blur, not as in pointy) the corners look. The default value is 1000.0, which looks fine, but I noticed that if you set the value insanely low (i.e., 80), you get a little vignetting around the edges, which reminds me of my crummy old TVs:
On to the next set of parameters.

The second set of options are either enabled or disabled by using 'comments,' which in this case are represented as a double-slash, like this //.

The first option is 'linear processing,' which calculates the interpolation in linear gamma:
The effect is subtle (black outlines will be thinner), but disabling it via commenting it out (//#define LINEAR_PROCESSING) can improve the framerate with some video cards.

Next, we have 'Enable screen curvature.' Pretty self-explanatory, but if you comment this line out, you'll get straight top/bottom/sides of the screen, but with the corner arcs intact (barely visible in this shot):
By commenting this out and setting 'cornersize' to 0.0, you get an idealized flat CRT tube, which is represented by the 'CRT-flat' shader variant:

The next two options work together (that is, if you uncomment one, you should comment the other). By default, the shader uses 3x oversampling of the beam profile, which makes brighter pixels bleed further into the surrounding scanlines. Here it is with the default oversampling, scaled up 400% (look closely; it's most obvious on the bright red of Mario's sleeve):

and here it is with the gaussian beam profile:
The gaussian beam profile may have better performance on some video cards.

Those are all of the options available for the standard CRT shader, but there are a few more things we can add in. The first of which is support for interlaced video. Some games did a sort of fake doubling of their vertical resolution by showing different lines of a progressive signal on alternating frames in a process known as interlacing. If handled incorrectly, you will see a ton of combing artifacts (weird horizontal lines on moving objects), as well as distorted scanlines (first image taken using this older, non-interlace-supported version):

These pictures unfortunately cannot show the jutter in the lower image that occurs from showing different lines on each alternating frame.

Next, we have support for pseudo-hires transparency--whereby games created a fake transparency effect by doubling the horizontal resolution to 512 and then covering the transparent area with thin vertical lines, which were then squished together and blurred when the CRT displayed the image--in the games that utilize it, including Jurassic Park and Kirby's Dreamland 3:

This blurring is accomplished by adding another pass to the shader that does nothing but squish the horizontal resolution from 512 to 256. Note: this variant is multipass and therefore incompatible with the official bsnes/higan GUI. Retroarch can use it just fine with bsnes-derived emulation cores, but it is incompatible with FBO/2-pass shader stacking.

Finally, to simulate the natural phosphor glow of a CRT display, cgwg created a multipass version of his CRT shader that includes a "halation" pass, which provides a soft blur that is most noticeable against a black background, such as the Nintendo logo screen in Super Mario World (it also seems to be doing some aspect ratio correction, though I'm unsure if that's intentional):
Here's a shot of Super Metroid's title screen without and with halation, respectively:

Note: Like the TV blur variant, this halation variant is multipass and thus incompatible with the official bsnes/higan GUI. It works fine with Retroarch but is incompatible with the FBO/2-pass feature.

Now, beyond the CRT display, our childhood gaming experiences were also colored by limitations of the NTSC signal standard and the lossy, analog hookups we used to connect our consoles. To capture these effects, we turn to blargg's NTSC filter, which runs on the CPU rather than the GPU and can thus be combined with our CRT shader.

The NTSC filter is available in these CPU filter packs (compatible with RetroArch but not bsnes/higan):
Linux 32-bit
Linux 64-bit
Mac OS X

The least intrusive variant of the filter is the RGB preset, which simulates a high-quality RGB connection. This variant provides NTSC color gamut correction but leaves the signal otherwise untouched:
Next, we have the S-video variant, which has color correction and some fringing, but is still relatively high-quality. This is the variant I use most of the time:
The third variant, composite, brings in a lot of signal noise and color bleed, just as you would get through a composite, RCA-cable connection:
The worst-quality variant is RF, which looks just as messy as the composite signal, but with added jutter every other frame, just like when using the RF adapter bundled with classic consoles:
One thing to be aware of, though, when using any variant of the NTSC filter is that it also does some gamma correction, which can be overwhelming to some people when combined with the CRT shader. To mitigate this effect, you can modify the CRT shader's 'gamma of simulated CRT' to match the 'gamma of display monitor' value to disable the shader's own gamma correction (shown here with the S-video variant):

My personal preferences, depending on the game/system: 'radius of curvature' of 2.0, cornersize of 0.03, cornersmooth of 80.0 and NTSC-Svideo. Sometimes I'll use the flat variant (especially on displays that have problems with moire patterns caused by curvature+scanlines), and I often use the halation variant when I'm on a machine with sufficient resources. I always use interlacing support, which has been incorporated into all of the latest versions, and TV blur when possible (i.e., when using Retroarch rather than the official bsnes/higan GUI).

Sunday, July 15, 2012

SFxT: How to Duplicate A Character - Proof of Concept

There are a lot of reasons why you might want to duplicate a character in SFxT, the most obvious (to me) being that you would like to play a team that consists of 2 copies of a single character that you are most skilled with. To accomplish this task, we need to edit a few files and make copies and rename a bunch of others, so lets get some tools together first.

As usual, you'll need piecemontee's Asset Explorer (aka SF4Viewer) and a text editor (I recommend Notepad++; regular Notepad WILL NOT WORK). You'll also probably want a bulk renaming utility, such as the aptly named Bulk Rename Utility. You should also take this opportunity to make backups of any files we plan to edit, as mistakes can render your game unplayable. Repeat: MAKE BACKUPS BEFORE TRYING ANY OF THIS.

NOTE: this is currently a proof of concept. The duplicate character will only work in training mode unless we duplicate more files, which I'll add to the post if/when I track them all down. Fights with intro animations will crash. See the bottom of the post for Robhal's update! Also, duplicating characters will corrupt your save data, so be sure to back that up first.

STEP 1 - Creating our duplicate

The first step to duplicating a character is to make a copy of the target's character folder. For this tutorial, I'll be using Ken as an example. So, navigate to [install dir]\resource\CMN\battle\chara, right-click on the desired character folder, select copy, then right-click elsewhere in the directory and choose 'paste.' You should end up with a folder named '*** - copy.'

Next, rename that folder to a non-conflicting 3-letter prefix that will represent your new character duplicate. In my case, I'm going to use 'KE2.' Note: you must use exactly 3 letters for your prefix or the game will fail to even load.

Once that's done, navigate into your new folder and open your bulk renaming utility. Select all of your files in the folder and rename all of them to match your 3-letter prefix (that is, all of the KEN* files should be renamed to KE2*), then do the same for all of the files in the 'sound' folder (these steps will be different depending on which renaming utility you use, so I'm not going to go over it here).

It should end up looking like this:

Now, we'll need to make duplicate call sounds (*_CAL_*) for each character we expect to partner our duplicate with. For the sake of simplicity, I'm only going to partner KE2 with regular ol' KEN, but if you wanted to be able to partner him/her with any other character, you would need to make calls in each potential partner's 'sound' directory. So, navigate to his folder, go into 'sound' and then make a copy of his *_CAL_KEN_E and ...J files. Rename them to match your duplicate prefex, like this:

Note: Failure to create the proper calls in each partner's sound folder will result in a game crash. (It's interesting that Capcom has these duplicate calls in the first place, suggesting that single-character teams were a possibility at some point).

Ok, with that done, we're ready to move on to step 2.

STEP 2 - Telling the game about our duplicate

SFxT keeps track of the available characters and what to think about them in the const.lua file, located in the [install dir]\resource\CMN\boot\game directory. Make a copy of the file and move it to a directory where you have write permissions, such as your desktop.

Then, open the file in your text editor and you should see a section labeled "charas" at the top of the file. Navigate down to the subsection for your duplicated character (in my case, Ken) and copy everything from his/her subsection, starting with the open-curly-braces (this thing: { ) and ending with close-curly-braces-followed-by-a-comma (this part: }, ).

Now, paste that in (right after the original is fine) and change the first line, labeled "['code']" to your duplicate's code (in my case, KE2). You can change the name here, too, but I don't think it really matters. While we're here, you can also modify anything else you want, such as vitality, "official partner," default voice, etc.

Save the file and replace the original (make a backup first, if you haven't already). Now we're ready for step 3.

STEP 3 - Creating a character select slot

SFxT determines which character slots are available through the selector_layout.emz file, located in the [install dir]\resource\CMN\ui\pre_battle directory. However, if you try to open the file directly, you'll see just a bunch of gibberish because the file is compressed. To decompress it, just open piecemontee's Asset Explorer and drag the file into the left-hand window pane. It will automatically create a backup of the original file (called selector_layout.emz.compressed) in the same directory, which is convenient. Once that is done, open the now-decompressed file in your text editor and it should look something like this:

This grid represents the character select icon grid, and we can use this file to move characters around (in case you want your favorite character to be closer to the top, for instance) or create new slots altogether by overwriting blanks (#BL). You can also remove characters by replacing them with blanks.

As you can see in the above pic, I have already replaced one of the blanks with my duplicated character abbreviation, to the left of the Random Select box. Note: Make sure you place it somewhere adjacent to an existing character or else you won't be able to move the cursor to it (i.e., the far corners are not good spots).

Once you're finished making changes, save the file and replace the original, making sure to back it up first, if you haven't already.

Now we're ready to launch our game. If all goes well, you should see a new character select slot on the character select screen (to the left of the Random box, just as we stipulated in our text file), which is treated as an entirely new character as far as double-picking is concerned:

Some Current Unknowns

First, I don't know how the game determines which character portrait goes into the character select screen, which is why our dupe has a blank box. Second, I don't know why the duped character is positioned strangely when you select him/her (my KE2 always appears hanging off the screen if I select him first for some reason).

If you figure out anything cool, please leave a comment.

Have fun, share your work and ALWAYS MAKE BACKUPS.

UPDATE - Robhal figured out the missing files to make the duplicated characters usable in vs. and story modes:

And now these my instructions below are continuation from hunterk's method above, to be able to play the duplicated character in Arcade and Versus mode.
1. Go to folder "\Street Fighter X Tekken\stream\CMN\battle\chara", right-click on the desired character folder, select copy, then right-click elsewhere in the directory and choose 'paste.' You should end up with a folder named '*** - copy.' Next, rename that folder to a non-conflicting 3-letter prefix that will represent your new character duplicate. In this case, 'KE2'. Then navigate into the new folder and rename all the files (just two files) to match your 3-letter prefix (that is, all of the KEN* files should be renamed to KE2*).

2. Go to folder "\Street Fighter X Tekken\resource\CMN\ui\battle\comment\cmt", copy the desired character file (in here, KEN.cmt) and rename it to 'KE2.cmt'. OPTIONAL: You may also move the file 'KE2.cmt' to folder "\Street Fighter X Tekken\patch\CMN\ui\battle\comment\cmt" for easier maintenance.

3. Go to folder "\Street Fighter X Tekken\resource\ENG\ui\battle\comment", copy the desired character file (in here, KEN.m4s) and rename it to 'KE2.m4s'. OPTIONAL: You may also move the file 'KE2.m4s' to folder "\Street Fighter X Tekken\patch\ENG\ui\battle\comment" for easier maintenance.

4.Go to folder "\Street Fighter X Tekken\resource\ENG\ui\battle\subtitle", copy the desired character file (in here, KEN.m4s) and rename it to 'KE2.m4s'. OPTIONAL: You may also move the file 'KE2.m4s' to folder "\Street Fighter X Tekken\patch\ENG\ui\battle\subtitle" for easier maintenance.

1. In any battle, when the duplicated character use the Cross Rush (ABC Launcher), he/she still has that zoom-in camera animation.
2. In battle with intro, the duplicated character has a weird subtitle. And in the winning screen, the duplicated character has a blank comment.
3. You can't view the quickcombo preview, the trial, and the command list of the duplicated character, otherwise it will fall back to title screen.
4. When you play the Arcade mode, make sure you pick the duplicated character as 2nd character (tag partner). The reason is if you pick the duplicated character as 1st character and when you defeat the final boss (either Akuma or Ogre), after the credit screen, it will fall back to title screen. --> (I haven't found the solution to this issue yet)

Analytics Tracking Footer