-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathloader.py
148 lines (113 loc) · 5.65 KB
/
loader.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
"""
RenderPipeline
Copyright (c) 2014-2016 tobspr <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from __future__ import print_function
import time
from panda3d.core import PNMImage, VirtualFileSystem, VirtualFileMountRamdisk
from panda3d.core import Shader
from rpcore.globals import Globals
from rpcore.rpobject import RPObject
__all__ = ("RPLoader",)
class timed_loading_operation(object): # noqa # pylint: disable=invalid-name,too-few-public-methods
""" Context manager for a synchronous loading operation, keeping track
on how much time elapsed during the loading process, and warning about
long loading times. """
WARNING_COUNT = 0
def __init__(self, resource):
self.resource = resource
if isinstance(self.resource, (list, tuple)):
self.resource = ', '.join(self.resource)
def __enter__(self):
self.start_time = time.process_time()
def __exit__(self, *args):
duration = (time.process_time() - self.start_time) * 1000.0
if duration > 80.0 and timed_loading_operation.WARNING_COUNT < 5:
RPObject.global_warn(
"RPLoader", "Loading '" + self.resource + "' took", round(duration, 2), "ms")
timed_loading_operation.WARNING_COUNT += 1
if timed_loading_operation.WARNING_COUNT == 5:
RPObject.global_warn(
"RPLoader", "Skipping further loading warnings (max warning count reached)")
class RPLoader(RPObject):
""" Generic loader class used by the pipeline. All loading of assets happens
here, which enables us to keep track of used resources """
@classmethod
def load_texture(cls, filename):
""" Loads a 2D-texture from disk """
with timed_loading_operation(filename):
return Globals.base.loader.load_texture(filename)
@classmethod
def load_cube_map(cls, filename, read_mipmaps=False):
""" Loads a cube map from disk """
with timed_loading_operation(filename):
return Globals.base.loader.load_cube_map(filename, readMipmaps=read_mipmaps)
@classmethod
def load_3d_texture(cls, filename):
""" Loads a 3D-texture from disk """
with timed_loading_operation(filename):
return Globals.base.loader.load_3d_texture(filename)
@classmethod
def load_font(cls, filename):
""" Loads a font from disk """
with timed_loading_operation(filename):
return Globals.base.loader.load_font(filename)
@classmethod
def load_shader(cls, *args):
""" Loads a shader from disk """
with timed_loading_operation(args):
if len(args) == 1:
return Shader.load_compute(Shader.SL_GLSL, args[0])
return Shader.load(Shader.SL_GLSL, *args)
@classmethod
def load_model(cls, filename):
""" Loads a model from disk """
with timed_loading_operation(filename):
return Globals.base.loader.load_model(filename)
@classmethod
def load_sliced_3d_texture(cls, fname, tile_size_x, tile_size_y=None, num_tiles=None):
""" Loads a texture from the given filename and dimensions. If only
one dimensions is specified, the other dimensions are assumed to be
equal. This internally loads the texture into ram, splits it into smaller
sub-images, and then calls the load_3d_texture from the Panda loader """
tempfile_name = "/$$slice_loader_temp-" + str(time.time()) + "/"
tile_size_y = tile_size_x if tile_size_y is None else tile_size_y
num_tiles = tile_size_x if num_tiles is None else num_tiles
# Load sliced image from disk
tex_handle = cls.load_texture(fname)
source = PNMImage()
tex_handle.store(source)
width = source.get_x_size()
# Find slice properties
num_cols = width // tile_size_x
temp_img = PNMImage(
tile_size_x, tile_size_y, source.get_num_channels(), source.get_maxval())
# Construct a ramdisk to write the files to
vfs = VirtualFileSystem.get_global_ptr()
ramdisk = VirtualFileMountRamdisk()
vfs.mount(ramdisk, tempfile_name, 0)
# Extract all slices and write them to the virtual disk
for z_slice in range(num_tiles):
slice_x = (z_slice % num_cols) * tile_size_x
slice_y = (z_slice // num_cols) * tile_size_y
temp_img.copy_sub_image(source, 0, 0, slice_x, slice_y, tile_size_x, tile_size_y)
temp_img.write(tempfile_name + str(z_slice) + ".png")
# Load the de-sliced texture from the ramdisk
texture_handle = cls.load_3d_texture(tempfile_name + "/#.png")
vfs.unmount(ramdisk)
return texture_handle