babylon.glTFBinaryExtension.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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: (bufferView: 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 loadTextureAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (texture: Texture) => 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 sampler: IGLTFSampler = gltfRuntime.samplers[texture.sampler];
  60. var createMipMaps =
  61. (sampler.minFilter === ETextureFilterType.NEAREST_MIPMAP_NEAREST) ||
  62. (sampler.minFilter === ETextureFilterType.NEAREST_MIPMAP_LINEAR) ||
  63. (sampler.minFilter === ETextureFilterType.LINEAR_MIPMAP_NEAREST) ||
  64. (sampler.minFilter === ETextureFilterType.LINEAR_MIPMAP_LINEAR);
  65. var samplingMode = Texture.BILINEAR_SAMPLINGMODE;
  66. var bufferView: IGLTFBufferView = gltfRuntime.bufferViews[sourceExt.bufferView];
  67. var imageBytes = GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, EComponentType.UNSIGNED_BYTE);
  68. var blob = new Blob([imageBytes], { type: sourceExt.mimeType });
  69. var blobURL = URL.createObjectURL(blob);
  70. var revokeBlobURL = () => URL.revokeObjectURL(blobURL);
  71. var newTexture = new Texture(blobURL, gltfRuntime.scene, !createMipMaps, true, samplingMode, revokeBlobURL, revokeBlobURL);
  72. newTexture.wrapU = GLTFUtils.GetWrapMode(sampler.wrapS);
  73. newTexture.wrapV = GLTFUtils.GetWrapMode(sampler.wrapT);
  74. newTexture.name = name;
  75. texture.babylonTexture = newTexture;
  76. onSuccess(newTexture);
  77. return true;
  78. }
  79. public loadShaderDataAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (shaderData: string) => void, onError: () => void): boolean {
  80. var shader: IGLTFShader = gltfRuntime.shaders[id];
  81. if (!shader.extensions || !(this.name in shader.extensions)) {
  82. return false;
  83. }
  84. var binaryExtensionShader: IGLTFBinaryExtensionShader = shader.extensions[this.name];
  85. var bufferView: IGLTFBufferView = gltfRuntime.bufferViews[binaryExtensionShader.bufferView];
  86. var shaderBytes = GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, EComponentType.UNSIGNED_BYTE);
  87. var shaderData = GLTFUtils.DecodeBufferToText(shaderBytes);
  88. onSuccess(shaderData);
  89. return true;
  90. }
  91. // Parses a glTF binary array buffer into its content and body
  92. private _parseBinary(data: ArrayBuffer): IGLTFBinaryExtension {
  93. var binaryReader = new BinaryReader(data);
  94. var magic = GLTFUtils.DecodeBufferToText(binaryReader.getUint8Array(4));
  95. if (magic != "glTF") {
  96. Tools.Error("Unexpected magic: " + magic);
  97. return null;
  98. }
  99. var version = binaryReader.getUint32();
  100. if (version != 1) {
  101. Tools.Error("Unsupported version: " + version);
  102. return null;
  103. }
  104. var length = binaryReader.getUint32();
  105. if (length != data.byteLength) {
  106. Tools.Error("Length in header does not match actual data length: " + length + " != " + data.byteLength);
  107. return null;
  108. }
  109. var contentLength = binaryReader.getUint32();
  110. var contentFormat = <EContentFormat>binaryReader.getUint32();
  111. var content: Object;
  112. switch (contentFormat) {
  113. case EContentFormat.JSON:
  114. var jsonText = GLTFUtils.DecodeBufferToText(binaryReader.getUint8Array(contentLength));
  115. content = JSON.parse(jsonText);
  116. break;
  117. default:
  118. Tools.Error("Unexpected content format: " + contentFormat);
  119. return null;
  120. }
  121. var body = binaryReader.getUint8Array();
  122. return {
  123. content: content,
  124. body: body
  125. };
  126. };
  127. }
  128. class BinaryReader {
  129. private _arrayBuffer: ArrayBuffer;
  130. private _dataView: DataView;
  131. private _byteOffset: number;
  132. constructor(arrayBuffer: ArrayBuffer) {
  133. this._arrayBuffer = arrayBuffer;
  134. this._dataView = new DataView(arrayBuffer);
  135. this._byteOffset = 0;
  136. }
  137. public getUint32(): number {
  138. var value = this._dataView.getUint32(this._byteOffset, true);
  139. this._byteOffset += 4;
  140. return value;
  141. }
  142. public getUint8Array(length?: number): Uint8Array {
  143. if (!length) {
  144. length = this._arrayBuffer.byteLength - this._byteOffset;
  145. }
  146. var value = new Uint8Array(this._arrayBuffer, this._byteOffset, length);
  147. this._byteOffset += length;
  148. return value;
  149. }
  150. }
  151. GLTFFileLoader.RegisterExtension(new GLTFBinaryExtension());
  152. }