2

嘿,最近我从这里学习使用 ursina 。
但是如果我要添加更多blocks/cubes游戏会滞后很多,
我发现通过组合所有立方体的网格并渲染一个单孔网格,它不会滞后太多但是问题是

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

如果你想要这是我的代码

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()

在这个链接 中,我找不到用立方体来做到这一点的方法,我什至可以用游戏中的立方体来做到这一点 bcos 的几何the cubes isn't going to be the same every time

任何帮助,将不胜感激

谢谢!!

4

1 回答 1

0

看看这个: 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()
于 2021-12-31T16:51:30.107 回答