From 560fc9505877c53adc811de5d689947afccbf89b Mon Sep 17 00:00:00 2001 From: march3 Date: Fri, 17 Mar 2023 13:56:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=AA=E9=98=B3=E7=B3=BB=E4=B8=89=E4=BD=93?= =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scenes/func.py | 25 ++++++++++++++++++++++++- simulators/ursina/ursina_ui.py | 33 +++++++++++++++++++++++++++++---- simulators/ursina_simulator.py | 8 ++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/scenes/func.py b/scenes/func.py index 081c840..bba9931 100644 --- a/scenes/func.py +++ b/scenes/func.py @@ -66,12 +66,35 @@ def ursina_run(bodies, """ from simulators.ursina_simulator import UrsinaSimulator, UrsinaPlayer - from ursina import application, Sequence + from ursina import application, Sequence, camera, held_keys, time, clamp,Entity,Text body_sys = System(bodies) simulator = UrsinaSimulator(body_sys) player = UrsinaPlayer(position, simulator.ursina_views) + # # 创建一个实体(在屏幕中央)和一个摄像机 + # TODO: 未使用 + # entity = Entity(model='cube', position=(0, 0, 5), scale=2) + # camera = Camera() + # + # # 设置初始的 FOV 值(默认值为 90) + # camera.fov = 60 + # + # # 创建一个用于显示当前 FOV 值的文本 + # fov_text = Text(text=f'FOV: {camera.fov}', position=(-0.5, 0.4), scale=2) + # # 每一帧更新摄像机 FOV 值 + # def update(): + # # 通过鼠标滚轮来调整 FOV 值 + # camera.fov -= held_keys['scroll'] * 10 * time.dt + # # 限制 FOV 值的范围(1 到 120 之间) + # camera.fov = clamp(camera.fov, 1, 120) + # # 更新文本内容 + # fov_text.text = f'FOV: {camera.fov:.2f}' # 保留两位小数 + # + # # 将摄像机移到实体旁边,并对着它 + # camera.position = entity.position + (0, 0, -5) + # camera.look_at(entity.position) + def callback_update(): for ursina_view in simulator.ursina_views: simulator.check_and_evolve() diff --git a/simulators/ursina/ursina_ui.py b/simulators/ursina/ursina_ui.py index f52aab7..7d166c0 100644 --- a/simulators/ursina/ursina_ui.py +++ b/simulators/ursina/ursina_ui.py @@ -7,9 +7,10 @@ # python_version :3.8 # ============================================================================== from ursina import Ursina, window, Entity, Grid, Mesh, camera, Text, application, color, mouse, Vec2, Vec3, \ - load_texture, held_keys, Button, ButtonList, destroy + load_texture, held_keys, Button, ButtonList, destroy, scene, distance from ursina.prefabs.first_person_controller import FirstPersonController +from common.consts import AU from simulators.ursina.ui_component import UiSlider, SwithButton, UiButton from simulators.ursina.ursina_config import UrsinaConfig from simulators.ursina.ursina_event import UrsinaEvent @@ -128,23 +129,45 @@ class UrsinaUI: def bodies_button_list_click(self, item): if item is not None: - print("select->", item) + # TODO: 先找到位置,确定摄像机的位置 + # print("select->", item) + # UrsinaConfig.SCALE_FACTOR + x = item.planet.scale_x * 10 + camera.position = item.planet.position + Vec3(-x, 0, 0) + camera.look_at(item.planet) + camera.rotation = (0, 90, 0) + # camera.forward = (1, 0, 0) # 设置相机的方向向量为x轴方向 destroy(self.bodies_button_list) + # my_entity = Entity(model='cube', color=color.red, position=(0, 1, 5)) + # + # # 获取当前摄像机 + # camera = scene.camera + # + # # 计算 Entity 和摄像机之间的距离 + # distance_to_entity = distance(my_entity, camera) + # + # print('距离:', distance_to_entity) + def on_searching_bodies_click(self): results = UrsinaEvent.on_searching_bodies() if len(results) > 0: sub_name, bodies = results[0] # print(results[0]) button_dict = {"[关闭]": lambda: self.bodies_button_list_click(None)} - + camera = scene.camera for body in bodies: def callback_action(b=body): self.bodies_button_list_click(b) - button_dict[body.name] = callback_action + distance_to_entity = distance(body.planet, camera) + d = distance_to_entity / UrsinaConfig.SCALE_FACTOR / AU + name = f"{body.name}\t距离:{d:.4f}天文单位" + button_dict[name] = callback_action + if hasattr(self, "bodies_button_list"): + destroy(self.bodies_button_list) self.bodies_button_list = ButtonList(button_dict, font=UrsinaConfig.CN_FONT, button_height=1.5) # self.bodies_button_list.input = self.bodies_button_list_input @@ -205,6 +228,8 @@ class UrsinaUI: # print(key) elif key == 'space': self.wp.enabled = not self.wp.enabled + elif key == 'left mouse down': + print(key) # application.paused = not application.paused # Pause/unpause the game. # elif key == 'tab': # # application.time_scale 属性控制游戏时间流逝的速度。 diff --git a/simulators/ursina_simulator.py b/simulators/ursina_simulator.py index bc0d69d..35e1813 100644 --- a/simulators/ursina_simulator.py +++ b/simulators/ursina_simulator.py @@ -155,6 +155,8 @@ class UrsinaSimulator(Simulator): return lights def run(self, dt, **kwargs): + + window.title = '宇宙模拟器' # 设定时间间隔为0.01秒 interval = 0.01 self.evolve_dt = dt * interval @@ -181,6 +183,12 @@ class UrsinaSimulator(Simulator): if cosmic_bg is not None and os.path.exists(cosmic_bg): self.cosmic_background(cosmic_bg) + # 设置 camera 的裁剪面和位置 + camera.clip_plane_near = 0.1 + # camera.clip_plane_far = 1000 + # camera.position = (0, 10, -20) + # camera.rotation_x = -30 + ui = UrsinaUI() EditorCamera(ignore_paused=True) -- GitLab