понедельник, 30 июля 2012 г.

Fade A Whole Scene From Black & White to Colour

This can be done by following these steps. First, you'll need Unity Pro, because modifying the scene with a full screen image effect is a Pro-only feature.
Now, Unity Pro comes with a fullscreen Greyscale image effect, but it's all-or-nothing, you can't fade it in or out, you can only switch it on or off. So what I'm going to do is give you the instructions to tweak these files to add fading functionality to it. Specifically, I will add a variable which controls the fade amount, which you can then modify yourself at runtime. Here's what you have to do:
  1. Make sure the Pro Standard Assets are imported into your project. You'll be editing a couple of files in here, but these are only copies of your Pro Standard Assets - if you start another project and include Pro Standard Assets, you'll get the original unmodified versions again.
  2. In your project view, look in the "Pro Standard Assets / Image Based" folder. Inside are two files named "Grayscale Effect". One is a C# script, and one is a .shader file. Open the .shader file to edit the source (it's the one with the "S" icon).
  3. Add this line inside the "Properties" section (at line 5):
    _EffectAmount ("Effect Amount", Range (0,1)) = 1.0
  4. Add this line under the "uniform" declarations (at line 22):
    uniform float _EffectAmount;
  5. Modify the 'return' line to look like this (which is now at line 31):
    return lerp(original, output, _EffectAmount);
  6. Now save and close GreyscaleEffect.shader, and open the other "GreyscaleEffect" file, which is a C# file called "GreyscaleEffect.cs"
  7. Under the Public variable declarations, add this line (at line 8):
    public float effectAmount = 1;
  8. Just before the 'Graphics.Blit' line, add this line (at line 13):
    material.SetFloat("_EffectAmount", effectAmount);
You now have an extra variable in the Greyscale script which you can modify to control the fading. You can modify this value either in the animation editor, or via another script.
Just add this image effect script to your camera, then create a reference to the GreyscaleEffect script instance at runtime. You can then modify the "effectAmount" variable anywhere from 0.0 to 1.0 to fade the effect in and out. Note: there's no error checking for whether that value goes beyond 1.0, or lower than 0.0 so you'll have to manage that yourself (weird colourful effects ensue if you don't!).

Комментариев нет:

Отправить комментарий