-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·395 lines (322 loc) · 13.3 KB
/
main.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env ppython
import sys
sys.path.append("./build/pynui")
from os import listdir
from math import *
from time import clock
from sets import Set
from pynui import *
from app.hands import *
from app.cursor import *
from app.helper import *
from app.config import *
from app.flow import *
import app.states as states
from panda3d.core import *
from direct.interval.MetaInterval import Sequence
from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from direct.filter.CommonFilters import CommonFilters
from direct.filter.FilterManager import FilterManager
collision_mask = BitMask32(0x10)
def file_list():
list = [image_path + f for f in sorted(listdir(image_path))]
return list if len(list) <= max_pics else list[len(list)-max_pics:]
motion_blur_enabled = True
class App(ShowBase):
def __init__(self):
ShowBase.__init__(self)
base.disableMouse()
props = WindowProperties()
props.setCursorHidden(base.win.isFullscreen())
base.win.requestProperties(props)
self.win.setClearColor(VBase4(0, 0, 0, 0))
self.nui = Nui()
self.nui.smooth_factor = 0.9
if overscan:
dimensions = VBase4(0.035, 0.965, 0.035, 0.965)
self.cam.node().getDisplayRegion(0).setDimensions(dimensions)
self.cam2d.node().getDisplayRegion(0).setDimensions(dimensions)
self.camLens.setFov(90)
self.camLens.setNear(0.01)
self.camLens.setFar(2)
cam_pos = Vec3(0, -1, 0)
cam_pos.z = -cam_pos.y * tan(vfov()/2) * (1 - floor_ratio*2)
self.cam.setPos(cam_pos)
self.create_main_scene()
self.create_label_texture()
self.picsNode = self.scene.attachNewNode("Pics")
self.create_wall()
self.create_floor()
self.create_url_overlay()
if motion_blur_enabled:
dr = base.win.makeDisplayRegion()
dr.setClearDepthActive(True)
dr.setSort(20)
cam = NodePath(Camera("motion blur camera", lens=base.cam.node().getLens()))
cam.reparentTo(base.cam)
cam.node().setScene(self.picsNode)
tempnode = NodePath(PandaNode("temp node"))
tempnode.setShader(load_shader("motion-blur-1-pass"))
tempnode.setShaderInput("scene", self.scene_quad.getTexture())
cam.node().setInitialState(tempnode.getState())
dr.setCamera(cam)
self.loaded_files = Set()
before = clock()
print "Loading", len(file_list()), "files..."
for file in file_list():
self.load_file(file)
self.picsNode.prepareScene(base.win.getGsg())
print "Loaded in", str(clock() - before) + "s"
self.selection = 0
for (pic, pos, scale) in self.pics_pos_scale():
pic.setScale(scale)
pic.setPos(pos)
self.cursor = Cursor(self)
self.cursor_user = None
self.collision_traverser = CollisionTraverser('CollisionTraverser')
pickerNode = CollisionNode('cursor')
pickerNode.setFromCollideMask(collision_mask)
self.cursor_ray = CollisionRay(self.cam.getPos(), Vec3.unitZ())
pickerNode.addSolid(self.cursor_ray)
pickerNP = self.scene.attachNewNode(pickerNode)
self.collision_handler = CollisionHandlerEvent()
self.collision_handler.addInPattern('%fn-into-%in')
self.collision_handler.addAgainPattern('%fn-again-%in')
self.collision_handler.addOutPattern('%fn-out-%in')
self.collision_traverser.addCollider(pickerNP, self.collision_handler)
self.update_task = PythonTask(self.update, 'UpdateTask')
self.taskMgr.add(self.update_task)
self.hand_tracker = HandTracker()
states.Start(self)
self.last_look_for_new_file = 0
self.thumbnail_layout = None
def create_main_scene(self):
self.scene = NodePath(PandaNode("main scene"))
buffer = self.scene_buffer = base.win.makeTextureBuffer("main scene buffer", 0, 0)
buffer.setSort(-1)
buffer.setClearColor(Vec4(0,0,0,1))
scene_camera=base.makeCamera(buffer, lens=base.cam.node().getLens())
scene_camera.node().setScene(self.scene)
scene_camera.node().setTransform(self.cam.node().getTransform())
self.scene_quad = buffer.getTextureCard()
self.scene_quad.reparentTo(render2d)
def load_file(self, file):
self.loaded_files.add(file)
try:
texture = loader.loadTexture(file)
except:
return
texture.setMinfilter(Texture.FTLinearMipmapLinear)
texture.setWrapU(Texture.WMClamp)
texture.setWrapV(Texture.WMClamp)
maker = CardMaker('pic')
maker.setFrame(-1, 1, -1, 1)
pic = self.picsNode.attachNewNode(maker.generate())
pic.setTransparency(TransparencyAttrib.MAlpha, 1)
pic.setTexture(texture)
pic.setCollideMask(collision_mask)
pic.setPrevTransform(pic.getTransform())
pic.setShaderInput("old", pic.getPrevTransform().getMat())
while self.picsNode.getNumChildren() > max_pics and self.selection > 0:
self.picsNode.getChildren()[0].remove()
self.selection -= 1
return pic
def look_for_new_file(self):
pic = None
for file in file_list():
if file not in self.loaded_files:
print 'Loading new image ', file
pic = self.load_file(file)
(pic, pos, scale) = self.pics_pos_scale()[-1]
pic.setScale(scale)
pic.setPos(pos + Vec3(1,0,0))
break
def update(self, task):
self.collision_traverser.traverse(self.scene)
for pic in self.picsNode.getChildren():
node = NodePath("temp_transform_node")
node.setTransform(pic.getPrevTransform())
pic.setShaderInput("old", node)
pic.setPrevTransform(pic.getTransform())
self.nui.update()
self.label_texture.setRamMipmapPointerFromInt(self.nui.label_map, 0, 640*480*4)
self.floor.prepareScene(base.win.getGsg())
self.hand_tracker.update(self.nui.users)
if self.cursor_user is not None:
left_hand = self.hand_tracker.hands.get((self.cursor_user, Skeleton.left), None)
right_hand = self.hand_tracker.hands.get((self.cursor_user, Skeleton.right), None)
if left_hand is None or (right_hand is not None and right_hand.position.y > left_hand.position.y):
hand = right_hand
else:
hand = left_hand
if hand:
self.cursor.set_side(hand.side)
pos = hand.position
pos.x /= 0.25
pos.y /= 0.2
self.cursor.node.setPos(pos.x, 0, (pos.y + 1) / 2 * self.wall_top)
self.cursor_ray.setDirection(self.cursor.node.getPos() - self.cam.getPos())
if task.time - self.last_look_for_new_file > look_for_files_interval:
self.last_look_for_new_file = task.time
self.look_for_new_file()
return Task.cont
# Iterate through the default positions and scales of the pictures
def pics_pos_scale(self):
items = []
index = 0
for pic in self.picsNode.getChildren():
texture = pic.getTexture()
aspect_ratio = texture.getXSize() * 1.0 / texture.getYSize()
(width, height) = self.fit_wall(aspect_ratio, pic_margin + pic_strip_width)
scale = Vec3(width / 2, 1, height / 2)
if index == self.selection - 1:
x = -1 - scale.x + pic_strip_width
elif index == self.selection:
x = 0
elif index == self.selection + 1:
x = 1 + scale.x - pic_strip_width
else:
x = (index - self.selection) * 2
pos = Vec3(x, -0.01, self.wall_top/2)
items.append((pic, pos, scale))
index += 1
return items
def animate_pic(self, pic, pos, scale, time):
name = str(pic.getKey())
base.taskMgr.remove(name)
interpolate(name, pic.setPos, cubic_interpolator(pic.getPos(), pos, Vec3()), time)
interpolate(name, pic.setScale, cubic_interpolator(pic.getScale(), scale, Vec3(0,0,0)), time)
# Move each picture to its default position based on the current selection
def rearrange_pics(self, base_time_on_distance = False):
for (pic, pos, scale) in self.pics_pos_scale():
time = 0.5 if not base_time_on_distance else self.time_between(pos, pic.getPos())
self.animate_pic(pic, pos, scale, time)
def slide(self, direction):
new_selection = self.selection + direction
if new_selection < 0 or new_selection >= self.picsNode.getNumChildren():
return
self.selection = new_selection
self.rearrange_pics()
def arrange_thumbnails(self, user, reflow=True):
self.cursor.show()
self.cursor_user = user
if reflow or not self.thumbnail_layout:
self.thumbnail_layout = Flow(self.picsNode.getChildren(), 2, self.wall_top, pic_margin, thumbnail_margin)
for (pic, pos, scale) in self.thumbnail_layout.layout_items:
pos = Vec3(pos.x, 0, pos.y)
scale = Vec3(scale.x, 1, scale.y)
if pic.getPos() != pos or pic.getScale() != scale:
self.animate_pic(pic, pos, scale, self.time_between(pos, pic.getPos()))
else:
base.taskMgr.remove(str(pic.getKey()))
def arrange_normal(self):
self.cursor_user = None
self.rearrange_pics(True)
def index_of_pic(self, pic):
index = 0
for p in self.picsNode.getChildren():
if p == pic: return index
index += 1
return -1
def highlight_pic(self, pic):
index = self.index_of_pic(pic)
if index >= len(self.thumbnail_layout.layout_items):
print "Error: tried to highlight pic not in current thumbnail_layout."
return
pos = self.thumbnail_layout.layout_items[index][1]
pos = Vec3(pos.x, 0, pos.y)
dir = base.cam.getPos() - pos
dir.normalize()
pos += dir * 0.2
base.taskMgr.remove(str(pic.getKey()))
interpolate(str(pic.getKey()), pic.setPos, cubic_interpolator(pic.getPos(), pos, Vec3()), 0.3)
def select_pic(self, pic):
self.selection = self.index_of_pic(pic)
def time_between(self, a, b):
return min(2, 0.2 + log(1 + (a - b).length()) / 5)
# Return the size that will fit the wall at a desired aspect ratio
def fit_wall(self, aspect_ratio, margin):
return fit(aspect_ratio, 2 - margin * 2, self.wall_top * pic_height_ratio)
@property
def wall_top(self):
return (- self.cam.getPos().y) * tan(vfov()/2) + self.cam.getPos().z
def create_wall(self):
maker = CardMaker('wall')
maker.setFrame(-1, 1, 0, self.wall_top)
wall = self.scene.attachNewNode(maker.generate())
buffer = base.win.makeTextureBuffer('shadow', 512, 512)
buffer.setClearColor(VBase4(0, 0, 0, 0))
buffer.setSort(-6)
display = buffer.makeDisplayRegion()
camera = base.cam.attachNewNode(Camera('shadow'))
display.setCamera(camera)
camera.node().setLens(base.camLens)
camera.node().setScene(self.picsNode)
camera.setPos(-0.002, 0, 0.002)
blur_x = make_filter_buffer(buffer, 'blur-x',-5)
blur = make_filter_buffer(blur_x, 'blur-y',-4)
wall.setShaderInput('shadow', blur.getTexture())
texture = loader.loadTexture("resources/wall.png")
texture.setWrapU(Texture.WMClamp)
texture.setWrapV(Texture.WMClamp)
wall.setShaderInput('diffuse', texture)
wall.setShader(load_shader('wall'))
wall.setBin('background', 10)
wall.setDepthWrite(False)
def create_floor(self):
name = 'floor'
z = 0
buffer = base.win.makeTextureBuffer(name, 512, 512)
buffer.setClearColor(VBase4(0, 0, 0, 0))
buffer.setSort(-6)
display = buffer.makeDisplayRegion()
camera = self.scene.attachNewNode(Camera(name))
display.setCamera(camera)
camera.node().setInitialState(RenderState.make(CullFaceAttrib.makeReverse()))
camera.node().setLens(base.camLens)
symmetry = Mat4.translateMat(0,0,-z) * Mat4.scaleMat(1,1,-1) * Mat4.translateMat(0,0,z)
camera.setMat(base.cam.getMat() * symmetry)
camera.node().setScene(self.picsNode)
buffer = blur_buffer(buffer, -5)
maker = CardMaker(name)
maker.setFrame(-1, 1, -1, 0)
self.floor = self.scene.attachNewNode(maker.generate())
self.floor.setShader(load_shader('floor'))
self.floor.setShaderInput('reflection_tex', buffer.getTexture())
self.floor.setShaderInput('diffuse_tex', loader.loadTexture('resources/floor.jpg'))
self.floor.setShaderInput('normal_map', loader.loadTexture('resources/floor_NRM.jpg'))
self.floor.setShaderInput('shadow_tex', self.blurred_label_texture)
self.floor.setMat(Mat4.rotateMat(-90, VBase3.unitX()) * Mat4.translateMat(0, 0, z))
def create_label_texture(self):
self.label_texture = Texture()
self.label_texture.setWrapU(Texture.WMBorderColor)
self.label_texture.setWrapV(Texture.WMBorderColor)
self.label_texture.setBorderColor(VBase4())
self.label_texture.setCompression(Texture.CMOff)
self.label_texture.setup2dTexture(640, 480, Texture.TUnsignedByte, Texture.FRgba8)
self.label_texture.makeRamImage()
buffer = base.win.makeTextureBuffer('labels', 640, 480)
buffer.setClearColor(Vec4(0,0,0,0))
camera = base.makeCamera2d(buffer)
maker = CardMaker('labels')
maker.setFrameFullscreenQuad()
card = NodePath(maker.generate())
card.setTexture(self.label_texture)
camera.node().setScene(card)
for i in range(5):
buffer = blur_buffer(buffer)
self.blurred_label_texture = buffer.getTexture()
def create_url_overlay(self):
texture = loader.loadTexture('resources/url.png')
maker = CardMaker('')
width = texture.getXSize() * 2.0 / 1920
ratio = texture.getXSize() * 1.0 / texture.getYSize()
maker.setFrame(1 - width, 1, -1, -1 + width/ratio*base.camLens.getAspectRatio())
node = render2d.attachNewNode(maker.generate())
node.setTransparency(TransparencyAttrib.MAlpha, 1)
node.setTexture(texture)
if __name__ == '__main__':
loadPrcFile("local-config.prc")
app = App()
app.run()