-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathviewer.py
109 lines (83 loc) · 3.53 KB
/
viewer.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
import math
import os
import sys
from direct.showbase.ShowBase import ShowBase
import panda3d.core as p3d
import simplepbr
p3d.load_prc_file_data(
'',
'window-size 1024 768\n'
'texture-minfilter mipmap\n'
'texture-anisotropic-degree 16\n'
)
class App(ShowBase):
def __init__(self):
if len(sys.argv) < 2:
print("Missing input file")
sys.exit(1)
super().__init__()
self.pipeline = simplepbr.init()
infile = p3d.Filename.from_os_specific(os.path.abspath(sys.argv[1]))
p3d.get_model_path().prepend_directory(infile.get_dirname())
# self.skybox = simplepbr.utils.make_skybox(self.env_map.filtered_env_map)
# self.skybox.reparent_to(self.render)
self.model_root = self.loader.load_model(infile, noCache=True)
self.accept('escape', sys.exit)
self.accept('q', sys.exit)
self.accept('w', self.toggle_wireframe)
self.accept('t', self.toggle_texture)
self.accept('n', self.toggle_normal_maps)
self.accept('e', self.toggle_emission_maps)
self.accept('o', self.toggle_occlusion_maps)
self.accept('a', self.toggle_ambient_light)
self.accept('shift-l', self.model_root.ls)
self.accept('shift-a', self.model_root.analyze)
self.model_root.reparent_to(self.render)
bounds = self.model_root.getBounds()
center = bounds.get_center()
if bounds.is_empty():
radius = 1
else:
radius = bounds.get_radius()
fov = self.camLens.get_fov()
distance = radius / math.tan(math.radians(min(fov[0], fov[1]) / 2.0))
self.camLens.set_near(min(self.camLens.get_default_near(), radius / 2))
self.camLens.set_far(max(self.camLens.get_default_far(), distance + radius * 2))
trackball = self.trackball.node()
trackball.set_origin(center)
trackball.set_pos(0, distance, 0)
trackball.setForwardScale(distance * 0.006)
# Create a light if the model does not have one
if not self.model_root.find('**/+Light'):
self.light = self.render.attach_new_node(p3d.PointLight('light'))
self.light.set_pos(0, -distance, distance)
self.render.set_light(self.light)
# Move lights to render
self.model_root.clear_light()
for light in self.model_root.find_all_matches('**/+Light'):
light.parent.wrt_reparent_to(self.render)
self.render.set_light(light)
# Add some ambient light
self.ambient = self.render.attach_new_node(p3d.AmbientLight('ambient'))
self.ambient.node().set_color((.2, .2, .2,.2))
self.render.set_light(self.ambient)
if self.model_root.find('**/+Character'):
self.anims = p3d.AnimControlCollection()
p3d.autoBind(self.model_root.node(), self.anims, ~0)
if self.anims.get_num_anims() > 0:
self.anims.get_anim(0).loop(True)
def toggle_normal_maps(self):
self.pipeline.use_normal_maps = not self.pipeline.use_normal_maps
def toggle_emission_maps(self):
self.pipeline.use_emission_maps = not self.pipeline.use_emission_maps
def toggle_occlusion_maps(self):
self.pipeline.use_occlusion_maps = not self.pipeline.use_occlusion_maps
def toggle_ambient_light(self):
if self.render.has_light(self.ambient):
self.render.clear_light(self.ambient)
else:
self.render.set_light(self.ambient)
def main():
App().run()
if __name__ == '__main__':
main()