GLTFExporter.js 20 KB

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