GLTFExporter.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /**
  2. * @author fernandojsg / http://fernandojsg.com
  3. */
  4. //------------------------------------------------------------------------------
  5. // Constants
  6. //------------------------------------------------------------------------------
  7. var WEBGL_CONSTANTS = {
  8. POINTS: 0x0000,
  9. LINES: 0x0001,
  10. LINE_LOOP: 0x0002,
  11. LINE_STRIP: 0x0003,
  12. TRIANGLES: 0x0004,
  13. TRIANGLE_STRIP: 0x0005,
  14. TRIANGLE_FAN: 0x0006,
  15. UNSIGNED_BYTE: 0x1401,
  16. UNSIGNED_SHORT: 0x1403,
  17. FLOAT: 0x1406,
  18. UNSIGNED_INT: 0x1405,
  19. ARRAY_BUFFER: 0x8892,
  20. ELEMENT_ARRAY_BUFFER: 0x8893,
  21. NEAREST: 0x2600,
  22. LINEAR: 0x2601,
  23. NEAREST_MIPMAP_NEAREST: 0x2700,
  24. LINEAR_MIPMAP_NEAREST: 0x2701,
  25. NEAREST_MIPMAP_LINEAR: 0x2702,
  26. LINEAR_MIPMAP_LINEAR: 0x2703
  27. };
  28. var THREE_TO_WEBGL = {
  29. // @TODO Replace with computed property name [THREE.*] when available on es6
  30. 1003: WEBGL_CONSTANTS.NEAREST,
  31. 1004: WEBGL_CONSTANTS.LINEAR,
  32. 1005: WEBGL_CONSTANTS.NEAREST_MIPMAP_NEAREST,
  33. 1006: WEBGL_CONSTANTS.LINEAR_MIPMAP_NEAREST,
  34. 1007: WEBGL_CONSTANTS.NEAREST_MIPMAP_LINEAR,
  35. 1008: WEBGL_CONSTANTS.LINEAR_MIPMAP_LINEAR
  36. };
  37. //------------------------------------------------------------------------------
  38. // GLTF Exporter
  39. //------------------------------------------------------------------------------
  40. THREE.GLTFExporter = function () {};
  41. THREE.GLTFExporter.prototype = {
  42. constructor: THREE.GLTFExporter,
  43. /**
  44. * Parse scenes and generate GLTF output
  45. * @param {THREE.Scene or [THREE.Scenes]} input THREE.Scene or Array of THREE.Scenes
  46. * @param {Function} onDone Callback on completed
  47. * @param {Object} options options
  48. * trs: Exports position, rotation and scale instead of matrix
  49. */
  50. parse: function ( input, onDone, options ) {
  51. options = options || {};
  52. var outputJSON = {
  53. asset: {
  54. version: "2.0",
  55. generator: "THREE.JS GLTFExporter" // @QUESTION Does it support spaces?
  56. }
  57. };
  58. var byteOffset = 0;
  59. var dataViews = [];
  60. /**
  61. * Compare two arrays
  62. */
  63. /**
  64. * Compare two arrays
  65. * @param {Array} array1 Array 1 to compare
  66. * @param {Array} array2 Array 2 to compare
  67. * @return {Boolean} Returns true if both arrays are equal
  68. */
  69. function equalArray ( array1, array2 ) {
  70. return ( array1.length === array2.length ) && array1.every( function( element, index ) {
  71. return element === array2[ index ];
  72. });
  73. }
  74. /**
  75. * Get the min and he max vectors from the given attribute
  76. * @param {THREE.WebGLAttribute} attribute Attribute to find the min/max
  77. * @return {Object} Object containing the `min` and `max` values (As an array of attribute.itemSize components)
  78. */
  79. function getMinMax ( attribute ) {
  80. var output = {
  81. min: new Array( attribute.itemSize ).fill( Number.POSITIVE_INFINITY ),
  82. max: new Array( attribute.itemSize ).fill( Number.NEGATIVE_INFINITY )
  83. };
  84. for ( var i = 0; i < attribute.count; i++ ) {
  85. for ( var a = 0; a < attribute.itemSize; a++ ) {
  86. var value = attribute.array[ i * attribute.itemSize + a ];
  87. output.min[ a ] = Math.min( output.min[ a ], value );
  88. output.max[ a ] = Math.max( output.max[ a ], value );
  89. }
  90. }
  91. return output;
  92. }
  93. /**
  94. * Process a buffer to append to the default one.
  95. * @param {THREE.BufferAttribute} attribute Attribute to store
  96. * @param {Integer} componentType Component type (Unsigned short, unsigned int or float)
  97. * @return {Integer} Index of the buffer created (Currently always 0)
  98. */
  99. function processBuffer ( attribute, componentType ) {
  100. if ( !outputJSON.buffers ) {
  101. outputJSON.buffers = [
  102. {
  103. byteLength: 0,
  104. uri: ''
  105. }
  106. ];
  107. }
  108. // Create a new dataview and dump the attribute's array into it
  109. var dataView = new DataView( new ArrayBuffer( attribute.array.byteLength ) );
  110. var offset = 0;
  111. var offsetInc = componentType === WEBGL_CONSTANTS.UNSIGNED_SHORT ? 2 : 4;
  112. for ( var i = 0; i < attribute.count; i++ ) {
  113. for (var a = 0; a < attribute.itemSize; a++ ) {
  114. var value = attribute.array[ i * attribute.itemSize + a ];
  115. if ( componentType === WEBGL_CONSTANTS.FLOAT ) {
  116. dataView.setFloat32( offset, value, true );
  117. } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_INT ) {
  118. dataView.setUint8( offset, value, true );
  119. } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_SHORT ) {
  120. dataView.setUint16( offset, value, true );
  121. }
  122. offset += offsetInc;
  123. }
  124. }
  125. // We just use one buffer
  126. dataViews.push( dataView );
  127. // Always using just one buffer
  128. return 0;
  129. }
  130. /**
  131. * Process and generate a BufferView
  132. * @param {[type]} data [description]
  133. * @return {[type]} [description]
  134. */
  135. function processBufferView ( data, componentType ) {
  136. var isVertexAttributes = componentType === WEBGL_CONSTANTS.FLOAT;
  137. if ( !outputJSON.bufferViews ) {
  138. outputJSON.bufferViews = [];
  139. }
  140. var gltfBufferView = {
  141. buffer: processBuffer( data, componentType ),
  142. byteOffset: byteOffset,
  143. byteLength: data.array.byteLength,
  144. byteStride: data.itemSize * ( componentType === WEBGL_CONSTANTS.UNSIGNED_SHORT ? 2 : 4 ),
  145. target: isVertexAttributes ? WEBGL_CONSTANTS.ARRAY_BUFFER : WEBGL_CONSTANTS.ELEMENT_ARRAY_BUFFER
  146. };
  147. byteOffset += data.array.byteLength;
  148. outputJSON.bufferViews.push( gltfBufferView );
  149. // @TODO Ideally we'll have just two bufferviews: 0 is for vertex attributes, 1 for indices
  150. var output = {
  151. id: outputJSON.bufferViews.length - 1,
  152. byteLength: 0
  153. };
  154. return output;
  155. }
  156. /**
  157. * Process attribute to generate an accessor
  158. * @param {THREE.WebGLAttribute} attribute Attribute to process
  159. * @return {Integer} Index of the processed accessor on the "accessors" array
  160. */
  161. function processAccessor ( attribute ) {
  162. if ( !outputJSON.accessors ) {
  163. outputJSON.accessors = [];
  164. }
  165. var types = [
  166. 'SCALAR',
  167. 'VEC2',
  168. 'VEC3',
  169. 'VEC4'
  170. ];
  171. var componentType;
  172. // Detect the component type of the attribute array (float, uint or ushort)
  173. if ( attribute.array.constructor === Float32Array ) {
  174. componentType = WEBGL_CONSTANTS.FLOAT;
  175. } else if ( attribute.array.constructor === Uint32Array ) {
  176. componentType = WEBGL_CONSTANTS.UNSIGNED_INT;
  177. } else if ( attribute.array.constructor === Uint16Array ) {
  178. componentType = WEBGL_CONSTANTS.UNSIGNED_SHORT;
  179. } else {
  180. throw new Error( 'THREE.GLTFExporter: Unsupported bufferAttribute component type.' );
  181. }
  182. var minMax = getMinMax( attribute );
  183. var bufferView = processBufferView( attribute, componentType );
  184. var gltfAccessor = {
  185. bufferView: bufferView.id,
  186. byteOffset: bufferView.byteOffset,
  187. componentType: componentType,
  188. count: attribute.count,
  189. max: minMax.max,
  190. min: minMax.min,
  191. type: types[ attribute.itemSize - 1 ]
  192. };
  193. outputJSON.accessors.push( gltfAccessor );
  194. return outputJSON.accessors.length - 1;
  195. }
  196. /**
  197. * Process image
  198. * @param {Texture} map Texture to process
  199. * @return {Integer} Index of the processed texture in the "images" array
  200. */
  201. function processImage ( map ) {
  202. if ( !outputJSON.images ) {
  203. outputJSON.images = [];
  204. }
  205. var gltfImage = {};
  206. if ( options.embedImages ) {
  207. // @TODO { bufferView, mimeType }
  208. } else {
  209. // @TODO base64 based on options
  210. gltfImage.uri = map.image.src;
  211. }
  212. outputJSON.images.push( gltfImage );
  213. return outputJSON.images.length - 1;
  214. }
  215. /**
  216. * Process sampler
  217. * @param {Texture} map Texture to process
  218. * @return {Integer} Index of the processed texture in the "samplers" array
  219. */
  220. function processSampler ( map ) {
  221. if ( !outputJSON.samplers ) {
  222. outputJSON.samplers = [];
  223. }
  224. var gltfSampler = {
  225. magFilter: THREE_TO_WEBGL[ map.magFilter ],
  226. minFilter: THREE_TO_WEBGL[ map.minFilter ],
  227. wrapS: THREE_TO_WEBGL[ map.wrapS ],
  228. wrapT: THREE_TO_WEBGL[ map.wrapT ]
  229. };
  230. outputJSON.samplers.push( gltfSampler );
  231. return outputJSON.samplers.length - 1;
  232. }
  233. /**
  234. * Process texture
  235. * @param {Texture} map Map to process
  236. * @return {Integer} Index of the processed texture in the "textures" array
  237. */
  238. function processTexture ( map ) {
  239. if (!outputJSON.textures) {
  240. outputJSON.textures = [];
  241. }
  242. var gltfTexture = {
  243. sampler: processSampler( map ),
  244. source: processImage( map )
  245. };
  246. outputJSON.textures.push( gltfTexture );
  247. return outputJSON.textures.length - 1;
  248. }
  249. /**
  250. * Process material
  251. * @param {THREE.Material} material Material to process
  252. * @return {Integer} Index of the processed material in the "materials" array
  253. */
  254. function processMaterial ( material ) {
  255. if ( !outputJSON.materials ) {
  256. outputJSON.materials = [];
  257. }
  258. if ( !( material instanceof THREE.MeshStandardMaterial ) ) {
  259. console.warn( 'Currently just THREE.StandardMaterial is supported. Material conversion may lose information.' );
  260. }
  261. // @QUESTION Should we avoid including any attribute that has the default value?
  262. var gltfMaterial = {
  263. pbrMetallicRoughness: {}
  264. };
  265. // pbrMetallicRoughness.baseColorFactor
  266. var color = material.color.toArray().concat( [ material.opacity ] );
  267. if ( !equalArray( color, [ 1, 1, 1, 1 ] ) ) {
  268. gltfMaterial.pbrMetallicRoughness.baseColorFactor = color;
  269. }
  270. if ( material instanceof THREE.MeshStandardMaterial ) {
  271. gltfMaterial.pbrMetallicRoughness.metallicFactor = material.metalness;
  272. gltfMaterial.pbrMetallicRoughness.roughnessFactor = material.roughness;
  273. } else {
  274. gltfMaterial.pbrMetallicRoughness.metallicFactor = 0.5;
  275. gltfMaterial.pbrMetallicRoughness.roughnessFactor = 0.5;
  276. }
  277. // pbrMetallicRoughness.baseColorTexture
  278. if ( material.map ) {
  279. gltfMaterial.pbrMetallicRoughness.baseColorTexture = {
  280. index: processTexture( material.map ),
  281. texCoord: 0 // @FIXME
  282. };
  283. }
  284. if ( material instanceof THREE.MeshBasicMaterial ||
  285. material instanceof THREE.LineBasicMaterial ||
  286. material instanceof THREE.PointsMaterial ) {
  287. } else {
  288. // emissiveFactor
  289. var emissive = material.emissive.clone().multiplyScalar( material.emissiveIntensity ).toArray();
  290. if ( !equalArray( emissive, [ 0, 0, 0 ] ) ) {
  291. gltfMaterial.emissiveFactor = emissive;
  292. }
  293. // emissiveTexture
  294. if ( material.emissiveMap ) {
  295. gltfMaterial.emissiveTexture = {
  296. index: processTexture( material.emissiveMap ),
  297. texCoord: 0 // @FIXME
  298. };
  299. }
  300. }
  301. // normalTexture
  302. if ( material.normalMap ) {
  303. gltfMaterial.normalTexture = {
  304. index: processTexture( material.normalMap ),
  305. texCoord: 0 // @FIXME
  306. };
  307. }
  308. // occlusionTexture
  309. if ( material.aoMap ) {
  310. gltfMaterial.occlusionTexture = {
  311. index: processTexture( material.aoMap ),
  312. texCoord: 0 // @FIXME
  313. };
  314. }
  315. // alphaMode
  316. if ( material.transparent ) {
  317. gltfMaterial.alphaMode = 'BLEND'; // @FIXME We should detect MASK or BLEND
  318. }
  319. // doubleSided
  320. if ( material.side === THREE.DoubleSide ) {
  321. gltfMaterial.doubleSided = true;
  322. }
  323. if ( material.name ) {
  324. gltfMaterial.name = material.name;
  325. }
  326. outputJSON.materials.push( gltfMaterial );
  327. return outputJSON.materials.length - 1;
  328. }
  329. /**
  330. * Process mesh
  331. * @param {THREE.Mesh} mesh Mesh to process
  332. * @return {Integer} Index of the processed mesh in the "meshes" array
  333. */
  334. function processMesh( mesh ) {
  335. if ( !outputJSON.meshes ) {
  336. outputJSON.meshes = [];
  337. }
  338. var geometry = mesh.geometry;
  339. // Use the correct mode
  340. if ( mesh instanceof THREE.LineSegments ) {
  341. mode = WEBGL_CONSTANTS.LINES;
  342. } else if ( mesh instanceof THREE.LineLoop ) {
  343. mode = WEBGL_CONSTANTS.LINE_LOOP;
  344. } else if ( mesh instanceof THREE.Line ) {
  345. mode = WEBGL_CONSTANTS.LINE_STRIP;
  346. } else if ( mesh instanceof THREE.Points ) {
  347. mode = WEBGL_CONSTANTS.POINTS;
  348. } else {
  349. if ( !( geometry instanceof THREE.BufferGeometry) ) {
  350. var geometryTemp = new THREE.BufferGeometry();
  351. geometryTemp.fromGeometry( geometry );
  352. geometry = geometryTemp;
  353. }
  354. if ( mesh.drawMode === THREE.TriangleFanDrawMode ) {
  355. console.warn( 'GLTFExporter: TriangleFanDrawMode and wireframe incompatible.' );
  356. mode = WEBGL_CONSTANTS.TRIANGLE_FAN;
  357. } else if ( mesh.drawMode === THREE.TriangleStripDrawMode ) {
  358. mode = mesh.material.wireframe ? WEBGL_CONSTANTS.LINE_STRIP : WEBGL_CONSTANTS.TRIANGLE_STRIP;
  359. } else {
  360. mode = mesh.material.wireframe ? WEBGL_CONSTANTS.LINES : WEBGL_CONSTANTS.TRIANGLES;
  361. }
  362. }
  363. var gltfMesh = {
  364. primitives: [
  365. {
  366. mode: mode,
  367. attributes: {},
  368. material: processMaterial( mesh.material )
  369. }
  370. ]
  371. };
  372. if ( geometry.index ) {
  373. gltfMesh.primitives[ 0 ].indices = processAccessor( geometry.index );
  374. }
  375. // We've just one primitive per mesh
  376. var gltfAttributes = gltfMesh.primitives[ 0 ].attributes;
  377. var attributes = geometry.attributes;
  378. // Conversion between attributes names in threejs and gltf spec
  379. var nameConversion = {
  380. uv: 'TEXCOORD_0',
  381. uv2: 'TEXCOORD_1',
  382. color: 'COLOR_0',
  383. skinWeight: 'WEIGHTS_0',
  384. skinIndex: 'JOINTS_0'
  385. };
  386. // @QUESTION Detect if .vertexColors = THREE.VertexColors?
  387. // For every attribute create an accessor
  388. for ( var attributeName in geometry.attributes ) {
  389. var attribute = geometry.attributes[ attributeName ];
  390. attributeName = nameConversion[ attributeName ] || attributeName.toUpperCase();
  391. gltfAttributes[ attributeName ] = processAccessor( attribute );
  392. }
  393. outputJSON.meshes.push( gltfMesh );
  394. return outputJSON.meshes.length - 1;
  395. }
  396. /**
  397. * Process camera
  398. * @param {THREE.Camera} camera Camera to process
  399. * @return {Integer} Index of the processed mesh in the "camera" array
  400. */
  401. function processCamera( camera ) {
  402. if ( !outputJSON.cameras ) {
  403. outputJSON.cameras = [];
  404. }
  405. var isOrtho = camera instanceof THREE.OrthographicCamera;
  406. var gltfCamera = {
  407. type: isOrtho ? 'orthographic' : 'perspective'
  408. };
  409. if ( isOrtho ) {
  410. gltfCamera.orthographic = {
  411. xmag: camera.right * 2,
  412. ymag: camera.top * 2,
  413. zfar: camera.far,
  414. znear: camera.near
  415. };
  416. } else {
  417. gltfCamera.perspective = {
  418. aspectRatio: camera.aspect,
  419. yfov: THREE.Math.degToRad( camera.fov ) / camera.aspect,
  420. zfar: camera.far,
  421. znear: camera.near
  422. };
  423. }
  424. if ( camera.name ) {
  425. gltfCamera.name = camera.type;
  426. }
  427. outputJSON.cameras.push( gltfCamera );
  428. return outputJSON.cameras.length - 1;
  429. }
  430. /**
  431. * Process Object3D node
  432. * @param {THREE.Object3D} node Object3D to processNode
  433. * @return {Integer} Index of the node in the nodes list
  434. */
  435. function processNode ( object ) {
  436. if ( !outputJSON.nodes ) {
  437. outputJSON.nodes = [];
  438. }
  439. var gltfNode = {};
  440. if ( options.trs ) {
  441. var rotation = object.quaternion.toArray();
  442. var position = object.position.toArray();
  443. var scale = object.scale.toArray();
  444. if ( !equalArray( rotation, [ 0, 0, 0, 1 ] ) ) {
  445. gltfNode.rotation = rotation;
  446. }
  447. if ( !equalArray( position, [ 0, 0, 0 ] ) ) {
  448. gltfNode.position = position;
  449. }
  450. if ( !equalArray( scale, [ 1, 1, 1 ] ) ) {
  451. gltfNode.scale = scale;
  452. }
  453. } else {
  454. object.updateMatrix();
  455. if (! equalArray( object.matrix.elements, [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ] ) ) {
  456. gltfNode.matrix = object.matrix.elements;
  457. }
  458. }
  459. if ( object.name ) {
  460. gltfNode.name = object.name;
  461. }
  462. if ( object.userData && Object.keys( object.userData ).length > 0 ) {
  463. try {
  464. gltfNode.extras = JSON.parse( JSON.stringify( object.userData ) );
  465. } catch (e) {
  466. throw new Error( 'GLTFExporter: userData can\'t be serialized' );
  467. }
  468. }
  469. if ( object instanceof THREE.Mesh ||
  470. object instanceof THREE.Line ||
  471. object instanceof THREE.Points ) {
  472. gltfNode.mesh = processMesh( object );
  473. } else if ( object instanceof THREE.Camera ) {
  474. gltfNode.camera = processCamera( object );
  475. }
  476. if ( object.children.length > 0 ) {
  477. gltfNode.children = [];
  478. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  479. var child = object.children[ i ];
  480. if ( child instanceof THREE.Mesh ||
  481. child instanceof THREE.Camera ||
  482. child instanceof THREE.Group ||
  483. child instanceof THREE.Line ||
  484. child instanceof THREE.Points) {
  485. gltfNode.children.push( processNode( child ) );
  486. }
  487. }
  488. }
  489. outputJSON.nodes.push( gltfNode );
  490. return outputJSON.nodes.length - 1;
  491. }
  492. /**
  493. * Process Scene
  494. * @param {THREE.Scene} node Scene to process
  495. */
  496. function processScene( scene ) {
  497. if ( !outputJSON.scenes ) {
  498. outputJSON.scenes = [];
  499. outputJSON.scene = 0;
  500. }
  501. var gltfScene = {
  502. nodes: []
  503. };
  504. if ( scene.name ) {
  505. gltfScene.name = scene.name;
  506. }
  507. outputJSON.scenes.push( gltfScene );
  508. for ( var i = 0, l = scene.children.length; i < l; i ++ ) {
  509. var child = scene.children[ i ];
  510. // @TODO We don't process lights yet
  511. if ( child instanceof THREE.Mesh ||
  512. child instanceof THREE.Camera ||
  513. child instanceof THREE.Group ||
  514. child instanceof THREE.Line ||
  515. child instanceof THREE.Points) {
  516. gltfScene.nodes.push( processNode( child ) );
  517. }
  518. }
  519. }
  520. /**
  521. * Creates a THREE.Scene to hold a list of objects and parse it
  522. * @param {Array} objects List of objects to process
  523. */
  524. function processObjects ( objects ) {
  525. var scene = new THREE.Scene();
  526. scene.name = 'AuxScene';
  527. for ( var i = 0; i < objects.length; i++ ) {
  528. // We push directly to children instead of calling `add` to prevent
  529. // modify the .parent and break its original scene and hierarchy
  530. scene.children.push( objects[ i ] );
  531. }
  532. processScene( scene );
  533. }
  534. function processInput( input ) {
  535. input = input instanceof Array ? input : [ input ];
  536. var objectsWithoutScene = [];
  537. for ( i = 0; i < input.length; i++ ) {
  538. if ( input[ i ] instanceof THREE.Scene ) {
  539. processScene( input[ i ] );
  540. } else {
  541. objectsWithoutScene.push( input[ i ] );
  542. }
  543. }
  544. if ( objectsWithoutScene.length > 0 ) {
  545. processObjects( objectsWithoutScene );
  546. }
  547. }
  548. processInput( input );
  549. // Generate buffer
  550. // Create a new blob with all the dataviews from the buffers
  551. var blob = new Blob( dataViews, { type: 'application/octet-binary' } );
  552. // Update the bytlength of the only main buffer and update the uri with the base64 representation of it
  553. if ( outputJSON.buffers && outputJSON.buffers.length > 0 ) {
  554. outputJSON.buffers[ 0 ].byteLength = blob.size;
  555. objectURL = URL.createObjectURL( blob );
  556. var reader = new window.FileReader();
  557. reader.readAsDataURL( blob );
  558. reader.onloadend = function() {
  559. base64data = reader.result;
  560. outputJSON.buffers[ 0 ].uri = base64data;
  561. onDone( outputJSON );
  562. };
  563. } else {
  564. onDone ( outputJSON );
  565. }
  566. }
  567. };