Wireframe Rendering?

Hi guys and gals, I’ve looked around but can’t seem to find any info on whether it’s possible to have the main camera render in wireframe mode? I did find some stuff on Unity Pro but that doesn’t help me :frowning:

I’d quite like to do some cool retro stuff, a la Battlezone.

Is this possible?

Hey guys, I prefer c# so I rewrote and uhm changed the script a bit
it renders every line two times so now you can choose, which line of the triangles to render, which gives some kind of random but faster mesh, quite interesting too.

using UnityEngine;
using System.Collections;

public class wireframe : MonoBehaviour {

	public bool render_mesh_normaly = true;
	public bool render_lines_1st = false;
	public bool render_lines_2nd = false;
	public bool render_lines_3rd = false;
	public Color lineColor = new Color (0.0f, 1.0f, 1.0f);
	public Color backgroundColor = new Color (0.0f, 0.5f, 0.5f);
	public bool ZWrite = true;
	public bool AWrite = true;
	public bool blend = true;
	public float lineWidth = 3;
	public int size = 0;
	
	private Vector3[] lines ;
	private ArrayList lines_List ;
	public Material lineMaterial ;
	//private MeshRenderer meshRenderer; 

	/*
	████████       ▄▀▀■  ▀▀█▀▀  ▄▀▀▄  █▀▀▄  ▀▀█▀▀ 
	████████       ▀■■▄    █    █■■█  █▀▀▄    █   
	████████       ■▄▄▀    █    █  █  █  █    █   
	*/

	
	void Start () {
		//meshRenderer = gameObject.GetComponent<MeshRenderer>();
		if (lineMaterial == null) {
			lineMaterial = new Material ("Shader \"Lines/Colored Blended\" {" +
										"SubShader { Pass {" +
										"   BindChannels { Bind \"Color\",color }" +
										"   Blend SrcAlpha OneMinusSrcAlpha" +
										"   ZWrite on Cull Off Fog { Mode Off }" +
										"} } }");
				
			/*lineMaterial = new Material ("Shader \"Lines/Colored Blended\" {" +
										"SubShader { Pass {" +
										"	Blend SrcAlpha OneMinusSrcAlpha" +
										"	ZWrite Off Cull Front Fog { Mode Off }" +
										"} } }");*/
		}
		lineMaterial.hideFlags = HideFlags.HideAndDontSave;
		lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
		lines_List = new ArrayList();
		
		MeshFilter filter  = gameObject.GetComponent<MeshFilter>();
		Mesh mesh = filter.mesh;
		Vector3[] vertices = mesh.vertices;
		int[] triangles = mesh.triangles;
		
		for (int i = 0; i+2 < triangles.Length; i+=3)
		{
			lines_List.Add(vertices[triangles*]);*
  •  	lines_List.Add(vertices[triangles[i + 1]]);*
    
  •  	lines_List.Add(vertices[triangles[i+ 2]]);*
    
  •  }*
    
  •  //lines_List.CopyTo(lines);//arrays are faster than array lists*
    
  •  lines = (Vector3[]) lines_List.ToArray(typeof(Vector3));*
    
  •  lines_List.Clear();//free memory from the arraylist*
    
  •  size = lines.Length;*
    
  • }*

  • // to simulate thickness, draw line as a quad scaled along the camera’s vertical axis.*

  • void DrawQuad(Vector3 p1,Vector3 p2 ){*
    _ float thisWidth = 1.0f/Screen.width * lineWidth * 0.5f;_

  •  Vector3 edge1 = Camera.main.transform.position - (p2+p1)/2.0f;	//vector from line center to camera*
    
  •  Vector3 edge2 = p2-p1;	//vector from point to point*
    

_ Vector3 perpendicular = Vector3.Cross(edge1,edge2).normalized * thisWidth;_

  •  GL.Vertex(p1 - perpendicular);*
    
  •  GL.Vertex(p1 + perpendicular);*
    
  •  GL.Vertex(p2 + perpendicular);*
    
  •  GL.Vertex(p2 - perpendicular);*
    
  • }*

  • Vector3 to_world(Vector3 vec)*

  • {*

  •  return gameObject.transform.TransformPoint(vec);*
    
  • }*

_ /*_

  • ████████ █ █ █▀▀▄ █▀▀▄ ▄▀▀▄ ▀▀█▀▀ █▀▀▀*

  • ████████ █ █ █▀▀ █ █ █■■█ █ █■■*

  • ████████ ▀▄▄▀ █ █▄▄▀ █ █ █ █▄▄▄*
    _ */_

  • void OnRenderObject () {*

  •  gameObject.renderer.enabled=render_mesh_normaly;*
    
  •  if (lines == null || lines.Length < lineWidth) {*
    
  •  	print("No lines");*
    
  •  }* 
    
  •  else*
    
  •  {*
    
  •  	lineMaterial.SetPass(0);*
    
  •  	GL.Color(lineColor);*
    
  •  	if (lineWidth == 1) {*
    
  •  		GL.Begin(GL.LINES);*
    
  •  		for(int i = 0; i+2 < lines.Length; i+=3)*
    
  •  		{*
    

Vector3 vec1 = to_world(lines*);
Vector3 vec2 = to_world(lines[i+1]);
Vector3 vec3 = to_world(lines[i+2]);
if(render_lines_1st){
_
GL.Vertex(vec1);_
_
GL.Vertex(vec2);_
_
}_
if(render_lines_2nd){
_
GL.Vertex(vec2);_
_
GL.Vertex(vec3);_
_
}_
if(render_lines_3rd){
_
GL.Vertex(vec3);_
_
GL.Vertex(vec1);_
_
}_
_
}_
_
} else {_
_
GL.Begin(GL.QUADS);_
_
for(int i = 0; i+2 < lines.Length; i+=3) {_
Vector3 vec1 = to_world(lines);
Vector3 vec2 = to_world(lines[i+1]);
Vector3 vec3 = to_world(lines[i+2]);
if(render_lines_1st) DrawQuad(vec1,vec2);
if(render_lines_2nd) DrawQuad(vec2,vec3);
if(render_lines_3rd) DrawQuad(vec3,vec1);
_ }
}
GL.End();
}
}
}*_

http://www.starscenesoftware.com/vectrosity.html

http://www.starscenesoftware.com/tankzone.html

If you don’t mind having a fixed black colour for the triangles, you can use the inbuilt VR/SpatialMapping/Wireframe shader, available from 2017 onwards.

On standard displays, this will draw a wireframe on top of black triangles. On AR displays like HoloLens, the triangles will be transparent, leaving the wireframe.

The advantage of this approach is that there are no external dependencies and it works in seconds with any MeshFilter. I often use it for debugging procedural mesh generation scripts.

118694-capture.png

For anyone who stumbles across this, here is the working script from the forums:

    // attach to an object with a mesh filter 
//vers 12/11/2008

var lineColor : Color; 
var backgroundColor : Color; 
var ZWrite = true; 
var AWrite = true; 
var blend = true; 

private var lines : Vector3[]; 
private var linesArray : Array; 
private var lineMaterial : Material; 
private var meshRenderer : MeshRenderer; 


function Start () 
{ 
	renderer.enabled = false;
   meshRenderer = GetComponent(MeshRenderer); 
   if(!meshRenderer) meshRenderer = gameObject.AddComponent(MeshRenderer); 
   meshRenderer.material = new Material("Shader \"Lines/Background\" { Properties { _Color (\"Main Color\", Color) = (1,1,1,1) } SubShader { Pass {" + (ZWrite ? " ZWrite on " : " ZWrite off ") + (blend ? " Blend SrcAlpha OneMinusSrcAlpha" : " ") + (AWrite ? " Colormask RGBA " : " ") + "Lighting Off Offset 1, 1 Color[_Color] }}}"); 
    
// Old Syntax without Bind :    
//   lineMaterial = new Material("Shader \"Lines/Colored Blended\" { SubShader { Pass { Blend SrcAlpha OneMinusSrcAlpha ZWrite On Cull Front Fog { Mode Off } } } }"); 

// New Syntax with Bind : 
   lineMaterial = new Material("Shader \"Lines/Colored Blended\" { SubShader { Pass { Blend SrcAlpha OneMinusSrcAlpha BindChannels { Bind \"Color\",color } ZWrite On Cull Front Fog { Mode Off } } } }"); 
    
   lineMaterial.hideFlags = HideFlags.HideAndDontSave; 
   lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave; 
    
   linesArray = new Array(); 
   var filter : MeshFilter = GetComponent(MeshFilter); 
   var mesh = filter.sharedMesh; 
   var vertices = mesh.vertices; 
   var triangles = mesh.triangles; 
    
   for (i = 0; i < triangles.length / 3; i++) 
   { 
      linesArray.Add(vertices[triangles[i * 3]]); 
      linesArray.Add(vertices[triangles[i * 3 + 1]]); 
      linesArray.Add(vertices[triangles[i * 3 + 2]]); 
   } 
    
   lines = linesArray.ToBuiltin(Vector3); 
} 


function OnRenderObject() 
{    
   meshRenderer.sharedMaterial.color = backgroundColor; 
   lineMaterial.SetPass(0); 
    
   GL.PushMatrix(); 
   GL.MultMatrix(transform.localToWorldMatrix); 
   GL.Begin(GL.LINES); 
   GL.Color(lineColor); 
    
   for (i = 0; i < lines.length / 3; i++) 
   { 
      GL.Vertex(lines[i * 3]); 
      GL.Vertex(lines[i * 3 + 1]); 
       
      GL.Vertex(lines[i * 3 + 1]); 
      GL.Vertex(lines[i * 3 + 2]); 
       
      GL.Vertex(lines[i * 3 + 2]); 
      GL.Vertex(lines[i * 3]); 
   } 
          
   GL.End(); 
   GL.PopMatrix(); 
}

@POLYGAMe You can use GL.wireframe = true;