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_index0和accessor_index1存儲的是點的索引,數據類型是scaler,bufferView屬性對應的是存儲點的值存儲地,可在bufferView里面去找對應的id的buffer信息。byteOffset是當前索引值對應bufferView的偏移量,如果是索引,byteStride是0,如果是點的位置坐標,byteStride是3*4即12。count是數據的總個數,componentType是點存儲的字節數,如果頂點的個數小於65535,及componentType用兩個字節就可以,用5123,否則就四個字節用5126。對應的其他屬性可以以此類推。

關於GLTF的其他屬性的值就不一一介紹了,根據上面的例子可以大概看明白,不明白的也可以問我。

由於場景數據沒有運動,所以沒有給animations的值。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM