Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 31st, 2012  |  syntax: C#  |  size: 4.15 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. class Point {
  5.         public Vector3 p;
  6.         public Point next;
  7. }
  8.  
  9. public class LinesGR : MonoBehaviour {
  10.  
  11.         public Shader shader;
  12.  
  13.         private Mesh ml;
  14.         private Material lmat;
  15.        
  16.         private Mesh ms;
  17.         private Material smat;
  18.        
  19.         private Vector3 s;
  20.  
  21.         private float lineSize = 0.3f;
  22.        
  23.         private GUIStyle labelStyle;
  24.         private GUIStyle linkStyle;
  25.        
  26.         private Point first;
  27.  
  28.         void Start () {
  29.                 ml = new Mesh();
  30.                 lmat = new Material(shader);
  31.                 lmat.color = new Color(0,0,0,0.3f);
  32.                
  33.                 ms = new Mesh();
  34.                 smat = new Material(shader);
  35.                 smat.color = new Color(0,0,0,0.1f);
  36.  
  37.         }
  38.  
  39.         void Update() {
  40.                 if (Input.touchCount > 0)
  41.                 {
  42.                         Vector3 e = GetNewPoint();
  43.                        
  44.                         if(first == null) {
  45.                                 first = new Point();
  46.                                 first.p = transform.InverseTransformPoint(e);
  47.                         }
  48.                        
  49.                         if(s != Vector3.zero) {
  50.                                 Vector3 ls = transform.TransformPoint(s);
  51.                                 AddLine(ml, MakeQuad(ls, e, lineSize), false);
  52.                                
  53.                                 Point points = first;
  54.                                 while(points.next != null) {
  55.                                         Vector3 next = transform.TransformPoint(points.p);
  56.                                         points = points.next;
  57.                                 }
  58.                                
  59.                                 Point np = new Point();
  60.                                 np.p = transform.InverseTransformPoint(e);
  61.                                 points.next = np;
  62.        
  63.                         }
  64.                        
  65.                         s = transform.InverseTransformPoint(e);
  66.                 } else {
  67.                         if(Input.GetMouseButton(0)) {
  68.                                
  69.                                 Vector3 e = GetNewPoint();
  70.                                
  71.                                 if(first == null) {
  72.                                         first = new Point();
  73.                                         first.p = transform.InverseTransformPoint(e);
  74.                                 }
  75.                                
  76.                                 if(s != Vector3.zero) {
  77.                                         Vector3 ls = transform.TransformPoint(s);
  78.                                         AddLine(ml, MakeQuad(ls, e, lineSize), false);
  79.                                        
  80.                                         Point points = first;
  81.                                         while(points.next != null) {
  82.                                                 Vector3 next = transform.TransformPoint(points.p);
  83.                                                 points = points.next;
  84.                                         }
  85.                                        
  86.                                         Point np = new Point();
  87.                                         np.p = transform.InverseTransformPoint(e);
  88.                                         points.next = np;
  89.        
  90.                                 }
  91.                                
  92.                                 s = transform.InverseTransformPoint(e);
  93.                         } else {
  94.                                 s = Vector3.zero;
  95.                         }
  96.         }
  97.                
  98.                 Draw();
  99.                 processInput();
  100.         }
  101.        
  102.         void Draw() {
  103.                 Graphics.DrawMesh(ml, transform.localToWorldMatrix, lmat, 0);
  104.                 Graphics.DrawMesh(ms, transform.localToWorldMatrix, smat, 0);
  105.         }
  106.        
  107.         Vector3[] MakeQuad(Vector3 s, Vector3 e, float w) {
  108.                 w = w / 2;
  109.                 Vector3[] q = new Vector3[4];
  110.  
  111.                 Vector3 n = Vector3.Cross(s, e);
  112.                 Vector3 l = Vector3.Cross(n, e-s);
  113.                 l.Normalize();
  114.                
  115.                 q[0] = transform.InverseTransformPoint(s + l * w);
  116.                 q[1] = transform.InverseTransformPoint(s + l * -w);
  117.                 q[2] = transform.InverseTransformPoint(e + l * w);
  118.                 q[3] = transform.InverseTransformPoint(e + l * -w);
  119.  
  120.                 return q;
  121.         }
  122.        
  123.         void AddLine(Mesh m, Vector3[] quad, bool tmp) {
  124.                         int vl = m.vertices.Length;
  125.                        
  126.                         Vector3[] vs = m.vertices;
  127.                         if(!tmp || vl == 0) vs = resizeVertices(vs, 4);
  128.                         else vl -= 4;
  129.                        
  130.                         vs[vl] = quad[0];
  131.                         vs[vl+1] = quad[1];
  132.                         vs[vl+2] = quad[2];
  133.                         vs[vl+3] = quad[3];
  134.                        
  135.                         int tl = m.triangles.Length;
  136.                        
  137.                         int[] ts = m.triangles;
  138.                         if(!tmp || tl == 0) ts = resizeTraingles(ts, 6);
  139.                         else tl -= 6;
  140.                         ts[tl] = vl;
  141.                         ts[tl+1] = vl+1;
  142.                         ts[tl+2] = vl+2;
  143.                         ts[tl+3] = vl+1;
  144.                         ts[tl+4] = vl+3;
  145.                         ts[tl+5] = vl+2;
  146.                        
  147.                         m.vertices = vs;
  148.                         m.triangles = ts;
  149.                         m.RecalculateBounds();
  150.         }
  151.        
  152.         void processInput() {
  153.                 if(Input.GetKeyDown(KeyCode.C) || Input.touchCount >= 4) {
  154.                         ml = new Mesh();
  155.                         ms = new Mesh();
  156.                         transform.rotation = Quaternion.identity;
  157.                         first = null;
  158.                 }
  159.         }
  160.        
  161.         Vector3 GetNewPoint() {
  162.                 if (Input.touchCount > 0)
  163.                 {
  164.                         return Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(Input.touchCount - 1).position.x, Input.GetTouch(Input.touchCount - 1).position.y, Camera.main.transform.position.z * -1.0f));
  165.                 }
  166.                 return Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z * -1.0f));
  167.         }
  168.        
  169.         Vector3[] resizeVertices(Vector3[] ovs, int ns) {
  170.                 Vector3[] nvs = new Vector3[ovs.Length + ns];
  171.                 for(int i = 0; i < ovs.Length; i++) nvs[i] = ovs[i];
  172.                 return nvs;
  173.         }
  174.        
  175.         int[] resizeTraingles(int[] ovs, int ns) {
  176.                 int[] nvs = new int[ovs.Length + ns];
  177.                 for(int i = 0; i < ovs.Length; i++) nvs[i] = ovs[i];
  178.                 return nvs;
  179.         }
  180. }