From 58a6255b6b9dd5b86287e0b5edb45c742597cc8c Mon Sep 17 00:00:00 2001 From: march3 Date: Fri, 4 Aug 2023 17:03:37 +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 --- objs/water_drop.py | 49 +++++++++++++++++++ .../solar_system/solar_system_reality.py | 3 ++ sim_scenes/tri_bodies/water_drop_01.py | 34 ++++++++----- simulators/ursina/ui/ui_panel.py | 4 +- 4 files changed, 75 insertions(+), 15 deletions(-) diff --git a/objs/water_drop.py b/objs/water_drop.py index 0600d75..294c84e 100644 --- a/objs/water_drop.py +++ b/objs/water_drop.py @@ -43,8 +43,56 @@ class WaterDrop(Obj): } super().__init__(**params) +from ursina.prefabs.primitives import Shader + +matcap_shader = Shader(name='matcap_shader', language=Shader.GLSL, vertex = '''#version 140 +uniform mat4 p3d_ModelViewProjectionMatrix; +uniform mat4 p3d_ModelMatrix; +uniform mat4 p3d_ModelViewMatrix; +uniform mat3 p3d_NormalMatrix; +in vec4 p3d_Vertex; +in vec3 p3d_Normal; + +out vec3 eye; +out vec3 view_normal; +// reflect alternative: +// r = e - 2. * dot( n, e ) * n; + +void main() { + gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex; + + eye = normalize(vec3(p3d_ModelViewMatrix * vec4(p3d_Vertex.xyz, 1.0))); + view_normal = normalize( p3d_NormalMatrix * p3d_Normal ); +} +''', +fragment=''' +#version 130 +uniform sampler2D p3d_Texture0; +uniform vec4 p3d_ColorScale; + +in vec3 eye; +in vec3 view_normal; +out vec4 fragColor; + +void main() { + + vec3 r = reflect( eye, view_normal ); + float m = 2. * sqrt( pow( r.x, 2. ) + pow( r.y, 2. ) + pow( r.z + 1., 2. ) ); + vec2 vN = r.xy / m + .5; + + vec3 base = texture2D( p3d_Texture0, vN ).rgb; + // vec3 base = texture2D( p3d_Texture0, uv ).rgb; + fragColor = vec4( base, 1. ) * p3d_ColorScale; +} + +''', +) if __name__ == '__main__': + + + shader = matcap_shader + water_drop = WaterDrop( # texture="drops_normal.png" # texture="drops_uvw.png" @@ -56,6 +104,7 @@ if __name__ == '__main__': def on_ready(): water_drop.planet.rotation_x = 90 + water_drop.planet.shader = shader, water_drop.show_demo(size_scale=1000000, on_ready_fun=on_ready) diff --git a/sim_scenes/solar_system/solar_system_reality.py b/sim_scenes/solar_system/solar_system_reality.py index 5e22cf0..470bf4f 100644 --- a/sim_scenes/solar_system/solar_system_reality.py +++ b/sim_scenes/solar_system/solar_system_reality.py @@ -250,6 +250,8 @@ if __name__ == '__main__': # print(time_data.get_datetime(str(current_time))) ControlUI.current_ui.show_message(dt.strftime('%Y-%m-%d %H:%M:%S'), + position=(0.60, -0.465), + origin=(-0.5, 0.5), font="verdana.ttf", close_time=-1) @@ -267,4 +269,5 @@ if __name__ == '__main__': position=(0, 0.2 * AU, -3 * AU), gravity_works=False, # 关闭万有引力的计算 show_grid=False, + show_camera_info=False, timer_enabled=True) diff --git a/sim_scenes/tri_bodies/water_drop_01.py b/sim_scenes/tri_bodies/water_drop_01.py index 2e01b6d..4996505 100644 --- a/sim_scenes/tri_bodies/water_drop_01.py +++ b/sim_scenes/tri_bodies/water_drop_01.py @@ -16,6 +16,7 @@ from simulators.ursina.entities.body_timer import TimeData from simulators.ursina.entities.entity_utils import create_directional_light from simulators.ursina.ursina_config import UrsinaConfig from simulators.ursina.ursina_event import UrsinaEvent +from ursina import camera if __name__ == '__main__': """ @@ -45,7 +46,9 @@ if __name__ == '__main__': texture="drops_bright.png", # trail_color=[200, 200, 255], init_velocity=[-WATER_SPEED, 0, 0], - size_scale=4e4).set_ignore_gravity(True).set_light_disable(True) + # size_scale=4e4, + size_scale=1e3 + ).set_ignore_gravity(True).set_light_disable(True) # moon = Moon(init_position=[0, 0, 363104], # 距地距离约: 363104 至 405696 km # init_velocity=[-1.03, 0, 0], size_scale=2e1) # 月球放大 10 倍,距离保持不变 # moon.set_light_disable(True) @@ -88,15 +91,18 @@ if __name__ == '__main__': def on_timer_changed(time_data: TimeData): - if time_data.total_days > 27.5: - exit(0) - if water_drop.position[0] < -WATER_RANGE: - water_drop.planet.rotation_z = -90 - water_drop.velocity = [WATER_SPEED, 0, 0] - elif water_drop.position[0] > WATER_RANGE: - water_drop.planet.rotation_z = 90 - water_drop.velocity = [-WATER_SPEED, 0, 0] - # camera_look_at(water_drop, rotation_z=0) + if time_data.total_days > 0.2: + if water_drop.position[0] < -WATER_RANGE: + water_drop.planet.rotation_z = -90 + water_drop.velocity = [WATER_SPEED, 0, 0] + elif water_drop.position[0] > WATER_RANGE: + water_drop.planet.rotation_z = 90 + water_drop.velocity = [-WATER_SPEED, 0, 0] + else: + water_drop.acceleration = [-9.8e-4, 0, 0] + + camera_look_at(water_drop, rotation_z=0) + # camera.y += UrsinaConfig.SCALE_FACTOR * 100 def on_ready(): @@ -110,8 +116,8 @@ if __name__ == '__main__': water_drop.planet.rotation_z = 90 - # water_drop.init_position = (0, 0, 0) - # water_drop.init_velocity = [0, 0, 0] + water_drop.init_position = (0, 0, 0) + water_drop.init_velocity = [0, 0, 0] # 订阅事件后,上面2个函数功能才会起作用 @@ -127,11 +133,13 @@ if __name__ == '__main__': SECONDS_PER_DAY / 24, # SECONDS_PER_WEEK * 4, # position=(0, 0, -220000), - position=(0, 0, 0), + position=(0, 0, -20000), + # position=(0, 0, 0), show_grid=False, # cosmic_bg="", # gravity_works=False, # save_cube_map=True, + show_timer=True, timer_enabled=True, show_camera_info=False, show_control_info=False, diff --git a/simulators/ursina/ui/ui_panel.py b/simulators/ursina/ui/ui_panel.py index 4faf556..cca8e0f 100644 --- a/simulators/ursina/ui/ui_panel.py +++ b/simulators/ursina/ui/ui_panel.py @@ -63,7 +63,7 @@ class UiPanel(WindowPanel): """ pass - def show_message(self, message, font=None, close_time=3): + def show_message(self, message, font=None, origin=(0, 0), position=(0, .25), close_time=3): """ 显示消息框 @param message: 消息内容 @@ -78,7 +78,7 @@ class UiPanel(WindowPanel): # else: # font = None # 创建消息框 - message_box = Text(text=message, font=font, background=True, origin=(0, 0), y=.25) + message_box = Text(text=message, font=font, background=True, origin=origin, position=position) self.last_message_box = message_box -- GitLab