Ursina-自定义模型拒绝纹理

发布于 2025-02-03 10:22:48 字数 4421 浏览 3 评论 0原文

我在Python 3.8中使用Ursina,

我有一个为实体制作的自定义模型。我对该模型有一个纹理,从我的3D建模工具导出为.png图像。我尝试使用这样的纹理: entity(model ='monolith',比例= 0.2,name ='monolith',position =(50,0,100),collider ='网格',color = color.dark_gray,texture ='/models/monolith')

在游戏中,模型只是黑色。我尝试使用不同的模型,相同的结果尝试了这一点 - 自定义模型没有任何东西显示其纹理。

如何让Ursina纹理我的自定义模型?

对于这个具体示例,我正在使用.OBJ文件,但是我已经看到了.fbx,.glb和.stl 3D对象

如果有帮助,我用

完成代码:

from classes.constants import *
fsmode = getinput("Fullscreen? [Y/N] > ")

if (fsmode == "Y"):fsmode = True
else:fsmode = False

import time as systime, socket, socketserver, random
from ursina import *
import ursina.audio as audio
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.prefabs.health_bar import HealthBar
from ursina.shaders import *
from threading import Thread as thread

from classes.class_override import *
from classes.custom_classes import *

grav=1
player_model = "/models/player"
pcolor = color.rgb(185, 185, 135)
remoteplayer_color = color.rgb(145, 185, 145)
remoteplayer_disconnected = color.rgb(25,25,25,25)
stored_camera_y = 0
duty_cycle=0
remote_player_connected = False
r_player_justconnected = False
paused = False
block_outgoing_updates = False

app = Ursina(development_mode=False, show_ursina_splash_screen=True,fullscreen=fsmode,title="Reality Simulator (now with Portals!)")

random.seed(0)
Entity.default_shader = lit_with_shadows_shader

ground = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground")
ground2 = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground", position=(128,0,128))
ground_brick = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground", position=(0,0,128))
ground_brick2 = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground", position=(128,0,0))
spawning_platform = Entity(model='cube', collider='box', scale=(3,0.5,3), texture='brick', position=(0,0,-10))

editor_camera = EditorCamera(enabled=False, ignore_paused=True, move_speed=30)

player = FirstPersonController(model=player_model, z=-10, y=0.5, color=pcolor, origin_y=0.12, gravity=grav, speed=8, jump_height = 4, height=3, collider='mesh', name="player", cursor=Entity(parent=camera.ui, model='quad', color=color.pink, scale=.001, rotation_z=45))
# player_healthbar = HealthBar(max_value=player.max_hp, animation_duration=0.1, value=player.max_hp, parent=camera.ui)
p2 = Entity(model=player_model, color=remoteplayer_disconnected, scale=1, name='remote_player', x=-5,y=0,z=-5, rotation_y=180)
# sim_symbol = Entity(model='diamond', collider='mesh', color=color.green, name='sim_symbol', parent=player, position=(0,6,0), scale=(.5,1,.5), visible=False)
player.visible_self = False
# player = FirstPersonController(model=player_model, z=-10, color=color.orange, origin_y=-.5, speed=8)
# player.collider = MeshCollider(player, Vec3(0,1,0), Vec3(1,2,1))

arm = Entity(model='portal_gun_2', parent=camera, position=(.5,-.25,0.45), scale=.004, origin_z=-.5, rotation_z=0, on_cooldown=False, name="gun", color=pcolor, texture='/textures/w_portalgun.png')
portal_gun_showcase = Entity(model=('portal_gun.fbx'), position=(25,2,-5), scale=.05, origin_z=-.5, rotation_z=0, on_cooldown=False, name="pgun_showcase", collider='box', texture=('/textures/w_portalgun.png'))

def genstairs():
    stairs(128,0.3,7,30)

thread(name="stair_gen", target=genstairs).start()

def update():
    global player, stored_camera_y, duty_cycle
    duty_cycle += 1

    if (block_outgoing_updates):
        arm.color=color.rgb(25,25,25,25)
    else:
        arm.color=pcolor

    if held_keys['escape']:
        os.abort()

    if held_keys['x']:
        player.y += 10

    if (held_keys['left shift'] and not held_keys['control']):
        player.speed = player.sprint_speed
    elif (player.speed == player.sprint_speed):player.speed=8

    if player.get_position()[1] < -20:
        prot = player.rotation
        destroy(player)
        player = FirstPersonController(model=player_model, z=-10, color=pcolor, speed=8, rotation=prot, origin_y=0.12, gravity=grav, jump_height = 4, height=3, collider='mesh', name=player_model)
        player.visible_self = False
        print("info: player reset")

mouse.visible = False
mouse.locked = True
app.run()

I'm using Ursina in Python 3.8

I have a custom model that I made for an entity. I have a texture for that model, exported from my 3D modeling tool as a .png image. I tried applying the texture like this:
Entity(model='monolith', scale=0.2, name='monolith', position=(50,0,100), collider='mesh', color=color.dark_gray, texture='/models/monolith')

In-game, the model is just solid black. I tried this with a different model, same result - nothing with a custom model is showing its texture.

How can I get Ursina to texture my custom models?

For this specific example, I'm using a .obj file, but I've seen this issue with .fbx, .glb, and .stl 3D objects
If it helps to know, I made my 3D model with this online tool.

Complete code:

from classes.constants import *
fsmode = getinput("Fullscreen? [Y/N] > ")

if (fsmode == "Y"):fsmode = True
else:fsmode = False

import time as systime, socket, socketserver, random
from ursina import *
import ursina.audio as audio
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.prefabs.health_bar import HealthBar
from ursina.shaders import *
from threading import Thread as thread

from classes.class_override import *
from classes.custom_classes import *

grav=1
player_model = "/models/player"
pcolor = color.rgb(185, 185, 135)
remoteplayer_color = color.rgb(145, 185, 145)
remoteplayer_disconnected = color.rgb(25,25,25,25)
stored_camera_y = 0
duty_cycle=0
remote_player_connected = False
r_player_justconnected = False
paused = False
block_outgoing_updates = False

app = Ursina(development_mode=False, show_ursina_splash_screen=True,fullscreen=fsmode,title="Reality Simulator (now with Portals!)")

random.seed(0)
Entity.default_shader = lit_with_shadows_shader

ground = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground")
ground2 = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground", position=(128,0,128))
ground_brick = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground", position=(0,0,128))
ground_brick2 = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground", position=(128,0,0))
spawning_platform = Entity(model='cube', collider='box', scale=(3,0.5,3), texture='brick', position=(0,0,-10))

editor_camera = EditorCamera(enabled=False, ignore_paused=True, move_speed=30)

player = FirstPersonController(model=player_model, z=-10, y=0.5, color=pcolor, origin_y=0.12, gravity=grav, speed=8, jump_height = 4, height=3, collider='mesh', name="player", cursor=Entity(parent=camera.ui, model='quad', color=color.pink, scale=.001, rotation_z=45))
# player_healthbar = HealthBar(max_value=player.max_hp, animation_duration=0.1, value=player.max_hp, parent=camera.ui)
p2 = Entity(model=player_model, color=remoteplayer_disconnected, scale=1, name='remote_player', x=-5,y=0,z=-5, rotation_y=180)
# sim_symbol = Entity(model='diamond', collider='mesh', color=color.green, name='sim_symbol', parent=player, position=(0,6,0), scale=(.5,1,.5), visible=False)
player.visible_self = False
# player = FirstPersonController(model=player_model, z=-10, color=color.orange, origin_y=-.5, speed=8)
# player.collider = MeshCollider(player, Vec3(0,1,0), Vec3(1,2,1))

arm = Entity(model='portal_gun_2', parent=camera, position=(.5,-.25,0.45), scale=.004, origin_z=-.5, rotation_z=0, on_cooldown=False, name="gun", color=pcolor, texture='/textures/w_portalgun.png')
portal_gun_showcase = Entity(model=('portal_gun.fbx'), position=(25,2,-5), scale=.05, origin_z=-.5, rotation_z=0, on_cooldown=False, name="pgun_showcase", collider='box', texture=('/textures/w_portalgun.png'))

def genstairs():
    stairs(128,0.3,7,30)

thread(name="stair_gen", target=genstairs).start()

def update():
    global player, stored_camera_y, duty_cycle
    duty_cycle += 1

    if (block_outgoing_updates):
        arm.color=color.rgb(25,25,25,25)
    else:
        arm.color=pcolor

    if held_keys['escape']:
        os.abort()

    if held_keys['x']:
        player.y += 10

    if (held_keys['left shift'] and not held_keys['control']):
        player.speed = player.sprint_speed
    elif (player.speed == player.sprint_speed):player.speed=8

    if player.get_position()[1] < -20:
        prot = player.rotation
        destroy(player)
        player = FirstPersonController(model=player_model, z=-10, color=pcolor, speed=8, rotation=prot, origin_y=0.12, gravity=grav, jump_height = 4, height=3, collider='mesh', name=player_model)
        player.visible_self = False
        print("info: player reset")

mouse.visible = False
mouse.locked = True
app.run()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫轩蝶泪 2025-02-10 10:22:48

您可以尝试entity.flip_faces()在Ursina中添加此代码后,您的代码将看起来像这样:

from classes.constants import *
fsmode = getinput("Fullscreen? [Y/N] > ")

if (fsmode == "Y"):fsmode = True
else:fsmode = False

import time as systime, socket, socketserver, random
from ursina import *
import ursina.audio as audio
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.prefabs.health_bar import HealthBar
from ursina.shaders import *
from threading import Thread as thread

from classes.class_override import *
from classes.custom_classes import *

grav=1
player_model = "/models/player"
pcolor = color.rgb(185, 185, 135)
remoteplayer_color = color.rgb(145, 185, 145)
remoteplayer_disconnected = color.rgb(25,25,25,25)
stored_camera_y = 0
duty_cycle=0
remote_player_connected = False
r_player_justconnected = False
paused = False
block_outgoing_updates = False

app = Ursina(development_mode=False, show_ursina_splash_screen=True,fullscreen=fsmode,title="Reality Simulator (now with Portals!)")

random.seed(0)
Entity.default_shader = lit_with_shadows_shader

ground = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground")
ground2 = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground", position=(128,0,128))
ground_brick = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground", position=(0,0,128))
ground_brick2 = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground", position=(128,0,0))
spawning_platform = Entity(model='cube', collider='box', scale=(3,0.5,3), texture='brick', position=(0,0,-10))

editor_camera = EditorCamera(enabled=False, ignore_paused=True, move_speed=30)

player = FirstPersonController(model=player_model, z=-10, y=0.5, color=pcolor, origin_y=0.12, gravity=grav, speed=8, jump_height = 4, height=3, collider='mesh', name="player", cursor=Entity(parent=camera.ui, model='quad', color=color.pink, scale=.001, rotation_z=45))
# player_healthbar = HealthBar(max_value=player.max_hp, animation_duration=0.1, value=player.max_hp, parent=camera.ui)
p2 = Entity(model=player_model, color=remoteplayer_disconnected, scale=1, name='remote_player', x=-5,y=0,z=-5, rotation_y=180)
# sim_symbol = Entity(model='diamond', collider='mesh', color=color.green, name='sim_symbol', parent=player, position=(0,6,0), scale=(.5,1,.5), visible=False)
player.visible_self = False
# player = FirstPersonController(model=player_model, z=-10, color=color.orange, origin_y=-.5, speed=8)
# player.collider = MeshCollider(player, Vec3(0,1,0), Vec3(1,2,1))

arm = Entity(model='portal_gun_2', parent=camera, position=(.5,-.25,0.45), scale=.004, origin_z=-.5, rotation_z=0, on_cooldown=False, name="gun", color=pcolor, texture='/textures/w_portalgun.png')
portal_gun_showcase = Entity(model=('portal_gun.fbx'), position=(25,2,-5), scale=.05, origin_z=-.5, rotation_z=0, on_cooldown=False, name="pgun_showcase", collider='box', texture=('/textures/w_portalgun.png'))

arm.flip_faces()
portal_gun_showcase.flip_faces()

def genstairs():
    stairs(128,0.3,7,30)

thread(name="stair_gen", target=genstairs).start()

def update():
    global player, stored_camera_y, duty_cycle
    duty_cycle += 1

    if (block_outgoing_updates):
        arm.color=color.rgb(25,25,25,25)
    else:
        arm.color=pcolor

    if held_keys['escape']:
        os.abort()

    if held_keys['x']:
        player.y += 10

    if (held_keys['left shift'] and not held_keys['control']):
        player.speed = player.sprint_speed
    elif (player.speed == player.sprint_speed):player.speed=8

    if player.get_position()[1] < -20:
        prot = player.rotation
        destroy(player)
        player = FirstPersonController(model=player_model, z=-10, color=pcolor, speed=8, rotation=prot, origin_y=0.12, gravity=grav, jump_height = 4, height=3, collider='mesh', name=player_model)
        player.visible_self = False
        print("info: player reset")

mouse.visible = False
mouse.locked = True
app.run()

You can try Entity.flip_faces() function in ursina after adding this you code will look like this:

from classes.constants import *
fsmode = getinput("Fullscreen? [Y/N] > ")

if (fsmode == "Y"):fsmode = True
else:fsmode = False

import time as systime, socket, socketserver, random
from ursina import *
import ursina.audio as audio
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.prefabs.health_bar import HealthBar
from ursina.shaders import *
from threading import Thread as thread

from classes.class_override import *
from classes.custom_classes import *

grav=1
player_model = "/models/player"
pcolor = color.rgb(185, 185, 135)
remoteplayer_color = color.rgb(145, 185, 145)
remoteplayer_disconnected = color.rgb(25,25,25,25)
stored_camera_y = 0
duty_cycle=0
remote_player_connected = False
r_player_justconnected = False
paused = False
block_outgoing_updates = False

app = Ursina(development_mode=False, show_ursina_splash_screen=True,fullscreen=fsmode,title="Reality Simulator (now with Portals!)")

random.seed(0)
Entity.default_shader = lit_with_shadows_shader

ground = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground")
ground2 = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground", position=(128,0,128))
ground_brick = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground", position=(0,0,128))
ground_brick2 = Entity(model='plane', collider='box', scale=128, texture='grass', texture_scale=(4,4), name="ground", position=(128,0,0))
spawning_platform = Entity(model='cube', collider='box', scale=(3,0.5,3), texture='brick', position=(0,0,-10))

editor_camera = EditorCamera(enabled=False, ignore_paused=True, move_speed=30)

player = FirstPersonController(model=player_model, z=-10, y=0.5, color=pcolor, origin_y=0.12, gravity=grav, speed=8, jump_height = 4, height=3, collider='mesh', name="player", cursor=Entity(parent=camera.ui, model='quad', color=color.pink, scale=.001, rotation_z=45))
# player_healthbar = HealthBar(max_value=player.max_hp, animation_duration=0.1, value=player.max_hp, parent=camera.ui)
p2 = Entity(model=player_model, color=remoteplayer_disconnected, scale=1, name='remote_player', x=-5,y=0,z=-5, rotation_y=180)
# sim_symbol = Entity(model='diamond', collider='mesh', color=color.green, name='sim_symbol', parent=player, position=(0,6,0), scale=(.5,1,.5), visible=False)
player.visible_self = False
# player = FirstPersonController(model=player_model, z=-10, color=color.orange, origin_y=-.5, speed=8)
# player.collider = MeshCollider(player, Vec3(0,1,0), Vec3(1,2,1))

arm = Entity(model='portal_gun_2', parent=camera, position=(.5,-.25,0.45), scale=.004, origin_z=-.5, rotation_z=0, on_cooldown=False, name="gun", color=pcolor, texture='/textures/w_portalgun.png')
portal_gun_showcase = Entity(model=('portal_gun.fbx'), position=(25,2,-5), scale=.05, origin_z=-.5, rotation_z=0, on_cooldown=False, name="pgun_showcase", collider='box', texture=('/textures/w_portalgun.png'))

arm.flip_faces()
portal_gun_showcase.flip_faces()

def genstairs():
    stairs(128,0.3,7,30)

thread(name="stair_gen", target=genstairs).start()

def update():
    global player, stored_camera_y, duty_cycle
    duty_cycle += 1

    if (block_outgoing_updates):
        arm.color=color.rgb(25,25,25,25)
    else:
        arm.color=pcolor

    if held_keys['escape']:
        os.abort()

    if held_keys['x']:
        player.y += 10

    if (held_keys['left shift'] and not held_keys['control']):
        player.speed = player.sprint_speed
    elif (player.speed == player.sprint_speed):player.speed=8

    if player.get_position()[1] < -20:
        prot = player.rotation
        destroy(player)
        player = FirstPersonController(model=player_model, z=-10, color=pcolor, speed=8, rotation=prot, origin_y=0.12, gravity=grav, jump_height = 4, height=3, collider='mesh', name=player_model)
        player.visible_self = False
        print("info: player reset")

mouse.visible = False
mouse.locked = True
app.run()
东风软 2025-02-10 10:22:48

以前的解决方案对我不起作用,这对我有用,我使用了底漆Panda3D库来处理正常方向的反转。

from ursina import *
from panda3d.core import GeomNode

app = Ursina()

# Laad je model
entity = Entity(model='your_model.stl')

# Toegang tot de onderliggende NodePath
nodepath = entity.model

# Normals omdraaien
for node in nodepath.find_all_matches('**/+GeomNode'):
    geom_node = node.node()
    for i in range(geom_node.get_num_geoms()):
        geom = geom_node.modify_geom(i)
        # Draai de geometrie om
        geom.reverse_in_place()

# Start de Ursina-app
app.run()

The previous solutions did not work for me, this works for me, I used the underlaying Panda3D library to handle the inversion of the normal directions.

from ursina import *
from panda3d.core import GeomNode

app = Ursina()

# Laad je model
entity = Entity(model='your_model.stl')

# Toegang tot de onderliggende NodePath
nodepath = entity.model

# Normals omdraaien
for node in nodepath.find_all_matches('**/+GeomNode'):
    geom_node = node.node()
    for i in range(geom_node.get_num_geoms()):
        geom = geom_node.modify_geom(i)
        # Draai de geometrie om
        geom.reverse_in_place()

# Start de Ursina-app
app.run()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文