ya2 · news · projects · code · about

srgb textures
authorFlavio Calva <f.calva@gmail.com>
Fri, 21 Jan 2022 20:08:20 +0000 (21:08 +0100)
committerFlavio Calva <f.calva@gmail.com>
Fri, 21 Jan 2022 20:08:20 +0000 (21:08 +0100)
lib/lib/p3d/gfx.py
pmachines/items/background.py
pmachines/items/box.py
pmachines/scene.py
prj.org

index 1c038c5fd23789bc27b1d18235fd23c890b31bef..073740cea38e406432521287c791b6bc62a22f42 100755 (executable)
@@ -11,6 +11,14 @@ from direct.actor.Actor import Actor
 from lib.lib.p3d.p3d import LibP3d
 
 
+def set_srgb(model):
+    for texture in model.find_all_textures():
+        if texture.get_format() in [Texture.F_rgba, Texture.F_rgbm]:
+            texture.set_format(Texture.F_srgb_alpha)
+        elif texture.get_format() in [Texture.F_rgb]:
+            texture.set_format(Texture.F_srgb)
+
+
 class RenderToTexture:
 
     def __init__(self, size=(256, 256)):
index d217b9502c71df5d406f8f208a7f6ec31fe991de..171918accbd096ebda326e16103349fb0a1beda1 100644 (file)
@@ -1,5 +1,6 @@
 from itertools import product
 from panda3d.core import NodePath
+from lib.lib.p3d.gfx import set_srgb
 
 
 class Background:
@@ -19,6 +20,7 @@ class Background:
             model.set_pos(left + end_size * col, offset, bottom + end_size * row)
         self._root.clear_model_nodes()
         self._root.flatten_strong()
+        set_srgb(self._root)
 
     def destroy(self):
         self._root.remove_node()
index d25ee141839ec7b1e2b26a1b034d5c0efa38c7b6..b76e966885077b6f44dbca82d4b9cde30623bded 100644 (file)
@@ -2,7 +2,7 @@ from panda3d.core import CullFaceAttrib, Point3, NodePath, Point2, Texture, \
     Plane, Vec3, BitMask32
 from panda3d.bullet import BulletBoxShape, BulletRigidBodyNode, BulletGhostNode
 from direct.gui.OnscreenText import OnscreenText
-from lib.lib.p3d.gfx import P3dGfxMgr
+from lib.lib.p3d.gfx import P3dGfxMgr, set_srgb
 
 
 class Box:
@@ -19,9 +19,11 @@ class Box:
         self._np = render.attach_new_node(self.node)
         world.attach_ghost(self.node)
         self._model = loader.load_model('assets/gltf/box/box.gltf')
+        set_srgb(self._model)
         self._model.flatten_light()
         self._model.reparent_to(self._np)
         self._set_outline_model()
+        set_srgb(self._outline_model)
         self._model.hide(BitMask32(0x01))
         self._outline_model.hide(BitMask32(0x01))
         self._start_drag_pos = None
index 8bf27647976a8e4701a6592d49fc4bfa3139e9f6..4f7dc9b34eba4de090b8bf76a0b0523332e29aeb 100644 (file)
@@ -110,40 +110,33 @@ class Scene(DirectObject):
             self.__next_btn, self.__prev_btn, self.__rewind_btn]
         [btn.destroy() for btn in btns]
 
-    def _set_directional_light(self, name, hpr, color):
-        light = DirectionalLight(name)
+    def _set_spotlight(self, name, pos, look_at, color, shadows=False):
+        light = Spotlight(name)
+        if shadows:
+            light.setLens(PerspectiveLens())
         light_np = render.attach_new_node(light)
-        light_np.set_hpr(*hpr)
+        light_np.set_pos(pos)
+        light_np.look_at(look_at)
         light.set_color(color)
         render.set_light(light_np)
         return 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._key_light = self._set_directional_light(
-            'key light', (315, -60, 0), (3.6, 3.6, 3.6, 1))
-        self._fill_light = self._set_directional_light(
-            'fill light', (195, -30, 0), (.4, .4, .4, 1))
-        self._rim_light = self._set_directional_light(
-            'rim light', (75, -30, 0), (.3, .3, .3, 1))
-        slight = Spotlight('slight')
-        slight.set_color((.1, .1, .1, 1))
-        slight.setLens(PerspectiveLens())
-        self.__shadow_light = render.attachNewNode(slight)
-        self.__shadow_light.set_pos(-5, -80, 5)
-        self.__shadow_light.look_at((0, 0, 0))
-        render.set_light(self.__shadow_light)
-        self.__shadow_light.node().set_shadow_caster(True, 2048, 2048)
-        self.__shadow_light.node().get_lens().set_film_size(2048, 2048)
-        self.__shadow_light.node().get_lens().set_near_far(1, 256)
-        self.__shadow_light.node().set_camera_mask(BitMask32(0x01))
+        alight.set_color((.15, .15, .15, 1))
+        self._alnp = render.attach_new_node(alight)
+        render.set_light(self._alnp)
+        self._key_light = self._set_spotlight(
+            'key light', (-5, -80, 5), (0, 0, 0), (2.8, 2.8, 2.8, 1))
+        self._shadow_light = self._set_spotlight(
+            'key light', (-5, -80, 5), (0, 0, 0), (.58, .58, .58, 1), True)
+        self._shadow_light.node().set_shadow_caster(True, 2048, 2048)
+        self._shadow_light.node().get_lens().set_film_size(2048, 2048)
+        self._shadow_light.node().get_lens().set_near_far(1, 256)
+        self._shadow_light.node().set_camera_mask(BitMask32(0x01))
 
     def _unset_lights(self):
-        for light in [self._key_light, self._fill_light, self._rim_light,
-                      self.__shadow_light]:
+        for light in [self._alnp, self._key_light, self._shadow_light]:
             render.clear_light(light)
             light.remove_node()
 
diff --git a/prj.org b/prj.org
index 9be78b10c05838ce26ec269e6cd65ca7f065dadf..3e94264b81e2c05e76c15c786ffa6c37fd0db865 100644 (file)
--- a/prj.org
+++ b/prj.org
@@ -1,9 +1,8 @@
 * issues
 * todo
-** srgb textures
 ** glitch when dragging with shadows
 *** try excluding the highlight model
-*** or see panda3d's doc for managin shadows
+*** or see panda3d's doc for managing shadows
 *** better working with only the shadow light
 ** refactoring
 ** implement the operations of the buttons