GLTF数据格式解析,自制模型转换工具

GLTF是新的一种高效传输和加载3D场景和模型的格式。可以支持很多实现三维的引擎。

GLTF是由Khronos Group设计和规定的。

GLTF其实就是一个json文件,描述的就是3D场景数据的组成和构造。

GLTF最外层的属性有:

1.scenes,nodes,cameras,animations(这些属性描述的就是模型的基本结构

2.mesh,textures,images,samplers(这些属性描述的模型的纹理信息,用于展示场景

3.buffer,bufferViews,accessors(这些属性描述的是场景模型的点的信息点的位置,法线和纹理坐标值

4.materials,techniques,programs,shaders(这些属性是场景/模型的材质信息,用于渲染3D模型

5.skins(结合点的表面信息??没用过,给的都是空值)

 说到这里,给大家看一个gltf的文件。

{
  "accessors": {
    "accessor_index0": {
      "bufferView": "bufferView_index",
      "byteOffset": 0,
      "byteStride": 0,
      "componentType": 5123,
      "count": 231432,
      "type": "SCALAR"
    },
    "accessor_index1": {
      "bufferView": "bufferView_index",
      "byteOffset": 462864,
      "byteStride": 0,
      "componentType": 5123,
      "count": 1020,
      "type": "SCALAR"
    },
    "accessor_position": {
      "bufferView": "bufferView_vertex",
      "byteOffset": 0,
      "byteStride": 12,
      "componentType": 5126,
      "count": 58900,
      "max": [
        308.304321,
        159.551575,
        145.747
      ],
      "min": [
        179.30542,
        -72.13728,
        71.62281
      ],
      "type": "VEC3"
    },
    "accessor_normal": {
      "bufferView": "bufferView_vertex",
      "byteOffset": 706800,
      "byteStride": 12,
      "componentType": 5126,
      "count": 58900,
      "max": [
        1.0,
        1.0,
        1.0
      ],
      "min": [
        1.0,
        1.0,
        1.0
      ],
      "type": "VEC3"
    },
    "accessor_uv": {
      "bufferView": "bufferView_vertex",
      "byteOffset": 1413600,
      "byteStride": 8,
      "componentType": 5126,
      "count": 58900,
      "max": [
        0.997774363,
        0.8115263
      ],
      "min": [
        0.00197040662,
        0.00195908546
      ],
      "type": "VEC2"
    }
  },
  "animations": {},
  "asset": {
    "generator": "infoearth",
    "premultipliedAlpha": true,
    "profile": {
      "api": "webgl",
      "version": "1.0.2"
    },
    "version": 1
  },
  "bufferViews": {
    "bufferView_index": {
      "buffer": "model_buffer",
      "byteLength": 464904,
      "byteOffset": 0,
      "target": 34963
    },
    "bufferView_vertex": {
      "buffer": "model_buffer",
      "byteLength": 1884800,
      "byteOffset": 464904,
      "target": 34962
    }
  },
  "buffers": {
    "model_buffer": {
      "byteLength": 2349704,
      "type": "arraybuffer",
      "uri": "Tile_+004_+004.bin"
    }
  },
  "images": {
    "texture0": {
      "name": "texture0",
      "uri": "Tile_+004_+004_0.jpg"
    },
    "texture1": {
      "name": "texture1",
      "uri": "notexture.png"
    }
  },
  "materials": {
    "material0": {
      "name": "material0",
      "technique": "technique0",
      "values": {
        "diffuse": "texture_texture0"
      }
    },
    "material1": {
      "name": "material1",
      "technique": "technique0",
      "values": {
        "diffuse": "texture_texture1"
      }
    }
  },
  "meshes": {
    "mesh_model": {
      "name": "mesh_model",
      "primitives": [
        {
          "attributes": {
            "NORMAL": "accessor_normal",
            "POSITION": "accessor_position",
            "TEXCOORD_0": "accessor_uv"
          },
          "indices": "accessor_index0",
          "material": "material0",
          "mode": 4
        },
        {
          "attributes": {
            "NORMAL": "accessor_normal",
            "POSITION": "accessor_position",
            "TEXCOORD_0": "accessor_uv"
          },
          "indices": "accessor_index1",
          "material": "material1",
          "mode": 4
        }
      ]
    }
  },
  "nodes": {
    "node": {
      "children": [],
      "matrix": [
        1,
        0,
        0,
        0,
        0,
        1,
        0,
        0,
        0,
        0,
        1,
        0,
        0,
        0,
        0,
        1
      ],
      "meshes": [
        "mesh_model"
      ],
      "name": "node"
    }
  },
  "programs": {
    "program_0": {
      "attributes": [
        "a_normal",
        "a_position",
        "a_texcoord0"
      ],
      "fragmentShader": "FS",
      "vertexShader": "VS"
    }
  },
  "samplers": {
    "sampler_0": {
      "magFilter": 9729,
      "minFilter": 9729,
      "wrapS": 10497,
      "wrapT": 10497
    }
  },
  "scene": "defaultScene",
  "scenes": {
    "defaultScene": {
      "nodes": [
        "node"
      ]
    }
  },
  "shaders": {
    "FS": {
      "type": 35632,
      "uri": "FS.glsl"
    },
    "VS": {
      "type": 35633,
      "uri": "VS.glsl"
    }
  },
  "skins": {},
  "techniques": {
    "technique0": {
      "attributes": {
        "a_normal": "normal",
        "a_position": "position",
        "a_texcoord0": "texcoord0"
      },
      "parameters": {
        "diffuse": {
          "type": 35678
        },
        "modelViewMatrix": {
          "semantic": "MODELVIEW",
          "type": 35676
        },
        "normal": {
          "semantic": "NORMAL",
          "type": 35665
        },
        "normalMatrix": {
          "semantic": "MODELVIEWINVERSETRANSPOSE",
          "type": 35675
        },
        "position": {
          "semantic": "POSITION",
          "type": 35665
        },
        "projectionMatrix": {
          "semantic": "PROJECTION",
          "type": 35676
        },
        "texcoord0": {
          "semantic": "TEXCOORD_0",
          "type": 35664
        }
      },
      "program": "program_0",
      "states": {
        "enable": [
          2929,
          2884
        ]
      },
      "uniforms": {
        "u_diffuse": "diffuse",
        "u_modelViewMatrix": "modelViewMatrix",
        "u_normalMatrix": "normalMatrix",
        "u_projectionMatrix": "projectionMatrix"
      }
    }
  },
  "textures": {
    "texture_texture0": {
      "format": 6408,
      "internalFormat": 6408,
      "sampler": "sampler_0",
      "source": "texture0",
      "target": 3553,
      "type": 5121
    },
    "texture_texture1": {
      "format": 6408,
      "internalFormat": 6408,
      "sampler": "sampler_0",
      "source": "texture1",
      "target": 3553,
      "type": 5121
    }
  }
}
 这是本人通过obj格式的数据自己转换的gltf格式的数据。下面简单介绍下其内容。

      accessors属性里面存放的是点的信息。其中accessor_index0accessor_index1存储的是点的索引,数据类型是scaler,bufferView属性对应的是存储点的值存储地,可在bufferView里面去找对应的id的buffer信息。byteOffset是当前索引值对应bufferView的偏移量,如果是索引,byteStride是0,如果是点的位置坐标,byteStride是3*4即12。count是数据的总个数,componentType是点存储的字节数,如果顶点的个数小于65535,及componentType用两个字节就可以,用5123,否则就四个字节用5126。对应的其他属性可以以此类推。

关于GLTF的其他属性的值就不一一介绍了,根据上面的例子可以大概看明白,不明白的也可以问我。

由于场景数据没有运动,所以没有给animations的值。

你可能感兴趣的:(科研技术)