-
Notifications
You must be signed in to change notification settings - Fork 4
/
smartphoneremote_ue5_reciever.py
142 lines (132 loc) · 6.51 KB
/
smartphoneremote_ue5_reciever.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
import preference, arcore, argparse, os, requests, math, tback
import numpy as np
currentFrame = 0
movementMultiplier = 1
cameraZRotationOffsetAdditive = 0
recieverCLIParser = argparse.ArgumentParser(description = 'Smartphone Remote middleman for UE5.')
recieverCLIParser.add_argument(
'--bind', '-b',
required = False,
type = int,
dest = 'recieverCLIArgs_bindPort',
help = 'the port where the reciever will listen for incoming connections (always listens on 0.0.0.0) (default: 8096)',
)
recieverCLIParser.add_argument(
'--generate-qr-code', '-g',
required = False,
type = str,
dest = 'recieverCLIArgs_generateQRCode',
help = '(yes/no) whether or not the program should generate a QR code for easy connections (default: no)'
)
recieverCLIParser.add_argument(
'--unreal-engine-api-root', '-u',
required = False,
type = str,
dest = 'recieverCLIArgs_unrealEngineAPIRoot',
help = '(http://a.d.d.r:port) the root point for the unreal engine web control api (default: http://127.0.0.1:30010)'
)
recieverCLIParser.add_argument(
'--unreal-engine-camera-path', '-c',
required = False,
type = str,
dest = 'recieverCLIArgs_unrealEngineCameraPath',
help = 'the path for the unreal engine scene\'s camera (default: debug value)'
)
recieverCLIParser.add_argument(
'--camera-z-rotation-offset', '-z',
required = False,
type = float,
dest = 'recieverCLIArgs_zRotationOffset',
help = 'the camera\'s starting z rotation (yaw) offset (additive) (default: 0)'
)
recieverCLIParser.add_argument(
'--speed-dividend', '-s',
required = False,
type = int,
dest = 'recieverCLIArgs_speedDividend',
help = 'only send requests on every n\'th frame (default: 2)'
)
recieverCLIArgs = recieverCLIParser.parse_args()
if (not recieverCLIArgs.recieverCLIArgs_bindPort): recieverCLIArgs.recieverCLIArgs_bindPort = 8096
if (not recieverCLIArgs.recieverCLIArgs_zRotationOffset): recieverCLIArgs.recieverCLIArgs_zRotationOffset = cameraZRotationOffsetAdditive
if (not recieverCLIArgs.recieverCLIArgs_speedDividend): recieverCLIArgs.recieverCLIArgs_speedDividend = 2
if (not recieverCLIArgs.recieverCLIArgs_unrealEngineAPIRoot): recieverCLIArgs.recieverCLIArgs_unrealEngineAPIRoot = 'http://127.0.0.1:30010'
if (not recieverCLIArgs.recieverCLIArgs_unrealEngineCameraPath): recieverCLIArgs.recieverCLIArgs_unrealEngineCameraPath = '/Game/StarterContent/Maps/Minimal_Default.Minimal_Default:PersistentLevel.CineCameraActor_0'
recieverCLIArgs.recieverCLIArgs_generateQRCode = False if (str(recieverCLIArgs.recieverCLIArgs_generateQRCode).lower() != 'yes') else True
if (recieverCLIArgs.recieverCLIArgs_generateQRCode): preference.generate_connexion_qrcode('{}:{}'.format(preference.get_current_ip(), recieverCLIArgs.recieverCLIArgs_bindPort), os.getcwd())
print('CURRENT BOUND ADDRESS: {}:{}'.format(preference.get_current_ip(), recieverCLIArgs.recieverCLIArgs_bindPort))
print('EASY CONNECT QR CODE SAVED TO CURRENT DIRECTORY' if recieverCLIArgs.recieverCLIArgs_generateQRCode else "NO QR CODE IMAGE GENERATED (USE -h FOR MORE INFO)")
preference.print_qrcode('{}:{}'.format(preference.get_current_ip(), recieverCLIArgs.recieverCLIArgs_bindPort))
UE5_VIEW_MATRIX = np.matrix([
[-1, 0, 0, 0],
[0, 0, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 1]
])
def handleARFrameRecieved(frame):
global currentFrame, lastCameraRotations
try:
currentFrame += 1
if (not currentFrame % recieverCLIArgs.recieverCLIArgs_speedDividend == 0): return '' # for pacing the requests
# i dont know. thanks, internet.
camPose_orig = frame.camera.view_matrix * UE5_VIEW_MATRIX
camPose = camPose_orig.tolist()
camOrigin = frame.root.world_matrix * UE5_VIEW_MATRIX
phi_y = np.arctan2(camPose[0][2], math.sqrt(1 - (camPose[0][2])**2)) #angle beta in wiki
# or just phi_y = np.arcsin(R[0,2])
phi_x = np.arctan2(-camPose[1][2],camPose[2][2]) #angle alpha in wiki
phi_z = np.arctan2(-camPose[0][1],camPose[0][0]) #angle gamma in wiki
cameraRotation = [math.degrees(phi_x), math.degrees(phi_y), math.degrees(phi_z)]
camPose[3] = (camPose_orig[3] - camOrigin[3])*(1/np.linalg.norm(camOrigin[1])).tolist()
cameraLocation = camPose[3].tolist()[-1]
# send the data
rotationRequestJSONDataRotation = {
"objectPath" : recieverCLIArgs.recieverCLIArgs_unrealEngineCameraPath,
"functionName":"SetActorRotation",
"parameters": {
"NewRotation": {
"Pitch": 270 - cameraRotation[0],
"Yaw": cameraRotation[2] + recieverCLIArgs.recieverCLIArgs_zRotationOffset,
"Roll": 360 - cameraRotation[1]
}
},
"generateTransaction":False
}
requests.put(recieverCLIArgs.recieverCLIArgs_unrealEngineAPIRoot + '/remote/object/call', json = rotationRequestJSONDataRotation)
rotationRequestJSONDataLocation = {
"objectPath" : recieverCLIArgs.recieverCLIArgs_unrealEngineCameraPath,
"functionName":"SetActorLocation",
"parameters": {
"NewLocation": {
"X": math.degrees(cameraLocation[1]) * movementMultiplier,
"Y": math.degrees(cameraLocation[0]) * movementMultiplier,
"Z": math.degrees(cameraLocation[2]) * movementMultiplier
}
},
"generateTransaction":False
}
requests.put(recieverCLIArgs.recieverCLIArgs_unrealEngineAPIRoot + '/remote/object/call', json = rotationRequestJSONDataLocation)
except Exception as ex:
tback.printTraceback(ex)
def handleARRecording(status):
global currentFrame
returnState = 'NONE'
if (status == 'START'): # start
returnState = 'STARTED'
print('STARTED RECORDING')
elif (status == 'STATE'): # update
returnState = str(currentFrame)
elif (status == 'STOP'): # stop
returnState = 'STOPPED'
print('STOPPED RECORDING')
exit()
return returnState
def handleARGetScene(*args):
return b''
if (__name__ == '__main__'):
ARHandler = arcore.ArEventHandler()
ARHandler.bindOnFrameReceived(handleARFrameRecieved)
ARHandler.bindRecord(handleARRecording)
ARHandler.bindGetScene(handleARGetScene)
AR = arcore.ArCoreInterface(ARHandler, port = int(recieverCLIArgs.recieverCLIArgs_bindPort))
AR.start()