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.

ExtrudeRandomEdges.cs 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #if UNITY_EDITOR || UNITY_STANDALONE
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using ProBuilder2.Common;
  6. using ProBuilder2.MeshOperations; // Extrude lives here
  7. using System.Linq;
  8. /**
  9. * Do a snake-like thing with a quad and some extrudes.
  10. */
  11. public class ExtrudeRandomEdges : MonoBehaviour
  12. {
  13. private pb_Object pb;
  14. pb_Face lastExtrudedFace = null;
  15. public float distance = 1f;
  16. /**
  17. * Build a starting point (in this case, a quad)
  18. */
  19. void Start()
  20. {
  21. pb = pb_ShapeGenerator.PlaneGenerator(1, 1, 0, 0, ProBuilder2.Common.Axis.Up, false);
  22. pb.SetFaceMaterial(pb.faces, pb_Constant.DefaultMaterial);
  23. lastExtrudedFace = pb.faces[0];
  24. }
  25. void OnGUI()
  26. {
  27. if(GUILayout.Button("Extrude Random Edge"))
  28. {
  29. ExtrudeEdge();
  30. }
  31. }
  32. void ExtrudeEdge()
  33. {
  34. pb_Face sourceFace = lastExtrudedFace;
  35. // fetch a random perimeter edge connected to the last face extruded
  36. List<pb_WingedEdge> wings = pb_WingedEdge.GetWingedEdges(pb);
  37. IEnumerable<pb_WingedEdge> sourceWings = wings.Where(x => x.face == sourceFace);
  38. List<pb_Edge> nonManifoldEdges = sourceWings.Where(x => x.opposite == null).Select(y => y.edge.local).ToList();
  39. int rand = (int) Random.Range(0, nonManifoldEdges.Count);
  40. pb_Edge sourceEdge = nonManifoldEdges[rand];
  41. // get the direction this edge should extrude in
  42. Vector3 dir = ((pb.vertices[sourceEdge.x] + pb.vertices[sourceEdge.y]) * .5f) - sourceFace.distinctIndices.Average(x => pb.vertices[x]);
  43. dir.Normalize();
  44. // this will be populated with the extruded edge
  45. pb_Edge[] extrudedEdges;
  46. // perform extrusion
  47. pb.Extrude(new pb_Edge[] { sourceEdge }, 0f, false, true, out extrudedEdges);
  48. // get the last extruded face
  49. lastExtrudedFace = pb.faces.Last();
  50. // not strictly necessary, but makes it easier to handle element selection
  51. pb.SetSelectedEdges( extrudedEdges );
  52. // translate the vertices
  53. pb.TranslateVertices(pb.SelectedTriangles, dir * distance);
  54. // rebuild mesh with new geometry added by extrude
  55. pb.ToMesh();
  56. // rebuild mesh normals, textures, collisions, etc
  57. pb.Refresh();
  58. }
  59. }
  60. #endif