GLTFExporter.js 19 KB

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