Cylinder.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. import WhiteBoxMath as whiteBoxMath
  7. import WhiteBoxInit as init
  8. import argparse
  9. import azlmbr.legacy.general as general
  10. import azlmbr.bus as bus
  11. import azlmbr.whitebox.api as api
  12. # usage: pyRunFile path/to/file/cylinder.py <sides> <size>
  13. # number of sides is used to determine the number of vertices in each circle
  14. def create_cylinder(whiteBoxMesh, sides=16, size=0.5):
  15. # create all the vertices as two circles (top/bottom) with one additional vertex for the center of each circle
  16. # create center vertices for each circle
  17. top_vertex_pos = whiteBoxMath.spherical_to_cartesian(0.0, 0.0, size)
  18. bottom_vertex_pos = whiteBoxMath.spherical_to_cartesian(180.0, 0.0, size)
  19. top_vertex = whiteBoxMesh.AddVertex(top_vertex_pos)
  20. bottom_vertex = whiteBoxMesh.AddVertex(bottom_vertex_pos)
  21. # set up angles/distance for each of the cylinder circles
  22. top_circle_vertices = []
  23. bottom_circle_vertices = []
  24. angle_increment = 360.0 / sides
  25. # distance from the center of the cylinder to the vertices of the top/bottom circles
  26. # simplifies to size times the square root of 2
  27. vertex_dist = size * 1.414
  28. # add vertices for each of the cylinder circles
  29. for side in range (0, sides):
  30. # internal angle from center of cylinder to any point on top/bottom circles
  31. top_circle_pos = whiteBoxMath.spherical_to_cartesian(45.0, side * angle_increment, vertex_dist)
  32. bottom_circle_pos = whiteBoxMath.spherical_to_cartesian(135.0, side * angle_increment, vertex_dist)
  33. # add to list for vertices for the top/bottom circles
  34. top_circle_vertex = whiteBoxMesh.AddVertex(top_circle_pos)
  35. bottom_circle_vertex = whiteBoxMesh.AddVertex(bottom_circle_pos)
  36. top_circle_vertices.append(top_circle_vertex)
  37. bottom_circle_vertices.append(bottom_circle_vertex)
  38. # create faces
  39. top_circle_fvh = []
  40. bottom_circle_fvh = []
  41. for side in range (0, sides):
  42. index1 = side
  43. index2 = (side + 1) % sides
  44. # add to list for face vertex handles for top/bottom circles
  45. top_circle_fvh.append(api.util_MakeFaceVertHandles(top_vertex, top_circle_vertices[index1], top_circle_vertices[index2]))
  46. bottom_circle_fvh.append(api.util_MakeFaceVertHandles(bottom_vertex, bottom_circle_vertices[index2], bottom_circle_vertices[index1]))
  47. # add quad polygons to create the side of the cylinder
  48. whiteBoxMesh.AddQuadPolygon(top_circle_vertices[index1], bottom_circle_vertices[index1], bottom_circle_vertices[index2], top_circle_vertices[index2])
  49. # add top/bottom faces
  50. whiteBoxMesh.AddPolygon(top_circle_fvh)
  51. whiteBoxMesh.AddPolygon(bottom_circle_fvh)
  52. if __name__ == "__main__":
  53. # cmdline arguments
  54. parser = argparse.ArgumentParser(description='Creates a cylinder shaped white box mesh.')
  55. parser.add_argument('sides', nargs='?', default=16, type=int, help='number of vertices in each circle')
  56. parser.add_argument('size', nargs='?', default=0.5, type=float, help='size of the cylinder')
  57. args = parser.parse_args()
  58. # initialize whiteBoxMesh
  59. whiteBoxEntity = init.create_white_box_entity("WhiteBox-Cylinder")
  60. whiteBoxMeshComponent = init.create_white_box_component(whiteBoxEntity)
  61. whiteBoxMesh = init.create_white_box_handle(whiteBoxMeshComponent)
  62. # clear whiteBoxMesh to make a cylinder from scratch
  63. whiteBoxMesh.Clear()
  64. create_cylinder(whiteBoxMesh, args.sides, args.size)
  65. # update whiteBoxMesh
  66. init.update_white_box(whiteBoxMesh, whiteBoxMeshComponent)