| 1 | from panda3d.core import AmbientLight, DirectionalLight, Point3 |
| 2 | from direct.showbase.DirectObject import DirectObject |
| 3 | from pmachines.items.background import Background |
| 4 | from pmachines.items.box import Box |
| 5 | |
| 6 | |
| 7 | class Scene(DirectObject): |
| 8 | |
| 9 | def __init__(self, world): |
| 10 | super().__init__() |
| 11 | self._world = world |
| 12 | self._set_camera() |
| 13 | self._set_lights() |
| 14 | self._set_input() |
| 15 | Background() |
| 16 | self.items = [Box(world)] |
| 17 | |
| 18 | def _set_camera(self): |
| 19 | base.camera.set_pos(0, -20, 0) |
| 20 | base.camera.look_at(0, 0, 0) |
| 21 | |
| 22 | def _set_directional_light(self, name, hpr, color): |
| 23 | light = DirectionalLight(name) |
| 24 | light_np = render.attach_new_node(light) |
| 25 | light_np.set_hpr(*hpr) |
| 26 | light.set_color(color) |
| 27 | render.set_light(light_np) |
| 28 | |
| 29 | def _set_lights(self): |
| 30 | alight = AmbientLight('alight') # for ao |
| 31 | alight.set_color((.4, .4, .4, 1)) |
| 32 | alnp = render.attach_new_node(alight) |
| 33 | render.set_light(alnp) |
| 34 | self._set_directional_light('key light', (315, -60, 0), |
| 35 | (3.6, 3.6, 3.6, 1)) |
| 36 | self._set_directional_light('fill light', (195, -30, 0), |
| 37 | (.4, .4, .4, 1)) |
| 38 | self._set_directional_light('rim light', (75, -30, 0), (.3, .3, .3, 1)) |
| 39 | |
| 40 | def _set_input(self): |
| 41 | self.accept('mouse1-up', self.on_click) |
| 42 | |
| 43 | def on_click(self): |
| 44 | if not base.mouseWatcherNode.has_mouse(): return |
| 45 | p_from = Point3() # in camera coordinates |
| 46 | p_to = Point3() # in camera coordinates |
| 47 | base.camLens.extrude(base.mouseWatcherNode.get_mouse(), p_from, p_to) |
| 48 | p_from = render.get_relative_point(base.cam, p_from) # global coords |
| 49 | p_to = render.get_relative_point(base.cam, p_to) # global coords |
| 50 | for hit in self._world.ray_test_all(p_from, p_to).get_hits(): |
| 51 | for item in [i for i in self.items if hit.get_node() == i.node]: |
| 52 | item.on_click(hit) |