Hololens——HoloGram之包围盒计算
说明:
年前,在学习Hololens相关知识时,做了一个类似Hololens中微软自行开发的HoloGram程序,大致效果如下:
具体的效果链接视频——链接:http://pan.baidu.com/s/1nuAJVSH 密码:4koe
今天主要写一下这个模型包围盒。
建一个立方体的包围盒预制体,之后通过计算模型的包围盒Positon、Scale,来实例化该包围盒的Posion、Scale
如下所示:
包围盒组成部分包含:
1:一个Unity所创建的Cube,如下所示:
通过双面Shader达到如上的效果,双面Shader如何所示:
1. Shader "Custom/DoubleShader" {
2. Properties{
3. _Color("Main Color", Color) = (1,1,1,1)//Tint Color
4. _MainTex("Base (RGB)", 2D) = "white" {}
5. _MainTex_2("Base (RGB)", 2D) = "white" {}
6. }
7.
8. SubShader{
9.
10.
11. LOD 100
12. tags{ "Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True" }
13. Blend SrcAlpha OneMinusSrcAlpha
14.
15. Pass{
16. Cull Front
17. Lighting Off
18. SetTexture[_MainTex]{ combine texture }
19. SetTexture[_MainTex]
20. {
21. ConstantColor[_Color]
22. Combine Previous * Constant
23. }
24. }
25.
26. Pass
27. {
28. Cull Back
29. Lighting Off
30. SetTexture[_MainTex_2]{ combine texture }
31. SetTexture[_MainTex_2]
32. {
33. ConstantColor[_Color]
34. Combine Previous * Constant
35. }
36. }
37. }
38. }
2:八个Untiy创建的cube组成的顶点:
3:12个自己创建的球体(自己创建的球体减少了面数)
代码如下所示:
1. using UnityEngine;
2. ///
3. /// Draw a new MeshBounds
4. /// Made By Bruce.XXQQ
5. ///
6. public class MeshBounds : MonoBehaviour
7. {
8. public Transform target = null;
9. Renderer[] renderers;
10.
11. void Start()
12. {
13. if (target == null)
14. target = transform.parent;
15. Reset();
16. }
17. void Reset()
18. {
19. DeleteAllCollider(target);
20. var center = GetAllBoundsCenter(target,out renderers); ;
21. var bounds = addNewCollider(center, renderers);
22.
23. transform.position = bounds.center;
24. transform.localScale = bounds.size;
25.
26. //update all chirdren localScale
27. Vector3 pscale = transform.lossyScale;
28. for (int i = 0; i < transform.childCount; i++)
29. {
30. Transform s = transform.GetChild(i);
31. Vector3 sc = s.lossyScale;
32. float max = Mathf.Max(sc.x, sc.y, sc.z);
33. s.localScale = new Vector3(max * 1f / pscale.x, max * 1f / pscale.y, max * 1f / pscale.z);
34. }
35. }
36.
37. // delete all collider
38. void DeleteAllCollider(Transform transformTarget)
39. {
40. Collider[] colliders = transformTarget.GetComponentsInChildren();
41. foreach (Collider child in colliders)
42. {
43. DestroyImmediate(child);
44. }
45. }
46.
47. //get all Bounds Center
48. Vector3 GetAllBoundsCenter(Transform transformTarget,out Renderer[] renders)
49. {
50. Vector3 center = Vector3.zero;
51. renders = transformTarget.GetComponentsInChildren();
52. foreach (Renderer child in renders)
53. {
54. center += child.bounds.center;
55. }
56. center /= transformTarget.GetComponentsInChildren().Length;
57. return center;
58. }
59.
60. // add a new bounds and Collider
61. Bounds addNewCollider(Vector3 center,Renderer[]renders)
62. {
63. Bounds bounds = new Bounds(center, Vector3.zero);
64. foreach (Renderer child in renders)
65. {
66. bounds.Encapsulate(child.bounds);
67. }
68. // add a new collider
69. BoxCollider boxCollider = target.gameObject.AddComponent();
70. boxCollider.center = bounds.center - target.position;
71. boxCollider.size = bounds.size;
72. return bounds;
73. }
74. }
测试unitypackage链接:
链接:http://pan.baidu.com/s/1nvtzm6P 密码:pnii