babylon.glTFBinaryExtension.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. module BABYLON {
  2. const BinaryExtensionBufferName = "binary_glTF";
  3. enum EContentFormat {
  4. JSON = 0
  5. };
  6. interface IGLTFBinaryExtension {
  7. content: Object;
  8. body: Uint8Array;
  9. };
  10. interface IGLTFBinaryExtensionShader {
  11. bufferView: string;
  12. };
  13. interface IGLTFBinaryExtensionImage {
  14. bufferView: string;
  15. mimeType: string;
  16. height: number;
  17. width: number;
  18. };
  19. export class GLTFBinaryExtension extends GLTFFileLoaderExtension {
  20. public constructor() {
  21. super("KHR_binary_glTF");
  22. }
  23. public loadRuntimeAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onSuccess: (gltfRuntime: IGLTFRuntime) => void, onError: () => void): boolean {
  24. if (!(data instanceof ArrayBuffer)) {
  25. return false;
  26. }
  27. var binary: IGLTFBinaryExtension = this._parseBinary(<ArrayBuffer>data);
  28. if (!binary) {
  29. onError();
  30. return true;
  31. }
  32. var gltfRuntime = GLTFFileLoaderBase.CreateRuntime(binary.content, scene, rootUrl);
  33. if (gltfRuntime.extensionsUsed.indexOf(this.name) === -1) {
  34. Tools.Warn("glTF binary file does not have " + this.name + " specified in extensionsUsed");
  35. gltfRuntime.extensionsUsed.push(this.name);
  36. }
  37. gltfRuntime.loadedBufferViews[BinaryExtensionBufferName] = binary.body;
  38. onSuccess(gltfRuntime);
  39. return true;
  40. }
  41. public loadBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (buffer: ArrayBufferView) => void, onError: () => void): boolean {
  42. if (gltfRuntime.extensionsUsed.indexOf(this.name) === -1) {
  43. return false;
  44. }
  45. if (id !== BinaryExtensionBufferName) {
  46. return false;
  47. }
  48. // Buffer is already loaded in loadRuntimeAsync
  49. onSuccess(null);
  50. return true;
  51. }
  52. public loadTextureBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (buffer: ArrayBufferView) => void, onError: () => void): boolean {
  53. var texture: IGLTFTexture = gltfRuntime.textures[id];
  54. var source: IGLTFImage = gltfRuntime.images[texture.source];
  55. if (!source.extensions || !(this.name in source.extensions)) {
  56. return false;
  57. }
  58. var sourceExt: IGLTFBinaryExtensionImage = source.extensions[this.name];
  59. var bufferView: IGLTFBufferView = gltfRuntime.bufferViews[sourceExt.bufferView];
  60. var buffer = GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, EComponentType.UNSIGNED_BYTE);
  61. onSuccess(buffer);
  62. return true;
  63. }
  64. public loadShaderStringAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (shaderString: string) => void, onError: () => void): boolean {
  65. var shader: IGLTFShader = gltfRuntime.shaders[id];
  66. if (!shader.extensions || !(this.name in shader.extensions)) {
  67. return false;
  68. }
  69. var binaryExtensionShader: IGLTFBinaryExtensionShader = shader.extensions[this.name];
  70. var bufferView: IGLTFBufferView = gltfRuntime.bufferViews[binaryExtensionShader.bufferView];
  71. var shaderBytes = GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, EComponentType.UNSIGNED_BYTE);
  72. var shaderString = GLTFUtils.DecodeBufferToText(shaderBytes);
  73. onSuccess(shaderString);
  74. return true;
  75. }
  76. // Parses a glTF binary array buffer into its content and body
  77. private _parseBinary(data: ArrayBuffer): IGLTFBinaryExtension {
  78. var binaryReader = new BinaryReader(data);
  79. var magic = GLTFUtils.DecodeBufferToText(binaryReader.getUint8Array(4));
  80. if (magic != "glTF") {
  81. Tools.Error("Unexpected magic: " + magic);
  82. return null;
  83. }
  84. var version = binaryReader.getUint32();
  85. if (version != 1) {
  86. Tools.Error("Unsupported version: " + version);
  87. return null;
  88. }
  89. var length = binaryReader.getUint32();
  90. if (length != data.byteLength) {
  91. Tools.Error("Length in header does not match actual data length: " + length + " != " + data.byteLength);
  92. return null;
  93. }
  94. var contentLength = binaryReader.getUint32();
  95. var contentFormat = <EContentFormat>binaryReader.getUint32();
  96. var content: Object;
  97. switch (contentFormat) {
  98. case EContentFormat.JSON:
  99. var jsonText = GLTFUtils.DecodeBufferToText(binaryReader.getUint8Array(contentLength));
  100. content = JSON.parse(jsonText);
  101. break;
  102. default:
  103. Tools.Error("Unexpected content format: " + contentFormat);
  104. return null;
  105. }
  106. var body = binaryReader.getUint8Array();
  107. return {
  108. content: content,
  109. body: body
  110. };
  111. };
  112. }
  113. class BinaryReader {
  114. private _arrayBuffer: ArrayBuffer;
  115. private _dataView: DataView;
  116. private _byteOffset: number;
  117. constructor(arrayBuffer: ArrayBuffer) {
  118. this._arrayBuffer = arrayBuffer;
  119. this._dataView = new DataView(arrayBuffer);
  120. this._byteOffset = 0;
  121. }
  122. public getUint32(): number {
  123. var value = this._dataView.getUint32(this._byteOffset, true);
  124. this._byteOffset += 4;
  125. return value;
  126. }
  127. public getUint8Array(length?: number): Uint8Array {
  128. if (!length) {
  129. length = this._arrayBuffer.byteLength - this._byteOffset;
  130. }
  131. var value = new Uint8Array(this._arrayBuffer, this._byteOffset, length);
  132. this._byteOffset += length;
  133. return value;
  134. }
  135. }
  136. GLTFFileLoader.RegisterExtension(new GLTFBinaryExtension());
  137. }