-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboaSpaceNavigator.ms
159 lines (129 loc) · 5.16 KB
/
boaSpaceNavigator.ms
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
global boaSpaceNavigatorStructDef
struct boaSpaceNavigatorStructDef
(
hook,
assembly,
moveSensitivity = 1.0 / 10000,
rotationSensitivity = 1.0 / 500,
randomSensitivity = 50,
randomSmoothing = #(),
randomMaxSamples = 20,
cameraMoveSmoothing = #(),
cameraRotSmoothing = #(),
cameraSmoothingMaxSamples = 10,
useRandom = false,
useSmoothing = false,
fn onMessageReceived sender args =
(
local tm
local viewType = viewport.getType()
local camIfAny
local moveSensitivityResult = viewport.getFocalDistance() * boaSpaceNavigator.moveSensitivity
if viewType == #view_camera do
(
camIfAny = viewport.getCamera()
tm = camIfAny.transform
)
if viewType == #view_persp_user do tm = getViewTM()
if tm != undefined do
(
local rawtrans = [args.TransX, args.TransY, -args.TransZ]
local rawrot = [args.RotX, args.RotY, args.RotZ]
--print (args.TransX as string + args.TransY as string + args.TransZ as string)
--print (args.RotX as string + " " + args.RotY as string + " " + args.RotZ as string)
if boaSpaceNavigator.useRandom then
(
append boaSpaceNavigator.randomSmoothing (random [-1.0,-1.0,-1.0] [1.0,1.0,1.0])
if boaSpaceNavigator.randomSmoothing.count > boaSpaceNavigator.randomMaxSamples do
deleteItem boaSpaceNavigator.randomSmoothing 1
)
append boaSpaceNavigator.cameraMoveSmoothing rawtrans
if boaSpaceNavigator.cameraMoveSmoothing.count > boaSpaceNavigator.cameraSmoothingMaxSamples do
deleteItem boaSpaceNavigator.cameraMoveSmoothing 1
append boaSpaceNavigator.cameraRotSmoothing rawrot
if boaSpaceNavigator.cameraRotSmoothing.count > boaSpaceNavigator.cameraSmoothingMaxSamples do
deleteItem boaSpaceNavigator.cameraRotSmoothing 1
local smoothRandom
local trans = rawtrans
local rot = rawrot
if boaSpaceNavigator.useSmoothing do
(
smoothRandom = [0,0,0]; for val in boaSpaceNavigator.randomSmoothing do ( smoothRandom += val ) ; smoothRandom = smoothRandom / boaSpaceNavigator.randomSmoothing.count
trans = [0,0,0]; for val in boaSpaceNavigator.cameraMoveSmoothing do ( trans += val ) ; trans = trans / boaSpaceNavigator.cameraMoveSmoothing.count
rot = [0,0,0]; for val in boaSpaceNavigator.cameraRotSmoothing do ( rot += val ) ; rot = rot / boaSpaceNavigator.cameraRotSmoothing.count
)
if boaSpaceNavigator.useRandom do
(
trans += smoothRandom * boaSpaceNavigator.randomSensitivity
rot += smoothRandom * boaSpaceNavigator.randomSensitivity
)
local finalTrans = (trans*moveSensitivityResult)
local finalRotX = (rot.X*boaSpaceNavigator.rotationSensitivity)
--finalRotX = 0
local finalRotZ = (Rot.Y*boaSpaceNavigator.rotationSensitivity)
--in coordsys world rotate tm (eulerangles 0 0 finalRotZ)--(eulerangles -args.RotZ 0 args.RotY)
--in coordsys local rotate tm (eulerangles finalRotX 0 0)--(eulerangles -args.RotZ 0 args.RotY)
--in coordsys local rotate tm ((eulerAngles finalRotX 0 0) as quat) -- rotates Z axis in world space
if viewType == #view_camera do
(
preTranslate tm finalTrans
--preRotateX tm finalRotX
--preRotateY tm finalRotZ
in coordsys local move camIfAny finalTrans--(eulerangles -args.RotZ 0 args.RotY)
in coordsys world rotate camIfAny (eulerangles 0 0 finalRotZ)--(eulerangles -args.RotZ 0 args.RotY)
in coordsys local rotate camIfAny (eulerangles finalRotX 0 0)--(eulerangles -args.RotZ 0 args.RotY)
--in coordsys local rotate tm ((eulerAngles finalRotX 0 0) as quat) -- rotates Z axis in world space
--camIfAny.transform = tm
)
if viewType == #view_persp_user do
(
translate tm -finalTrans
rotate tm (quat finalRotZ tm.row3)
rotate tm ((eulerAngles -finalRotX 0 0) as quat)
viewport.setTM tm
)
)
),
fn firstTimeCreating =
(
-- Loading from bytes so dll is not locked
local currentScriptFolder = getFilenamePath (getThisScriptFilename())
local dllsFolder = pathConfig.appendPath currentScriptFolder @"dlls"
local dllPath = pathConfig.appendPath dllsFolder @"BoaSpaceNavigatorWrapper.dll"
this.assembly = (dotnetClass "System.Reflection.assembly").Load ((dotnetClass "System.IO.File").ReadAllBytes dllpath)
this.hook = (dotNetClass "System.Activator").CreateInstance (assembly.GetType("BoaSpaceNavigatorWrapper.WindowHooker"))
this.hook.AssignHandle (dotnetobject "IntPtr" (windows.getMAXHWND()))
this.hook.Initialize()
),
fn register =
(
print "register"
dotNet.addEventHandler this.hook "MessageReceived" this.onMessageReceived
),
fn deregister =
(
print "deregister"
--/* -- Run this only once TODO: include this in script by default
dotNet.removeAllEventHandlers this.hook
--this.hook.dispose() -- we don't dipose because we are going to reuse it in the next instance created
),
on create do
(
if boaSpaceNavigator == undefined then
(
-- load assembly
this.firstTimeCreating()
)
else
(
this.hook = boaSpaceNavigator.hook
this.assembly = boaSpaceNavigator.assembly
boaSpaceNavigator.deregister()
boaSpaceNavigator.hook = undefined
boaSpaceNavigator.assembly = undefined
)
-- register stuff
this.register()
)
)
boaSpaceNavigator = boaSpaceNavigatorStructDef()