| 177 | // *********************************************************************** |
| 178 | |
| 179 | bool ImportGltf(Arena* pArena, lua_State* L, u8 format, String source) { |
| 180 | // Load file |
| 181 | String fileContents; |
| 182 | fileContents.pData = ReadWholeFile(source, &fileContents.length, pArena); |
| 183 | |
| 184 | // @todo: if first few bytes is "gltf" in ascii, then it's a binary, glb file, you can verify this with the extension |
| 185 | |
| 186 | // ParseJson |
| 187 | JsonValue parsed = ParseJsonFile(pArena, fileContents); |
| 188 | |
| 189 | // validate gltf |
| 190 | bool validGltf = parsed["asset"]["version"].ToString() == "2.0"; |
| 191 | if (!validGltf) { |
| 192 | Log::Info("File %s is not a valid gltf file for us to import", source); |
| 193 | return 1; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | lua_newtable(L); // top level table for gltf |
| 198 | lua_newtable(L); // scene table |
| 199 | |
| 200 | // ------------------------------------- |
| 201 | // Import Scene |
| 202 | |
| 203 | // note that the nodelist in the top level scene does not include child nodes |
| 204 | // so they are not included in this list, but will be parsed recursively |
| 205 | JsonValue& topLevelNodeList = parsed["scenes"][0]["nodes"]; |
| 206 | i32 topLevelNodesCount = topLevelNodeList.Count(); |
| 207 | for (int i = 0; i < topLevelNodesCount; i++) { |
| 208 | i32 nodeId = topLevelNodeList[i].ToInt(); |
| 209 | ParseJsonNodeRecursively(L, parsed, nodeId); |
| 210 | } |
| 211 | |
| 212 | lua_setfield(L, -2, "scene"); |
| 213 | |
| 214 | |
| 215 | // ------------------------------------- |
| 216 | // Load buffer data |
| 217 | |
| 218 | ResizableArray<GltfBuffer> rawDataBuffers(pArena); |
| 219 | JsonValue& jsonBuffers = parsed["buffers"]; |
| 220 | for (int i = 0; i < jsonBuffers.Count(); i++) { |
| 221 | GltfBuffer buf; |
| 222 | buf.byteLength = jsonBuffers[i]["byteLength"].ToInt(); |
| 223 | |
| 224 | String encodedBuffer = jsonBuffers[i]["uri"].ToString(); |
| 225 | // @todo: the substr 37 tells us which type of gltf file this is, glb, bin as separate file, or base 64 |
| 226 | // these should be the options: |
| 227 | // data:dontcaretext;base64, |
| 228 | // this of course is what we have here, a base64 encoded string, embedded in the uri |
| 229 | // filename.bin |
| 230 | // this will be another file to load, with the binary data directly there |
| 231 | // no entry other than byte length, then it is glb and the binary is in the next chunk or chunks |
| 232 | String decoded = DecodeBase64(pArena, SubStr(encodedBuffer, 37)); |
| 233 | buf.pBytes = (u8*)decoded.pData; |
| 234 | |
| 235 | rawDataBuffers.PushBack(buf); |
| 236 | } |
no test coverage detected