unity中gameobject如何添加线框网格

  • 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[i]]);
  • 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[i]);
  • 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[i]);
  • 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();
  • }
  • }
  • }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值