Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

demo.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. window.addEventListener('load', () => {
  2. const my = { };
  3. async function initialize(ar)
  4. {
  5. // add lights
  6. const ambientLight = new THREE.AmbientLight(0xb7b7b7);
  7. const directionalLight = new THREE.DirectionalLight(0xffffff, 0.4);
  8. directionalLight.position.set(0, 0, -1);
  9. directionalLight.target.position.set(0, 0, 0);
  10. ar.scene.add(directionalLight);
  11. ar.scene.add(ambientLight);
  12. // create a group of objects as a child of ar.root
  13. const group = createGroup('in-front');
  14. //const group = createGroup('on-top'); // try this option!
  15. ar.root.add(group);
  16. // create cubes
  17. const cubeA = createCube(-0.75, 0, 0xffff00);
  18. const cubeB = createCube(0.75, 0, 0x00ff00);
  19. group.add(cubeA, cubeB);
  20. // create the ground
  21. const ground = createGround(0x3d5afe);
  22. group.add(ground);
  23. // load a 3D model
  24. const modelURL = '../assets/my-3d-model.glb';
  25. const model = await loadModel(modelURL);
  26. group.add(model);
  27. // export objects
  28. my.cubes = [ cubeA, cubeB ];
  29. my.group = group;
  30. my.model = model;
  31. my.ground = ground;
  32. }
  33. function animate(ar)
  34. {
  35. const ROTATION_CYCLES_PER_SECOND = 1.0;
  36. const TWO_PI = 2.0 * Math.PI;
  37. const delta = ar.session.time.delta;
  38. // rotate the cubes
  39. for(const cube of my.cubes)
  40. cube.rotateY(TWO_PI * ROTATION_CYCLES_PER_SECOND * delta);
  41. }
  42. function createGroup(mode = 'in-front')
  43. {
  44. const group = new THREE.Group();
  45. if(mode == 'in-front') {
  46. group.rotation.set(-Math.PI/2, 0, 0);
  47. group.position.set(0, -0.5, 0.5);
  48. }
  49. else if(mode == 'on-top') {
  50. group.rotation.set(0, 0, 0);
  51. group.position.set(0, 0, 0);
  52. }
  53. return group;
  54. }
  55. function createCube(x, y, color, length = 0.25)
  56. {
  57. const geometry = new THREE.BoxGeometry(length, length, length);
  58. const material = new THREE.MeshPhongMaterial({ color });
  59. const cube = new THREE.Mesh(geometry, material);
  60. cube.position.set(x, y, 1.25);
  61. return cube;
  62. }
  63. function createGround(color)
  64. {
  65. const geometry = new THREE.RingGeometry(0.001, 1, 8);
  66. const material = new THREE.MeshPhongMaterial({ color: color, side: THREE.DoubleSide });
  67. const ground = new THREE.Mesh(geometry, material);
  68. material.transparent = true;
  69. material.opacity = 0.75;
  70. return ground;
  71. }
  72. async function loadModel(filepath)
  73. {
  74. const loader = new THREE.GLTFLoader();
  75. const gltf = await loader.loadAsync(filepath);
  76. const model = gltf.scene;
  77. return model;
  78. }
  79. async function startARSession()
  80. {
  81. if(!AR.isSupported()) {
  82. throw new Error(
  83. 'This device is not compatible with this AR experience.\n\n' +
  84. 'User agent: ' + navigator.userAgent
  85. );
  86. }
  87. //AR.Settings.powerPreference = 'low-power';
  88. const tracker = AR.Tracker.ImageTracker();
  89. await tracker.database.add([{
  90. name: 'my-reference-image',
  91. image: document.getElementById('my-reference-image')
  92. }]);
  93. const viewport = AR.Viewport({
  94. container: document.getElementById('ar-viewport'),
  95. hudContainer: document.getElementById('ar-hud')
  96. });
  97. const video = document.getElementById('my-video');
  98. const useWebcam = (video === null);
  99. const source = useWebcam ?
  100. AR.Source.Camera({ resolution: 'md' }) :
  101. AR.Source.Video(video);
  102. const session = await AR.startSession({
  103. mode: 'immersive',
  104. viewport: viewport,
  105. trackers: [ tracker ],
  106. sources: [ source ],
  107. stats: true,
  108. gizmos: true,
  109. });
  110. const scan = document.getElementById('scan');
  111. tracker.addEventListener('targetfound', event => {
  112. session.gizmos.visible = false;
  113. if(scan)
  114. scan.hidden = true;
  115. });
  116. tracker.addEventListener('targetlost', event => {
  117. session.gizmos.visible = true;
  118. if(scan)
  119. scan.hidden = false;
  120. });
  121. return session;
  122. }
  123. // enchant!
  124. encantar(startARSession, animate, initialize);
  125. });