Remove vertices in Mesh model

67 Views Asked by At

I'm creating Minecraft in Ursina. I create a chunk through add vertices to a mesh model.

But now I have a problem, I can not delete the vertices in the mesh. An error occurs each time the code calls Mesh.Generate()

The following is how I try to delete the vertices and the error.

from ursina import *
from ursina.shaders import basic_lighting_shader, lit_with_shadows_shader
from ursina.prefabs.first_person_controller import FirstPersonController

app = Ursina()

bte = Entity(model='block.obj',color=color.rgba(1,1,0,0.4),visible=False)
bte.scale=1.1
bte.origin_y+=0.05

sun = DirectionalLight(rotation=Vec3(45, 45, 45))

virtices = [# 底面
            [0, 0, 0], #0
            [0, 0, 1], #1
            [1, 0, 1], #2
            [1, 0, 1], #3
            [1, 0, 0], #4
            [0, 0, 0], #5

            # 顶面
            [0, 1, 0], #0
            [1, 1, 0], #1
            [1, 1, 1], #2
            [1, 1, 1], #3
            [0, 1, 1], #4
            [0, 1, 0], #5
            
            # 左面
            [0, 0, 0], #0
            [0, 1, 0], #1
            [0, 1, 1], #2
            [0, 1, 1], #3
            [0, 0, 1], #4
            [0, 0, 0], #5

            # 右面
            [1, 0, 0], #0
            [1, 0, 1], #1
            [1, 1, 1], #2
            [1, 1, 1], #3
            [1, 1, 0], #4
            [1, 0, 0], #5

            # 前面
            [0, 0, 0], #0
            [1, 0, 0], #1
            [1, 1, 0], #2
            [1, 1, 0], #3
            [0, 1, 0], #4
            [0, 0, 0], #5

            # 后面
            [0, 0, 1], #0
            [0, 1, 1], #1
            [1, 1, 1], #2
            [1, 1, 1], #3
            [1, 0, 1], #4
            [0, 0, 1], #5
            ]
normals = [[-1 if number == 0 else number for number in sublist] for sublist in virtices]
amesh = Mesh()

amesh.normals.extend(normals)
amesh.uvs.extend([[0,0], [0,1], [1,1], [1,1], [1,0], [0,0], # 底面
                  [0,0], [1,0], [1,1], [1,1], [0,1], [0,0], # 顶面
                  [0,0], [0,1], [1,1], [1,1], [1,0], [0,0], # 左面
                  [0,0], [1,0], [1,1], [1,1], [0,1], [0,0], # 右面
                  [0,0], [1,0], [1,1], [1,1], [0,1], [0,0], # 前面
                  [0,0], [0,1], [1,1], [1,1], [1,0], [0,0], # 后面
                  ])
amesh.vertices.extend(virtices)
amesh.vertices.remove([0, 0, 0])
amesh.generate()

error:

:prc(warning): Invalid integer value for ConfigVariable win-size: 864.0 :prc(warning): Invalid integer value for ConfigVariable win-size: 1536.0 Known pipe types: wglGraphicsPipe (3 aux display modules not yet loaded.) set window position: Vec2(192, 108) :prc(warning): changing default value for ConfigVariable paste-emit-keystrokes from '1' to '0'. :pnmimage:png(warning): iCCP: known incorrect sRGB profile package_folder: D:\MiniConda\Lib\site-packages\ursina asset_folder: d:\Downloads\myCode\Python\小游戏\Minecraft-Ursina-2.0 read obj at: d:\Downloads\myCode\Python\小游戏\Minecraft-Ursina-2.0\assets\block.obj saved .bam to: d:\Downloads\myCode\Python\小游 戏\Minecraft-Ursina-2.0\models_compressed\block.bam Assertion failed: (num_vertices + num_unused_vertices_per_primitive) % (num_vertices_per_primitive + num_unused_vertices_per_primitive) == 0 at line 388 of c:\buildslave\sdk-windows-amd64\build\panda\src\gobj\geomPrimitive.cxx Traceback (most recent call last): File "d:\Downloads\myCode\Python\小游戏\Minecraft-Ursina-2.0\test.py", line 75, in amesh.generate() File "D:\MiniConda\Lib\site-packages\ursina\mesh.py", line 139, in generate prim.close_primitive() AssertionError: (num_vertices + num_unused_vertices_per_primitive) % (num_vertices_per_primitive + num_unused_vertices_per_primitive) == 0 at line 388 of c:\buildslave\sdk-windows-amd64\build\panda\src\gobj\geomPrimitive.cxx

1

There are 1 best solutions below

0
On

Try this; the remove function doesn't work here for no reason;) :

if len(p.vertices)!=0:
   p_verts=p.vertices
   p_norms=p.normals
   p_uvs=p.uvs
   p.clear()
   p_verts.pop(p_verts.index([0,0,0]))
   p_uvs.pop(p_verts.index([0,0,0]))
   p.vertices=p_verts
   p.normals=p_norms
   p.uvs=p_uvs
   p.generate()