Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

three-with-encantar.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * @file three.js plugin for encantar.js
  3. * @author Alexandre Martins (https://github.com/alemart)
  4. * @license LGPL-3.0-or-later
  5. */
  6. /* Usage of the indicated versions is encouraged */
  7. __THIS_PLUGIN_HAS_BEEN_TESTED_WITH__({
  8. 'encantar.js': { version: '0.3.0' },
  9. 'three.js': { version: '147' }
  10. });
  11. /**
  12. * Base class for augmented scenes
  13. */
  14. class ARScene
  15. {
  16. /**
  17. * Start the AR session
  18. * @abstract
  19. * @returns {Promise<Session> | SpeedyPromise<Session>}
  20. */
  21. startSession()
  22. {
  23. throw new Error('Abstract method');
  24. }
  25. /**
  26. * Initialize the augmented scene
  27. * @abstract
  28. * @param {ARPluginSystem} ar
  29. * @returns {void | Promise<void> | SpeedyPromise<void>}
  30. */
  31. init(ar)
  32. {
  33. throw new Error('Abstract method');
  34. }
  35. /**
  36. * Update / animate the augmented scene
  37. * @abstract
  38. * @param {ARPluginSystem} ar
  39. * @returns {void}
  40. */
  41. update(ar)
  42. {
  43. throw new Error('Abstract method');
  44. }
  45. /**
  46. * Release the augmented scene
  47. * @param {ARPluginSystem} ar
  48. * @returns {void}
  49. */
  50. release(ar)
  51. {
  52. // optional implementation
  53. }
  54. }
  55. /**
  56. * Helper for augmenting the scenes with three.js
  57. */
  58. class ARPluginSystem
  59. {
  60. /**
  61. * AR Session
  62. * @returns {Session}
  63. */
  64. get session()
  65. {
  66. return this._session;
  67. }
  68. /**
  69. * Current frame: an object holding data to augment the physical scene.
  70. * If the AR scene is not initialized, this will be null.
  71. * @returns {Frame | null}
  72. */
  73. get frame()
  74. {
  75. return this._frame;
  76. }
  77. /**
  78. * The root is a 3D object that is automatically aligned to the physical
  79. * scene. Objects of your virtual scene should be descendants of this node.
  80. * The root is only visible if something is being tracked.
  81. * @returns {THREE.Group}
  82. */
  83. get root()
  84. {
  85. return this._root;
  86. }
  87. /**
  88. * The three.js scene
  89. * @returns {THREE.Scene}
  90. */
  91. get scene()
  92. {
  93. return this._scene;
  94. }
  95. /**
  96. * A camera that is automatically adjusted for AR
  97. * @returns {THREE.Camera}
  98. */
  99. get camera()
  100. {
  101. return this._camera;
  102. }
  103. /**
  104. * The three.js renderer
  105. * @returns {THREE.WebGLRenderer}
  106. */
  107. get renderer()
  108. {
  109. return this._renderer;
  110. }
  111. /**
  112. * Constructor
  113. */
  114. constructor()
  115. {
  116. this._session = null;
  117. this._frame = null;
  118. this._origin = null;
  119. this._root = null;
  120. this._scene = null;
  121. this._camera = null;
  122. this._renderer = null;
  123. }
  124. }
  125. /**
  126. * Do magic to connect encantar.js to three.js
  127. * @param {ARScene} scene
  128. * @returns {Promise<ARPluginSystem> | SpeedyPromise<ARPluginSystem>}
  129. */
  130. function encantar(scene)
  131. {
  132. const ar = new ARPluginSystem();
  133. function mix(frame)
  134. {
  135. ar._root.visible = false;
  136. for(const result of frame.results) {
  137. if(result.tracker.type == 'image-tracker') {
  138. if(result.trackables.length > 0) {
  139. const trackable = result.trackables[0];
  140. const projectionMatrix = result.viewer.view.projectionMatrix;
  141. const viewMatrixInverse = result.viewer.pose.transform.matrix;
  142. const modelMatrix = trackable.pose.transform.matrix;
  143. align(projectionMatrix, viewMatrixInverse, modelMatrix);
  144. ar._root.visible = true;
  145. return;
  146. }
  147. }
  148. }
  149. }
  150. function align(projectionMatrix, viewMatrixInverse, modelMatrix)
  151. {
  152. ar._camera.projectionMatrix.fromArray(projectionMatrix.read());
  153. ar._camera.projectionMatrixInverse.copy(ar._camera.projectionMatrix).invert();
  154. ar._camera.matrix.fromArray(viewMatrixInverse.read());
  155. ar._camera.updateMatrixWorld(true);
  156. ar._origin.matrix.fromArray(modelMatrix.read());
  157. ar._origin.updateMatrixWorld(true);
  158. }
  159. function animate(time, frame)
  160. {
  161. ar._frame = frame;
  162. mix(frame);
  163. scene.update(ar);
  164. ar._renderer.render(ar._scene, ar._camera);
  165. ar._session.requestAnimationFrame(animate);
  166. }
  167. return scene.startSession().then(session => {
  168. ar._session = session;
  169. ar._scene = new THREE.Scene();
  170. ar._origin = new THREE.Group();
  171. ar._origin.matrixAutoUpdate = false;
  172. ar._scene.add(ar._origin);
  173. ar._root = new THREE.Group();
  174. ar._origin.add(ar._root);
  175. ar._camera = new THREE.PerspectiveCamera();
  176. ar._camera.matrixAutoUpdate = false;
  177. ar._renderer = new THREE.WebGLRenderer({
  178. canvas: session.viewport.canvas,
  179. alpha: true,
  180. });
  181. session.addEventListener('end', event => {
  182. ar._root.visible = false;
  183. scene.release(ar);
  184. ar._frame = null;
  185. });
  186. session.viewport.addEventListener('resize', event => {
  187. const size = session.viewport.virtualSize;
  188. ar._renderer.setPixelRatio(1.0);
  189. ar._renderer.setSize(size.width, size.height, false);
  190. });
  191. let init = scene.init(ar);
  192. if(!(typeof init === 'object' && 'then' in init))
  193. init = Promise.resolve();
  194. return init.then(() => {
  195. ar.session.requestAnimationFrame(animate);
  196. return ar;
  197. });
  198. }).catch(error => {
  199. console.error(error);
  200. alert(error.message);
  201. throw error;
  202. });
  203. }
  204. /**
  205. * Version check
  206. * @param {object} json
  207. */
  208. function __THIS_PLUGIN_HAS_BEEN_TESTED_WITH__(json)
  209. {
  210. window.addEventListener('load', () => {
  211. try { AR, __THREE__;
  212. const versionOf = { 'encantar.js': AR.version.replace(/-.*$/, ''), 'three.js': __THREE__ };
  213. const check = (x,v,w) => v !== w ? console.warn(`\n\n\nWARNING\n\nThis plugin has been tested with ${x} version ${v}. The version in use is ${w}. Usage of ${x} version ${v} is recommended instead.\n\n\n`) : void 0;
  214. for(const [x, expected] of Object.entries(json))
  215. check(x, expected.version, versionOf[x]);
  216. }
  217. catch(e) {
  218. alert(e.message);
  219. }
  220. });
  221. }