mmatl / pyrender

Easy-to-use glTF 2.0-compliant OpenGL renderer for visualization of 3D scenes.
http://pyrender.readthedocs.io/
MIT License
1.31k stars 226 forks source link

pyrender OffScreen set Alpha channel get only 3 channels output #209

Open lucasjinreal opened 2 years ago

lucasjinreal commented 2 years ago

I have tested serveral script, all others are normal, except on program, somehow I set RenderFlags to RGBA, and set scence background color to 4 channels, but get returned result were 3 channels without Alpha.

Part of code:

def render(self, img, verts, cam, angle=None, axis=None, mesh_filename=None, color=[1.0, 1.0, 0.9], rotate=False):
        mesh = trimesh.Trimesh(vertices=verts, faces=self.faces, process=False)

        Rx = trimesh.transformations.rotation_matrix(
            math.radians(180), [1, 0, 0])
        mesh.apply_transform(Rx)

        if rotate:
            rot = trimesh.transformations.rotation_matrix(
                np.radians(60), [0, 1, 0])
            mesh.apply_transform(rot)

        if angle and axis:
            R = trimesh.transformations.rotation_matrix(
                math.radians(angle), axis)
            mesh.apply_transform(R)

        if mesh_filename is not None:
            mesh.export(mesh_filename)

        sx, sy, tx, ty = cam

        camera = WeakPerspectiveCamera(
            scale=[sx, sy],
            translation=[tx, ty],
            zfar=1000.
        )

        material = pyrender.MetallicRoughnessMaterial(
            metallicFactor=0.0,
            alphaMode='OPAQUE',
            smooth=True,
            wireframe=True,
            roughnessFactor=1.0,
            emissiveFactor=(0.1, 0.1, 0.1),
            baseColorFactor=(color[0], color[1], color[2], 1.0)
        )

        mesh = pyrender.Mesh.from_trimesh(mesh, material=material)
        mesh_node = self.scene.add(mesh, 'mesh')

        camera_pose = np.eye(4)
        cam_node = self.scene.add(camera, pose=camera_pose)

        if self.wireframe:
            render_flags = RenderFlags.RGBA | RenderFlags.ALL_WIREFRAME
        else:
            render_flags = RenderFlags.RGBA
        print(render_flags)
        rgb, _ = self.renderer.render(self.scene, flags=render_flags)
        print('rended rgb: ', rgb.shape)
        cv2.imshow('aa', rgb)
        cv2.waitKey(0)
        valid_mask = (rgb[:, :, -1] > 0)[:, :, np.newaxis]
        output_img = rgb[:, :, :-1] * valid_mask + (1 - valid_mask) * img
        image = output_img.astype(np.uint8)

log out:

rended rgb:  (1920, 1080, 3)

this is really so weired, it should be rended rgb: (1920, 1080, 4)

shubham-goel commented 2 years ago

I’m running into the same issue!

alon-ne commented 2 years ago

I also have this issue