MaterialLoader.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.MaterialLoader = function () {};
  5. THREE.MaterialLoader.prototype = {
  6. constructor: THREE.MaterialLoader,
  7. addEventListener: THREE.EventDispatcher.prototype.addEventListener,
  8. hasEventListener: THREE.EventDispatcher.prototype.hasEventListener,
  9. removeEventListener: THREE.EventDispatcher.prototype.removeEventListener,
  10. dispatchEvent: THREE.EventDispatcher.prototype.dispatchEvent,
  11. load: function ( url ) {
  12. var scope = this;
  13. var request = new XMLHttpRequest();
  14. request.addEventListener( 'load', function ( event ) {
  15. var response = scope.parse( JSON.parse( event.target.responseText ) );
  16. scope.dispatchEvent( { type: 'load', content: response } );
  17. }, false );
  18. request.addEventListener( 'progress', function ( event ) {
  19. scope.dispatchEvent( { type: 'progress', loaded: event.loaded, total: event.total } );
  20. }, false );
  21. request.addEventListener( 'error', function () {
  22. scope.dispatchEvent( { type: 'error', message: 'Couldn\'t load URL [' + url + ']' } );
  23. }, false );
  24. request.open( 'GET', url, true );
  25. request.send( null );
  26. },
  27. parse: function ( json ) {
  28. var material;
  29. switch ( json.type ) {
  30. case 'MeshBasicMaterial':
  31. material = new THREE.MeshBasicMaterial( {
  32. color: json.color,
  33. opacity: json.opacity,
  34. transparent: json.transparent,
  35. wireframe: json.wireframe
  36. } );
  37. break;
  38. case 'MeshLambertMaterial':
  39. material = new THREE.MeshLambertMaterial( {
  40. color: json.color,
  41. ambient: json.ambient,
  42. emissive: json.emissive,
  43. opacity: json.opacity,
  44. transparent: json.transparent,
  45. wireframe: json.wireframe
  46. } );
  47. break;
  48. case 'MeshPhongMaterial':
  49. material = new THREE.MeshPhongMaterial( {
  50. color: json.color,
  51. ambient: json.ambient,
  52. emissive: json.emissive,
  53. specular: json.specular,
  54. shininess: json.shininess,
  55. opacity: json.opacity,
  56. transparent: json.transparent,
  57. wireframe: json.wireframe
  58. } );
  59. break;
  60. case 'MeshNormalMaterial':
  61. material = new THREE.MeshNormalMaterial( {
  62. opacity: json.opacity,
  63. transparent: json.transparent,
  64. wireframe: json.wireframe
  65. } );
  66. break;
  67. case 'MeshDepthMaterial':
  68. material = new THREE.MeshDepthMaterial( {
  69. opacity: json.opacity,
  70. transparent: json.transparent,
  71. wireframe: json.wireframe
  72. } );
  73. break;
  74. }
  75. return material;
  76. }
  77. };
粤ICP备19079148号