@@ -17,21 +17,27 @@ private void OnDisable()
1717
1818 private void OnDestroy ( )
1919 {
20- if ( m_Mesh ) DestroyImmediate ( m_Mesh ) ;
20+ if ( m_Mesh )
21+ {
22+ DestroyImmediate ( m_Mesh ) ;
23+ DestroyImmediate ( m_LineMaterial ) ;
24+ DestroyImmediate ( m_QuadMaterial ) ;
25+ }
2126 }
2227
2328 public Transform m_Camera ;
2429 public Mesh m_Mesh ;
25- public Material m_Material ;
30+ public Material m_LineMaterial ;
31+ public Material m_QuadMaterial ;
2632 public Matrix4x4 m_Matrix ;
2733 public bool m_EqualSize ;
2834 public bool m_Active = true ;
29- public int m_Layer = 0 ;
30-
35+
3136 public List < Vector3 > m_Vertices = new List < Vector3 > ( 16 ) ;
3237 public List < Color > m_Color = new List < Color > ( 16 ) ;
3338 public List < int > m_Lines = new List < int > ( 16 ) ;
3439 public List < int > m_Quads = new List < int > ( 16 ) ;
40+ public List < Vector2 > m_UV = new List < Vector2 > ( 16 ) ;
3541
3642 public void AddLine ( Vector3 start , Vector3 end , Color color )
3743 {
@@ -42,6 +48,8 @@ public void AddLine(Vector3 start, Vector3 end, Color color)
4248 m_Lines . Add ( m - 1 ) ;
4349 m_Color . Add ( color ) ;
4450 m_Color . Add ( color ) ;
51+ m_UV . Add ( default ( Vector2 ) ) ;
52+ m_UV . Add ( default ( Vector2 ) ) ;
4553 }
4654
4755 public void AddRay ( Vector3 pos , Vector3 dir , Color color )
@@ -51,12 +59,12 @@ public void AddRay(Vector3 pos, Vector3 dir, Color color)
5159 AddLine ( pos , pos + dir , color ) ;
5260 }
5361
54- public void AddQuad ( Vector3 pos , Vector3 up , Vector3 right , Color color )
62+ public void AddQuad ( Vector3 pos , Vector2 size , Color color )
5563 {
56- m_Vertices . Add ( pos + up - right ) ;
57- m_Vertices . Add ( pos - up - right ) ;
58- m_Vertices . Add ( pos - up + right ) ;
59- m_Vertices . Add ( pos + up + right ) ;
64+ m_Vertices . Add ( pos ) ;
65+ m_Vertices . Add ( pos ) ;
66+ m_Vertices . Add ( pos ) ;
67+ m_Vertices . Add ( pos ) ;
6068 var m = m_Vertices . Count ;
6169 m_Quads . Add ( m - 4 ) ;
6270 m_Quads . Add ( m - 3 ) ;
@@ -66,20 +74,24 @@ public void AddQuad(Vector3 pos, Vector3 up, Vector3 right, Color color)
6674 m_Color . Add ( color ) ;
6775 m_Color . Add ( color ) ;
6876 m_Color . Add ( color ) ;
77+ m_UV . Add ( new Vector2 ( - size . x , - size . y ) ) ;
78+ m_UV . Add ( new Vector2 ( size . x , - size . y ) ) ;
79+ m_UV . Add ( new Vector2 ( size . x , size . y ) ) ;
80+ m_UV . Add ( new Vector2 ( - size . x , size . y ) ) ;
6981 }
7082
7183 public void AddQuad ( Vector3 pos , float size , Color color )
7284 {
7385 if ( m_EqualSize )
7486 size *= GetHandleSize ( pos ) ;
75- AddQuad ( pos , m_Camera . up * size , m_Camera . right * size , color ) ;
87+ AddQuad ( pos , Vector2 . one * size , color ) ;
7688 }
7789
7890 public void AddQuad ( Vector3 pos , float size , float colorFactor )
7991 {
8092 if ( m_EqualSize )
8193 size *= GetHandleSize ( pos ) ;
82- AddQuad ( pos + m_Camera . forward * - size , m_Camera . up * size , m_Camera . right * size , HSVToRGB ( colorFactor ) ) ;
94+ AddQuad ( pos , Vector2 . one * size , HSVToRGB ( colorFactor ) ) ;
8395 }
8496
8597 private float GetHandleSize ( Vector3 position )
@@ -181,32 +193,32 @@ public void Init(Transform transform, Transform camera, bool depth, bool equalSi
181193 m_Color . Clear ( ) ;
182194 m_Lines . Clear ( ) ;
183195 m_Quads . Clear ( ) ;
196+ m_UV . Clear ( ) ;
184197
185198 m_Matrix = transform . localToWorldMatrix ;
186199 m_Camera = camera ;
187200 m_EqualSize = equalSize ;
188- m_Layer = transform . gameObject . layer ;
201+ }
202+
203+ public void UpdateGO ( Transform transform )
204+ {
205+ m_Matrix = transform . localToWorldMatrix ;
189206 }
190207
191208 private void InitMaterial ( bool writeDepth )
192209 {
193- if ( ! m_Material )
210+ if ( ! m_LineMaterial )
194211 {
195- // Unity has a built-in shader that is useful for drawing
196- // simple colored things.
197- var shader = Shader . Find ( "Hidden/Internal-Colored" ) ;
198- m_Material = new Material ( shader ) ;
199- m_Material . hideFlags = HideFlags . DontSave ;
200- // Turn on alpha blending
201- m_Material . SetInt ( "_SrcBlend" , ( int ) BlendMode . SrcAlpha ) ;
202- m_Material . SetInt ( "_DstBlend" , ( int ) BlendMode . OneMinusSrcAlpha ) ;
203- // Turn backface culling off
204- m_Material . SetInt ( "_Cull" , ( int ) CullMode . Off ) ;
205- // Turn off depth writes
206- m_Material . SetInt ( "_ZWrite" , 0 ) ;
212+ var shader = Shader . Find ( "Hidden/InternalLineColorful" ) ;
213+ m_LineMaterial = new Material ( shader ) ;
214+ m_LineMaterial . hideFlags = HideFlags . DontSave ;
215+ var shader2 = Shader . Find ( "Hidden/InternalQuadColorful" ) ;
216+ m_QuadMaterial = new Material ( shader2 ) ;
217+ m_QuadMaterial . hideFlags = HideFlags . DontSave ;
207218 }
208219 // Set external depth on/off
209- m_Material . SetInt ( "_ZTest" , writeDepth ? 4 : 0 ) ;
220+ m_LineMaterial . SetInt ( "_ZTest" , writeDepth ? 4 : 0 ) ;
221+ m_QuadMaterial . SetInt ( "_ZTest" , writeDepth ? 4 : 0 ) ;
210222 }
211223
212224 public void End ( )
@@ -215,6 +227,7 @@ public void End()
215227
216228 m_Mesh . SetVertices ( m_Vertices ) ;
217229 m_Mesh . SetColors ( m_Color ) ;
230+ m_Mesh . SetUVs ( 0 , m_UV ) ;
218231 m_Mesh . subMeshCount = 2 ;
219232 m_Mesh . SetIndices ( m_Lines . ToArray ( ) , MeshTopology . Lines , 0 ) ;
220233 m_Mesh . SetIndices ( m_Quads . ToArray ( ) , MeshTopology . Quads , 1 ) ;
@@ -229,13 +242,15 @@ public void Clear()
229242 m_Color . Clear ( ) ;
230243 m_Lines . Clear ( ) ;
231244 m_Quads . Clear ( ) ;
245+ m_UV . Clear ( ) ;
232246
233247 }
234248
235249 public void Render ( )
236250 {
237- m_Material . SetPass ( 0 ) ;
251+ m_LineMaterial . SetPass ( 0 ) ;
238252 Graphics . DrawMeshNow ( m_Mesh , m_Matrix , 0 ) ;
253+ m_QuadMaterial . SetPass ( 0 ) ;
239254 Graphics . DrawMeshNow ( m_Mesh , m_Matrix , 1 ) ;
240255 }
241256}
0 commit comments