/
utils.py
162 lines (128 loc) · 4.21 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from __future__ import annotations
import math
import panda3d.core as p3d
from direct.showbase.ShowBase import ShowBase
from . import _shaderutils as shaderutils
def make_skybox(cubemap: p3d.Texture) -> p3d.NodePath[p3d.GeomNode]:
verts = [
-1.0, 1.0, -1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
-1.0, -1.0, 1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0,
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
]
num_verts = len(verts) // 3
vdata = p3d.GeomVertexData('skybox', p3d.GeomVertexFormat.get_v3(), p3d.Geom.UH_static)
vdata.set_num_rows(num_verts)
vertex = p3d.GeomVertexWriter(vdata, 'vertex')
for idx in range(0, len(verts), 3):
vertex.add_data3(*verts[idx:idx+3])
primitive = p3d.GeomTriangles(p3d.Geom.UH_static)
primitive.add_consecutive_vertices(0, num_verts)
geom = p3d.Geom(vdata)
geom.add_primitive(primitive)
node = p3d.GeomNode('skybox')
node.add_geom(geom)
node.set_bounds(p3d.OmniBoundingVolume())
np = p3d.NodePath(node)
shader = shaderutils.make_shader(
'skybox',
'skybox.vert',
'skybox.frag',
{}
)
np.set_shader(shader)
np.set_shader_input('skybox', cubemap)
np.set_depth_write(False)
np.set_bin('background', 0)
return np
def load_sdr_lut(filename: str) -> p3d.Texture:
'''Load an SDR color LUT embedded in a screenshot'''
path = p3d.Filename(filename)
vfs = p3d.VirtualFileSystem.get_global_ptr()
failed = (
not vfs.resolve_filename(path, p3d.get_model_path().value)
or not path.is_regular_file()
)
if failed:
raise RuntimeError(f'Failed to find file {filename}')
image = p3d.PNMImage(path)
lutdim = 64
xsize, ysize = image.get_size()
tiles_per_row = xsize // lutdim
num_rows = math.ceil(lutdim / tiles_per_row)
ysize -= num_rows * lutdim
texture = p3d.Texture()
texture.setup_3d_texture(
lutdim, lutdim, lutdim,
p3d.Texture.T_unsigned_byte,
p3d.Texture.F_rgb8
)
texture.minfilter = p3d.Texture.FT_linear
texture.magfilter = p3d.Texture.FT_linear
for tileidx in range(lutdim):
xstart = tileidx % tiles_per_row * lutdim
ystart = tileidx // tiles_per_row * lutdim + ysize
islice = p3d.PNMImage(lutdim, lutdim, 3, 255)
islice.copy_sub_image(image, 0, 0, xstart, ystart, lutdim, lutdim)
texture.load(islice, tileidx, 0)
return texture
def sdr_lut_screenshot( # type: ignore[no-untyped-def]
showbase: ShowBase,
*args,
**kwargs
) -> str | None:
'''Take a screenshot with an embedded SDR color LUT'''
filename = showbase.screenshot(*args, **kwargs)
if not filename:
return filename
lutdim = 64
stepsize = 256 // lutdim
image = p3d.PNMImage(filename)
xsize, ysize = image.get_size()
tiles_per_row = xsize // lutdim
num_rows = math.ceil(lutdim / tiles_per_row)
image.expand_border(0, 0, num_rows * lutdim, 0, (0, 0, 0, 1))
steps = list(range(0, 256, stepsize))
maxoffset = len(steps) - 1
for tileidx, bcol in enumerate(steps):
xbase = tileidx % tiles_per_row * lutdim
ybase = tileidx // tiles_per_row * lutdim + ysize
for xoff, rcol in enumerate(steps):
xcoord = xbase + xoff
for yoff, gcol in enumerate(steps):
ycoord = ybase + maxoffset - yoff
image.set_xel_val(xcoord, ycoord, rcol, gcol, bcol)
image.write(filename)
return filename