-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_controller.py
197 lines (143 loc) · 5.97 KB
/
camera_controller.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
from direct.task.Task import Task
from panda3d.core import Vec3
from panda3d.core import Lens, OrthographicLens, PerspectiveLens, LensNode
from input_listener import *
import sys,os
class Camera_Controller:
def __init__(self,base):
self.base = base
self.base.disableMouse()
x_win = self.base.win.getXSize()
y_win = self.base.win.getYSize()
aspect_win = float(x_win)/float(y_win)
self.active_lens = 1
self.ortho_lens = OrthographicLens()
self.ortho_lens.setAspectRatio(aspect_win)
self.ortho_lens.setNearFar(1.0,100.0)
self.persp_lens = PerspectiveLens()
self.persp_lens.setAspectRatio(aspect_win)
self.persp_lens.setFov(5.0)
self.persp_lens.setNearFar(1.0,100.0)
self.lenses = [self.persp_lens, self.ortho_lens]
self.set_lens(lens=self.active_lens)
self.set_view(Vec3(50.0,50.0,50.0))
self.old_x = None
self.old_y = None
self.zoom_speed = 0.05
self.pan_speed = 0.005
self.rotate_speed = 0.1
#these are pointers
self.keys = self.base.keyboard_reader.keys
self.key_map = self.base.keyboard_reader.key_map
#registering camera functions
self.base.taskMgr.add(hold_caller_multikey(trigger=self.keys, indices=[self.key_map['shift'], self.key_map['mouse2']], values=[1,1], init_handle=None, loop_handle=self.pan_camera, cleanup_handle=self.mouse_delta_cleanup), 'pan_camera_task')
self.base.taskMgr.add(hold_caller_multikey(trigger=self.keys, indices=[self.key_map['control'], self.key_map['shift'], self.key_map['mouse2']], values=[0, 0,1], init_handle=None, loop_handle=self.rotate_camera_fixed_pivot, cleanup_handle=self.mouse_delta_cleanup), 'rotate_camera_task')
self.base.taskMgr.add(hold_caller_multikey(trigger=self.keys, indices=[self.key_map['control'], self.key_map['mouse2']], values=[1,1], init_handle=None, loop_handle=self.zoom_camera, cleanup_handle=self.mouse_delta_cleanup), 'rotate_camera_task')
#register camera presets
self.base.taskMgr.add( delta_caller(handle = self.set_viewpoint_front , trigger = self.keys, index = self.key_map['1'], value=1), 'camera_preset_front_task')
self.base.taskMgr.add( delta_caller(handle = self.set_viewpoint_side , trigger = self.keys, index = self.key_map['2'], value=1), 'camera_preset_side_task')
self.base.taskMgr.add( delta_caller(handle = self.set_viewpoint_top , trigger = self.keys, index = self.key_map['3'], value=1), 'camera_preset_top_task')
#register switching perspective
#TODO: currently disabled because perspective camera is stupid and should feel bad
#self.base.taskMgr.add( delta_caller(handle = self.switch_perspective , trigger = self.keys, index = self.key_map['5'], value=1), 'camera_switch_perspective_task')
#makes the zoom level of the orthographic camera more reasonable
self.fixed_zoom_camera(10.0)
def set_view(self, position):
self.pos = Vec3(position)
self.base.cam.setPos(self.pos)
self.pivot = Vec3(0.0,0.0,0.0)
self.base.cam.lookAt(0.0,0.0,0.0)
self.hpr = self.base.cam.getHpr()
def set_lens(self,lens):
old_pos = self.base.cam.getPos()
self.active_lens = lens
self.base.cam.node().setLens(self.lenses[self.active_lens])
self.base.cam.setPos(old_pos)
def mouse_delta_cleanup(self):
self.old_x = None
self.old_y = None
def get_mouse_delta(self):
md = self.base.win.getPointer(0)
x = md.getX()
y = md.getY()
d_x = None
d_y = None
if self.old_x == None or self.old_y == None:
self.old_x = x
self.old_y = y
else:
d_y = (y - self.old_y)
d_x = (x - self.old_x)
self.old_x = x
self.old_y = y
return (d_x, d_y)
def pan_camera(self):
pan_speed = self.pan_speed
(d_x,d_y) = self.get_mouse_delta()
if (d_x != None) and (d_y != None):
up = self.base.cam.getMat().getRow3(2)
side = self.base.cam.getMat().getRow3(0)
d_up = up * d_y * pan_speed
d_side = side * -d_x * pan_speed
self.pos = self.pos + d_up + d_side
self.pivot = self.pivot + d_up + d_side
self.base.cam.setPos(self.pos)
def zoom_forward(self):
self.fixed_zoom_camera(self.zoom_speed)
def zoom_backward(self):
self.fixed_zoom_camera(-self.zoom_speed)
def fixed_zoom_camera(self, zoom_speed):
if self.active_lens == 0:
forward = self.base.cam.getMat().getRow3(1)
d_forward = forward * zoom_speed
self.pos = self.pos + d_forward
self.base.cam.setPos(self.pos)
if self.active_lens == 1:
zoom_level = self.ortho_lens.getFilmSize()
z_x = max(zoom_level.x + zoom_speed, 0.1)
z_y = z_x*0.75
self.ortho_lens.setFilmSize(z_x , z_y)
def zoom_camera(self):
zoom_speed = self.zoom_speed
(d_x,d_y) = self.get_mouse_delta()
if (d_y != None):
if self.active_lens == 0:
forward = self.base.cam.getMat().getRow3(1)
d_forward = forward * d_y * zoom_speed
self.pos = self.pos + d_forward
self.base.cam.setPos(self.pos)
if self.active_lens == 1:
zoom_level = self.ortho_lens.getFilmSize()
z_x = max(zoom_level.x + d_y * zoom_speed, 0.1)
z_y = z_x*0.75
self.ortho_lens.setFilmSize(z_x , z_y)
def rotate_camera_fixed_location(self):
rotate_speed = self.rotate_speed
(d_x,d_y) = self.get_mouse_delta()
if (d_x != None) and (d_y != None):
self.hpr = self.hpr + Vec3(-d_x * rotate_speed ,-d_y * rotate_speed ,0)
self.base.cam.setHpr(self.hpr)
def rotate_camera_fixed_pivot(self):
pivot = self.pivot
rotate_speed = self.rotate_speed
(d_x,d_y) = self.get_mouse_delta()
if (d_x != None) and (d_y != None):
up = self.base.cam.getMat().getRow3(2)
side = self.base.cam.getMat().getRow3(0)
d_up = up * d_y * rotate_speed
d_side = side * -d_x * rotate_speed
old_length = self.pos.length()
self.pos = self.pos + d_up + d_side
self.pos = self.pos / self.pos.length() * old_length
print self.pos.length()
self.base.cam.setPos(self.pos)
self.base.cam.lookAt(pivot)
def set_viewpoint_front(self):
self.set_view(Vec3(20.0,0.0,0.0))
def set_viewpoint_side(self):
self.set_view(Vec3(0.0,20.0,0.0))
def set_viewpoint_top(self):
self.set_view(Vec3(0.0,0.0,20.0))
def switch_perspective(self):
lens = (self.active_lens + 1) % 2
self.set_lens(lens)