-
Notifications
You must be signed in to change notification settings - Fork 22
/
CameraControl.py
95 lines (69 loc) · 3.21 KB
/
CameraControl.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
import avango
import avango.script
import math
def clamp(minimum, x, maximum):
return max(minimum, min(x, maximum))
class CameraControl(avango.script.Script):
OutTransform = avango.gua.SFMatrix4()
OutTransform.value = avango.gua.make_identity_mat()
XBOX_X = avango.SFFloat()
XBOX_Y = avango.SFFloat()
XBOX_LZ = avango.SFFloat()
XBOX_RZ = avango.SFFloat()
def __init__(self):
self.super(CameraControl).__init__()
# how much mouse was moved since last frame
self._mouse_delta = avango.gua.Vec2(0.0, 0.0)
self._camera_offset = 0.01
self._camera_rotation = avango.gua.Vec2(0.0, 180.0)
self._mouse_last_pos = None
self._rotation_speed = 0.5
self._scroll_speed = 1
self._rotation_speed_xbox = 4.0
self._scroll_speed_xbox = 0.1
self._listen_mouse = True
def my_constructor(self, target_node, application_window,
camera_offset=0.01):
self._target = target_node
self._camera_offset = camera_offset
def handle_mouse(m):
if self._listen_mouse:
if self._mouse_last_pos is not None:
self._mouse_delta = self._mouse_last_pos - m
self._mouse_last_pos = m
application_window.on_move_cursor(handle_mouse)
def handle_scroll(s):
if self._listen_mouse:
self._camera_offset += s.y * self._scroll_speed
application_window.on_scroll(handle_scroll)
self.always_evaluate(True)
def listen_mouse(self, do_listen=True):
self._listen_mouse = do_listen
if not do_listen:
self._mouse_last_pos = None
def evaluate(self):
if math.fabs(self.XBOX_X.value) > 0.3:
self._mouse_delta.x = self._mouse_delta.x +\
(self.XBOX_X.value*self._rotation_speed_xbox)
if math.fabs(self.XBOX_Y.value) > 0.3:
self._mouse_delta.y = self._mouse_delta.y +\
(self.XBOX_Y.value*self._rotation_speed_xbox)
self._camera_offset += self.XBOX_LZ.value * self._scroll_speed_xbox
self._camera_offset -= self.XBOX_RZ.value * self._scroll_speed_xbox
# this cant be done in the constructor because the bbox scale
# is only correct after first rendered frame
target_height = self._target.BoundingBox.value.Max.value.y -\
self._target.BoundingBox.value.Min.value.y
# camera steering:
target_trans = self._target.WorldTransform.value.get_translate()
self._camera_rotation.x -= self._rotation_speed * self._mouse_delta.y
self._camera_rotation.x = clamp(-90, self._camera_rotation.x, 10)
self._camera_rotation.y -= self._rotation_speed * self._mouse_delta.x
self.OutTransform.value = \
avango.gua.make_trans_mat(
target_trans + avango.gua.Vec3(0.0, target_height, 0.0)) *\
avango.gua.make_rot_mat(self._camera_rotation.y, 0.0, 1.0, 0.0) *\
avango.gua.make_rot_mat(self._camera_rotation.x, 1.0, 0.0, 0.0) *\
avango.gua.make_trans_mat(0, 0, self._camera_offset *
target_height)
self._mouse_delta = avango.gua.Vec2(0.0, 0.0)