GLTFExporter.js 19 KB

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