diff --git a/sim_scenes/solar_system/halley_comet_sim.py b/sim_scenes/solar_system/halley_comet_sim.py index 1b169898432bd2905f1b9eac77c3db33c61199f3..6873552968d5b77633b3ab7f359960e243d8f324 100644 --- a/sim_scenes/solar_system/halley_comet_sim.py +++ b/sim_scenes/solar_system/halley_comet_sim.py @@ -165,8 +165,8 @@ class HalleyCometSim(HalleyCometSimBase): else: alpha = MAX_ALPHA # 修改彗星尾巴的透明度 - self.halley_comet.planet.comet_trail.alpha = alpha - self.halley_comet.planet.comet_sphere.alpha = alpha + self.halley_comet.planet.comet_trail.set_alpha(alpha) + # self.halley_comet.planet.comet_sphere.alpha = alpha def show_milestone_lable(self, last_trail, dt): """ @@ -370,7 +370,8 @@ class HalleyCometSim(HalleyCometSimBase): # 创建太阳系天体的真实轨迹(太阳和哈雷彗星除外) self.create_orbit_lines() # 创建信息显示面板 - self.text_panel = create_text_panel(font="fonts/sanjixiaozhuanti.ttf", font_scale=1.5) + # self.text_panel = create_text_panel(font="fonts/sanjixiaozhuanti.ttf", font_scale=1.5) + self.text_panel = create_text_panel(font="fonts/DroidSansFallback.ttf", font_scale=1.3) def on_timer_changed(self, time_data): """ diff --git a/simulators/ursina/entities/planet.py b/simulators/ursina/entities/planet.py index d2e834533535712d1decae3ab60072cd0c4a6851..940d5799b458cf65c964f9cd55086f5700e7128f 100644 --- a/simulators/ursina/entities/planet.py +++ b/simulators/ursina/entities/planet.py @@ -18,7 +18,7 @@ from common.color_utils import adjust_brightness, conv_to_vec4_color, get_invers from common.func import find_file from simulators.views.body_view import BodyView from simulators.ursina.ursina_mesh import create_sphere, create_torus, create_arrow_line, create_line, create_label, \ - create_cone, create_frustum + create_cone, create_frustum, create_comet_trail import math @@ -182,14 +182,19 @@ class Planet(Entity): create_rings(self) def create_comet_trail(self, comet_info): - comet_mesh = create_frustum(*comet_info) + comet_mesh = create_comet_trail(*comet_info) texture = find_texture("comet_trail.png") self.comet_trail = Entity(parent=self, model=comet_mesh, texture=texture, scale=20, position=(0, 0, 0), rotation=(0, 90, 90), double_sided=True, alpha=0.8) - self.comet_sphere = Entity(parent=self, model="sphere", texture="", color=color.white, scale=2.2, double_sided=True, alpha=0.6) + def set_alpha(alpha): + self.comet_trail.alpha = alpha + self.comet_sphere.alpha = alpha + + self.comet_trail.set_alpha = set_alpha + # 设置行星环不受灯光影响,否则看不清行星环 self.comet_trail.set_light_off() self.comet_sphere.set_light_off() diff --git a/simulators/ursina/ursina_mesh.py b/simulators/ursina/ursina_mesh.py index bc88e1fefaeeee5f195862ff00c8259444bf7e4d..00f3197f204a38c0c6fd4ddbcd0663c5b6997fca 100644 --- a/simulators/ursina/ursina_mesh.py +++ b/simulators/ursina/ursina_mesh.py @@ -116,7 +116,7 @@ def create_cone(radius, height, subdivisions, r=0.1): return Mesh(vertices=verts, triangles=tris, normals=normals, uvs=uvs, mode='triangle') -import math +# import math def create_frustum(bottom_radius, top_radius, height, subdivisions): @@ -172,6 +172,83 @@ def create_frustum(bottom_radius, top_radius, height, subdivisions): return Mesh(vertices=verts, triangles=tris, normals=normals, uvs=uvs, mode='triangle') + +def create_comet_trail(head_radius, trail_radius, height, subdivisions): + """ + 创建一个彗星尾巴 + @param head_radius: 圆台底部的半径 + @param trail_radius: 圆台顶部的半径 + @param height: 圆台的高度 + @param subdivisions: 细分数,用于控制圆台的光滑度 + @return: Mesh对象,表示创建的圆台 + """ + verts = [] # 顶点列表 + tris = [] # 三角面索引列表 + normals = [] # 法线列表 + uvs = [] # UV坐标列表 + + # for y in range(subdivisions + 1): + # for x in range(subdivisions + 1): + # x_segment = x / subdivisions + # y_segment = -y / subdivisions + # x_pos = cos(x_segment * 2 * pi) * sin(y_segment * pi) + # y_pos = cos(y_segment * pi) + # z_pos = sin(x_segment * 2 * pi) * sin(y_segment * pi) + # + # verts.append(Vec3(x_pos, y_pos, z_pos) * head_radius * 10) + # uvs.append(Vec2(x_segment, y_segment)) + # normals.append(Vec3(x_pos, y_pos, z_pos)) + # + # for y in range(subdivisions): + # for x in range(subdivisions): + # first = (y * (subdivisions + 1)) + x + # second = first + subdivisions + 1 + # # tris.append((first, second + 1, second)) + # # tris.append((first, first + 1, second + 1)) + # + # tris.append((second, second + 1, first)) + # tris.append((second + 1, first + 1, first)) + + # 生成圆台底部的顶点和UV坐标 + for i in range(subdivisions): + angle = 2 * math.pi * i / subdivisions + x = head_radius * math.cos(angle) + z = head_radius * math.sin(angle) + verts.append(Vec3(x, 0, z)) + uvs.append(Vec2(i / subdivisions, 0)) + + # 生成圆台顶部的顶点和UV坐标 + for i in range(subdivisions): + angle = 2 * math.pi * i / subdivisions + x = trail_radius * math.cos(angle) + z = trail_radius * math.sin(angle) + verts.append(Vec3(x, height, z)) + uvs.append(Vec2(i / subdivisions, 1)) + + # 生成圆台侧面的顶点、法线和三角面 + for i in range(subdivisions): + # 底部顶点索引 + bottom_index = i + # 顶部顶点索引 + top_index = i + subdivisions + # 下一个底部顶点索引 + next_bottom_index = (i + 1) % subdivisions + # 下一个顶部顶点索引 + next_top_index = (i + 1) % subdivisions + subdivisions + + # 侧面三角形 + tris.append((bottom_index, next_bottom_index, top_index)) + tris.append((next_bottom_index, next_top_index, top_index)) + + # 侧面法线 + normal = (verts[top_index] - verts[bottom_index]).cross( + verts[next_bottom_index] - verts[bottom_index]).normalized() + normals.append(normal) + normals.append(normal) + + return Mesh(vertices=verts, triangles=tris, normals=normals, uvs=uvs, mode='triangle') + + def create_cylinder(radius, height, subdivisions): """ 创建一个圆柱体