Staircase.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/staircase.py <steps> <depth> <height> <width>
  13. # formula to determine the new amount of faces added with each polygon translation
  14. def faces_added(step):
  15. return (step + 2) * 4
  16. # returns the face handles used to make the top back block, used for extruding upward
  17. def top_back_face_handle(num_faces):
  18. return api.FaceHandle(num_faces - 4)
  19. # determines the edge that needs to be hidden at the back of the staircase
  20. # next edge seems to related to prev edge by the recursive sequence e(n+1) = e(n)+20+6*n
  21. def edge_to_hide(prev_edge, step):
  22. if step == 0:
  23. return 20
  24. return prev_edge + 20 + 6 * step
  25. def create_staircase_from_white_box_mesh(whiteBoxMesh, num_steps, depth, height, width):
  26. # number of faces the mesh starts with
  27. curr_faces = 12
  28. # backmost face of the white box mesh, this will be extruded to add new steps for the staircase
  29. back_face = api.FaceHandle(10)
  30. # backmost face of the white box mesh, this will be extruded to add width to the staircase
  31. side_face = api.FaceHandle(5)
  32. # change the default whiteBoxMesh which will act as the first step to the staircase
  33. if height != 1.0 and height > 0.0:
  34. offset = height - 1.0
  35. top_polygon = whiteBoxMesh.FacePolygonHandle(azlmbr.object.construct('FaceHandle', 0))
  36. whiteBoxMesh.TranslatePolygon(top_polygon, offset)
  37. if depth != 1.0 and depth > 0.0:
  38. offset = depth - 1.0
  39. back_polygon = whiteBoxMesh.FacePolygonHandle(back_face)
  40. whiteBoxMesh.TranslatePolygon(back_polygon, offset)
  41. if width != 1.0 and width > 0.0:
  42. offset = width - 1.0
  43. side_polygon = whiteBoxMesh.FacePolygonHandle(side_face)
  44. whiteBoxMesh.TranslatePolygon(side_polygon, offset)
  45. prev_edge = 0
  46. # create rest of the staircase steps
  47. for step in range(0, num_steps):
  48. # extrude the back to create room for a new step
  49. back_polygon = whiteBoxMesh.TranslatePolygonAppend(whiteBoxMesh.FacePolygonHandle(back_face), depth)
  50. curr_faces += faces_added(step)
  51. back_faces = back_polygon.FaceHandles
  52. back_face = back_faces[-1]
  53. # extrude upward to create the new step
  54. top_back_polygon = whiteBoxMesh.FacePolygonHandle(top_back_face_handle(curr_faces))
  55. whiteBoxMesh.TranslatePolygonAppend(top_back_polygon, height)
  56. curr_faces += 8
  57. # hide any edge created in the backside from extruding upward
  58. prev_edge = edge_to_hide(prev_edge, step)
  59. whiteBoxMesh.HideEdge(api.EdgeHandle(prev_edge))
  60. def create_staircase(whiteBoxMesh, num_steps=2, depth=1.0, height=1.0, width=1.0):
  61. # if calling create_staircase directly, we need to start with a unit cube
  62. whiteBoxMesh.InitializeAsUnitCube()
  63. create_staircase_from_white_box_mesh(whiteBoxMesh, num_steps, depth, height, width)
  64. if __name__ == "__main__":
  65. # cmdline arguments
  66. parser = argparse.ArgumentParser(description='Creates a staircase shaped white box mesh.')
  67. parser.add_argument('num_steps', nargs='?', default=4, type=int, help='number of steps in the staircase')
  68. parser.add_argument('depth', nargs='?', default=1.0, type=float, help='depth of each step in the staircase')
  69. parser.add_argument('height', nargs='?', default=1.0, type=float, help='height of each step in the staircase')
  70. parser.add_argument('width', nargs='?', default=1.0, type=float, help='width of each step in the staircase')
  71. args = parser.parse_args()
  72. # initialize whiteBoxMesh
  73. whiteBoxEntity = init.create_white_box_entity("WhiteBox-Staircase")
  74. whiteBoxMeshComponent = init.create_white_box_component(whiteBoxEntity)
  75. whiteBoxMesh = init.create_white_box_handle(whiteBoxMeshComponent)
  76. create_staircase_from_white_box_mesh(whiteBoxMesh, args.num_steps, args.depth, args.height, args.width)
  77. # update whiteBoxMesh
  78. init.update_white_box(whiteBoxMesh, whiteBoxMeshComponent)