2using System.Collections.Generic;
22 HighlightType highlightType = HighlightType.Wireframe;
26 private bool fallback =
false;
30 MeshFilter mf = GetComponent<MeshFilter>();
32 if(mf ==
null || mf.sharedMesh ==
null)
34 mf = GetComponentInChildren<MeshFilter>();
43 if(mf ==
null || mf.sharedMesh ==
null)
44 highlightType = HighlightType.Bounds;
46 switch( highlightType )
48 case HighlightType.Wireframe:
49 mesh = GenerateWireframe( mf.sharedMesh );
50 material = pb_BuiltinResource.GetMaterial(pb_BuiltinResource.mat_Wireframe);
53 case HighlightType.Bounds:
55 Bounds bounds = mf !=
null && mf.sharedMesh !=
null ? mf.sharedMesh.bounds :
new Bounds(Vector3.zero, Vector3.one);
58 var coll = GetComponentInChildren<Collider>();
61 if (coll.gameObject != gameObject)
68 bounds.center = Vector3.zero;
70 bounds.extents *= 0.9f;
73 mesh = GenerateBounds( bounds );
74 material = pb_BuiltinResource.GetMaterial(pb_BuiltinResource.mat_UnlitVertexColor);
77 case HighlightType.Glow:
79 material = pb_BuiltinResource.GetMaterial(pb_BuiltinResource.mat_Highlight);
83 mesh.RecalculateBounds();
84 material.SetVector(
"_Center", mesh.bounds.center);
86 Graphics.DrawMesh(mesh, transform.localToWorldMatrix,
material, 0);
89 internal void OnDestroy()
91 if( highlightType == HighlightType.Wireframe || highlightType == HighlightType.Bounds )
92 pb_ObjectUtility.Destroy(mesh);
97 Graphics.DrawMesh(mesh, transform.localToWorldMatrix,
material, 0);
103 Mesh GenerateWireframe(Mesh mesh)
106 m.indexFormat = IndexFormat.UInt32;
107 m.vertices = mesh.vertices;
108 m.normals = mesh.normals;
109 int[] tris =
new int[mesh.triangles.Length * 2];
110 int[] mtris = mesh.triangles;
113 for(
int i = 0; i < mtris.Length; i+=3)
115 tris[c++] = mtris[i+0];
116 tris[c++] = mtris[i+1];
117 tris[c++] = mtris[i+1];
118 tris[c++] = mtris[i+2];
119 tris[c++] = mtris[i+2];
120 tris[c++] = mtris[i+0];
124 m.SetIndices(tris, MeshTopology.Lines, 0);
132 Mesh GenerateBounds(Bounds bounds)
135 Vector3 ext = bounds.extents + bounds.extents.normalized * .1f;
138 List<Vector3> v =
new List<Vector3>();
140 v.AddRange( DrawBoundsEdge(cen, -ext.x, -ext.y, -ext.z, .2f) );
141 v.AddRange( DrawBoundsEdge(cen, -ext.x, -ext.y, ext.z, .2f) );
142 v.AddRange( DrawBoundsEdge(cen, ext.x, -ext.y, -ext.z, .2f) );
143 v.AddRange( DrawBoundsEdge(cen, ext.x, -ext.y, ext.z, .2f) );
145 v.AddRange( DrawBoundsEdge(cen, -ext.x, ext.y, -ext.z, .2f) );
146 v.AddRange( DrawBoundsEdge(cen, -ext.x, ext.y, ext.z, .2f) );
147 v.AddRange( DrawBoundsEdge(cen, ext.x, ext.y, -ext.z, .2f) );
148 v.AddRange( DrawBoundsEdge(cen, ext.x, ext.y, ext.z, .2f) );
151 int[] t =
new int[48];
154 for(
int i = 0; i < 48; i++)
163 m.indexFormat = IndexFormat.UInt32;
164 m.vertices = v.ToArray();
166 m.SetIndices(t, MeshTopology.Lines, 0);
169 m.normals = v.ToArray();
175 Vector3[] DrawBoundsEdge(Vector3 center,
float x,
float y,
float z,
float size)
185 v[1] = (p + ( -(x/Mathf.Abs(x)) *
Vector3.right * Mathf.Min(size, Mathf.Abs(x))));
188 v[3] = (p + ( -(y/Mathf.Abs(y)) *
Vector3.up * Mathf.Min(size, Mathf.Abs(y))));
191 v[5] = (p + ( -(z/Mathf.Abs(z)) *
Vector3.forward * Mathf.Min(size, Mathf.Abs(z))));