栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

求助 gltf 怎么创建两个mesh

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

求助 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")
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1005345.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号