pokepetter / ursina

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

How to increase the performance with this python script?: #636

Closed DrFHMNBU closed 7 months ago

DrFHMNBU commented 7 months ago

I have this in the same directory: grass_side dirt grass And this python file in same directory, called pruebamc104.py:

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
cubes = {}
def g(x,y,z):
    cubes[str(x)+" "+str(z)+" "+'0'] = Entity(model='cube', texture='dirt', position=(x, y, z), scale=(1,5,1))
    cubes[str(x)+" "+str(z)+" "+'1'] = Entity(parent='cube', model='cube', texture='grass_side', position=(x, y + 3, z), scale=(1.01,1,1.01))
    cubes[str(x)+" "+str(z)+" "+'2'] = Entity(parent='cube', model='cube', texture='grass', position=(x, y + 3, z), scale=(1,1.01,1))
def getSurface(i,k):
    return int((sin(i/5)+cos(k/5))*5)
for i in range(-20,20):
    for k in range(-20,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)
camera.x=0
camera.y=0
camera.z=0
def calcular_distancia(x1,y1,z1,x2,y2,z2):
    return ((x1-x2)**2+(y1-y2)**2+(z1-z2)**2)**0.5
def regenerar():
    for i in range(-20,20):
        for k in range(-20,20):
            surface=int(getSurface(int(i+camera.x),int(k+camera.z)))

            cubes[str(i)+" "+str(k)+" "+'0'].x=int(i+camera.x)
            cubes[str(i)+" "+str(k)+" "+'0'].y=int(surface)
            cubes[str(i)+" "+str(k)+" "+'0'].z=int(k+camera.z)

            cubes[str(i)+" "+str(k)+" "+'1'].x=int(i+camera.x)
            cubes[str(i)+" "+str(k)+" "+'1'].y=int(surface+3)
            cubes[str(i)+" "+str(k)+" "+'1'].z=int(k+camera.z)

            cubes[str(i)+" "+str(k)+" "+'2'].x=int(i+camera.x)
            cubes[str(i)+" "+str(k)+" "+'2'].y=int(surface+3)
            cubes[str(i)+" "+str(k)+" "+'2'].z=int(k+camera.z)
prev_position_x=camera.x
prev_position_y=camera.y
prev_position_z=camera.z
def update():
    global prev_position_x, prev_position_y, prev_position_z
    # 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)
    if getSurface(camera.x,camera.z) + 6 < camera.y - 0.1:
        camera.y-=0.1
    else:
        if getSurface(camera.x,camera.z) + 6 > camera.y + 0.1:
            camera.y+=0.1
    if calcular_distancia(prev_position_x,0,prev_position_z,camera.x,0,camera.z)>1:
        regenerar()
        prev_position_x=camera.x
        prev_position_y=camera.y
        prev_position_z=camera.z
app.run()

But it runs fastly at 10 of render distance, but at 20 runs slowly, and at 40 crashes, so... its possible to optimize it? Sorry my misunderstanding and thanks anyway!

pokepetter commented 7 months ago

Please don't use this for general programming help. This is for reporting issues with ursina.

However, you should use fewer entities. Use Mesh for procedural geometry, not a million separate entities.