You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ursina第一人称相机下雾效异常问题求助

解决Ursina中水下雾效随视角/位置变化的问题

问题原因

你遇到的雾效不稳定问题,核心原因有两个:

  • 部分实体(比如地面)未使用支持雾效计算的Shader,导致不同物体的雾效逻辑不一致
  • 默认雾点绑定在场景原点而非玩家相机,移动或转向时雾的衰减基准点错位,引发透明度异常

修复步骤

  • 给所有实体添加雾效Shader:Ursina默认Shader不处理雾,需指定basic_lighting_shader或lit_shader,保证所有物体的雾效计算统一
  • 绑定雾点到玩家相机:通过update函数实时将雾的起始点设置为玩家相机位置,让雾始终以玩家为中心扩散
  • 切换为线性雾模式:线性雾的衰减更均匀,相比指数雾(fog_density)更适合模拟恒定的水下环境

修改后的完整代码

import panda3d.core
import ursina
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.shaders import basic_lighting_shader

app = Ursina()

random.seed(0)

# 给地面添加支持雾效的Shader
ground = Entity(
    model='cube',
    collider='box',
    scale=(64, 1, 64),
    texture='Textures/floor1.png',
    texture_scale=(4,4),
    shader=basic_lighting_shader
)
# 测试实体同步添加Shader
tester = Entity(
    model='sphere',
    collider='box',
    scale=2,
    texture="grass",
    shader=basic_lighting_shader
)

editor_camera = EditorCamera(enabled=False, ignore_paused=True)
player = FirstPersonController(model='cube', z=10, color=color.orange, origin_y=-.5, speed=8)
player.collider = BoxCollider(player, Vec3(0,1,0), Vec3(1,2,1))

Sky(color=color.black)

def update():
    # 实时将雾的起始点绑定到玩家相机位置
    scene.fog.set_linear_onset_point(player.camera_position)
    # 线性雾完全不透明的距离,可按需调整
    scene.fog.set_linear_falloff_point(30)

def input(key):
    if key == "g":
        print(scene.fog.getLinearOnsetPoint())
        print(scene.fog.get_linear_onset_point())

def pause_input(key):
    if key == "escape":
        quit()
    if key == 'tab':
        editor_camera.enabled = not editor_camera.enabled
        player.visible_self = editor_camera.enabled
        player.cursor.enabled = not editor_camera.enabled
        mouse.locked = not editor_camera.enabled
        editor_camera.position = player.position
        application.paused = editor_camera.enabled

# 设置线性雾模式,替代原指数雾
scene.fog_mode = 'linear'
scene.fog_color = color.blue
# 初始化雾的起止参数
scene.fog.set_linear_onset_point(player.camera_position)
scene.fog.set_linear_falloff_point(30)

pause_handler = Entity(ignore_paused=True, input=pause_input)

app.run()

额外优化提示

  • 若要增强水下真实感,可将fog_color调整为深青蓝色,或给相机添加post_processing滤镜模拟水下色彩偏移
  • 线性雾的falloff_point值越小,雾浓度越高;数值越大,雾的影响范围越广

内容的提问来源于stack exchange,提问作者clodon2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 09:45:46