unity-game-enginerenderergameobjectwireframe

Unity3D render GameObject as Wireframe


I am new to Unity3D and have a question about the rendering of gameobjects. I needed the model from an .obj file so I just imported it and was able to place it as a prefab in the gameview. I also added a script for moving and turning this object.

What I wanted to do is to display the object as a wireframe so that a user recognizes the object structure but can see through it. I found this example script to put on a camera

using UnityEngine;

public class Example : MonoBehaviour
{
    // Attach this script to a camera, this will make it render in wireframe
    void OnPreRender()
    {
        GL.wireframe = true;
    }

    void OnPostRender()
    {
        GL.wireframe = false;
    }
}

I got this example right from here: https://docs.unity3d.com/ScriptReference/GL-wireframe.html

My problem with this can be seen in the picture. It somehow renders the objects two times. The wireframe one looks exactly like the one I wanted however the second one is strange and I don't know why it is there and how to get rid of this. The unwanted object also moves in parallel to the wireframe one if this helps to debug. enter image description here

What is the best way to display the object as a wireframe? Thank you in advance!


Solution

  • Working with OnPostRender is not the way to go, as you change things after the camera already rendered them. If the object should always be shown as wireframe you should apply a wireframe shader to the objects Material. AFAIK untiy does not have a build in wireframe shader, but there are multiple sites that have tutorials or repos that created a wireframe shader, you can find them easily if you google it.

    Edit: here is an example from Shaders Laboratory

    Edit 2: I presume you render the scene with two Camera's and the none wireframe object (which is the same object as the wireframe object) is the object that is rendered with the second camera that does not use the Wireframe. If you really want to use it this way, then you should add a Layer to the object and tell the other camera (under Culling Mask) not to render objects with this Layer.