The Swartz-Manning’s first exhibit will provide a detailed history of Aaron Swartz Day. https://www.aaronswartzday.org/vr
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HighlightNearestFace.cs 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using UnityEngine;
  2. using System.Collections;
  3. using ProBuilder2.Common;
  4. /**
  5. * Move a sphere around the surface of a ProBuilder mesh, changing the
  6. * vertex color of the nearest face.
  7. *
  8. * Scene setup: Create a Unity Sphere primitive in a new scene, then attach
  9. * this script to the sphere. Press 'Play'
  10. */
  11. public class HighlightNearestFace : MonoBehaviour
  12. {
  13. // The distance covered by the plane.
  14. public float travel = 50f;
  15. // The speed at which the sphere will move.
  16. public float speed = .2f;
  17. // ProBuilder mesh component
  18. private pb_Object target;
  19. // The nearest face to this sphere.
  20. private pb_Face nearest = null;
  21. void Start()
  22. {
  23. // Generate a 50x50 plane with 25 subdivisions, facing up, with no smoothing applied.
  24. target = pb_ShapeGenerator.PlaneGenerator(travel, travel, 25, 25, ProBuilder2.Common.Axis.Up, false);
  25. target.SetFaceMaterial(target.faces, pb_Constant.DefaultMaterial);
  26. target.transform.position = new Vector3(travel * .5f, 0f, travel * .5f);
  27. // Rebuild the mesh (apply pb_Object data to UnityEngine.Mesh)
  28. target.ToMesh();
  29. // Rebuild UVs, Colors, Collisions, Normals, and Tangents
  30. target.Refresh();
  31. // Orient the camera in a good position
  32. Camera cam = Camera.main;
  33. cam.transform.position = new Vector3(25f, 40f, 0f);
  34. cam.transform.localRotation = Quaternion.Euler( new Vector3(65f, 0f, 0f) );
  35. }
  36. void Update()
  37. {
  38. float time = Time.time * speed;
  39. Vector3 position = new Vector3(
  40. Mathf.PerlinNoise(time, time) * travel,
  41. 2,
  42. Mathf.PerlinNoise(time + 1f, time + 1f) * travel
  43. );
  44. transform.position = position;
  45. if(target == null)
  46. {
  47. Debug.LogWarning("Missing the ProBuilder Mesh target!");
  48. return;
  49. }
  50. // instead of testing distance by converting each face's center to world space,
  51. // convert the world space of this object to the pb-Object local transform.
  52. Vector3 pbRelativePosition = target.transform.InverseTransformPoint(transform.position);
  53. // reset the last colored face to white
  54. if(nearest != null)
  55. target.SetFaceColor(nearest, Color.white);
  56. // iterate each face in the pb_Object looking for the one nearest
  57. // to this object.
  58. int faceCount = target.faces.Length;
  59. float smallestDistance = Mathf.Infinity;
  60. nearest = target.faces[0];
  61. for(int i = 0; i < faceCount; i++)
  62. {
  63. float distance = Vector3.Distance(pbRelativePosition, FaceCenter(target, target.faces[i]));
  64. if(distance < smallestDistance)
  65. {
  66. smallestDistance = distance;
  67. nearest = target.faces[i];
  68. }
  69. }
  70. // Set a single face's vertex colors. If you're updating more than one face, consider using
  71. // the pb_Object.SetColors(Color[] colors); function instead.
  72. target.SetFaceColor(nearest, Color.blue);
  73. // Apply the stored vertex color array to the Unity mesh.
  74. target.RefreshColors();
  75. }
  76. /**
  77. * Returns the average of each vertex position in a face.
  78. * In local space.
  79. */
  80. private Vector3 FaceCenter(pb_Object pb, pb_Face face)
  81. {
  82. Vector3[] vertices = pb.vertices;
  83. Vector3 average = Vector3.zero;
  84. // face holds triangle data. distinctIndices is a
  85. // cached collection of the distinct indices that
  86. // make up the triangles. Ex:
  87. // tris = {0, 1, 2, 2, 3, 0}
  88. // distinct indices = {0, 1, 2, 3}
  89. foreach(int index in face.distinctIndices)
  90. {
  91. average.x += vertices[index].x;
  92. average.y += vertices[index].y;
  93. average.z += vertices[index].z;
  94. }
  95. float len = (float) face.distinctIndices.Length;
  96. average.x /= len;
  97. average.y /= len;
  98. average.z /= len;
  99. return average;
  100. }
  101. }