BabylonLoader.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.BabylonLoader = function ( manager ) {
  5. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  6. };
  7. THREE.BabylonLoader.prototype = {
  8. constructor: THREE.BabylonLoader,
  9. load: function ( url, onLoad, onProgress, onError ) {
  10. var scope = this;
  11. var loader = new THREE.XHRLoader( scope.manager );
  12. loader.load( url, function ( text ) {
  13. onLoad( scope.parse( JSON.parse( text ) ) );
  14. }, onProgress, onError );
  15. },
  16. parse: function ( json ) {
  17. var materials = this.parseMaterials( json );
  18. var scene = this.parseObjects( json, materials );
  19. return scene;
  20. },
  21. parseMaterials: function ( json ) {
  22. var materials = {};
  23. for ( var i = 0, l = json.materials.length; i < l; i ++ ) {
  24. var data = json.materials[ i ];
  25. var material = new THREE.MeshPhongMaterial();
  26. material.name = data.name;
  27. material.color.fromArray( data.diffuse );
  28. material.emissive.fromArray( data.emissive );
  29. material.specular.fromArray( data.specular );
  30. material.shininess = data.specularPower;
  31. material.opacity = data.alpha;
  32. materials[ data.id ] = material;
  33. }
  34. if ( json.multiMaterials ) {
  35. for ( var i = 0, l = json.multiMaterials.length; i < l; i ++ ) {
  36. var data = json.multiMaterials[ i ];
  37. console.warn( 'THREE.BabylonLoader: Multi materials not yet supported.' );
  38. materials[ data.id ] = new THREE.MeshPhongMaterial();
  39. }
  40. }
  41. return materials;
  42. },
  43. parseGeometry: function ( json ) {
  44. var geometry = new THREE.BufferGeometry();
  45. // indices
  46. var indices = new Uint16Array( json.indices );
  47. geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
  48. // positions
  49. var positions = new Float32Array( json.positions );
  50. for ( var j = 2, jl = positions.length; j < jl; j += 3 ) {
  51. positions[ j ] = - positions[ j ];
  52. }
  53. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  54. // normals
  55. if ( json.normals ) {
  56. var normals = new Float32Array( json.normals );
  57. for ( var j = 2, jl = normals.length; j < jl; j += 3 ) {
  58. normals[ j ] = - normals[ j ];
  59. }
  60. geometry.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
  61. }
  62. // uvs
  63. if ( json.uvs ) {
  64. var uvs = new Float32Array( json.uvs );
  65. geometry.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );
  66. }
  67. // offsets
  68. var subMeshes = json.subMeshes;
  69. if ( subMeshes ) {
  70. for ( var j = 0, jl = subMeshes.length; j < jl; j ++ ) {
  71. var subMesh = subMeshes[ j ];
  72. geometry.addGroup( subMesh.indexStart, subMesh.indexCount );
  73. }
  74. }
  75. return geometry;
  76. },
  77. parseObjects: function ( json, materials ) {
  78. var objects = {};
  79. var scene = new THREE.Scene();
  80. var cameras = json.cameras;
  81. for ( var i = 0, l = cameras.length; i < l; i ++ ) {
  82. var data = cameras[ i ];
  83. var camera = new THREE.PerspectiveCamera( ( data.fov / Math.PI ) * 180, 1.33, data.minZ, data.maxZ );
  84. camera.name = data.name;
  85. camera.position.fromArray( data.position );
  86. if ( data.rotation ) camera.rotation.fromArray( data.rotation );
  87. objects[ data.id ] = camera;
  88. }
  89. var lights = json.lights;
  90. for ( var i = 0, l = lights.length; i < l; i ++ ) {
  91. var data = lights[ i ];
  92. var light;
  93. switch ( data.type ) {
  94. case 0:
  95. light = new THREE.PointLight();
  96. break;
  97. case 1:
  98. light = new THREE.DirectionalLight();
  99. break;
  100. case 2:
  101. light = new THREE.SpotLight();
  102. break;
  103. case 3:
  104. light = new THREE.HemisphereLight();
  105. break;
  106. }
  107. light.name = data.name;
  108. if ( data.position ) light.position.set( data.position[ 0 ], data.position[ 1 ], - data.position[ 2 ] );
  109. light.color.fromArray( data.diffuse );
  110. if ( data.groundColor ) light.groundColor.fromArray( data.groundColor );
  111. if ( data.intensity ) light.intensity = data.intensity;
  112. objects[ data.id ] = light;
  113. scene.add( light );
  114. }
  115. var meshes = json.meshes;
  116. for ( var i = 0, l = meshes.length; i < l; i ++ ) {
  117. var data = meshes[ i ];
  118. var object;
  119. if ( data.indices ) {
  120. var geometry = this.parseGeometry( data );
  121. object = new THREE.Mesh( geometry, materials[ data.materialId ] );
  122. } else {
  123. object = new THREE.Group();
  124. }
  125. object.name = data.name;
  126. object.position.set( data.position[ 0 ], data.position[ 1 ], - data.position[ 2 ] );
  127. object.rotation.fromArray( data.rotation );
  128. if ( data.rotationQuaternion ) object.quaternion.fromArray( data.rotationQuaternion );
  129. object.scale.fromArray( data.scaling );
  130. // object.visible = data.isVisible;
  131. if ( data.parentId ) {
  132. objects[ data.parentId ].add( object );
  133. } else {
  134. scene.add( object );
  135. }
  136. objects[ data.id ] = object;
  137. }
  138. return scene;
  139. }
  140. };