Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Unity 2017.2 beta is now available for download.
    Dismiss Notice

Wireframe 3D?

Discussion in 'Editor & General Support' started by willgoldstone, Feb 9, 2008.

  1. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    586
    Hi guys,

    I'm looking into remaking an old game I made a few years back in Shockwave 3D. In shockwave in Director it was and simple a choosing a render mode to set mdoels to appear in wireframe a la 'BattleZone', how can this be done in Unity? I assume it'd be a shader thing? Not really sure. Any tips or thoughts on this much appreciated.


    Cheers

    Will
     
  2. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Something like..

    Code (csharp):
    1.  
    2. function OnRenderObject ()
    3. {
    4.     GL.PushMatrix();
    5.     GL.Begin(GL.LINES);
    6.  
    7.     GL.modelview = Matrix4x4.TRS(transform.position, transform.rotation, transform.lossyScale);
    8.    
    9.     for(triangle in mesh)
    10.     {
    11.         GL.Vertex(vertex1);
    12.         GL.Color(color1);
    13.         GL.Vertex(vertex2);
    14.         GL.Color(color2);
    15.  
    16.         GL.Vertex(vertex2);
    17.         GL.Color(color2);
    18.         GL.Vertex(vertex3);
    19.         GL.Color(color3);
    20.  
    21.         GL.Vertex(vertex3);
    22.         GL.Color(color3);
    23.         GL.Vertex(vertex1);
    24.         GL.Color(color1);
    25.     }
    26.  
    27.     GL.End();
    28.     GL.PopMatrix();
    29. }
    30.  
    Look up the mesh and GL classes (GL is pro only).
     
  3. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,428
    Hey Yoggy,

    If this is a working wireframe shader (haven't tested it yet) could you please post this in the Wiki? I know other folks have asked for this in the past and it would make a great addition!
     
  4. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    890
    The only problem I see with Yoggy's script is that it's very slow (I think) - assuming that it actually is using immediate mode. Then again, UT might do some magic underneath to use VBOs or something of the sort.

    Another possibility would to have UT expose glPolygonMode() and DirectX equivalent. That would probably be pretty easy for them to implement.
     
  5. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,325
    Yoggy's code is pseudo code AFAIK. Here's my attempt at GL wireframe rendering so far (attach to Main Camera). I'd love to see someone really nail this though...


    Code (csharp):
    1. var P0 : Vector3;
    2. var P1 : Vector3;
    3. var P2 : Vector3;
    4.  
    5. var wires = new Array();
    6.  
    7. var lineColor : Color;
    8. var myMesh : GameObject;
    9.  
    10. static var lineMaterial : Material;
    11.  
    12. function Start ()
    13. {
    14.     CreateLineMaterial();
    15.    
    16.     var filter : MeshFilter = myMesh.GetComponent(MeshFilter);
    17.     var mesh = filter.mesh;
    18.     var vertices = mesh.vertices;
    19.     var triangles = mesh.triangles;
    20.    
    21.     for (k = 0; k < triangles.length / 3; k++)
    22.     {
    23.         wires.Add (vertices[triangles[k * 3]]);
    24.         wires.Add (vertices[triangles[k * 3 + 1]]);
    25.         wires.Add (vertices[triangles[k * 3 + 2]]);
    26.     }
    27.    
    28.     wires.Add (vertices[triangles[triangles.length - 2]]);
    29.     wires.Add (vertices[triangles[triangles.length - 1]]);
    30. }
    31.  
    32. function OnPostRender()
    33. {  
    34.     lineMaterial.SetPass(0);
    35.    
    36.     GL.Begin(GL.LINES);
    37.     GL.Color(lineColor);
    38.    
    39.     for (i = 0; i < wires.length / 3; i++)
    40.     {
    41.         P0 = myMesh.transform.TransformPoint (wires[i * 3]);
    42.         P1 = myMesh.transform.TransformPoint (wires[i * 3 + 1]);
    43.         P2 = myMesh.transform.TransformPoint (wires[i * 3 + 2]);
    44.        
    45.         GL.Vertex3(P0.x, P0.y, P0.z);
    46.         GL.Vertex3(P1.x, P1.y, P1.z);
    47.         GL.Vertex3(P2.x, P2.y, P2.z);
    48.         GL.Vertex3(P0.x, P0.y, P0.z);
    49.     }
    50.            
    51.     GL.End();
    52. }
    53.  
    54. static function CreateLineMaterial()
    55. {
    56.     if( !lineMaterial ) {
    57.         lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" +
    58.             "SubShader { Pass { " +
    59.             "    Blend SrcAlpha OneMinusSrcAlpha " +
    60.             "    ZWrite Off Cull Front Fog { Mode Off } " +
    61.             "} } }" );
    62.         lineMaterial.hideFlags = HideFlags.HideAndDontSave;
    63.         lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
    64.     }
    65. }
    Ethan
     
  6. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Yeah mine is just a guess, but I know ethan's works because he showed it to me earlier.
     
  7. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,325
    @ Yoggy

    Yeah, mine works, but it's pretty far from useable in any practical application (the framerate is fine, but the method is extremely convoluted). I wish you were more intrigued by this because if you were you'd probably bang out a killer GL wireframe renderer in an afternoon.

    As an incentive picture telephone/power cables dangling here and there for a certain game :wink:
     
  8. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,428
    I just tried Ethans shader and got this error:

    Code (csharp):
    1. Syntax error in : var at line 1
    Huh?
     
  9. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Sounds like a text encoding problem? Try pasting it into text edit, do a command shift T, and then paste into your text editor.

    I thought that only happened with shaders now though.

    @Ethan I guess I'll try it, shouldn't take that long :)
     
  10. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Did you paste it into a .shader? It's not a shader, it's a JavaScript.
     
  11. shawn

    shawn

    Unity Technologies

    Joined:
    Aug 4, 2007
    Posts:
    531
    Would be nice if we could just use glPolygonMode(). I'm assuming that this would interfere with other things that Unity renders though. :(
     
  12. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    @ arrku duhhrrhrh Neither one of us was thinking hehe.

    Heres a nice complete line mesh renderer:

    Code (csharp):
    1.  
    2. // attach to an object with a mesh filter
    3.  
    4. var lineColor : Color;
    5. var ZWrite = true;
    6. var AWrite = true;
    7. var blend = true;
    8.  
    9. private var lines : Vector3[];
    10. private var linesArray : Array;
    11. private var lineMaterial : Material;
    12. private var meshRenderer : MeshRenderer;
    13.  
    14. function Start ()
    15. {
    16.     meshRenderer = GetComponent(MeshRenderer);
    17.     if(!meshRenderer) meshRenderer = gameObject.AddComponent(MeshRenderer);
    18.     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] }}}");
    19.    
    20.     lineMaterial = new Material("Shader \"Lines/Colored Blended\" { SubShader { Pass { Blend SrcAlpha OneMinusSrcAlpha ZWrite Off Cull Front Fog { Mode Off } } } }");
    21.    
    22.     lineMaterial.hideFlags = HideFlags.HideAndDontSave;
    23.     lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
    24.    
    25.     linesArray = new Array();
    26.     var filter : MeshFilter = GetComponent(MeshFilter);
    27.     var mesh = filter.mesh;
    28.     var vertices = mesh.vertices;
    29.     var triangles = mesh.triangles;
    30.    
    31.     for (i = 0; i < triangles.length / 3; i++)
    32.     {
    33.         linesArray.Add(vertices[triangles[i * 3]]);
    34.         linesArray.Add(vertices[triangles[i * 3 + 1]]);
    35.         linesArray.Add(vertices[triangles[i * 3 + 2]]);
    36.     }
    37.    
    38.     lines = linesArray.ToBuiltin(Vector3);
    39. }
    40.  
    41.  
    42. function OnRenderObject()
    43. {  
    44.     meshRenderer.material.color = backgroundColor;
    45.     lineMaterial.SetPass(0);
    46.    
    47.     GL.PushMatrix();
    48.     GL.MultMatrix(transform.localToWorldMatrix);
    49.     GL.Begin(GL.LINES);
    50.     GL.Color(lineColor);
    51.    
    52.     for (i = 0; i < lines.length / 3; i++)
    53.     {
    54.         GL.Vertex(lines[i * 3]);
    55.         GL.Vertex(lines[i * 3 + 1]);
    56.        
    57.         GL.Vertex(lines[i * 3 + 1]);
    58.         GL.Vertex(lines[i * 3 + 2]);
    59.        
    60.         GL.Vertex(lines[i * 3 + 2]);
    61.         GL.Vertex(lines[i * 3]);
    62.     }
    63.          
    64.     GL.End();
    65.     GL.PopMatrix();
    66. }
    67.  
    68.  
     
  13. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,428
    Doof! :oops:

    Nice one Yoggy, I can think of lots of things to use this for.
     
  14. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    586
    Hi again,


    thanks so much for the responses Forest, Ethan, this is exactly the kind of thing i've been thinking of. I'll post some of my game up as soon as i find time to make it!


    Cheers guys,


    Will
     
  15. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    687
    thanks Forest and Ethan very nice.

    Cheers.
     
  16. diese440

    diese440

    Joined:
    May 25, 2007
    Posts:
    105
    Very nice Yoggy, but very slow on Windows and the lineColors seems to stay in black on Windows, 8) again...
    Is it a DirectX problem ?

    Cheers. :wink:
     
  17. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,370
    Awesome stuff, thank you all.
     
  18. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,357
    This shader can not be used on the Terrain. :cry:
    Is it some variant to add wireframe layer to the terrain?
     
  19. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    He needs to BindChannels in the shader, I think. Unity is usually good about getting bindings figured out for us, but in cases like this, it sometimes needs help mapping sources to targets.

    You will want to add something like this to the subshader pass (escaping as necessary)

    Code (csharp):
    1.  
    2.  BindChannels { Bind "Color",color }
    3.  
    Note though that I didn't actually run this with or without that fix, and I'm completely speculating that this is causing the black lines in Windows problem.
     
  20. diese440

    diese440

    Joined:
    May 25, 2007
    Posts:
    105
    Thanks Charles,
    Adding "BindChannels { Bind "Color",color }" to the subshader fix the "Black Lines Problem" on Windows/DirectX... :D
     
  21. tgraupmann

    tgraupmann

    Joined:
    Sep 14, 2007
    Posts:
    828
    I aim to be able to switch between a MeshRenderer and this wireframe script by using a toggle.

    Unfortunately, as soon as I add this script to my gameObject with a mesh/meshRender/meshFilter/meshCollider, the mesh disappears. I set a visible color to the wireframe and can see the wireframe. But if I disable the wireframe script, the regular mesh no longer renderers.

    I may need to recreate the gameObject/mesh/meshRender/meshFilter/meshCollider each time I switch between renderers, unless I'm missing something.

    I just realized that it's changing my existing mesh renderer material. If I change the material back my mesh renderers.

    So I changed the logic to only set the mesh renderer material if one was created.

    There is still one side effect in that my mesh collider stops functioning when the WireframeRenderer is added. I tried removing the collider and readding after adding the WireframeRenderer without luck???

    I'm getting some mighty big performance issues with this wireframe method. I have 9 X 65k vert meshes. Before switching to wireframe mode I get 70 FPS and with wireframe I'm seeing 2 FPS.
     
  22. noradninja

    noradninja

    Joined:
    Nov 12, 2007
    Posts:
    420
    Ethan,

    Do you need to have Unity iPhone Advanced for this to work on the iPhone? It works fine in the Editor, with iPhone emulation on, but the wires don't draw on the iPhone itself.
     
  23. tgraupmann

    tgraupmann

    Joined:
    Sep 14, 2007
    Posts:
    828
    I ended up using a Wireframe shader without any performance issues.
     
  24. noradninja

    noradninja

    Joined:
    Nov 12, 2007
    Posts:
    420
    Is it a CG shader? I am looking for something that will run this on the iPhone...
     
  25. makaleth

    makaleth

    Joined:
    Aug 12, 2009
    Posts:
    56
    tgraupmann would you be willing to share you technique. I could really use a suitable wireframe shader that would work on the iphone
     
  26. tgraupmann

    tgraupmann

    Joined:
    Sep 14, 2007
    Posts:
    828
  27. makaleth

    makaleth

    Joined:
    Aug 12, 2009
    Posts:
    56
    ahh cg shader so no iphone...

    Any solution for iphone wireframe? even if its slow?
     
  28. tgraupmann

    tgraupmann

    Joined:
    Sep 14, 2007
    Posts:
    828
    What are you trying to wireframe?

    A model or a billboard?

    If it's just a billboard, you could use a texture with a grid on it.
     
  29. rajmond

    rajmond

    Joined:
    May 18, 2010
    Posts:
    56
    Can someone please tell me where exactly to put this code " BindChannels { Bind "Color",color } " because I was trying it but didn't work for me. If you can paste the whole code for me.

    Thanks
     
  30. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,311
    As long as this topic has come up again, I should mention I've been working on a line-drawing and wireframe utility. Hopefully I'll have a release in the not too distant future, although it's still pretty much in an alpha state right now.

    • Not a shader, works on iPhone, doesn't need Unity Pro
    • "Free" anti-aliasing, which means you can have anti-aliased lines on iPhone as well (although you can have non-anti-aliased lines if you want)
    • Can use textures for making things like glowing lines without full-screen glow effects
    • 3D, 2D, and point drawing. Lines can be any number of pixels thick
    • Reasonably fast -- updates 200,000+ line segments/second on my 5.5-year-old G5 (haven't tested iPhone speed yet). Also lines only have to update when they change, or when the camera perspective changes, so static lines take no extra CPU time
    • Not limited to triangles or 4-sided polygons, has a utility for quickly making customized 3D vector shapes from existing meshes
    • Can have continuous or discontinuous lines with arbitrary colors for each segment. Separate vector objects can dynamically batch on iPhone (as long as they are <300 vertices each of course)
    • Helper scripts for making 3D vector objects behave more or less like standard game objects

    I started making a Battle Zone clone as a way of testing the routines and finding ways of making them more useful in a general production environment:

    [​IMG]

    First iPhone test (worked with no changes needed, yay):

    [​IMG]

    --Eric
     
  31. tgraupmann

    tgraupmann

    Joined:
    Sep 14, 2007
    Posts:
    828
    That's pretty sweet Eric. I was using wireframe as a debug or design tool. Looking retro there.
     
  32. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,311
    I mostly just wanted a simple way of drawing 2D lines for graphs and stuff that didn't need GL.LINES (which doesn't work on the iPhone), and it sort of expanded from there. ;)

    --Eric
     
  33. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,327
    That rules, Eric. You have any plans to sell that or post it to the wiki?
     
  34. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,311
    Yeah, it's got enough functionality that I'm probably going to sell it...not for too much though. I put VectorLine on the wiki a while ago, but there isn't much to it; this new one is way better and does far more.

    --Eric
     
  35. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,325
    Looking cool Eric, can't wait to try it out.
     
  36. diese440

    diese440

    Joined:
    May 25, 2007
    Posts:
    105
    +1 NICE ! :D
     
  37. diese440

    diese440

    Joined:
    May 25, 2007
    Posts:
    105
    Here is the code (look at "New Syntax with Bind" comment) :

    Code (csharp):
    1. // attach to an object with a mesh filter
    2. //vers 12/11/2008
    3.  
    4. var lineColor : Color;
    5. var ZWrite = true;
    6. var AWrite = true;
    7. var blend = true;
    8.  
    9. private var lines : Vector3[];
    10. private var linesArray : Array;
    11. private var lineMaterial : Material;
    12. private var meshRenderer : MeshRenderer;
    13.  
    14.  
    15. function Start ()
    16. {
    17.    meshRenderer = GetComponent(MeshRenderer);
    18.    if(!meshRenderer) meshRenderer = gameObject.AddComponent(MeshRenderer);
    19.    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] }}}");
    20.    
    21. // Old Syntax without Bind :    
    22. //   lineMaterial = new Material("Shader \"Lines/Colored Blended\" { SubShader { Pass { Blend SrcAlpha OneMinusSrcAlpha ZWrite On Cull Front Fog { Mode Off } } } }");
    23.  
    24. // New Syntax with Bind :
    25.    lineMaterial = new Material("Shader \"Lines/Colored Blended\" { SubShader { Pass { Blend SrcAlpha OneMinusSrcAlpha BindChannels { Bind \"Color\",color } ZWrite On Cull Front Fog { Mode Off } } } }");
    26.    
    27.    lineMaterial.hideFlags = HideFlags.HideAndDontSave;
    28.    lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
    29.    
    30.    linesArray = new Array();
    31.    var filter : MeshFilter = GetComponent(MeshFilter);
    32.    var mesh = filter.sharedMesh;
    33.    var vertices = mesh.vertices;
    34.    var triangles = mesh.triangles;
    35.    
    36.    for (i = 0; i < triangles.length / 3; i++)
    37.    {
    38.       linesArray.Add(vertices[triangles[i * 3]]);
    39.       linesArray.Add(vertices[triangles[i * 3 + 1]]);
    40.       linesArray.Add(vertices[triangles[i * 3 + 2]]);
    41.    }
    42.    
    43.    lines = linesArray.ToBuiltin(Vector3);
    44. }
    45.  
    46.  
    47. function OnRenderObject()
    48. {    
    49.    meshRenderer.sharedMaterial.color = backgroundColor;
    50.    lineMaterial.SetPass(0);
    51.    
    52.    GL.PushMatrix();
    53.    GL.MultMatrix(transform.localToWorldMatrix);
    54.    GL.Begin(GL.LINES);
    55.    GL.Color(lineColor);
    56.    
    57.    for (i = 0; i < lines.length / 3; i++)
    58.    {
    59.       GL.Vertex(lines[i * 3]);
    60.       GL.Vertex(lines[i * 3 + 1]);
    61.        
    62.       GL.Vertex(lines[i * 3 + 1]);
    63.       GL.Vertex(lines[i * 3 + 2]);
    64.        
    65.       GL.Vertex(lines[i * 3 + 2]);
    66.       GL.Vertex(lines[i * 3]);
    67.    }
    68.          
    69.    GL.End();
    70.    GL.PopMatrix();
    71. }
    72.  
    Hope this helps...
    :D
     
  38. scrobby4

    scrobby4

    Joined:
    Sep 3, 2009
    Posts:
    9
    Hi, everyone,

    If you just want the lines to be there but not the object, make the object invisible by adding the line below to the end of start method.

    Code (csharp):
    1. renderer.enabled = false;
    Best.
     
  39. benfattino

    benfattino

    Joined:
    Nov 26, 2010
    Posts:
    43
    Hi. I use this script for drawing wireframe from model. I have two question:
    1- How I can delete hidden edges from view?
    2- The color of line is always black? How solve this problem?
    Somebody help me? Thanks...
     
  40. SneakySoft

    SneakySoft

    Joined:
    Apr 5, 2010
    Posts:
    96
    Cool, this really helped me :)
     
    Last edited: Nov 27, 2010
  41. Rhapsodus

    Rhapsodus

    Joined:
    Aug 31, 2009
    Posts:
    30
    I thought I would post what I used based on the previous script, I also moved it to c#, might be useful to others. I made an optimization in removing uneeded Lines for faster rendering, when models hit 10k+ lines on an iphone, cutting out 1k duplicates proves a huge savings on FPS.

    Also, sometimes some models have tons of triangles and are very small, and you cant really see the up close wireframe, so i made a fidelity throttle to cut certain sides of the triangle

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System;
    6.  
    7. public class WireFrameLineRenderer : MonoBehaviour
    8. {
    9.     //****************************************************************************************
    10.     //  Material options
    11.     //****************************************************************************************
    12.     public Color LineColor;
    13.     public bool ZWrite = true;
    14.     public bool AWrite = true;
    15.     public bool Blend = true;
    16.     public int Fidelity = 3;
    17.    
    18.     //****************************************************************************************
    19.     // Line Data
    20.     //****************************************************************************************
    21.     private Vector3[] Lines;
    22.     private List<Line> LinesArray = new List<Line>();
    23.     private Material LineMaterial;
    24.    
    25.    
    26.     //*****************************************************************************************
    27.     // Helper class, Line is defined as two Points
    28.     //*****************************************************************************************
    29.     public class Line
    30.     {
    31.         public Vector3 PointA;
    32.         public Vector3 PointB;
    33.        
    34.         public Line (Vector3 a, Vector3 b)
    35.         {
    36.             PointA = a;
    37.             PointB = b;
    38.         }
    39.        
    40.         //*****************************************************************************************
    41.         // A == B if   Aa&Ab == Ba&Bb or Ab&Ba == Aa  Bb
    42.         //*****************************************************************************************
    43.         public static bool operator == (Line lA, Line lB )
    44.         {
    45.             if( lA.PointA == lB.PointA  lA.PointB == lB.PointB )
    46.             {
    47.                 return true;
    48.             }
    49.            
    50.             if ( lA.PointA == lB.PointB  lA.PointB == lB.PointA )
    51.             {
    52.                 return true;   
    53.             }
    54.                
    55.            
    56.             return false;
    57.         }      
    58.        
    59.         //*****************************************************************************************
    60.         // A != B if   !(Aa&Ab == Ba&Bb or Ab&Ba == Aa  Bb)
    61.         //*****************************************************************************************
    62.         public static bool operator != (Line lA, Line lB )
    63.         {
    64.             return !( lA == lB );
    65.         }          
    66.     }
    67.    
    68.     //*****************************************************************************************
    69.     // Parse the mesh this is attached to and save the line data
    70.     //*****************************************************************************************
    71.     public void Start ()
    72.     {      
    73.         LineMaterial = new Material("Shader \"Lines/Colored Blended\" { SubShader { Pass { Blend SrcAlpha OneMinusSrcAlpha ZWrite Off Cull Front Fog { Mode Off } } } }")
    74.         LineMaterial.hideFlags = HideFlags.HideAndDontSave;
    75.         LineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
    76.      
    77.         MeshFilter filter = GetComponent<MeshFilter>();
    78.         Mesh mesh = filter.sharedMesh;
    79.         Vector3[] vertices = mesh.vertices;
    80.         int[] triangles = mesh.triangles;
    81.        
    82.         for (int i = 0; i < triangles.Length/3; i++)
    83.         {
    84.             int j = i*3;
    85.             Line lineA = new Line( vertices[triangles[j]], vertices[triangles[j+1]] );
    86.             Line lineB = new Line( vertices[triangles[j+1]], vertices[triangles[j+ 2]] );
    87.             Line lineC = new Line( vertices[triangles[j+2]], vertices[triangles[j]] );
    88.  
    89.             if( Fidelity == 3 )
    90.             {
    91.                 AddLine( lineA );
    92.                 AddLine( lineB );
    93.                 AddLine( lineC );
    94.             }
    95.             else if ( Fidelity == 2 )
    96.             {
    97.                 AddLine( lineA );
    98.                 AddLine( lineB );
    99.             }
    100.             else if( Fidelity == 1 )
    101.             {
    102.                 AddLine(lineA);
    103.             }
    104.         }
    105.     }
    106.    
    107.     //****************************************************************************************
    108.     // Adds a line to the array if the equivalent line isn't stored already
    109.     //****************************************************************************************
    110.     public void AddLine(Line l)
    111.     {
    112.         bool found = false;
    113.         foreach( Line line in LinesArray )
    114.         {
    115.             if( l == line )
    116.             { found = true; break; }
    117.         }
    118.        
    119.         if( !found )
    120.         { LinesArray.Add( l ); }
    121.     }
    122.  
    123.     //****************************************************************************************
    124.     // Deferred rendering of wireframe, this should let materials go first
    125.     //****************************************************************************************
    126.     public void OnRenderObject()
    127.     {  
    128.         LineMaterial.SetPass(0);
    129.        
    130.         GL.PushMatrix();
    131.         GL.MultMatrix(transform.localToWorldMatrix);
    132.         GL.Begin(GL.LINES);
    133.         GL.Color(LineColor);
    134.        
    135.         foreach( Line line in LinesArray )
    136.         {
    137.             GL.Vertex( line.PointA );
    138.             GL.Vertex( line.PointB );
    139.         }
    140.              
    141.         GL.End();
    142.         GL.PopMatrix();
    143.     }
    144. }
    145.  
     
    Last edited: Mar 24, 2011
  42. benfattino

    benfattino

    Joined:
    Nov 26, 2010
    Posts:
    43
    I try this script, but unity give me this error:
    (28,22):warning CS0661: 'WireFrameLineRenderer.Line' defines operator == or operator != but does not override Object.GetHashcode()
     
  43. ratonmalo

    ratonmalo

    Joined:
    Apr 12, 2010
    Posts:
    243
    it's really works! for a object is great, actually in want to turn all the object of my scene in wire pressing a button.

    there is not any chance to apply this script to a group of objects or to the camera?

    thanks
     
    Last edited: Apr 14, 2011
  44. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,100
    @diese440, how do I get this to work? Do I copy the the code into a text file and call it "wireframe.shader" and attach it to the object to be rendered in wireframe? Or do I call it "wireframe.cs" ???

    EDIT:
    Never mind. I could never get this code to work. I have settled for Vectrosity instead. That works just fine.
     
    Last edited: May 22, 2011
  45. POLYGAMe

    POLYGAMe

    Joined:
    Jan 20, 2009
    Posts:
    196
    Hi guys, I managed to get the above script working but I can only run it with black lines... no matter what I change the line colour to. Any ideas?
     
  46. TomaszKielski

    TomaszKielski

    Joined:
    Jan 23, 2011
    Posts:
    101
    I have just tried those scripts and I like the Diese440 script; it runs very well and smoothly, thanks for sharing.
    The ideas behind ratonmalo remake sound right, but the remake itself is not fully succesful; the lines are very dotty and display in black only and there is no way to change the color.
     
  47. qw_zzz

    qw_zzz

    Joined:
    Nov 26, 2010
    Posts:
    16
    Hi all. how do i bind this script to a hotkey "W"? or a GUI button?

    thanks
     
  48. preAk

    preAk

    Joined:
    Sep 7, 2011
    Posts:
    2
    Hi guys........a bit of a newbie question here!!

    So sorry in advance.

    I want to use forestjohnson's wire frame render but need it to update every frame or update if the mesh is morphed in any way. Any ideas on how I would acheive this would be great! cheers k

     
  49. vished

    vished

    Joined:
    Oct 24, 2010
    Posts:
    26
    Doesn't work in Unity 3.5 and I don't want to pay for something that every other 3D app has out of the box.
    Any other ideas?
     
  50. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,596
    spend a few days writting a quad line system yourself instead of investing $15 into vectrocity would be the way to go here I fear.

    Also Unity is no 3D rendering / modelling application, its a game engine, where wireframe rendering at runtime is a far less usefull feature than one would assume without the API - Platform limitations even taken into account.