Creating transparency texture maps

If you want your player to partially see through an object, you'll need a transparent or semi-transparent material. Plastic film, cutouts, and grids are some of the artifacts you can produce with transparent texture maps.

Getting ready

For this recipe, we will create a material simulating a slide film. The main reason behind this choice is because this material will allows us to use full transparency to make rounded borders of the frame, and also use semi-transparency to illustrate the film itself. For the rest of this recipe, it is assumed you are capable of creating, using your image editor, an image like the one intended (basically, a beveled round-cornered rectangle containing a picture within). If not, please feel free to use the one included inside the 0423_03_04 folder.

How to do it...

To create a transparent material, follow these steps:

  1. Create a new material named slideMaterial.
  2. Select your material. In the Inspector view, under the material's name, use the drop-down menu to change its Shader value to Transparent/Diffuse.
  3. Open the base texture in your image editor (we'll use Adobe Photoshop to illustrate the next steps).
  4. Select the empty pixels around the rounded rectangle (this can be done with the Magic Wand tool with Anti-alias turned on and a Tolerance level of 0).
    How to do it...
  5. Open the Channels window (in Photoshop this can be done by going to Window | Channels).
  6. There should be three channels: Red, Green, and Blue. Create a new channel. This will be the Alpha channel that controls the level of transparency, where darker levels are more transparent than brighter levels.
  7. In the Channels window, select the Alpha channel and invert your selection (Ctrl + Shift + I for Windows and Command + Shift + I for Mac OS).
  8. Using the Paint Bucket tool, paint the selection area white:
    How to do it...
  9. Reset your selection area (using Ctrl + D/Command + D). Now, create a rectangular selection around the actual photo:
    How to do it...
  10. Go back to the Alpha channel and fill the selected rectangle light gray (R, G, and B value around 170, for instance):
    How to do it...
  11. Save your file (.psd or .tga formats are good options, as they keep the alpha channel).
  12. Inside the Unity Editor, import your image file access by navigating to Assets | Import New Asset....
  13. In the Project view, choose slideMaterial. Then, select the image file you created as a Base map (by either clicking on the Select button or dragging it from the Project view to the material slots). Your transparent material is ready:
    How to do it...

How it works...

Unity is able to read four channels of a texture map: R (red), G (green), B (blue), and A (alpha). Transparent shaders use RGB channels as the base texture (often called diffuse texture), while using the Alpha to set the transparency of the material according to each pixel's brightness level. Transparent shaders in the Cutout subgroup will not render semi-transparency; they just allow texture pixels to be invisible or fully opaque.

There's more...

Unity has a range of transparent shaders that can be used to achieve different results.

Using Cutouts

There is a subgroup of transparent shaders named Cutout. If you don't need semi-transparency in your material, it might be a good idea to use a Cutout shader. They are faster and allow the object to cast and receive shadows.

Applying Bumped Diffuse

To experiment, change the material's Shader value to Transparent/Bumped Diffuse. Then, apply the slideNormalTexture.png image file (included in the 0423_03_04 folder) as a normal bump map.

See also

  • The Creating a self-illuminated material recipe.
  • The Creating specular texture maps recipe.
  • The Solving transparent objects pop-ups recipe.
  • The Disabling culling for a material recipe.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.69.163