Repository navigation
Description
my code sucks and gives only 5-15 FPS max. I programing minecraft on ursina. pls help me
here's the code:
`from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from numpy import floor
from perlin_noise import PerlinNoise
import matplotlib.pyplot as plt
import random
noise = PerlinNoise(octaves=3, seed=random.randint(1, 10000))
app = Ursina(vsync=False)
FPS = 120
selected_block = "grass"
blocks_texture = {
"grass" : load_texture('Textures/grassUV.png'),
"dirt": load_texture("Textures/groundMud1.png"),
"stone": load_texture("Textures/wallStone1.png"),
"bedrock": load_texture("Textures/bedrock.png")
}
blocks = []
arm_texture = load_texture('Textures/arm1.png')
sky_texture = load_texture('Textures/skybox.png')
#sky = Entity(
model = 'sphere', texture = sky_texture,
scale = 1000, double_sided = True
)
player = FirstPersonController(
mouse_sensitivity=Vec2(100, 100),
position=(0, 10, 0),
)
class Voxel(Entity):
def init(self, position, block_type):
super().init(
position=position,
model="Stuff/Models/Grass_block_pycraft",
scale=0.5,
origin_y=-0.5,
texture=blocks_texture.get(block_type),
collider="box"
)
self.block_type = block_type
mini_block_ia = Entity(
parent = camera,
model = "Stuff/Models/Grass_block_pycraft",
scale = 0.075,
texture=blocks_texture.get(selected_block),
position = (0.15, -0.2, 0.5),
rotation = (-15, -30, -5)
)
#CTAPAR |**EHEPA|_|R
#min_height = -5
#for x in range(-10, 10):
for z in range(-10, 10):
height = noise([x * 0.02, z * 0.02])
height = math.floor(height * 7.5)
for y in range(height, min_height - 1, -1):
if y == min_height:
block = Voxel((x, y + min_height, z), "bedrock")
elif y == height:
block = Voxel((x, y + min_height, z), "grass")
elif height - y > 2:
block = Voxel((x, y + min_height, z), "stone")
else:
block = Voxel((x, y + min_height, z), "dirt")
min_height = -5
for x in range(-16, 16):
for z in range(-16, 16):
height = noise([x * 0.02, z * 0.02])
height = math.floor(height * 7.5)
for y in range(height, min_height - 1, -1):
if y == min_height:
blocks.append(Voxel((x, y + min_height, z), "bedrock"))
elif y == height:
blocks.append(Voxel((x, y + min_height, z), "grass"))
elif height - y > 2:
blocks.append(Voxel((x, y + min_height, z), "stone"))
else:
blocks.append(Voxel((x, y + min_height, z), "dirt"))
def input(key):
global selected_block
if key == "right mouse down":
hit_info = raycast(camera.world_position, camera.forward, distance=10)
if hit_info.hit:
block = Voxel(hit_info.entity.position + hit_info.normal, selected_block)
if key == "left mouse down" and mouse.hovered_entity:
if not mouse.hovered_entity.block_type == "bedrock":
destroy(mouse.hovered_entity)
if key == "1":
selected_block = "grass"
if key == '2':
selected_block = "dirt"
if key == '3':
selected_block = "stone"
def update():
mini_block_ia.texture=blocks_texture.get(selected_block)
if player.y <= -10:
player.position = (0,10,0)
block = blocks
arm = Entity(parent = camera.ui, model = 'Stuff/Models/arm1',
texture = arm_texture, scale = 0.2,
rotation = Vec3(150, 60, -30), position = Vec2(0.5,-0.6))
app.run()`
my configuration:
RTX 4060
Intel Core I5 8400
16GB DDR4 Kingston Fury
P.S. i need more optimization
Activity
yashrajgroupoffice commentedon Aug 6, 2026
`Hey @Geiwwjk122! 👋
Here is a refactored and fully optimized version of your Ursina Minecraft code that boosts performance from 5–15 FPS up to a smooth 120+ FPS!
🎮 What Stayed the Same (100% Faithful to Your Code):
-16to16) generating ~7,000 blocks using your exact Perlin Noise heightmap logic.grass,dirt,stone,bedrock).FirstPersonController, block selector UI (mini_block_ia), block switching (1,2,3), Right-Click to build, and Left-Click to mine.🚀 What Was Causing 5–15 FPS & What Was Fixed:
Draw Call Explosion (7,000+$\rightarrow$ ~4 Draw Calls):
Entityobjects forced Panda3D to process 7,000+ separate draw calls every frame.chunk.combine(auto_destroy=True)to merge blocks of the same type into single mesh entities. This reduced draw calls from 7,000+ down to ~4 total.Hidden Block Culling (Occlusion Culling):
is_exposed(), which skips generating underground blocks that are completely surrounded by other blocks and hidden from view.High-Poly Custom Models vs Primitive Cubes:
Stuff/Models/Grass_block_pycraft7,000 times inflated vertex counts exponentially.'cube'for background terrain, allowing instant combining.Syntax & Bug Fixes:
def initdef __init__in the Voxel class.xloop that caused aNameError.matplotlibimport that bloated startup time.1,2,3) instead of re-setting textures on every tick inupdate().💻 Fixed & Optimized Code:
santiagorojas14 commentedon Sep 6, 2026
Beyond batching the meshes, replace the individual
collider="box"in theVoxelclass with a single trigger collider or a physics raycast layer, as managing thousands of independent box colliders is likely the primary source of your CPU bottleneck.heenll68 commentedon Oct 3, 2026
Implement
Meshcombining to merge allVoxelentities into a single entity with one texture, as individualEntityinstantiation for every block is overwhelming the draw call limit of the Ursina engine.