Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display and render only a specific object in wireframe in Unity3D

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?

like image 719
Tak Avatar asked Jun 03 '15 05:06

Tak


2 Answers

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.

like image 52
Roberto Avatar answered Oct 19 '22 06:10

Roberto


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.

like image 26
Martin Rončka Avatar answered Oct 19 '22 06:10

Martin Rončka