BabylonLoader.js 4.8 KB

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