From c9bec8930aea9f333e90554f2e40b97c9d83b65a Mon Sep 17 00:00:00 2001 From: march3 Date: Fri, 2 Aug 2024 12:12:38 +0800 Subject: [PATCH] =?UTF-8?q?Python=E8=B6=85=E4=BA=BA-=E5=AE=87=E5=AE=99?= =?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 --- bodies/fixed_stars/fixed_star.py | 6 +-- sim_scenes/funny/china_earth.py | 81 ++++++++++++++++++++++++++++ sim_scenes/funny/utils/body_utils.py | 4 +- 3 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 sim_scenes/funny/china_earth.py diff --git a/bodies/fixed_stars/fixed_star.py b/bodies/fixed_stars/fixed_star.py index 4399439..004fa9d 100644 --- a/bodies/fixed_stars/fixed_star.py +++ b/bodies/fixed_stars/fixed_star.py @@ -74,9 +74,9 @@ class FixedStar(Body): # save_file = os.path.join(temp_dir, "%s_%s.png" % (texture_name, "_".join([str(i) for i in list(self.color)]))) save_file = os.path.join(temp_dir, "%s_%s.png" % (texture_name, str(self.__class__.__name__).lower())) - - if os.path.exists(save_file): - return save_file + if self.__class__ is not FixedStar: + if os.path.exists(save_file): + return save_file fixed_star_img = os.path.join(texture_path, texture) gen_fixed_star_texture(self.color, diff --git a/sim_scenes/funny/china_earth.py b/sim_scenes/funny/china_earth.py new file mode 100644 index 0000000..16c4391 --- /dev/null +++ b/sim_scenes/funny/china_earth.py @@ -0,0 +1,81 @@ +# -*- coding:utf-8 -*- +# title :组成文本的星球群(自定义星球) +# description :组成文本的星球群(自定义星球) +# author :Python超人 +# date :2023-03-26 +# link :https://gitcode.net/pythoncr/ +# python_version :3.8 +# ============================================================================== +from bodies import FixedStar, ColorBody, Earth, Sun +from common.consts import SECONDS_PER_HOUR, SECONDS_PER_DAY, SECONDS_PER_WEEK +from sim_scenes.func import ursina_run, create_sphere_sky +from sim_scenes.funny.utils.body_utils import gen_bodies_from_image +from simulators.ursina.ursina_event import UrsinaEvent + +D = 6000 # 基本距离单位:km(随意赋值) + + +def create_earth(name, texture, scale): + earth = Earth(name=name, texture=texture, rotate_angle=-23.44, + mass=2e32, size_scale=5 * scale, + init_position=[-450 * D, 100 * D, 6000 * D], # [ 左-右+, 上+下-, 远+近- ] + init_velocity=[0, 0, 0], + ignore_mass=True) + return earth + + +def show_text_bodies(): + """ + 显示图片文本的星球群 + """ + + # 观看摄像机的位置 + # camera_pos = (左-右+, 上+下-, 前+后-) + camera_pos = (D, D, -3000 * D) + # 根据 pixel_image 指定图片生成有色星球(注意:图片的像素不要太多) + bodies = gen_bodies_from_image( + pixel_image="./images/ilovechina.png", + texture="color_moon.png", + params={"camera_pos": camera_pos}, light_disable=False) + + # 放一个恒星作为背景 + # TODO: ignore_mass=True + # 注意:这里的算法是基于牛顿的万有引力(质量为0不受引力的影响在天体物理学中是不严谨) + sun = FixedStar(init_position=[camera_pos[0], camera_pos[1], camera_pos[2] * 30], + texture="fixed_star.png", color=(255, 255, 225), size_scale=20) + # glows = (glow_num:10, glow_scale:1.03 glow_alpha:0.1~1 glow_color_min_val) + sun.glows = (10, 1.05, 0.1) + bodies.append(sun) + + earth = create_earth(name="背景地球", texture="earth-huge.jpg", scale=1) + earth_cn = create_earth(name="背景地球", texture="earth-huge-cn-flag.png", scale=1.01) + earth_clouds = create_earth(name="地球云层", texture="transparent_clouds.png", scale=1.005) + + bodies.append(earth) + bodies.append(earth_cn) + bodies.append(earth_clouds) + + def on_ready(): + # from simulators.ursina.entities.entity_utils import create_directional_light + # # 运行前触发 + # # 为了较好的立体效果,可以增加太阳光线,光线指向木星(target=jupiter) + # create_directional_light(position=(200, 0, -300), light_num=3, target=earth) + from ursina import camera + # camera.fov = 40 + sky = create_sphere_sky(scale=800000) + sky.rotation_y = 180 + # sky.rotation_x = 20 + # sky.rotation_z = -65 + + UrsinaEvent.on_ready_subscription(on_ready) + # 使用 ursina 查看的运行效果 + # 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹 + # position = camera_pos = (左-右+, 上+下-, 前+后-) + ursina_run(bodies, SECONDS_PER_HOUR, + cosmic_bg='', + show_grid=False, + position=camera_pos, view_closely=True) + + +if __name__ == '__main__': + show_text_bodies() diff --git a/sim_scenes/funny/utils/body_utils.py b/sim_scenes/funny/utils/body_utils.py index 0b0dd06..6b3fde0 100644 --- a/sim_scenes/funny/utils/body_utils.py +++ b/sim_scenes/funny/utils/body_utils.py @@ -29,7 +29,7 @@ def get_scaled_body_pos(camera_pos, body_pos, scale_factor): return new_pos -def gen_bodies_from_image(pixel_image, params, texture="color_body.png"): +def gen_bodies_from_image(pixel_image, params, texture="color_body.png", light_disable=True): """ 根据像素图片以及参数,自动生成星球,注意图片像素不能太多,否则会导致电脑运行太慢 @param pixel_image: @@ -63,7 +63,7 @@ def gen_bodies_from_image(pixel_image, params, texture="color_body.png"): interval_factor = 20 # 星球间距因子 body_template = 'ColorBody(name="%s", mass=mass, color=(%d, %d, %d), size_scale=%.4f, ' \ 'init_position=get_position([-%g * D, %g * D, 0], %.4f), ' \ - f'init_velocity=[0, 0, 0], ignore_mass=True, texture="{texture}").set_light_disable(True)' + f'init_velocity=[0, 0, 0], ignore_mass=True, texture="{texture}").set_light_disable({light_disable})' bodies_str = "[" # 以图片像素为坐标,对角线的距离 -- GitLab