求助 gltf 怎么创建两个mesh

我想为gltf中不同的mesh赋予不同的材质,但是我创建两个mesh后,后面的mesh会不显示

请大牛们帮忙解答

下面是python代码,用的pygltflib包

import numpy as np
import pygltflib
from pygltflib.utils import ImageFormat, Image

# Define mesh using numpy:
points = np.array(
    [
        [0, 0, 0],
        [10, 0, 0],
        [0, 10, 0],
        [0, 0, 10],
        [0, 0, 10],
        [10, 0, 10],
        [0, 10, 10],
        [0, 0, 20],

    ],
    dtype="float32",
)
points1 = np.array(
    [
        [0, 0, 0],
        [10, 0, 0],
        [0, 10, 0],
        [0, 0, 10],
    ],
    dtype="float32",
)
points2 = np.array(
    [
        [0, 0, 10],
        [10, 0, 10],
        [0, 10, 10],
        [0, 0, 20],

    ],
    dtype="float32",
)
triangles = np.array(
    [
        [0, 2, 1],
        [0, 3, 2],
        [0, 1, 3],
        [1, 2, 3],
        [4, 6, 5],
        [4, 7, 6],
        [4, 5, 7],
        [5, 6, 7]
    ],
    dtype="uint8",
)
triangles1 = np.array(
    [
        [0, 2, 1],
        [0, 3, 2],
        [0, 1, 3],
        [1, 2, 3],
    ],
    dtype="uint8",
)
triangles2 = np.array(
    [
        [4, 6, 5],
        [4, 7, 6],
        [4, 5, 7],
        [5, 6, 7]
    ],
    dtype="uint8",
)

# Create glb-style GLTF2 with single scene, single node and single mesh from arrays of points and triangles:

triangles_binary_blob = triangles.flatten().tobytes()
triangles1_binary_blob = triangles1.flatten().tobytes()
triangles2_binary_blob = triangles2.flatten().tobytes()

points_binary_blob = points.tobytes()
points1_binary_blob = points1.tobytes()
points2_binary_blob = points2.tobytes()

gltf = pygltflib.GLTF2(
    scene=0,
    scenes=[pygltflib.Scene(nodes=[0])],
    nodes=[pygltflib.Node(mesh=0)],
    meshes=[
        pygltflib.Mesh(
            primitives=[
                pygltflib.Primitive(
                    attributes=pygltflib.Attributes(POSITION=1), indices=0
                ),
                pygltflib.Primitive(
                    attributes=pygltflib.Attributes(POSITION=3), indices=2
                )

            ],
        )
    ],
    accessors=[
        pygltflib.Accessor(
            bufferView=0,
            componentType=pygltflib.UNSIGNED_BYTE,
            count=triangles1.size,
            type=pygltflib.SCALAR,
            max=[int(triangles1.max())],
            min=[int(triangles1.min())],
        ),
        pygltflib.Accessor(
            bufferView=1,
            componentType=pygltflib.FLOAT,
            count=len(points1),
            type=pygltflib.VEC3,
            max=points1.max(axis=0).tolist(),
            min=points1.min(axis=0).tolist(),
        ),
        pygltflib.Accessor(
            bufferView=0,
            componentType=pygltflib.UNSIGNED_BYTE,
            byteOffset=len(triangles1_binary_blob),
            count=triangles2.size,
            type=pygltflib.SCALAR,
            max=[int(triangles2.max())],
            min=[int(triangles2.min())],
        ),
        pygltflib.Accessor(
            bufferView=1,
            componentType=pygltflib.FLOAT,
            byteOffset=len(points1_binary_blob),
            count=len(points2),
            type=pygltflib.VEC3,
            max=points2.max(axis=0).tolist(),
            min=points2.min(axis=0).tolist(),
        ),
    ],
    bufferViews=[
        pygltflib.BufferView(
            buffer=0,
            byteLength=len(triangles_binary_blob),
            target=pygltflib.ELEMENT_ARRAY_BUFFER,
        ),
        pygltflib.BufferView(
            buffer=0,
            byteOffset=len(triangles_binary_blob),
            byteLength=len(points_binary_blob),
            target=pygltflib.ARRAY_BUFFER,
        ),
    ],
    buffers=[
        pygltflib.Buffer(
            byteLength=len(triangles_binary_blob) + len(points_binary_blob)
        )
    ],

)
gltf.set_binary_blob(triangles_binary_blob + points_binary_blob)
gltf.save("test07.gltf")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值