webxr_ar_paint.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js ar - paint</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> ar - paint<br/>Enable chrome://flags/#webxr-ar-module<br/>(Chrome Android 80+)
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { TubePainter } from './jsm/misc/TubePainter.js';
  16. import { ARButton } from './jsm/webxr/ARButton.js';
  17. var container;
  18. var camera, scene, renderer;
  19. var controller, painter;
  20. var cursor = new THREE.Vector3();
  21. init();
  22. animate();
  23. function init() {
  24. container = document.createElement( 'div' );
  25. document.body.appendChild( container );
  26. scene = new THREE.Scene();
  27. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 20 );
  28. //
  29. renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
  30. renderer.setPixelRatio( window.devicePixelRatio );
  31. renderer.setSize( window.innerWidth, window.innerHeight );
  32. renderer.xr.enabled = true;
  33. container.appendChild( renderer.domElement );
  34. //
  35. document.body.appendChild( ARButton.createButton( renderer ) );
  36. // model
  37. var light = new THREE.HemisphereLight( 0xffffff, 0xbbbbff, 1 );
  38. light.position.set( 0, 1, 0 );
  39. scene.add( light );
  40. //
  41. painter = new TubePainter();
  42. painter.setSize( 0.4 );
  43. painter.mesh.material.side = THREE.DoubleSide;
  44. scene.add( painter.mesh );
  45. //
  46. function onSelectStart() {
  47. this.userData.isSelecting = true;
  48. this.userData.skipFrames = 2;
  49. }
  50. function onSelectEnd() {
  51. this.userData.isSelecting = false;
  52. }
  53. controller = renderer.xr.getController( 0 );
  54. controller.addEventListener( 'selectstart', onSelectStart );
  55. controller.addEventListener( 'selectend', onSelectEnd );
  56. controller.userData.skipFrames = 0;
  57. scene.add( controller );
  58. //
  59. window.addEventListener( 'resize', onWindowResize, false );
  60. }
  61. function onWindowResize() {
  62. camera.aspect = window.innerWidth / window.innerHeight;
  63. camera.updateProjectionMatrix();
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. }
  66. //
  67. function handleController( controller ) {
  68. var userData = controller.userData;
  69. cursor.set( 0, 0, - 0.2 ).applyMatrix4( controller.matrixWorld );
  70. if ( userData.isSelecting === true ) {
  71. if ( userData.skipFrames >= 0 ) {
  72. // TODO(mrdoob) Revisit this
  73. userData.skipFrames --;
  74. painter.moveTo( cursor );
  75. } else {
  76. painter.lineTo( cursor );
  77. painter.update();
  78. }
  79. }
  80. }
  81. function animate() {
  82. renderer.setAnimationLoop( render );
  83. }
  84. function render() {
  85. handleController( controller );
  86. renderer.render( scene, camera );
  87. }
  88. </script>
  89. </body>
  90. </html>