three-model.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. AFRAME.registerComponent('three-model', require('./src/loaders/three-model'));
  3. },{"./src/loaders/three-model":2}],2:[function(require,module,exports){
  4. var DEFAULT_ANIMATION = '__auto__';
  5. /**
  6. * three-model
  7. *
  8. * Loader for THREE.js JSON format. Somewhat confusingly, there are two
  9. * different THREE.js formats, both having the .json extension. This loader
  10. * supports both, but requires you to specify the mode as "object" or "json".
  11. *
  12. * Typically, you will use "json" for a single mesh, and "object" for a scene
  13. * or multiple meshes. Check the console for errors, if in doubt.
  14. *
  15. * See: https://clara.io/learn/user-guide/data_exchange/threejs_export
  16. */
  17. module.exports = {
  18. deprecated: true,
  19. schema: {
  20. src: { type: 'asset' },
  21. loader: { default: 'object', oneOf: ['object', 'json'] },
  22. enableAnimation: { default: true },
  23. animation: { default: DEFAULT_ANIMATION },
  24. animationDuration: { default: 0 },
  25. crossorigin: { default: '' }
  26. },
  27. init: function () {
  28. this.model = null;
  29. this.mixer = null;
  30. console.warn('[three-model] Component is deprecated. Use json-model or object-model instead.');
  31. },
  32. update: function (previousData) {
  33. previousData = previousData || {};
  34. var loader,
  35. data = this.data;
  36. if (!data.src) {
  37. this.remove();
  38. return;
  39. }
  40. // First load.
  41. if (!Object.keys(previousData).length) {
  42. this.remove();
  43. if (data.loader === 'object') {
  44. loader = new THREE.ObjectLoader();
  45. if (data.crossorigin) loader.setCrossOrigin(data.crossorigin);
  46. loader.load(data.src, function(loaded) {
  47. loaded.traverse( function(object) {
  48. if (object instanceof THREE.SkinnedMesh)
  49. loaded = object;
  50. });
  51. if(loaded.material)
  52. loaded.material.skinning = !!((loaded.geometry && loaded.geometry.bones) || []).length;
  53. this.load(loaded);
  54. }.bind(this));
  55. } else if (data.loader === 'json') {
  56. loader = new THREE.JSONLoader();
  57. if (data.crossorigin) loader.crossOrigin = data.crossorigin;
  58. loader.load(data.src, function (geometry, materials) {
  59. // Attempt to automatically detect common material options.
  60. materials.forEach(function (mat) {
  61. mat.vertexColors = (geometry.faces[0] || {}).color ? THREE.FaceColors : THREE.NoColors;
  62. mat.skinning = !!(geometry.bones || []).length;
  63. mat.morphTargets = !!(geometry.morphTargets || []).length;
  64. mat.morphNormals = !!(geometry.morphNormals || []).length;
  65. });
  66. var mesh = (geometry.bones || []).length
  67. ? new THREE.SkinnedMesh(geometry, new THREE.MultiMaterial(materials))
  68. : new THREE.Mesh(geometry, new THREE.MultiMaterial(materials));
  69. this.load(mesh);
  70. }.bind(this));
  71. } else {
  72. throw new Error('[three-model] Invalid mode "%s".', data.mode);
  73. }
  74. return;
  75. }
  76. var activeAction = this.model && this.model.activeAction;
  77. if (data.animation !== previousData.animation) {
  78. if (activeAction) activeAction.stop();
  79. this.playAnimation();
  80. return;
  81. }
  82. if (activeAction && data.enableAnimation !== activeAction.isRunning()) {
  83. data.enableAnimation ? this.playAnimation() : activeAction.stop();
  84. }
  85. if (activeAction && data.animationDuration) {
  86. activeAction.setDuration(data.animationDuration);
  87. }
  88. },
  89. load: function (model) {
  90. this.model = model;
  91. this.mixer = new THREE.AnimationMixer(this.model);
  92. this.el.setObject3D('mesh', model);
  93. this.el.emit('model-loaded', {format: 'three', model: model});
  94. if (this.data.enableAnimation) this.playAnimation();
  95. },
  96. playAnimation: function () {
  97. var clip,
  98. data = this.data,
  99. animations = this.model.animations || this.model.geometry.animations || [];
  100. if (!data.enableAnimation || !data.animation || !animations.length) {
  101. return;
  102. }
  103. clip = data.animation === DEFAULT_ANIMATION
  104. ? animations[0]
  105. : THREE.AnimationClip.findByName(animations, data.animation);
  106. if (!clip) {
  107. console.error('[three-model] Animation "%s" not found.', data.animation);
  108. return;
  109. }
  110. this.model.activeAction = this.mixer.clipAction(clip, this.model);
  111. if (data.animationDuration) {
  112. this.model.activeAction.setDuration(data.animationDuration);
  113. }
  114. this.model.activeAction.play();
  115. },
  116. remove: function () {
  117. if (this.mixer) this.mixer.stopAllAction();
  118. if (this.model) this.el.removeObject3D('mesh');
  119. },
  120. tick: function (t, dt) {
  121. if (this.mixer && !isNaN(dt)) {
  122. this.mixer.update(dt / 1000);
  123. }
  124. }
  125. };
  126. },{}]},{},[1]);