ya2 · news · projects · code · about

outline
authorFlavio Calva <f.calva@gmail.com>
Fri, 7 Jan 2022 19:04:30 +0000 (20:04 +0100)
committerFlavio Calva <f.calva@gmail.com>
Fri, 7 Jan 2022 19:04:30 +0000 (20:04 +0100)
pmachines/items/box.py
pmachines/scene.py

index 71c0c007fbe61976f1f382d129d072f108bbb7a6..ffb88f058fb9c614619c1c6f7f02b9b354e3b383 100644 (file)
@@ -1,3 +1,4 @@
+from panda3d.core import CullFaceAttrib
 from panda3d.bullet import BulletBoxShape, BulletRigidBodyNode
 
 class Box:
@@ -13,6 +14,24 @@ class Box:
         model = loader.load_model('assets/gltf/box/box.gltf')
         model.flatten_light()
         model.reparent_to(np)
+        self._set_outline_model(np)
+
+    def _set_outline_model(self, np):
+        self._outline_model = loader.load_model('assets/gltf/box/box.gltf')
+        clockw = CullFaceAttrib.MCullClockwise
+        self._outline_model.set_attrib(CullFaceAttrib.make(clockw))
+        self._outline_model.reparent_to(np)
+        self._outline_model.set_scale(-1.08, -1.08, -1.08)
+        self._outline_model.set_light_off()
+        self._outline_model.set_color(.4, .4, .4, 1)
+        self._outline_model.set_color_scale(.4, .4, .4, 1)
+        self._outline_model.hide()
 
     def on_click(self, hit):
         print(hit.get_hit_pos())
+
+    def on_mouse_on(self):
+        self._outline_model.show()
+
+    def on_mouse_off(self):
+        self._outline_model.hide()
index 3baf29aed1cac5a8ef6ef1ec3ad8df3df4ad77a7..925ed2b6b8b8647aa47c8c31d1ca20967ace078a 100644 (file)
@@ -14,6 +14,7 @@ class Scene(DirectObject):
         self._set_input()
         Background()
         self.items = [Box(world)]
+        taskMgr.add(self.on_frame, 'on_frame')
 
     def _set_camera(self):
         base.camera.set_pos(0, -20, 0)
@@ -40,13 +41,24 @@ class Scene(DirectObject):
     def _set_input(self):
         self.accept('mouse1-up', self.on_click)
 
-    def on_click(self):
-        if not base.mouseWatcherNode.has_mouse(): return
+    def _get_hits(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():
+        return self._world.ray_test_all(p_from, p_to).get_hits()
+
+    def on_click(self):
+        for hit in self._get_hits():
             for item in [i for i in self.items if hit.get_node() == i.node]:
                 item.on_click(hit)
+
+    def on_frame(self, task):
+        hit_nodes = [hit.get_node() for hit in self._get_hits()]
+        items_hit = [itm for itm in self.items if itm.node in hit_nodes]
+        items_no_hit = [itm for itm in self.items if itm not in items_hit]
+        [itm.on_mouse_on() for itm in items_hit]
+        [itm.on_mouse_off() for itm in items_no_hit]
+        return task.cont