提交 c616a5bf 编写于 作者: 三月三net's avatar 三月三net

Python超人-宇宙模拟器

上级 faa5674c
# -*- coding:utf-8 -*-
# title :地月场景模拟
# description :地月场景模拟
# author :Python超人
# date :2023-02-11
# link :https://gitcode.net/pythoncr/
# python_version :3.8
# ==============================================================================
from bodies import Body, Sun, Earth, Moon
from common.consts import SECONDS_PER_HOUR, SECONDS_PER_HALF_DAY, SECONDS_PER_DAY, SECONDS_PER_WEEK, SECONDS_PER_MONTH
from sim_scenes.func import ursina_run, camera_look_at
from bodies.body import AU
from simulators.ursina.entities.entity_utils import create_directional_light
from simulators.ursina.ursina_event import UrsinaEvent
if __name__ == '__main__':
"""
地球、月球
"""
OFFSETTING = 0
# TODO: 可以抵消月球带动地球的力,保持地球在原地
# OFFSETTING = 0.01265
bodies = [
Earth(init_position=[0, 0, 0], texture="earth_hd.jpg",
init_velocity=[OFFSETTING, 0, 0], size_scale=0.5e1), # 地球放大 5 倍,距离保持不变
Moon(init_position=[0, 0, 363104], # 距地距离约: 363104 至 405696 km
init_velocity=[-1.03, 0, 0], size_scale=1e1) # 月球放大 10 倍,距离保持不变
]
from ursina.shaders import lit_with_shadows_shader
from ursina import Vec2, Vec4, Entity
shader = lit_with_shadows_shader
shader.default_input = {
'texture_scale': Vec2(1, 1),
'texture_offset': Vec2(0, 0),
'shadow_color': Vec4(0.1, 0.1, 0.1, .5),
}
Entity.default_shader = shader
def on_ready():
Entity.body = Entity(model="sphere", texture='../textures/transparent.png', y=0, x=0, z=0,scale=10)
# 运行前触发
# 运行开始前,将摄像机指向地球
earth = bodies[0]
moon = bodies[1]
# 摄像机看向地球
camera_look_at(earth)
e_pos = earth.planet.position
from ursina.lights import DirectionalLight
sun = DirectionalLight(shadow_map_resolution=(1024, 1024), position=earth.planet.position)
sun.look_at(moon.planet.position)
sun.rotation_x = 0
Entity.sun = sun
sun._light.show_frustum()
# 创建太阳光
shadows_shader = create_directional_light(position=(200, 0, -300), target=earth, shadows=True)
earth.planet.shadows = shadows_shader
moon.planet.shadows = shadows_shader
def on_timer_changed(time_data):
Entity.sun.update_bounds()
# Entity.sun.update_bounds(bodies[1].planet)
pass
UrsinaEvent.on_timer_changed_subscription(on_timer_changed)
UrsinaEvent.on_ready_subscription(on_ready)
# 使用 ursina 查看的运行效果
# 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹
# position = 左-右+、上+下-、前+后-
ursina_run(bodies, SECONDS_PER_DAY,
position=(-300000, 1500000, -1000),
show_timer=True,
show_trail=True)
from ursina import *
from ursina.shaders import lit_with_shadows_shader
from ursina.lights import DirectionalLight
from simulators.ursina.ursina_mesh import create_sphere
shader = lit_with_shadows_shader
shader.default_input = {
'texture_scale': Vec2(1, 1),
'texture_offset': Vec2(0, 0),
'shadow_color': Vec4(0.1, 0.1, 0.1, .5),
}
Entity.default_shader = shader
app = Ursina()
moon = Entity(model=create_sphere(0.5, 32), texture="../textures/moon.png", y=1, color=color.light_gray)
earth = Entity(model=create_sphere(0.5, 32), texture='../textures/earth.png', y=1, x=1, z=3)
e_pos = earth.position
sun = DirectionalLight(shadow_map_resolution=(1024, 1024), position=[e_pos[0], e_pos[1] + 1, e_pos[2] + 1])
sun.look_at(moon)
sun._light.show_frustum()
# light = PointLight(parent=earth, intensity=10, range=10, color=color.white)
def update():
moon.x += (held_keys['d'] - held_keys['a']) * time.dt
moon.y += (held_keys['e'] - held_keys['q']) * time.dt
moon.z += (held_keys['w'] - held_keys['s']) * time.dt
sun.update_bounds()
# scene.fog_density = (100, 500)
# scene.fog_color = color.orange
# scene.fog_density = (10, 50)
Sky(color=color.light_gray, texture="../textures/cosmic1.png")
EditorCamera()
app.run()
......@@ -35,10 +35,6 @@ if __name__ == '__main__':
moon = bodies[1]
# 摄像机看向地球
camera_look_at(earth)
# 创建太阳光
shadows_shader = create_directional_light(position=(200, 0, -300), target=earth,shadows=True)
earth.planet.shadows = shadows_shader
moon.planet.shadows = shadows_shader
UrsinaEvent.on_ready_subscription(on_ready)
......
......@@ -14,7 +14,7 @@ from simulators.ursina.ursina_event import UrsinaEvent
if __name__ == '__main__':
"""
球、月球
月场景模拟
"""
# 地球的Y方向初始速度
EARTH_INIT_VELOCITY = 0 # 0km/s
......
......@@ -169,6 +169,16 @@ def create_directional_light(position, target=None, shadows=False, light_color=N
@return:
"""
from ursina.shaders import lit_with_shadows_shader
from ursina import Vec2, Vec4, Entity
shader = lit_with_shadows_shader
shader.default_input = {
'texture_scale': Vec2(1, 1),
'texture_offset': Vec2(0, 0),
'shadow_color': Vec4(0.1, 0.1, 0.1, .5),
}
Entity.default_shader = shader
if shadows:
Entity.default_shader = lit_with_shadows_shader
if light_color is None:
......
......@@ -8,9 +8,10 @@
# ==============================================================================
from ursina import Entity, Grid, color
from simulators.ursina.ursina_mesh import create_arrow_line
from common.singleton import Singleton
class WorldGrid(Entity):
class WorldGrid(Singleton, Entity):
"""
创建一个宇宙网格对象
"""
......@@ -33,3 +34,5 @@ class WorldGrid(Entity):
grid.set_light_off()
# self.draw_axises()
if not hasattr(self, "instance"):
self.instance = self
......@@ -234,7 +234,7 @@ class UrsinaSimulator(Simulator):
from ursina import Sky
sky = Sky(texture=texture, scale=sky_scale)
Sky.instance = sky
# sky = SphereSky(texture=texture, scale=sky_scale)
sky.scale = sky_scale
camera.sky = sky
......@@ -320,7 +320,6 @@ class UrsinaSimulator(Simulator):
if self.show_timer:
self.create_timer()
EditorCamera(ignore_paused=True)
# 防止打开中文输入法
# self.switch_to_english_input_method()
# file: 指定音乐文件的路径
......@@ -347,6 +346,8 @@ class UrsinaSimulator(Simulator):
audio = Audio(bg_music, pitch=1, loop=True, autoplay=True)
audio.volume = 0.3
EditorCamera(ignore_paused=True)
if self.show_timer:
UrsinaEvent.on_reset()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册