Display and render only a specific object in wireframe in Unity3D

Tak picture Tak · Jun 3, 2015 · Viewed 9.6k times · Source

I want to know how to view and render a specific game object (mesh) in wireframe, not the whole scene. I can change the scene to wireframe using GL.wireframe but the problem I want to view and render only a certain object (not the whole scene) in wireframe. Any advice please?

Answer

Roberto picture Roberto · Jun 3, 2015

Use Layers. Change the layer of the game object (dropdown in the top right of the Inspector window) from Default to another layer (you can create a new one choosing Add Layer... in the dropdown menu).

Then, create a new camera (or select the main camera, depending on what you want to achieve), and change its Culling Mask to the layer you are using in the game object.

For drawing the wireframe, you post this script in the camera that's supposed to draw that game object:

// from http://docs.unity3d.com/ScriptReference/GL-wireframe.html
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void OnPreRender() {
        GL.wireframe = true;
    }
    void OnPostRender() {
        GL.wireframe = false;
    }
}

You may have to use 2 cameras depending on what you want to achieve (one camera for the wireframe object, another camera to draw the rest of the scene), in this case you would set the Clear Flags of one of the cameras to Don't Clear. Make sure the Depth value of both cameras is the same.

The Clear Flags of a camera indicates what's going to happen with the pixels where there's nothing to be drawn (the empty space) of that camera, and also what happens when there are multiple cameras drawing to the same pixel.

In the case where Clear Flags is Don't Clear, it will not do anything with the empty space, leaving it for the other camera to fill with an object or a background. For the pixels where it should draw something, it will let the depth of the object decide what is going to be drawn, that is, the objects that are nearer the camera will be drawn on the top of the others.