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?
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.
There is an asset on the Unity asset store containing several shader materials which do exactly what you need. You can download it, analyze and write the shaders yourself, or simply use the asset provided.
You can then attach the shader to a material and that to a mesh renderer to attain the desired effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With