diff --git a/sim_lab/comets_jupiter.py b/sim_lab/comets_jupiter.py new file mode 100644 index 0000000000000000000000000000000000000000..a3d339bc8663a45d86461379a194da63dee12f83 --- /dev/null +++ b/sim_lab/comets_jupiter.py @@ -0,0 +1,120 @@ +# -*- coding:utf-8 -*- +# title :彗木相撞模拟 +# description :天象奇观:彗木相撞模拟 +# author :Python超人 +# date :2023-11-28 +# link :https://gitcode.net/pythoncr/ +# python_version :3.8 +# ============================================================================== +from bodies import Jupiter, Body +from objs import RockSnow, Rock, create_rock +from common.consts import SECONDS_PER_HOUR, SECONDS_PER_HALF_DAY, SECONDS_PER_DAY +from sim_scenes.func import ursina_run, camera_look_at, two_bodies_colliding, create_sphere_sky +from sim_scenes.universe_sim_scenes import UniverseSimScenes +from simulators.ursina.entities.body_timer import TimeData +from simulators.ursina.entities.entity_utils import create_directional_light +from simulators.ursina.ursina_event import UrsinaEvent +import random + + +def create_comet(index, radius, gravity_only_for): + """ + 随机生成石头(随机位置、随机初始速度、随机大小、随机旋转) + @param index: 索引号 + @param radius: 木星的半径,保证生成的石头在木星半径外 + @param gravity_only_for: 指定一个天体,石头只对该天体引力有效 + @return: + """ + # 随机生成石头的位置和初始速度信息 + pos = [-radius * random.randint(120, 200) / 100, + -radius * random.randint(120, 200) / 1000, + -radius * random.randint(100, 300) / 100] + # 随机速度 + vel = [0, -random.randint(90, 200) / 30, -random.randint(0, 50)] + # 石头随机大小 + size_scale = random.randint(600, 1200) + # 随机创建石头 + rock = create_rock( + no=index % 8 + 1, name=f'岩石{index + 1}', mass=4.4e10, + size_scale=size_scale, color=(255, 200, 0), + init_position=pos, init_velocity=vel, gravity_only_for=[gravity_only_for], + ) + + # 给石头一个随机旋转的方向和值 + rock.rotation = [0, 0, 0] + rock.rotation[random.randint(0, 2)] = random.randint(90, 200) / 100 + return rock + + +if __name__ == '__main__': + """ + 彗木相撞模拟 + """ + # 木星在中心位置 + jupiter = Jupiter(init_position=[0, 0, 0], init_velocity=[0, 0, 0], + size_scale=1, texture="jupiter_hd.jpg") + + bodies = [jupiter] + comets = [] + + for i in range(50): + # 随机生成石头 + comet = create_comet(i, jupiter.radius, gravity_only_for=jupiter) + bodies.append(comet) + comets.append(comet) + + + def on_timer_changed(time_data: TimeData): + # 运行中,每时每刻都会触发 + for comet in comets: + if comet.visibled: + # 如果是否可见,则旋转石头 + comet.planet.rotation += comet.rotation + # 循环判断每个石头与木星是否相碰撞,如果相碰撞就爆炸 + if two_bodies_colliding(comet, jupiter): + # 将石头隐藏、设置引力无效后,展示爆炸效果 + comet.explode(jupiter) + + + def on_reset(): + """ + 当按键盘的 “O” 键重置后,恢复所有石头的状态(石头可见、引力有效),这样就可以反复观看 + @return: + """ + for comet in comets: + comet.set_visible(True) + comet.ignore_mass = False + + + def on_ready(): + # 运行前触发 + create_sphere_sky(scale=2000) + # 为了较好的立体效果,可以增加太阳光线,光线指向木星(target=jupiter) + create_directional_light(position=(200, 0, -300), light_num=3, target=jupiter) + # 摄像机看向木星 + camera_look_at(jupiter, rotation_z=0) + + + # 订阅事件后,上面3个函数功能才会起作用 + # 按键盘的 “O” 重置键会触发 on_reset + UrsinaEvent.on_reset_subscription(on_reset) + # 运行前会触发 on_ready + UrsinaEvent.on_ready_subscription(on_ready) + # 运行中,每时每刻都会触发 on_timer_changed + UrsinaEvent.on_timer_changed_subscription(on_timer_changed) + + UniverseSimScenes.set_window_size((1920, 1079), False) + + # 使用 ursina 查看的运行效果 + # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 + # position = 左-右+、上+下-、前+后- + ursina_run(bodies, SECONDS_PER_HOUR / 10, + position=(0, 0, -300000), + show_exit_button=False, + show_camera_info=False, + show_control_info=False, + show_grid=False, + timer_enabled=True, + cosmic_bg='', + # show_timer=True, + view_closely=0.001) diff --git a/sim_scenes/featured/__init__.py b/sim_scenes/featured/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/sim_scenes/featured/earth_3d.py b/sim_scenes/featured/earth_3d.py new file mode 100644 index 0000000000000000000000000000000000000000..68aa9651e86810b3de847f6f9b362d98017313fd --- /dev/null +++ b/sim_scenes/featured/earth_3d.py @@ -0,0 +1,74 @@ +# -*- coding:utf-8 -*- +# title :地球3D效果 +# description :地球3D效果 +# author :Python超人 +# date :2023-11-28 +# link :https://gitcode.net/pythoncr/ +# python_version :3.8 +# ============================================================================== +from bodies import Earth +from common.consts import SECONDS_PER_HOUR +from sim_scenes.func import ursina_run, create_sphere_sky +from sim_scenes.universe_sim_scenes import UniverseSimScenes +from simulators.ursina.ursina_event import UrsinaEvent + +if __name__ == '__main__': + """ + 地球3D效果(透视3D地球) + """ + # 创建带有云层的地球 + earth = Earth( + # texture="earth_hd.jpg", + texture="earth_hd_trans.png", + rotate_angle=-23.44, + init_position=[0, 0, 0], + init_velocity=[0, 0, 0], + size_scale=1) + # 创建云层(texture纹理图使用了透明云层的图片,云层的 size_scale 要稍微比地球大一点) + clouds = Earth(name="地球云层", texture="transparent_clouds.png", + rotate_angle=-23.44, + size_scale=1.001, parent=earth) + + bodies = [earth, clouds] + + # TODO: 开启3D摄像机 + from simulators.ursina.entities.camera3d import Camera3d + + # 3D摄像机初始化(两眼到鼻梁的距离为1000公里效果) + Camera3d.init(eye_distance=1000) + + + # 录制: + # sim_video_3d_cap.bat featured earth_3d earth_3d.mp4 + + def on_ready(): + # 运行前触发 + create_sphere_sky(scale=2000) + + + def on_timer_changed(time_data): + print(earth.planet.rotation_y) + + # 地球转了一圈(360)就停止,370是留些余量 + if abs(earth.planet.rotation_y) > 365: + exit(0) + + + UniverseSimScenes.set_window_size((1920, 1079), False) + + UrsinaEvent.on_ready_subscription(on_ready) + UrsinaEvent.on_timer_changed_subscription(on_timer_changed) + + # 使用 ursina 查看的运行效果 + # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 + # position = 左-右+、上+下-、前+后- + ursina_run(bodies, + # SECONDS_PER_HOUR / 2, + SECONDS_PER_HOUR, + position=(1.45 * earth.radius, 0, -30000), + cosmic_bg="", # 无背景(黑色) + show_grid=False, # 不显示网格 + show_camera_info=False, # 不显示摄像机信息 + show_control_info=False, # 不显示控制提示信息 + timer_enabled=True, + view_closely=0.001) # 近距离观看 view_closely=True或0.001 diff --git a/tools/sim_video_3d_cap_ext.py b/tools/sim_video_3d_cap_ext.py index f9161bff46e8f950f197e998a2456f730cc9a8bb..b5dc98ecabd6179329f39fc5af3a64d48212fa28 100644 --- a/tools/sim_video_3d_cap_ext.py +++ b/tools/sim_video_3d_cap_ext.py @@ -42,6 +42,8 @@ def get_args(): if args.start_index > 0: args.save_name = args.save_name + ("_%s" % args.start_index) print("save_name:", args.save_name) + if args.save_name == '': + args.save_name = 'video.mp4' return args @@ -295,7 +297,7 @@ def make_3d_video(): if img is None: return None _3d_card = img[4:20, 3:20, ] - _3d_card_p = _3d_card[10, 10, ] + _3d_card_p = _3d_card[10, 10,] index = int(_3d_card_p[1]) + int(_3d_card_p[0]) # if index in r_frames.keys() and index in l_frames.keys(): @@ -366,7 +368,7 @@ def make_3d_video(): clear_frame_temp_files() # crop('video.mp4') print("视频保存完成") - print(args.save_name) + print(os.path.abspath(args.save_name)) if __name__ == '__main__': diff --git a/tools/sim_video_cap.py b/tools/sim_video_cap.py index 9293c4c1290883369321093585e8eb58f8c607ff..58a658206240927d4adbd04a9908136f7db929f1 100644 --- a/tools/sim_video_cap.py +++ b/tools/sim_video_cap.py @@ -34,6 +34,8 @@ def get_args(): print("fps:", args.fps) print("save_name:", args.save_name) print("wait:", args.wait) + if args.save_name == '': + args.save_name = 'video.mp4' return args @@ -183,4 +185,4 @@ if __name__ == '__main__': cv2.destroyAllWindows() # crop('video.mp4') print("视频保存完成时间:", get_current_time()) - print(args.save_name) + print(os.path.abspath(args.save_name))