webxr_vr_paint.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - 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> vr - paint
  12. </div>
  13. <script src="js/vr/HelioWebXRPolyfill.js"></script>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. import { TubePainter } from './jsm/misc/TubePainter.js';
  18. import { VRButton } from './jsm/webxr/VRButton.js';
  19. var container;
  20. var camera, scene, renderer;
  21. var controller1, controller2;
  22. var cursor = new THREE.Vector3();
  23. var controls;
  24. init();
  25. animate();
  26. function init() {
  27. container = document.createElement( 'div' );
  28. document.body.appendChild( container );
  29. scene = new THREE.Scene();
  30. scene.background = new THREE.Color( 0x222222 );
  31. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 50 );
  32. camera.position.set( 0, 1.6, 3 );
  33. controls = new OrbitControls( camera, container );
  34. controls.target.set( 0, 1.6, 0 );
  35. controls.update();
  36. var geometry = new THREE.BoxBufferGeometry( 0.5, 0.8, 0.5 );
  37. var material = new THREE.MeshStandardMaterial( {
  38. color: 0x444444,
  39. roughness: 1.0,
  40. metalness: 0.0
  41. } );
  42. var table = new THREE.Mesh( geometry, material );
  43. table.position.y = 0.35;
  44. table.position.z = 0.85;
  45. scene.add( table );
  46. var geometry = new THREE.PlaneBufferGeometry( 4, 4 );
  47. var material = new THREE.MeshStandardMaterial( {
  48. color: 0x222222,
  49. roughness: 1.0,
  50. metalness: 0.0
  51. } );
  52. var floor = new THREE.Mesh( geometry, material );
  53. floor.rotation.x = - Math.PI / 2;
  54. scene.add( floor );
  55. var grid = new THREE.GridHelper( 10, 20, 0x111111, 0x111111 );
  56. // grid.material.depthTest = false; // avoid z-fighting
  57. scene.add( grid );
  58. scene.add( new THREE.HemisphereLight( 0x888877, 0x777788 ) );
  59. var light = new THREE.DirectionalLight( 0xffffff, 0.5 );
  60. light.position.set( 0, 4, 0 );
  61. scene.add( light );
  62. //
  63. var painter1 = new TubePainter();
  64. scene.add( painter1.mesh );
  65. var painter2 = new TubePainter();
  66. scene.add( painter2.mesh );
  67. //
  68. renderer = new THREE.WebGLRenderer( { antialias: true } );
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. renderer.outputEncoding = THREE.sRGBEncoding;
  72. renderer.xr.enabled = true;
  73. container.appendChild( renderer.domElement );
  74. document.body.appendChild( VRButton.createButton( renderer ) );
  75. // controllers
  76. function onSelectStart() {
  77. this.userData.isSelecting = true;
  78. }
  79. function onSelectEnd() {
  80. this.userData.isSelecting = false;
  81. }
  82. function onSequeezeStart() {
  83. this.userData.isSqueezing = true;
  84. this.userData.positionAtSqueezeStart = this.position.y;
  85. this.userData.scaleAtSqueezeStart = this.scale.x;
  86. }
  87. function onSqueezeEned() {
  88. this.userData.isSqueezing = false;
  89. }
  90. controller1 = renderer.xr.getController( 0 );
  91. controller1.addEventListener( 'selectstart', onSelectStart );
  92. controller1.addEventListener( 'selectend', onSelectEnd );
  93. controller1.addEventListener( 'squeezestart', onSequeezeStart );
  94. controller1.addEventListener( 'squeezeend', onSqueezeEned );
  95. controller1.userData.painter = painter1;
  96. scene.add( controller1 );
  97. controller2 = renderer.xr.getController( 1 );
  98. controller2.addEventListener( 'selectstart', onSelectStart );
  99. controller2.addEventListener( 'selectend', onSelectEnd );
  100. controller2.addEventListener( 'squeezestart', onSequeezeStart );
  101. controller2.addEventListener( 'squeezeend', onSqueezeEned );
  102. controller2.userData.painter = painter2;
  103. scene.add( controller2 );
  104. //
  105. var geometry = new THREE.CylinderBufferGeometry( 0.01, 0.02, 0.08, 5 );
  106. geometry.rotateX( - Math.PI / 2 );
  107. var material = new THREE.MeshStandardMaterial( { flatShading: true } );
  108. var mesh = new THREE.Mesh( geometry, material );
  109. var pivot = new THREE.Mesh( new THREE.IcosahedronBufferGeometry( 0.01, 2 ) );
  110. pivot.name = 'pivot';
  111. pivot.position.z = - 0.05;
  112. mesh.add( pivot );
  113. controller1.add( mesh.clone() );
  114. controller2.add( mesh.clone() );
  115. //
  116. window.addEventListener( 'resize', onWindowResize, false );
  117. }
  118. function onWindowResize() {
  119. camera.aspect = window.innerWidth / window.innerHeight;
  120. camera.updateProjectionMatrix();
  121. renderer.setSize( window.innerWidth, window.innerHeight );
  122. }
  123. //
  124. function handleController( controller ) {
  125. var userData = controller.userData;
  126. var painter = userData.painter;
  127. var pivot = controller.getObjectByName( 'pivot' );
  128. if ( userData.isSqueezing === true ) {
  129. var delta = ( controller.position.y - userData.positionAtSqueezeStart ) * 5;
  130. var scale = Math.max( 0.1, userData.scaleAtSqueezeStart + delta );
  131. pivot.scale.setScalar( scale );
  132. painter.setSize( scale );
  133. }
  134. cursor.setFromMatrixPosition( pivot.matrixWorld );
  135. if ( userData.isSelecting === true ) {
  136. painter.lineTo( cursor );
  137. painter.update();
  138. } else {
  139. painter.moveTo( cursor );
  140. }
  141. }
  142. function animate() {
  143. renderer.setAnimationLoop( render );
  144. }
  145. function render() {
  146. handleController( controller1 );
  147. handleController( controller2 );
  148. renderer.render( scene, camera );
  149. }
  150. </script>
  151. </body>
  152. </html>