glTFBinaryExtension.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { GLTFLoaderExtension } from "./glTFLoader";
  2. import { GLTFUtils } from "./glTFLoaderUtils";
  3. import { Scene } from "babylonjs/scene";
  4. import { IGLTFLoaderData } from "../glTFFileLoader";
  5. import { IGLTFRuntime, IGLTFTexture, IGLTFImage, IGLTFBufferView, EComponentType, IGLTFShader } from "./glTFLoaderInterfaces";
  6. import { GLTFLoader, GLTFLoaderBase } from "./glTFLoader";
  7. import { IDataBuffer } from 'babylonjs/Misc/dataReader';
  8. const BinaryExtensionBufferName = "binary_glTF";
  9. interface IGLTFBinaryExtensionShader {
  10. bufferView: string;
  11. }
  12. interface IGLTFBinaryExtensionImage {
  13. bufferView: string;
  14. mimeType: string;
  15. height: number;
  16. width: number;
  17. }
  18. /** @hidden */
  19. export class GLTFBinaryExtension extends GLTFLoaderExtension {
  20. private _bin: IDataBuffer;
  21. public constructor() {
  22. super("KHR_binary_glTF");
  23. }
  24. public loadRuntimeAsync(scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: (gltfRuntime: IGLTFRuntime) => void, onError: (message: string) => void): boolean {
  25. var extensionsUsed = (<any>data.json).extensionsUsed;
  26. if (!extensionsUsed || extensionsUsed.indexOf(this.name) === -1 || !data.bin) {
  27. return false;
  28. }
  29. this._bin = data.bin;
  30. onSuccess(GLTFLoaderBase.CreateRuntime(data.json, scene, rootUrl));
  31. return true;
  32. }
  33. public loadBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (buffer: ArrayBufferView) => void, onError: (message: string) => void): boolean {
  34. if (gltfRuntime.extensionsUsed.indexOf(this.name) === -1) {
  35. return false;
  36. }
  37. if (id !== BinaryExtensionBufferName) {
  38. return false;
  39. }
  40. this._bin.readAsync(0, this._bin.byteLength).then(onSuccess, (error) => onError(error.message));
  41. return true;
  42. }
  43. public loadTextureBufferAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (buffer: ArrayBufferView) => void, onError: (message: string) => void): boolean {
  44. var texture: IGLTFTexture = gltfRuntime.textures[id];
  45. var source: IGLTFImage = gltfRuntime.images[texture.source];
  46. if (!source.extensions || !(this.name in source.extensions)) {
  47. return false;
  48. }
  49. var sourceExt: IGLTFBinaryExtensionImage = source.extensions[this.name];
  50. var bufferView: IGLTFBufferView = gltfRuntime.bufferViews[sourceExt.bufferView];
  51. var buffer = GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, EComponentType.UNSIGNED_BYTE);
  52. onSuccess(buffer);
  53. return true;
  54. }
  55. public loadShaderStringAsync(gltfRuntime: IGLTFRuntime, id: string, onSuccess: (shaderString: string) => void, onError: (message: string) => void): boolean {
  56. var shader: IGLTFShader = gltfRuntime.shaders[id];
  57. if (!shader.extensions || !(this.name in shader.extensions)) {
  58. return false;
  59. }
  60. var binaryExtensionShader: IGLTFBinaryExtensionShader = shader.extensions[this.name];
  61. var bufferView: IGLTFBufferView = gltfRuntime.bufferViews[binaryExtensionShader.bufferView];
  62. var shaderBytes = GLTFUtils.GetBufferFromBufferView(gltfRuntime, bufferView, 0, bufferView.byteLength, EComponentType.UNSIGNED_BYTE);
  63. setTimeout(() => {
  64. var shaderString = GLTFUtils.DecodeBufferToText(shaderBytes);
  65. onSuccess(shaderString);
  66. });
  67. return true;
  68. }
  69. }
  70. GLTFLoader.RegisterExtension(new GLTFBinaryExtension());