pokepetter / ursina

A game engine powered by python and panda3d.
https://pokepetter.github.io/ursina/
MIT License
2.23k stars 328 forks source link

Cant apply different textures for each face of the cube using ursina engine #634

Closed DrFHMNBU closed 12 months ago

DrFHMNBU commented 12 months ago

This is the function used to generate the cubes:

def g(x,y,z):
    cube = Entity()
    sides_face = Entity(parent=cube, model='cube', texture='grass_side', position=(x+0, y+0, z+0), scale=(1,1,1))
    top_face = Entity(parent=cube, model='cube', texture='grass', position=(x+0, y+0.5, z+0), scale=(1,0.01,1))
    bottom_face = Entity(parent=cube, model='cube', texture='dirt', position=(x+0, y-0.5, z+0), scale=(1,0.01,1))

But with transforming into it crashes:

def g(x,y,z):
    cube = Entity(parent=cube, model='cube', texture='grass_side', position=(x+0, y+0, z+0), scale=(1,1,1))
    cube.texture_top='grass'
    cube.texture_bottom='dirt'

This is the error log:

PS F:\xamppZ4\main v2\htdocs\python\games\mc\python-mc-clone-5> python3 .\pruebamc50.pyw
:prc(warning): Invalid integer value for ConfigVariable win-size: 819.2000122070312
:prc(warning): Invalid integer value for ConfigVariable win-size: 1024.0
Known pipe types:
  wglGraphicsPipe
(3 aux display modules not yet loaded.)
set window position: Vec2(128, 102.5)
:prc(warning): changing default value for ConfigVariable paste-emit-keystrokes from '1' to '0'.
:pnmimage:png(warning): iCCP: known incorrect sRGB profile
package_folder: C:\Users\usuario\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ursina
asset_folder: .
Traceback (most recent call last):
  File "F:\xamppZ4\main v2\htdocs\python\games\mc\python-mc-clone-5\pruebamc50.pyw", line 11, in <module>
    window.options_panel.enabled = False
AttributeError: 'Window' object has no attribute 'options_panel'
PS F:\xamppZ4\main v2\htdocs\python\games\mc\python-mc-clone-5> python3 .\pruebamc50.pyw
:prc(warning): Invalid integer value for ConfigVariable win-size: 819.2000122070312
:prc(warning): Invalid integer value for ConfigVariable win-size: 1024.0
Known pipe types:
  wglGraphicsPipe
(3 aux display modules not yet loaded.)
set window position: Vec2(128, 102.5)
:prc(warning): changing default value for ConfigVariable paste-emit-keystrokes from '1' to '0'.
:pnmimage:png(warning): iCCP: known incorrect sRGB profile
package_folder: C:\Users\usuario\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ursina
asset_folder: .
Traceback (most recent call last):
  File "F:\xamppZ4\main v2\htdocs\python\games\mc\python-mc-clone-5\pruebamc50.pyw", line 19, in <module>
    g(i,0,k)
  File "F:\xamppZ4\main v2\htdocs\python\games\mc\python-mc-clone-5\pruebamc50.pyw", line 13, in g
    cube = Entity(parent=cube, model='cube', texture='grass_side', position=(x+0, y+0, z+0), scale=(1,1,1))
UnboundLocalError: local variable 'cube' referenced before assignment

I want to render 6 materials for each face of the cube, so... any solution? Thanks anyway.

a2cy commented 12 months ago

the error youre getting is because youre trying to parent te cube to itself. if you want to use multiple textures on one Entity you have to create a texture atlas or use a texture array

DrFHMNBU commented 12 months ago

This is the atlas: atlas_grass_cube But I have this code:

from ursina import *
import math

app = Ursina()

window.borderless = False
window.exit_button.visible = False
window.fps_counter.enabled = False
window.entity_counter.enabled = False
window.collider_counter.enabled = False

def g(x,y,z):
    cube = Entity(model='cube', texture='dirt', position=(x, y, z), scale=(1,3,1))
    cube = Entity(parent='cube', model='cube', texture='grass_side', position=(x, y + 1, z), scale=(1.01,1,1.01))
    cube = Entity(parent='cube', model='cube', texture='grass', position=(x, y + 1, z), scale=(1,1.01,1))

def getSurface(i,k):
    return int((sin(i/5)+cos(k/5))*5)+int((sin(i/25)+cos(k/25))*25)-50

for i in range(20):
    for k in range(20):
        surface=getSurface(i,k)
        g(i,surface,k)

walkspeed=4
rotspeed=90
mouse_sensitivity = Vec2(20, 20)

window.color = color.rgb(0,127,255)

def update():
    if held_keys['space'] and mouse.locked == True and mouse.visible == False:
        camera.position += (0, time.dt*walkspeed, 0)
    if held_keys['x'] and mouse.locked == True and mouse.visible == False:
        camera.position += (0,-time.dt*walkspeed, 0) 
    if held_keys['w'] and mouse.locked == True and mouse.visible == False:
        camera.position += (math.sin(math.radians(camera.rotation_y)) * time.dt * walkspeed, 0, math.cos(math.radians(camera.rotation_y)) * time.dt * walkspeed)
    if held_keys['s'] and mouse.locked == True and mouse.visible == False:
        camera.position -= (math.sin(math.radians(camera.rotation_y)) * time.dt * walkspeed, 0, math.cos(math.radians(camera.rotation_y)) * time.dt * walkspeed)
    if held_keys['a'] and mouse.locked == True and mouse.visible == False:
        camera.position -= (math.cos(math.radians(camera.rotation_y)) * time.dt * walkspeed, 0, -math.sin(math.radians(camera.rotation_y)) * time.dt * walkspeed)
    if held_keys['d'] and mouse.locked == True and mouse.visible == False:
        camera.position += (math.cos(math.radians(camera.rotation_y)) * time.dt * walkspeed, 0, -math.sin(math.radians(camera.rotation_y)) * time.dt * walkspeed)
    if mouse.left:
        mouse.locked = True
        mouse.visible = False
    if held_keys['escape']:
        mouse.locked = False
        mouse.visible = True
    if mouse.locked == True and mouse.visible == False:
        camera.rotation_y += mouse.velocity[0] * mouse_sensitivity[1]
        camera.rotation_x -= mouse.velocity[1] * mouse_sensitivity[0]
        camera.rotation_x = clamp(camera.rotation_x, -90, 90)

app.run()

How to use it? With texture.load("atlas_grass_cube") ?: image

a2cy commented 12 months ago

you need to make your own cube model