Disabling culling for a material

When creating a transparent or semi-transparent object, we might want to see its internal faces. However, by default, Unity transparent shaders make them invisible. In this recipe, we will edit one of Unity's built-in transparent shaders in order to make those faces visible to the user.

Getting ready

In order to follow this recipe, you will need to download the source code to Unity's built-in shaders from http://unity3d.com/unity/download/archive. You will also need a texture with a transparency channel. We have provided one, named grid.tga, in the 0423_03_10 folder.

How to do it...

To disable culling for a material, follow these steps:

  1. Open the archive containing the source code to Unity's built-in shaders and extract the file named AlphaTest-BumpSpec.shader, available within the DefaultResources folder, onto your desktop.
  2. Rename the AlphaTest-BumpSpec.shader copy to AlphaTest-DoubleSided.shader. Then, open it in your code editor.
  3. Change the first line to: Shader "Transparent/Cutout/DoubleSided" {.
  4. Add the following line of code where indicated:
    SubShader {
    
    Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    
      LOD 400
       // Line of code to be added: 
      Cull Off
       // End of code to be added.
  5. Save the file and import it into your Unity project.
  6. Import the grid.tga texture.
  7. Let's create a new material. Access the Project view, click on the Create drop-down menu and choose Material. Rename it to Grid.
  8. Select the Grid material and, in the Inspector view, change its shader to Transparent/Cutout/DoubleSided.
  9. Apply the grid.tga texture as the Base texture for the Grid material. Your double-sided transparent material is ready for use. It can even cast shadows!
    How to do it...

How it works...

The Cull Off command makes Unity render both front and back faces of the object. Although it works fine for our Cutout shader, using it with other types of transparent shaders might lead to unexpected results.

There's more...

Here are some additional resources and solutions:

Learning more about shaders

Unity's documentation includes several articles on shader programming. You can check them out at http://docs.unity3d.com/Documentation/Components/SL-Reference.html.

Solving the problem by editing the object's geometry

You can get similar results by duplicating the faces of your 3D models, and then flipping normals of the new geometry.

See also

  • The Creating transparency texture maps recipe.
..................Content has been hidden....................

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