GLTFExporter.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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.NEAREST_MIPMAP_NEAREST,
  32. 1005: WEBGL_CONSTANTS.NEAREST_MIPMAP_LINEAR,
  33. 1006: WEBGL_CONSTANTS.LINEAR,
  34. 1007: WEBGL_CONSTANTS.LINEAR_MIPMAP_NEAREST,
  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.ShaderMaterial ) {
  259. console.warn( 'GLTFExporter: THREE.ShaderMaterial not supported.' );
  260. return null;
  261. }
  262. if ( !( material instanceof THREE.MeshStandardMaterial ) ) {
  263. console.warn( 'GLTFExporter: Currently just THREE.StandardMaterial is supported. Material conversion may lose information.' );
  264. }
  265. // @QUESTION Should we avoid including any attribute that has the default value?
  266. var gltfMaterial = {
  267. pbrMetallicRoughness: {}
  268. };
  269. // pbrMetallicRoughness.baseColorFactor
  270. var color = material.color.toArray().concat( [ material.opacity ] );
  271. if ( !equalArray( color, [ 1, 1, 1, 1 ] ) ) {
  272. gltfMaterial.pbrMetallicRoughness.baseColorFactor = color;
  273. }
  274. if ( material instanceof THREE.MeshStandardMaterial ) {
  275. gltfMaterial.pbrMetallicRoughness.metallicFactor = material.metalness;
  276. gltfMaterial.pbrMetallicRoughness.roughnessFactor = material.roughness;
  277. } else {
  278. gltfMaterial.pbrMetallicRoughness.metallicFactor = 0.5;
  279. gltfMaterial.pbrMetallicRoughness.roughnessFactor = 0.5;
  280. }
  281. // pbrMetallicRoughness.baseColorTexture
  282. if ( material.map ) {
  283. gltfMaterial.pbrMetallicRoughness.baseColorTexture = {
  284. index: processTexture( material.map ),
  285. texCoord: 0 // @FIXME
  286. };
  287. }
  288. if ( material instanceof THREE.MeshBasicMaterial ||
  289. material instanceof THREE.LineBasicMaterial ||
  290. material instanceof THREE.PointsMaterial ) {
  291. } else {
  292. // emissiveFactor
  293. var emissive = material.emissive.clone().multiplyScalar( material.emissiveIntensity ).toArray();
  294. if ( !equalArray( emissive, [ 0, 0, 0 ] ) ) {
  295. gltfMaterial.emissiveFactor = emissive;
  296. }
  297. // emissiveTexture
  298. if ( material.emissiveMap ) {
  299. gltfMaterial.emissiveTexture = {
  300. index: processTexture( material.emissiveMap ),
  301. texCoord: 0 // @FIXME
  302. };
  303. }
  304. }
  305. // normalTexture
  306. if ( material.normalMap ) {
  307. gltfMaterial.normalTexture = {
  308. index: processTexture( material.normalMap ),
  309. texCoord: 0 // @FIXME
  310. };
  311. }
  312. // occlusionTexture
  313. if ( material.aoMap ) {
  314. gltfMaterial.occlusionTexture = {
  315. index: processTexture( material.aoMap ),
  316. texCoord: 0 // @FIXME
  317. };
  318. }
  319. // alphaMode
  320. if ( material.transparent ) {
  321. gltfMaterial.alphaMode = 'BLEND'; // @FIXME We should detect MASK or BLEND
  322. }
  323. // doubleSided
  324. if ( material.side === THREE.DoubleSide ) {
  325. gltfMaterial.doubleSided = true;
  326. }
  327. if ( material.name ) {
  328. gltfMaterial.name = material.name;
  329. }
  330. outputJSON.materials.push( gltfMaterial );
  331. return outputJSON.materials.length - 1;
  332. }
  333. /**
  334. * Process mesh
  335. * @param {THREE.Mesh} mesh Mesh to process
  336. * @return {Integer} Index of the processed mesh in the "meshes" array
  337. */
  338. function processMesh( mesh ) {
  339. if ( !outputJSON.meshes ) {
  340. outputJSON.meshes = [];
  341. }
  342. var geometry = mesh.geometry;
  343. // Use the correct mode
  344. if ( mesh instanceof THREE.LineSegments ) {
  345. mode = WEBGL_CONSTANTS.LINES;
  346. } else if ( mesh instanceof THREE.LineLoop ) {
  347. mode = WEBGL_CONSTANTS.LINE_LOOP;
  348. } else if ( mesh instanceof THREE.Line ) {
  349. mode = WEBGL_CONSTANTS.LINE_STRIP;
  350. } else if ( mesh instanceof THREE.Points ) {
  351. mode = WEBGL_CONSTANTS.POINTS;
  352. } else {
  353. if ( !( geometry instanceof THREE.BufferGeometry) ) {
  354. var geometryTemp = new THREE.BufferGeometry();
  355. geometryTemp.fromGeometry( geometry );
  356. geometry = geometryTemp;
  357. }
  358. if ( mesh.drawMode === THREE.TriangleFanDrawMode ) {
  359. console.warn( 'GLTFExporter: TriangleFanDrawMode and wireframe incompatible.' );
  360. mode = WEBGL_CONSTANTS.TRIANGLE_FAN;
  361. } else if ( mesh.drawMode === THREE.TriangleStripDrawMode ) {
  362. mode = mesh.material.wireframe ? WEBGL_CONSTANTS.LINE_STRIP : WEBGL_CONSTANTS.TRIANGLE_STRIP;
  363. } else {
  364. mode = mesh.material.wireframe ? WEBGL_CONSTANTS.LINES : WEBGL_CONSTANTS.TRIANGLES;
  365. }
  366. }
  367. var gltfMesh = {
  368. primitives: [
  369. {
  370. mode: mode,
  371. attributes: {},
  372. }
  373. ]
  374. };
  375. var material = processMaterial( mesh.material );
  376. if ( material ) {
  377. gltfMesh.primitives[ 0 ].material = material;
  378. }
  379. if ( geometry.index ) {
  380. gltfMesh.primitives[ 0 ].indices = processAccessor( geometry.index );
  381. }
  382. // We've just one primitive per mesh
  383. var gltfAttributes = gltfMesh.primitives[ 0 ].attributes;
  384. var attributes = geometry.attributes;
  385. // Conversion between attributes names in threejs and gltf spec
  386. var nameConversion = {
  387. uv: 'TEXCOORD_0',
  388. uv2: 'TEXCOORD_1',
  389. color: 'COLOR_0',
  390. skinWeight: 'WEIGHTS_0',
  391. skinIndex: 'JOINTS_0'
  392. };
  393. // @QUESTION Detect if .vertexColors = THREE.VertexColors?
  394. // For every attribute create an accessor
  395. for ( var attributeName in geometry.attributes ) {
  396. var attribute = geometry.attributes[ attributeName ];
  397. attributeName = nameConversion[ attributeName ] || attributeName.toUpperCase();
  398. gltfAttributes[ attributeName ] = processAccessor( attribute );
  399. }
  400. outputJSON.meshes.push( gltfMesh );
  401. return outputJSON.meshes.length - 1;
  402. }
  403. /**
  404. * Process camera
  405. * @param {THREE.Camera} camera Camera to process
  406. * @return {Integer} Index of the processed mesh in the "camera" array
  407. */
  408. function processCamera( camera ) {
  409. if ( !outputJSON.cameras ) {
  410. outputJSON.cameras = [];
  411. }
  412. var isOrtho = camera instanceof THREE.OrthographicCamera;
  413. var gltfCamera = {
  414. type: isOrtho ? 'orthographic' : 'perspective'
  415. };
  416. if ( isOrtho ) {
  417. gltfCamera.orthographic = {
  418. xmag: camera.right * 2,
  419. ymag: camera.top * 2,
  420. zfar: camera.far,
  421. znear: camera.near
  422. };
  423. } else {
  424. gltfCamera.perspective = {
  425. aspectRatio: camera.aspect,
  426. yfov: THREE.Math.degToRad( camera.fov ) / camera.aspect,
  427. zfar: camera.far,
  428. znear: camera.near
  429. };
  430. }
  431. if ( camera.name ) {
  432. gltfCamera.name = camera.type;
  433. }
  434. outputJSON.cameras.push( gltfCamera );
  435. return outputJSON.cameras.length - 1;
  436. }
  437. /**
  438. * Process Object3D node
  439. * @param {THREE.Object3D} node Object3D to processNode
  440. * @return {Integer} Index of the node in the nodes list
  441. */
  442. function processNode ( object ) {
  443. if ( !outputJSON.nodes ) {
  444. outputJSON.nodes = [];
  445. }
  446. var gltfNode = {};
  447. if ( options.trs ) {
  448. var rotation = object.quaternion.toArray();
  449. var position = object.position.toArray();
  450. var scale = object.scale.toArray();
  451. if ( !equalArray( rotation, [ 0, 0, 0, 1 ] ) ) {
  452. gltfNode.rotation = rotation;
  453. }
  454. if ( !equalArray( position, [ 0, 0, 0 ] ) ) {
  455. gltfNode.position = position;
  456. }
  457. if ( !equalArray( scale, [ 1, 1, 1 ] ) ) {
  458. gltfNode.scale = scale;
  459. }
  460. } else {
  461. object.updateMatrix();
  462. if (! equalArray( object.matrix.elements, [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ] ) ) {
  463. gltfNode.matrix = object.matrix.elements;
  464. }
  465. }
  466. if ( object.name ) {
  467. gltfNode.name = object.name;
  468. }
  469. if ( object.userData && Object.keys( object.userData ).length > 0 ) {
  470. try {
  471. gltfNode.extras = JSON.parse( JSON.stringify( object.userData ) );
  472. } catch (e) {
  473. throw new Error( 'GLTFExporter: userData can\'t be serialized' );
  474. }
  475. }
  476. if ( object instanceof THREE.Mesh ||
  477. object instanceof THREE.Line ||
  478. object instanceof THREE.Points ) {
  479. gltfNode.mesh = processMesh( object );
  480. } else if ( object instanceof THREE.Camera ) {
  481. gltfNode.camera = processCamera( object );
  482. }
  483. if ( object.children.length > 0 ) {
  484. var children = [];
  485. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  486. var child = object.children[ i ];
  487. if ( child instanceof THREE.Mesh ||
  488. child instanceof THREE.Camera ||
  489. child instanceof THREE.Group ||
  490. child instanceof THREE.Line ||
  491. child instanceof THREE.Points) {
  492. children.push( processNode( child ) );
  493. }
  494. }
  495. if ( children.length > 0 ) {
  496. gltfNode.children = children;
  497. }
  498. }
  499. outputJSON.nodes.push( gltfNode );
  500. return outputJSON.nodes.length - 1;
  501. }
  502. /**
  503. * Process Scene
  504. * @param {THREE.Scene} node Scene to process
  505. */
  506. function processScene( scene ) {
  507. if ( !outputJSON.scenes ) {
  508. outputJSON.scenes = [];
  509. outputJSON.scene = 0;
  510. }
  511. var gltfScene = {
  512. nodes: []
  513. };
  514. if ( scene.name ) {
  515. gltfScene.name = scene.name;
  516. }
  517. outputJSON.scenes.push( gltfScene );
  518. for ( var i = 0, l = scene.children.length; i < l; i ++ ) {
  519. var child = scene.children[ i ];
  520. var nodes = [];
  521. // @TODO We don't process lights yet
  522. if ( child instanceof THREE.Mesh ||
  523. child instanceof THREE.Camera ||
  524. child instanceof THREE.Group ||
  525. child instanceof THREE.Line ||
  526. child instanceof THREE.Points) {
  527. nodes.push( processNode( child ) );
  528. }
  529. if ( nodes.length > 0 ) {
  530. gltfScene.nodes = nodes;
  531. }
  532. }
  533. }
  534. /**
  535. * Creates a THREE.Scene to hold a list of objects and parse it
  536. * @param {Array} objects List of objects to process
  537. */
  538. function processObjects ( objects ) {
  539. var scene = new THREE.Scene();
  540. scene.name = 'AuxScene';
  541. for ( var i = 0; i < objects.length; i++ ) {
  542. // We push directly to children instead of calling `add` to prevent
  543. // modify the .parent and break its original scene and hierarchy
  544. scene.children.push( objects[ i ] );
  545. }
  546. processScene( scene );
  547. }
  548. function processInput( input ) {
  549. input = input instanceof Array ? input : [ input ];
  550. var objectsWithoutScene = [];
  551. for ( i = 0; i < input.length; i++ ) {
  552. if ( input[ i ] instanceof THREE.Scene ) {
  553. processScene( input[ i ] );
  554. } else {
  555. objectsWithoutScene.push( input[ i ] );
  556. }
  557. }
  558. if ( objectsWithoutScene.length > 0 ) {
  559. processObjects( objectsWithoutScene );
  560. }
  561. }
  562. processInput( input );
  563. // Generate buffer
  564. // Create a new blob with all the dataviews from the buffers
  565. var blob = new Blob( dataViews, { type: 'application/octet-stream' } );
  566. // Update the bytlength of the only main buffer and update the uri with the base64 representation of it
  567. if ( outputJSON.buffers && outputJSON.buffers.length > 0 ) {
  568. outputJSON.buffers[ 0 ].byteLength = blob.size;
  569. objectURL = URL.createObjectURL( blob );
  570. var reader = new window.FileReader();
  571. reader.readAsDataURL( blob );
  572. reader.onloadend = function() {
  573. base64data = reader.result;
  574. outputJSON.buffers[ 0 ].uri = base64data;
  575. onDone( outputJSON );
  576. };
  577. } else {
  578. onDone ( outputJSON );
  579. }
  580. }
  581. };