-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoruo.py
More file actions
1234 lines (1011 loc) · 42.7 KB
/
toruo.py
File metadata and controls
1234 lines (1011 loc) · 42.7 KB
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# MIT License
#
# Copyright (c) 2021-2025 AKAGI-Rails
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""撮る夫くん - VRMNX用グローバルカメラ拡張機能
VRMNX用Python拡張の **撮る夫くん** は,VRMNXビュワーのグローバルカメラ(フライスルーカメラ)の機能をアップグレードします。
ImGUIの操作パネルで,FOVや被写界深度の設定を直感的に行うことができます。
撮る夫くんには以下のような機能があります。
- FOVなどのGUI操作
- FOV, F値と対象物までの距離から,一眼レフカメラの機構をシミュレートした被写界深度制御
- 手ブレ風エフェクト
- 列車追尾モード・追い撮りモード
- 視点保存
- ゲームパッドでのカメラ操作
Example:
撮る夫くんを有効にするには,レイアウトのイベントハンドラの冒頭に
``toruo.activate()`` を記述します::
import vrmapi
import toruo
def vrmevent(obj,ev,param):
toruo.activate(obj,ev,param)
if ev == 'init':
pass
イベントのuserIDの予約領域
撮る夫くんでは,VRMNXのイベントuserIDで以下の領域を予約します。::
1060000 - 1069999
撮る夫くんが内部で使用するイベントはこの領域内でuserIDを指定しています。
"""
__all__ = ['DEBUG', 'dFOV', 'dRot', 'dMov', 'shake_factor', 'shake_freq',
'activate', 'set_toruo', 'jump_toruo', 'setfactor', 'setshakemode', 'set_gcdist',
'screenshot']
__version__ = '3.5.0'
__author__ = "AKAGI"
try:
import vrmapi
LOG = vrmapi.LOG
except ModuleNotFoundError:
print('VRMAPIが見つかりません。VRMNXシステムでしか動作しません。')
raise
from math import sin,cos,tan,sqrt, pi, pow, ceil,floor
from random import triangular
import json
import os.path
import ctypes
import datetime
from itertools import chain
from mss.windows import MSS as msswin
import mss.tools
import pygetwindow as gw
DEBUG = True
LAYOUT = vrmapi.LAYOUT()
NXSYS = vrmapi.SYSTEM()
IMGUI = vrmapi.ImGui()
DIRECTORY, TAIL = os.path.split(NXSYS.GetLayoutPath())
#LOG(TAIL)
# フレームイベントの設定は activate イベントハンドラでinitのタイミングに移動
_PARENT = None # 親オブジェクト
#_gcam = [] # 地上カメラのリスト
_config = None # 撮る夫くん設定データ
_toruos = [] # 撮る夫くんたちのリスト
_childid = [0]
_systime = 0.0 # 前フレームの時刻を記録
_shakemode = [False] # Trueで手ブレON
_guidisp = True # TrueでGUI操作盤を表示
_shake_vt = 0.0 # 手ブレの累積量
_shake_hr = 0.0
_shake_dvt = 0.0 # 手ブレの差分
_shake_dhr = 0.0
_shake_evid = None
_aemode = [False] # プログラムオート
DEFAULT_AEPARAM = {'blurfin':[90.0], 'ftg':[5.0/12], 'f10':[25.0]}
_aeparam = DEFAULT_AEPARAM
_sunpos = [[0], [45]] # 太陽位置(緯度,経度)
_gcdist = [256.0] # グローバルカメラのfrom-to距離設定値(リアルタイムではない)
# DOF(被写界深度)がらみのパラメタ
_fov = [45.0]
_depth = [0.25] # 合焦中心距離までの逆数の4乗根
_fnum = [50.0] # F値
_blur = [1.0] # ぼけの強さ
_delta = [0.0008, 0.08] # 錯乱円径
# 設定値(公開プロパティ)
dFOV = 10.0 #: FOV操作感度
dRot = 0.5 #: 左右回転感度
dMov = 25.0 #: 移動量感度
shake_factor = 0.1 #: 手ブレ量
shake_freq = 4.0 #: 手ブレ周波数
# カメラモード選択
_toruomode = [0] #: 撮る夫くんのモード状態 0=ノーマル、1=追尾, 2=追い撮り
# 追尾モード
_trainlist = {'obj':[], 'id':[], 'name':[]}
_fuzzytrack = [False]
_tracking_car = None
_tracking_trainid = [0]
_tracking_trnlen = 0
_tracking_carnum = [0]
_tracking_dist = [256.0]
_tracking_relative = {'x':[0.0], 'y':[0.0], 'z':[0.0]}
_tracking_af = [False]
# 追い撮りモード
_following_relpos = {'r':[256.0], 'theta':[0.0], 'phi':[0.0]} # r:動径, theta:水平角, phi:垂直角 角度はdegree
# ゲームパッドFLG
_GPlist = [False, False, False, False] #: 接続されているとTrue
_gamepad_sw = [-1] #: -1で無効、0-3で撮る夫くんで使用するゲームパッドのデバイス番号
DEFAULT_GAMEPAD_PARAM = {'L0_sense':[1.0], 'L0_exp':[1.0], 'R0_sense':[1.0], 'R0_exp':[1.0], 'R0_Yinv':[False], 'v_sense':[1.0], 'zoom_sense':[1.0]}
_gamepad_param = DEFAULT_GAMEPAD_PARAM
_gamepad_RYsgn = 1
_dash_factor = 1.0 #: ダッシュ係数(1.0~2.0)
DASH_MAX = 3.0
DASH_DIFF = 8.0
ANALOG_MAX = 32768.0
GP_BUTTONS = [1,2,4,8, 0x10, 0x20, 0x100, 0x200, 0x1000, 0x2000, 0x4000, 0x8000]
# Event UserID 1060000 - 1069999
EVUID_TORUOFRAME = 1060000
EVUID_TORUOSWITCH = 1060001
EVUID_TORUOSHAKE = 1060101
# マイピクチャを取得(スクリーンショットの保存先)
_buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
if ctypes.windll.shell32.SHGetSpecialFolderPathW(None, _buf, 0x0027, False):
MYPICTURES = _buf.value
else:
vrmapi.LOG('[toruo] Failed to get Pictures folder path.')
MYPICTURES = os.getcwd()
del _buf
SSSAVEDIR = os.path.join(MYPICTURES, "screenshot")
os.makedirs(SSSAVEDIR, exist_ok=True)
APPNAMES = ('鉄道模型シミュレーターNX', 'VRMONLINE-NX')
def activate(obj, ev, param):
"""撮る夫くんを有効にするコマンド。
レイアウトオブジェクトのイベントハンドラの先頭に書いてください。(ifの中には入れない。)::
# LAYOUT
import vrmapi
import toruo
def vrmevent(obj,ev,param):
toruo.activate(obj,ev,param)
if ev == 'init':
pass # 以下省略
Args:
obj: イベントハンドラが受け取るobj
ev: イベントハンドラが受け取るev
param: イベントハンドラが受け取るparam
撮る夫くんの関係イベントはすべてこの関数の中で処理されます。
`vrmevent` が受け取ったパラメータをそのまま引き渡してください。
"""
if ev == 'init':
global _PARENT
_PARENT = obj
obj.SetEventFrame(EVUID_TORUOFRAME)
obj.SetEventKeyDown('P', EVUID_TORUOSWITCH)
refresh_GPlist()
_load_config()
_refresh_trainlist()
vrmapi.LOG('撮る夫くん(Ver.{}) stand by. {}'.format(__version__, DIRECTORY))
#print(MYPICTURES)
return
elif param['eventid'] == _shake_evid:
# (Afterイベント)
_update_shake()
return
elif ev == 'keydown':
if param['keycode'] == 'P':
# GUI表示のON/OFFを切替
# このイベントは他モジュールのイベントとの相互乗り入れのため、IDで区別しない
global _guidisp
_guidisp = not _guidisp
return
#if not (ev == 'frame' and param['eventUID'] == EVUID_TORUOFRAME):
if ev != 'frame':
# 撮る夫くんのフレームイベント以外ではreturn
return
if _guidisp:
_dispgui()
if not LAYOUT.IsViewGlobal():
return
ftime = _updateframetime(param['eventtime'])
campos = NXSYS.GetGlobalCameraPos()
global _shake_hr
global _shake_vt
global _dash_factor
global _depth
"""
# キー操作の処理
# ズームイン
stat = NXSYS.GetKeyStat('E') + NXSYS.GetKeyStat('W') + NXSYS.GetKeyStat('R') + 3.0
if stat > 0.0:
_zoom(-1, ftime)
# ズームアウト
stat = NXSYS.GetKeyStat('S') + NXSYS.GetKeyStat('D') + NXSYS.GetKeyStat('F') + 3.0
if stat > 0.0:
_zoom(1, ftime)
# 左回転
stat = NXSYS.GetKeyStat('W') + NXSYS.GetKeyStat('S') + 2.0
if stat > 0.0:
_rotate(campos, -1, ftime)
# 右回転
stat = NXSYS.GetKeyStat('R') + NXSYS.GetKeyStat('F') + 2.0
if stat > 0.0:
_rotate(campos, 1, ftime)
"""
# ゲームパッド操作
if _gamepad_sw[0] in [0,1,2,3]:
gp = _gamepad_sw[0] # ゲームパッドを取得
# スクリーンショット(mss)
if NXSYS.GetGamepadB(gp):
screenshot()
if _toruomode[0] != 2: # 追い撮りモード以外
# ダッシュ処理
if NXSYS.GetGamepadA(gp):
_dash_factor += DASH_DIFF * ftime
else:
_dash_factor -= DASH_DIFF * ftime
_dash_factor = clip(_dash_factor, 1.0, DASH_MAX)
# 並進移動
# 左スティック・水平面
LX = NXSYS.GetGamepadAnalogStickLX(gp)
LY = NXSYS.GetGamepadAnalogStickLY(gp)
# 上下移動
v = 0
if NXSYS.GetGamepadRB(gp):
v += +1 * _gamepad_param['v_sense'][0]
if NXSYS.GetGamepadLB(gp):
v += -1 * _gamepad_param['v_sense'][0]
if abs(LX) > 100 or abs(LY) > 100 or v != 0:
_move(campos, adjust_analogL(LX)*_dash_factor, v*_dash_factor, adjust_analogL(LY)*_dash_factor, ftime)
else:
if not NXSYS.GetGamepadA(gp):
_dash_factor = 1.0
# 見回し(ゲームパッド)
RX = NXSYS.GetGamepadAnalogStickRX(gp)
if abs(RX) > 100:
_rotate(campos, adjust_analogR(RX), ftime)
RY = NXSYS.GetGamepadAnalogStickRY(gp)
if abs(RY) > 100:
_rotatevt(campos, adjust_analogR(RY)*_gamepad_RYsgn, ftime)
else:
# 追い撮りモードの操作
LY = NXSYS.GetGamepadAnalogStickLY(gp)
_following_relpos['r'][0] = clip(_following_relpos['r'][0] - dMov * adjust_analogL(LY) * ftime * 0.0005, 16, 4321.0)
RX = NXSYS.GetGamepadAnalogStickRX(gp)
if abs(RX) > 100:
_following_relpos['theta'][0] = _following_relpos['theta'][0] - dRot * adjust_analogR(RX) * ftime * 100
RY = NXSYS.GetGamepadAnalogStickRY(gp)
if abs(RY) > 100:
_following_relpos['phi'][0] = _following_relpos['phi'][0] + dRot * adjust_analogR(RY)*_gamepad_RYsgn * ftime * 100
# ズームイン・ズームアウト(ゲームパッド)
stat = NXSYS.GetGamepadLEFT(gp)
if stat:
_zoom(1*_gamepad_param['zoom_sense'][0], ftime)
stat = NXSYS.GetGamepadRIGHT(gp)
if stat:
_zoom(-1*_gamepad_param['zoom_sense'][0], ftime)
# フォーカス(深度値)中心
if NXSYS.GetGamepadUP(gp):
# フォーカス遠方へ=深度値小さく
d = _depth[0] - 0.05*ftime
_depth[0] = clip(d, 0.1, 0.5)
_focus()
if NXSYS.GetGamepadDOWN(gp):
# フォーカス近くへ=深度値大きく
d = _depth[0] + 0.05*ftime
_depth[0] = clip(d, 0.1, 0.5)
_focus()
# 追尾処理
istracking = False # 初期化
if _toruomode[0] == 1 and _tracking_car:
# 追尾モード時
if _fuzzytrack[0]:
tgtpos = _tracktargetpos_fuzzy(_tracking_trainid[0], _tracking_carnum[0]-1)
else:
tgtpos = _getcarworldpos(car=_tracking_car)
dist = vecdistance(campos[0:3], tgtpos)
if dist < _tracking_dist[0]:
istracking = True
campos[3:6] = tgtpos
if _tracking_af[0]:
#global _blur
# Auto Focus (Program Exposure)
_depth[0] = pow(dist, -0.25)
#_blur[0] = 2*(135.0-_fov[0])/125.0
_focus()
if _toruomode[0] == 2 and _tracking_car:
# 追い撮りモード時
# atの取得
if _fuzzytrack[0]:
tgtpos = _tracktargetpos_fuzzy(_tracking_trainid[0], _tracking_carnum[0]-1)
else:
tgtpos = _getcarworldpos(car=_tracking_car)
# fromを車両ローカルからグローバルに計算
carroty = _tracking_car.GetRotateY()
r = _following_relpos['r'][0]
theta = (_following_relpos['theta'][0] + carroty) * pi / 180.0
phi = _following_relpos['phi'][0] * pi / 180.0
rel_x = r * sin(pi /2 - phi) * cos(theta + pi)
rel_z = r * sin(pi /2 - phi) * sin(theta + pi)
rel_y = r * cos(pi /2 - phi)
campos = vecadd(tgtpos, [rel_x, rel_y, rel_z]) + tgtpos
if _tracking_af[0]:
# オートフォーカス
_depth[0] = pow(r, -0.25)
_focus()
# 手ブレモード用flg
istracking = True
# ブレ計算
if _shakemode[0]:
if istracking:
_shake_hr += _shake_dhr * dRot * ftime
_shake_vt += _shake_dvt * dRot * ftime
_rotate(campos, _shake_hr, 1.0)
_rotatevt(campos, _shake_vt, 1.0)
else:
_rotate(campos, _shake_dhr, ftime)
_rotatevt(campos, _shake_dvt, ftime)
NXSYS.SetGlobalCameraPos(campos)
LOG('Toruo OK')
def jump_toruo(id=0):
"""保存済みの撮る夫くん座標にジャンプ
保存済みの撮る夫くんを ``id`` で呼び出し,その座標にジャンプします。
Args:
id (int): 撮る夫くんの保存番号
Note:
保存された撮る夫くんの内容は,レイアウトと同じディレクトリの toruo.json に記録されています。
リスト型で読み出されるので,先頭のデータがid=0になります。
この関数は,実行時に撮る夫くん座標に即時反映されます。よって,適切なイベントハンドラの中で呼び出す必要があります。
"""
global _fov
global _depth
global _fnum
global _blur
global _aemode
global _aeparam
d = _toruos[id]
NXSYS.SetGlobalCameraPos(d['pos'])
_fov[0] = d['fov']
_depth[0] = d['depth']
_fnum[0] = d['fnum']
_blur[0] = d['blur']
try:
_aemode[0] = d['aemode']
_aeparam = d['aeparam']
except KeyError:
pass
_focus()
def set_toruo(fov=None, depth=None, fnum=None, blur=None, aemode=None):
"""撮る夫くんの状態を直接指定する。
撮る夫くんの撮影設定パラメータを直接指定できます。
入力パラメータはどれも省略可です。必要な項目だけキーワード引数で指定してください。
Parameters:
fov (float): FOV(ズーム角度)
depth (float): 合焦中心位置までの距離の逆数の4乗根
fnum (float): 絞り(F値)
blur (float): ぼけの強さ
aemode (bool): プログラムAE
この関数は,実行時に即時反映されます。よって,適切なイベントハンドラの中で呼び出す必要があります。
たとえば, initイベントで実行すると初期状態の撮る夫くんの設定ができます。 ::
# LAYOUT
import vrmapi
import toruo
def vrmevent(obj,ev,param):
toruo.activate(obj,ev,param)
if ev == 'init':
toruo.set_toruo(depth=512**(-1/4), fnum=9.0)
カメラ座標は ``vrmapi.SYSTEM().SetGlobalCameraPos()`` によってください。
"""
global _fov
global _depth
global _fnum
global _blur
global _aemode
global _aeparam
if fov is not None:
_fov[0] = fov
if depth is not None:
_depth[0] = depth
if fnum is not None:
_fnum[0] = fnum
if blur is not None:
_blur[0] = blur
if aemode is not None:
_aemode[0] = aemode
_focus()
def setfactor(rotate=0.5, fov=10.0, move=25.0):
"""キー操作と手ブレの感度パラメータを設定
v.3.0.7ではmoveは不使用です。
Args:
rotate (float): 画角回転の感度 (default=0.5)
fov (float): ズーム感度 (default=10.0)
"""
global dRot
global dFOV
global dMov
dRot = rotate
dFOV = fov
dMov = move
def setshakemode(mode=False):
"""手ブレモードを設定
Args:
mode (Bool): Trueで手ブレON
"""
global _shakemode
global _shake_hr
global _shake_vt
_shake_vt = 0.0 # 手ブレ量をリセット
_shake_hr = 0.0
_shakemode[0] = mode
_update_shake()
def set_gcdist(dist=256.0):
"""グローバルカメラのfrom-toの距離を再設定する。
現在の視線の向きをキープして,グローバルカメラのat座標を
from座標からの指定距離に再設定する。
Parameters:
dist: from-toの距離(mm)
"""
pos = NXSYS.GetGlobalCameraPos()
pos_from = pos[0:3]
pos_at = pos[3:6]
vec_eye = vecadd(pos_at, vecscale(-1, pos_from)) # at - from
distnow = veclen(vec_eye)
new_eye = list(map(lambda x: dist/distnow*x, vec_eye))
pos[3:6] = vecadd(pos_from , new_eye)
NXSYS.SetGlobalCameraPos(pos)
def _update_shake():
global _shake_dhr
global _shake_dvt
global _shake_evid
_shake_dhr = triangular(-1*shake_factor, shake_factor)
_shake_dvt = triangular(-1*shake_factor, shake_factor)
if _shakemode[0]:
_shake_evid = _PARENT.SetEventAfter(triangular(0.0, 1.0/shake_freq), EVUID_TORUOSHAKE)
def refresh_GPlist():
"""接続されているゲームパッドのリストを更新"""
global _GPlist
gplist = [NXSYS.IsGamepadConnected(i) for i in range(4)]
_GPlist = gplist
vrmapi.LOG("[toruo] Gamepad connection refreshed.")
return gplist
def _save_toruo():
"""現在視点を保存。"""
d = dict()
d['pos'] = NXSYS.GetGlobalCameraPos()
d['fov'] = _fov[0]
d['depth'] = _depth[0]
d['fnum'] = _fnum[0]
d['blur'] = _blur[0]
d['aemode'] = _aemode[0]
d['aeparam'] = _aeparam
_toruos.append(d)
_save_config()
def _default_config():
"""空の設定ファイルを返す
撮る夫くんconfig v.3.0
"""
return {
'toruoconfig': 'toruoconfig', # ファイル書式宣言。これがないjsonは相手にしない。
'version': '3.0', # 書式バージョン宣言
'layouts': {
# dict of dict - レイアウトごとの情報を保存。順不同。
# key: レイアウトのファイルパスのTAIL (ファイル名)
# value: 設定内容(dict)
# TAIL: {
# 'filename': TAIL, # ビュワーが開いているファイルとこの値で一致判定する。
# 'timestamp': str(datetime.datetime.now()),
#
# # 'toruo'要素は旧バージョンと同等のリスト。
# # 保存済み撮る夫くんのID-1がリストの番号になる。
# 'toruos': _toruos
#
# 'gamepad': (int) #: ゲームパッドスイッチの状態(撮る夫くんでアクティブなデバイス番号。-1はOFF)
# 'gamepad_param': {'L0_sense':[1.0], 'L0_exp':[1.0], 'R0_sense':[1.0], 'R0_exp':[1.0], 'v_sense':[1.0]} by default
# 'aeparam': (dict) AEパラメータ
# }
}
}
def _check_config(config):
"""撮る夫くん設定ファイルのチェック
正規データに対しては何もせずそのまま返す。
無効なデータは,空の正規データで返す。
撮る夫くんconfig - v.3.0
"""
# マジックヘッダの確認
try:
magic = config['toruoconfig']
except (TypeError, KeyError) as e:
LOG("Invalid toruo setting")
print("Following error is muted;", e)
return _default_config()
if magic != 'toruoconfig':
LOG("[Toruo Warning] Invalid magicheader for toruo setting.")
return _default_config()
# バージョンのチェック (New in toruo v.3.4)
v = config.get('version')
if int(v[0]) < 3:
return _default_config()
return config
def _load_config(filename=os.path.join(DIRECTORY, 'toruo.json')):
"""保存済み撮る夫くんを読み込み
撮る夫くんconfig - v.3.0
レイアウトと同じディレクトリの toruo.json を探し,存在すればデータを読み込みます。
"""
global _config
global _toruos
global _gamepad_sw
global _gamepad_param
global _aeparam
try:
with open(filename) as js:
config = json.load(js)
except FileNotFoundError:
LOG("[Toruo Warning] Could not found toruo setting.")
_config = _default_config()
return
config = _check_config(config)
_config = config
# 自分のレイアウトファイルに相当するデータの引っ張り出し
lay = config['layouts'].get(TAIL, None)
if lay is None:
# 自分のレイアウトのセーブデータがない
LOG("[Toruo Warning] toruo data is not found.")
else:
# セーブデータが見つかった
_toruos = lay.get('toruos', []) # 撮る夫くんのリストをロード
# ゲームパッド感度設定をロードして復活
_gamepad_param = lay.get('gamepad_param', {})
for k,v in DEFAULT_GAMEPAD_PARAM.items():
_gamepad_param.setdefault(k,v)
global _gamepad_RYsgn
if _gamepad_param['R0_Yinv'][0]:
_gamepad_RYsgn = -1
else:
_gamepad_RYsgn = 1
# プログラムAE
_aeparam = lay.get('aeparam', {})
for k,v in DEFAULT_AEPARAM.items():
_aeparam.setdefault(k,v)
# ゲームパッドの設定をロードして復活
gp = lay.get('gamepad', -1)
if gp in [0,1,2,3]:
if NXSYS.IsGamepadConnected(gp):
# ゲームパッドの接続あり
_gamepad_sw = [gp]
else:
_gamepad_sw = [-1]
else:
_gamepad_sw = [-1]
_change_gamepad() # これは最後に(saveが呼ばれちゃうので)
def _save_config(filename=os.path.join(DIRECTORY, 'toruo.json')):
"""撮る夫くん保存状況をファイルに書き出し
撮る夫くんconfig - v.3.0
"""
global _config
config = _check_config(_config)
config['layouts'][TAIL] = {
'filename': TAIL, # ビュワーが開いているファイルとこの値で一致判定する。
'timestamp': str(datetime.datetime.now()),
# 'toruo'要素は旧バージョンと同等のリスト。
# 保存済み撮る夫くんのID-1がリストの番号になる。
'toruos': _toruos,
# ゲームパッドスイッチの状態。(撮る夫くんでアクティブなデバイス番号。-1はOFF)
'gamepad': _gamepad_sw[0],
'gamepad_param': _gamepad_param,
'aeparam': _aeparam
}
_config = config # グローバルに書き戻す
with open(filename, 'w') as js:
json.dump(config, js, indent=4)
vrmapi.LOG("toruo config saved.")
return
def _getcarworldpos(trainid=None, carnum=None, car=None):
"""指定車両におけるglobalの_tracking_relativeにある相対座標をレイアウト上の絶対座標にして返す
``trainid``と``carnum``の両方を指定するか,直接``car``を指定してください。
Args:
trainid (int, optional): 編成オブジェクトのID.
carnum (int, optional): 号車番号(先頭=0号車)
car (:obj:`vrmapi.VRMCar`): VRM車両オブジェクト
"""
if not car:
car = LAYOUT.GetTrain(trainid).GetCar(carnum)
rel = [_tracking_relative['x'][0], _tracking_relative['y'][0], _tracking_relative['z'][0]]
rel = vectrans(xrot_matrix3d(car.GetRotateX()), rel)
rel = vectrans(yrot_matrix3d(car.GetRotateY()), rel)
rel = vectrans(zrot_matrix3d(-1*car.GetRotateZ()), rel)
return vecadd(car.GetPosition(), rel)
def _tracktargetpos_fuzzy(trainid, carnum):
"""ファジィ号車番号で編成の相対座標を絶対座標に変換。
carnum は小数でもOK
"""
c1 = floor(carnum)
c2 = ceil(carnum)
if c1 == c2:
return _getcarworldpos(trainid=trainid, carnum=c1)
m1 = c2 - carnum
m2 = carnum - c1
pos1 = _getcarworldpos(trainid=trainid, carnum=c1)
pos2 = _getcarworldpos(trainid=trainid, carnum=c2)
return [m1*pos1[i]+m2*pos2[i] for i in range(3)] # m1,m2で線形補間
def _updateframetime(time_now):
global _systime
ftime = time_now - _systime
_systime = time_now # フレームの時刻を記録
return ftime
def _zoom(spd, ftime):
"""ズーム
Args:
spd: -1でズームイン,+1でズームアウト
ftime: フレーム描画時間
"""
global _fov
f = NXSYS.GetGlobalCameraFOV() + spd * dFOV * ftime
f = clip(f, 10.0, 135.0)
_fov[0] = f
NXSYS.SetGlobalCameraFOV(_fov[0])
_focus()
def _move(campos, x, y, z, ftime):
"""平行移動
リストで与えられる `campos` をインプレースに操作します。
帰り値はありません。
Args:
campos: NXSYS.GetGlobalCameraPos()の帰り値と同型
xyz: 入力移動量。camposと同じ次元。
ftime: フレームの描画時間
入力移動量のXZは向いてる方角に回転して現在座標に加算する。
Note:
VRM Layoutのワールド座標XZとスティック入力のXYで回転方向が違うので注意。
"""
# 0 1 2 3 4 5
# x y z x y z
# XZ方向を回転
rx = campos[3] - campos[0]
rz = campos[5] - campos[2]
vr = [rx, rz]
vl = veclen(vr)
cos_, sin_ = vecscale(1/vl/100, vr) # [cos, sin]
rot = [[-sin_, 0, cos_],
[0, 100, 0],
[cos_, 0, sin_]]
vm = vectrans(rot, [x,y,z]) # 回転済みの入力方向
vm = vecscale(ftime, vm)
campos[0] += vm[0]
campos[1] += vm[1]
campos[2] += vm[2]
campos[3] += vm[0]
campos[4] += vm[1]
campos[5] += vm[2]
def adjust_analogL(x):
sgnx = sgn(x)
absx = abs(x)
y = sgnx * ((absx/ANALOG_MAX)**_gamepad_param['L0_exp'][0]) * _gamepad_param['L0_sense'][0] * ANALOG_MAX
return y
def _rotate(campos, spd, ftime):
"""水平方向見回し
リストで与えられる `campos` をインプレースに操作します。
帰り値はありません。
Args:
campos: NXSYS.GetGlobalCameraPos()の帰り値と同型
spd: -1で左,+1で右回り
ftime: フレームの描画時間
"""
# list campos はMutableなので返り値を取らなくていい
dx = campos[3] - campos[0]
dz = campos[5] - campos[2]
dtheta = spd * dRot * ftime
cos_ = cos(dtheta)
sin_ = sin(dtheta)
campos[3] = campos[0] + cos_*dx - sin_*dz
campos[5] = campos[2] + sin_*dx + cos_*dz
def _rotatevt(campos, spd, ftime):
"""垂直見回し
リストで与えられる `campos` をインプレースに操作します。
帰り値はありません。
Args:
campos: NXSYS.GetGlobalCameraPos()の帰り値と同型
spd: -1で下,+1で上
ftime: フレームの描画時間
"""
x = sqrt((campos[3]-campos[0])**2 + (campos[5]-campos[2])**2)
y = campos[4]-campos[1]
alpha = tan(spd * dRot * ftime)
campos[4] = (y + x*alpha)/(1 - y/x*alpha) + campos[1] # tan加法定理の変形
def adjust_analogR(x):
sgnx = sgn(x)
absx = abs(x)
y = sgnx * ((absx/ANALOG_MAX)**_gamepad_param['R0_exp'][0]) * _gamepad_param['R0_sense'][0]
return y
def _focus():
"""被写界深度を更新
グローバル変数 `_depth`, `_fnum`, `_fov`, `_delta`, `_blur` を参照して
ボケを演算し設定します。
"""
global _blur
global _fnum
NXSYS.SetGlobalCameraFOV(_fov[0])
if _aemode[0]:
_blur[0] = 2*(_aeparam['blurfin'][0]-_fov[0])/(_aeparam['blurfin'][0]-10.0)
_fnum[0] = _aeparam['ftg'][0]*(_fov[0]-10.0)+_aeparam['f10'][0]
d1 = _depth[0]**4
d2 = _fnum[0] * tan(_fov[0]*pi/180.0)**2 / 100.0
NXSYS.SetFocusParam(d1+_delta[0]*d2,d1-_delta[0]*d2,d1+_delta[1]*d2,d1-_delta[1]*d2, _blur[0])
def _refresh_trainlist():
"""編成リストを更新
分割・併合の発生後には実行したいけど,公開プロパティにしてません。どうしよう…。
"""
global _trainlist
_trainlist['obj'] = []
LAYOUT.ListTrain(_trainlist['obj'])
_trainlist['obj'] = [t for t in _trainlist['obj'] if not t.GetDummyMode()]
_trainlist['id'] = [t.GetID() for t in _trainlist['obj'] if not t.GetDummyMode()]
_trainlist['name'] = [t.GetNAME() for t in _trainlist['obj'] if not t.GetDummyMode()]
def _dispgui():
"""操作パネル"""
global _shakemode
global _fov
global _depth
global _fnum
global _trainlist
global _toruomode
global _tracking_car
global _tracking_trainid
global _tracking_trnlen
global _tracking_carnum
global _tracking_relative
global _tracking_af
global _fuzzytrack
global _aemode
global _aeparam
global _gcdist
global _gamepad_sw
IMGUI.Begin("ToruoWin", "撮る夫くん")
action = False
action += IMGUI.SliderFloat("zoom", "FOV(ズーム角度)", _fov, 10.0, 135.0)
action += IMGUI.SliderFloat("focus", "合焦中心", _depth, 0.1, 0.5)
action += IMGUI.SliderFloat("fnum", "絞り(F値)", _fnum, 2.0, 200.0)
action += IMGUI.SliderFloat("blur", "ぼけの強さ", _blur, 0.0, 2.0)
if action:
_focus()
del action
IMGUI.Separator()
if IMGUI.Checkbox("program_ae", "プログラムAE", _aemode):
_focus()
IMGUI.SameLine()
if IMGUI.Checkbox("shake", "手ブレモード", _shakemode):
setshakemode(_shakemode[0])
if IMGUI.TreeNode("childbtn", "保存済み撮る夫くん"):
if _toruos:
for i,d in enumerate(_toruos):
if IMGUI.RadioButton("toruo{}".format(i), "撮る夫くん {}".format(i), _childid, i):
jump_toruo(i)
# vrmapi.LOG('撮る夫くん {}'.format(i))
else:
IMGUI.Text("撮る夫くんは保存されていません。")
if IMGUI.Button("addtoruo", "現在視点を保存"):
_save_toruo()
IMGUI.SameLine()
if IMGUI.Button("deltoruo", "現在の撮る夫くんを削除"):
del _toruos[_childid[0]]
_save_config()
IMGUI.TreePop()
if IMGUI.TreeNode("target", "モード選択"):
IMGUI.RadioButton("toruomode0_normal", "ノーマル", _toruomode, 0)
IMGUI.SameLine()
IMGUI.RadioButton("toruomode1_track", "列車追尾モード", _toruomode, 1)
IMGUI.SameLine()
if IMGUI.RadioButton("toruomode2_follow", "追い撮り", _toruomode, 2):
if _tracking_car is None:
# 追尾対象車両が未指定の場合、操作対象編成を追尾対象編成にしようとする
d = LAYOUT.GetActive()
if d['type'] == 'train':
trn = d['object']
if trn.GetDirection() == 1:
i = 0
else:
i = -1
_tracking_trainid[0] = trn.GetID()
_tracking_car = trn.GetCarList()[i] # 追尾対象車両は、アクティブ編成の進行方向先頭車
else:
# 編成の取得に失敗してたら戻る
_toruomode[0] = 0
IMGUI.Checkbox("trackaf", "オートフォーカス", _tracking_af)
if IMGUI.TreeNode("targettrn", "対象の編成"):
if IMGUI.Button("trnlist", "編成リストを更新"):
_refresh_trainlist()
if IMGUI.RadioButton("notrack", "追尾なし", _tracking_trainid, 0):
_tracking_car = None
for i, tid in enumerate(_trainlist['id']):
if IMGUI.RadioButton("trn{}".format(tid), _trainlist['name'][i], _tracking_trainid, tid):
_tracking_carnum[0] = 1
_tracking_car = LAYOUT.GetTrain(_tracking_trainid[0]).GetCar(_tracking_carnum[0]-1)
_tracking_trnlen = _trainlist['obj'][i].GetNumberOfCars()
vrmapi.LOG("[撮る夫くん]追尾対象変更 {}".format(_trainlist['name'][i]))
if IMGUI.Checkbox("trackfuzzy", "ファジィ追尾", _fuzzytrack):
_tracking_carnum[0] = int(round(_tracking_carnum[0]))
IMGUI.TreePop()
if _tracking_trainid[0]:
if _fuzzytrack[0]:
if IMGUI.SliderFloat('carnofuzzy', "ファジィ号車番号", _tracking_carnum, 1.0, _tracking_trnlen):
_tracking_car = LAYOUT.GetTrain(_tracking_trainid[0]).GetCar(int(round(_tracking_carnum[0]))-1)
else:
if IMGUI.SliderInt("carno", "号車番号", _tracking_carnum, 1, _tracking_trnlen):
_tracking_car = LAYOUT.GetTrain(_tracking_trainid[0]).GetCar(_tracking_carnum[0]-1)
IMGUI.Text("車体長: {0:.1f}mm".format(vecdistance(_tracking_car.GetLinkPosition(0), _tracking_car.GetLinkPosition(1))))
IMGUI.SliderFloat("relx", "相対X", _tracking_relative['x'], -150.0, 150.0)
IMGUI.SliderFloat("rely", "相対Y", _tracking_relative['y'], -150.0, 150.0)
IMGUI.SliderFloat("relz", "相対Z", _tracking_relative['z'], -150.0, 150.0)
if _toruomode[0] == 1: # 追尾モード
IMGUI.SliderFloat("trdist", "追尾距離", _tracking_dist, 100.0, 2500.0) # 追尾モードだけ。追い撮りモードでは不使用
if _toruomode[0] == 2: # 追い撮りモード
if IMGUI.TreeNode('following_rel_pad', "追い撮りカメラ操作"):
IMGUI.SliderFloat('following_rel_r', "相対距離", _following_relpos['r'], 16.0, 1024.0)
IMGUI.SliderFloat('following_rel_theta', "水平角度", _following_relpos['theta'], -180.0, 180.0)
IMGUI.SliderFloat('following_rel_phi', "垂直角度", _following_relpos['phi'], -85.0, 85.0)
IMGUI.TreePop()
IMGUI.Text(str(_tracking_car))