+from panda3d.core import AmbientLight, DirectionalLight, Point3
+from direct.showbase.DirectObject import DirectObject
+from pmachines.items.background import Background
+from pmachines.items.box import Box
+
+
+class Scene(DirectObject):
+
+ def __init__(self, world):
+ super().__init__()
+ self._world = world
+ self._set_camera()
+ self._set_lights()
+ self._set_input()
+ Background()
+ self.items = [Box(world)]
+
+ def _set_camera(self):
+ base.camera.set_pos(0, -20, 0)
+ base.camera.look_at(0, 0, 0)
+
+ def _set_directional_light(self, name, hpr, color):
+ light = DirectionalLight(name)
+ light_np = render.attach_new_node(light)
+ light_np.set_hpr(*hpr)
+ light.set_color(color)
+ render.set_light(light_np)
+
+ def _set_lights(self):
+ alight = AmbientLight('alight') # for ao
+ alight.set_color((.4, .4, .4, 1))
+ alnp = render.attach_new_node(alight)
+ render.set_light(alnp)
+ self._set_directional_light('key light', (315, -60, 0),
+ (3.6, 3.6, 3.6, 1))
+ self._set_directional_light('fill light', (195, -30, 0),
+ (.4, .4, .4, 1))
+ self._set_directional_light('rim light', (75, -30, 0), (.3, .3, .3, 1))
+
+ def _set_input(self):
+ self.accept('mouse1-up', self.on_click)
+
+ def on_click(self):
+ if not base.mouseWatcherNode.has_mouse(): return
+ p_from = Point3() # in camera coordinates
+ p_to = Point3() # in camera coordinates
+ base.camLens.extrude(base.mouseWatcherNode.get_mouse(), p_from, p_to)
+ p_from = render.get_relative_point(base.cam, p_from) # global coords
+ p_to = render.get_relative_point(base.cam, p_to) # global coords
+ for hit in self._world.ray_test_all(p_from, p_to).get_hits():
+ for item in [i for i in self.items if hit.get_node() == i.node]:
+ item.on_click(hit)