1

Hey there recently I was learning to use ursina from here.
But if I was to add more blocks/cubes the game lags quite a bit and
I found that by combining the meshes of all the cubes and rendering a one hole mesh it will not lag that much But the Problem is that

I can't find a way to combine the mesh of cubes into one big mesh

Here's my code:

def update():
    global block_pick, player, block_pickOriginal
    try:
        if block_pick != block_pickOriginal:
            a = Text(blockL[block_pick - 1], scale=1.5, position=Vec2(-.0, -0.35))
            block_pickOriginal = block_pick
            destroy(a, delay=.5)
    except:
        pass

    # print(player.world_position[2] // .5 // 2)

    # print(round(player.x) // .5)

    if held_keys['left mouse'] or held_keys['right mouse']:
        hand.active()
    else:
        hand.passive()

    if held_keys['q']:
        quit()
    if held_keys['1']:
        block_pick = 1
        grid.texture = grassS
    if held_keys['2']:
        block_pick = 2
        grid.texture = stoneS
    if held_keys['3']:
        block_pick = 3
        grid.texture = woodPlankS
    if held_keys['4']:
        block_pick = 4
        grid.texture = dirtS

    if held_keys['5']: block_pick = 5
    if held_keys['6']: block_pick = 6
    if held_keys['7']: block_pick = 7
    if held_keys['8']: block_pick = 8
    if held_keys['9']: block_pick = 9

    if player.world_position[1] < -100: player = FirstPersonController()

    # print(player.world_position[1])


class Voxel(Button):
    def __init__(self, position=(0, 0, 0), texture=grass_texture):
        super().__init__(
            parent=scene,
            position=position,
            model='assets/block',
            origin_y=0.5,
            texture=texture,
            color=color.color(0, 0, random.uniform(0.9, 1)),
            scale=0.5)

    def input(self, key):
        global voxel

        if self.hovered:
            if key == 'left mouse down':
                punch_sound.play()
                if block_pick == 1: voxel = Voxel(position=self.position + mouse.normal, texture=grass_texture)
                if block_pick == 2: voxel = Voxel(position=self.position + mouse.normal, texture=stone_texture)
                if block_pick == 3: voxel = Voxel(position=self.position + mouse.normal, texture=brick_texture)
                if block_pick == 4: voxel = Voxel(position=self.position + mouse.normal, texture=dirt_texture)
                if block_pick == 5: voxel = Voxel(position=self.position + mouse.normal, texture=coal_texture)
                if block_pick == 6: voxel = Voxel(position=self.position + mouse.normal, texture=glass_texture)
                if block_pick == 7: voxel = Voxel(position=self.position + mouse.normal, texture=iron_texture)
                if block_pick == 8: voxel = Voxel(position=self.position + mouse.normal, texture=gold_texture)
                if block_pick == 9: voxel = Voxel(position=self.position + mouse.normal, texture=diamond_texture)

            if key == 'right mouse down':
                punch_sound.play()
                destroy(self)

for z in range(20):
    for x in range(20):
       voxel = Voxel(position=(x, 0, z))
player = FirstPersonController()

In this link I can't find a way to do that with a cube can I even do that with a cube bcos in the game the geometry of the cubes isn't going to be the same every time

Any help would be appreciated

Thanks!!

Tanay
  • 561
  • 1
  • 3
  • 16
Prathamesh Bhatkar
  • 291
  • 1
  • 4
  • 15
  • Have a look at the [platformer tutorial](https://www.ursinaengine.org/platformer_tutorial.html), it uses a Mesh and the principle also works in 3D – Jan Wilamowski Jun 13 '21 at 13:14
  • It didn't work for me What I need is to combine two meshes but in the tutorial, they explain about putting mesh: BTW Thanks for the reply – Prathamesh Bhatkar Jun 13 '21 at 14:53
  • there's a mesh whose vertices get progressively expanded by adding those of a placeholder quad. You can do the same with a cube. Please provide details what didn't work for you. – Jan Wilamowski Jun 14 '21 at 01:10

1 Answers1

2

Have a look at this: https://www.youtube.com/watch?v=H0FgQb497zc&t

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from perlin_noise import PerlinNoise
from numpy import floor
app = Ursina()

terrain = Entity(model=None, collider=None)
noise = PerlinNoise(octaves=2, seed=100)
freq = 24
amp = 5

terrain_width = 60
for i in range(terrain_width*terrain_width):
    block = Entity(model='cube', color=color.green)
    block.x = floor(i/terrain_width)
    block.z = floor(i%terrain_width)
    block.y = floor(noise([block.x/freq, block.z/freq]) * amp)
    block.parent = terrain

terrain.combine()
terrain.collider = 'mesh'
terrain.texture = 'white_cube'

FirstPersonController()

app.run()
Tanay
  • 561
  • 1
  • 3
  • 16
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30712678) – BrokenBenchmark Jan 02 '22 at 21:24