scene_documentation.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.scene)
  4. def main_demo() -> None:
  5. with ui.scene().classes('w-full h-64') as scene:
  6. scene.sphere().material('#4488ff')
  7. scene.cylinder(1, 0.5, 2, 20).material('#ff8800', opacity=0.5).move(-2, 1)
  8. scene.extrusion([[0, 0], [0, 1], [1, 0.5]], 0.1).material('#ff8888').move(2, -1)
  9. with scene.group().move(z=2):
  10. scene.box().move(x=2)
  11. scene.box().move(y=2).rotate(0.25, 0.5, 0.75)
  12. scene.box(wireframe=True).material('#888888').move(x=2, y=2)
  13. scene.line([-4, 0, 0], [-4, 2, 0]).material('#ff0000')
  14. scene.curve([-4, 0, 0], [-4, -1, 0], [-3, -1, 0], [-3, 0, 0]).material('#008800')
  15. logo = 'https://avatars.githubusercontent.com/u/2843826'
  16. scene.texture(logo, [[[0.5, 2, 0], [2.5, 2, 0]],
  17. [[0.5, 0, 0], [2.5, 0, 0]]]).move(1, -3)
  18. teapot = 'https://upload.wikimedia.org/wikipedia/commons/9/93/Utah_teapot_(solid).stl'
  19. scene.stl(teapot).scale(0.2).move(-3, 4)
  20. avocado = 'https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/Avocado/glTF-Binary/Avocado.glb'
  21. scene.gltf(avocado).scale(40).move(-2, -3, 0.5)
  22. scene.text('2D', 'background: rgba(0, 0, 0, 0.2); border-radius: 5px; padding: 5px').move(z=2)
  23. scene.text3d('3D', 'background: rgba(0, 0, 0, 0.2); border-radius: 5px; padding: 5px').move(y=-2).scale(.05)
  24. @doc.demo('Handling Click Events', '''
  25. You can use the `on_click` argument to `ui.scene` to handle click events.
  26. The callback receives a `SceneClickEventArguments` object with the following attributes:
  27. - `click_type`: the type of click ("click" or "dblclick").
  28. - `button`: the button that was clicked (1, 2, or 3).
  29. - `alt`, `ctrl`, `meta`, `shift`: whether the alt, ctrl, meta, or shift key was pressed.
  30. - `hits`: a list of `SceneClickEventHit` objects, sorted by distance from the camera.
  31. The `SceneClickEventHit` object has the following attributes:
  32. - `object_id`: the id of the object that was clicked.
  33. - `object_name`: the name of the object that was clicked.
  34. - `x`, `y`, `z`: the x, y and z coordinates of the click.
  35. ''')
  36. def click_events() -> None:
  37. from nicegui import events
  38. def handle_click(e: events.SceneClickEventArguments):
  39. hit = e.hits[0]
  40. name = hit.object_name or hit.object_id
  41. ui.notify(f'You clicked on the {name} at ({hit.x:.2f}, {hit.y:.2f}, {hit.z:.2f})')
  42. with ui.scene(width=285, height=220, on_click=handle_click) as scene:
  43. scene.sphere().move(x=-1, z=1).with_name('sphere')
  44. scene.box().move(x=1, z=1).with_name('box')
  45. @doc.demo('Draggable objects', '''
  46. You can make objects draggable using the `.draggable` method.
  47. There is an optional `on_drag_start` and `on_drag_end` argument to `ui.scene` to handle drag events.
  48. The callbacks receive a `SceneDragEventArguments` object with the following attributes:
  49. - `type`: the type of drag event ("dragstart" or "dragend").
  50. - `object_id`: the id of the object that was dragged.
  51. - `object_name`: the name of the object that was dragged.
  52. - `x`, `y`, `z`: the x, y and z coordinates of the dragged object.
  53. You can also use the `drag_constraints` argument to set comma-separated JavaScript expressions
  54. for constraining positions of dragged objects.
  55. ''')
  56. def draggable_objects() -> None:
  57. from nicegui import events
  58. def handle_drag(e: events.SceneDragEventArguments):
  59. ui.notify(f'You dropped the sphere at ({e.x:.2f}, {e.y:.2f}, {e.z:.2f})')
  60. with ui.scene(width=285, height=220,
  61. drag_constraints='z = 1', on_drag_end=handle_drag) as scene:
  62. sphere = scene.sphere().move(z=1).draggable()
  63. ui.switch('draggable sphere',
  64. value=sphere.draggable_,
  65. on_change=lambda e: sphere.draggable(e.value))
  66. @doc.demo('Rendering point clouds', '''
  67. You can render point clouds using the `point_cloud` method.
  68. The `points` argument is a list of point coordinates, and the `colors` argument is a list of RGB colors (0..1).
  69. ''')
  70. def point_clouds() -> None:
  71. import numpy as np
  72. with ui.scene().classes('w-full h-64') as scene:
  73. x, y = np.meshgrid(np.linspace(-3, 3), np.linspace(-3, 3))
  74. z = np.sin(x) * np.cos(y) + 1
  75. points = np.dstack([x, y, z]).reshape(-1, 3)
  76. scene.point_cloud(points=points, colors=points, point_size=0.1)
  77. doc.reference(ui.scene)