gltf格式解析

gltf官网:https://github.com/KhronosGroup/glTF

官方文档:gltf20-reference-guide.pdf

gltf查看器:https://gltf-viewer.donmccurdy.com/

gltf格式解析教程:https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/README.md

 

下面是用python解析gltf到json和bytess数据示例

def __load_gltf__(file_name):
    gltf_buffers = []
    with open(file_name, 'r') as f:
        gltf_data = f.read()
    gltf_json = json.loads(gltf_data)
    root_dir = file_util.get_file_dir(file_name)
    for gltf_buffer in gltf_json["buffers"]:
        gltf_buffers.append(get_buffer_binary_data(gltf_buffer.get("uri"),root_dir,0,gltf_buffer.get("byteLength",pow(2,32)-1)))
    return gltf_json, gltf_buffers

def get_buffer_binary_data(uri,root_dir, byte_offset= None, byte_length=None):
    if re.match(r'^data:.*?;base64,', uri):
        uri_data = uri.split(',')[1]
        buffer_data = base64.b64decode(uri_data)
        if byte_offset is not None:
            buffer_data = buffer_data[byte_offset:]
    else:
        buffer_file = os.path.join(root_dir, uri)
        with open(buffer_file, 'rb') as buffer_fptr:
            if byte_offset is not None:
                buffer_fptr.seek(byte_offset)
            buffer_data = buffer_fptr.read(byte_length)
    return bytearray(buffer_data)

 

你可能感兴趣的:(3D相关,gltf,glb,3d)