webxr_vr_dragging.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - dragging</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 - dragging
  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 { VRButton } from './jsm/webxr/VRButton.js';
  18. import { XRControllerModelFactory } from './jsm/webxr/XRControllerModelFactory.js';
  19. var container;
  20. var camera, scene, renderer;
  21. var controller1, controller2;
  22. var controllerGrip1, controllerGrip2;
  23. var raycaster, intersected = [];
  24. var tempMatrix = new THREE.Matrix4();
  25. var controls, group;
  26. init();
  27. animate();
  28. function init() {
  29. container = document.createElement( 'div' );
  30. document.body.appendChild( container );
  31. scene = new THREE.Scene();
  32. scene.background = new THREE.Color( 0x808080 );
  33. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  34. camera.position.set( 0, 1.6, 3 );
  35. controls = new OrbitControls( camera, container );
  36. controls.target.set( 0, 1.6, 0 );
  37. controls.update();
  38. var geometry = new THREE.PlaneBufferGeometry( 4, 4 );
  39. var material = new THREE.MeshStandardMaterial( {
  40. color: 0xeeeeee,
  41. roughness: 1.0,
  42. metalness: 0.0
  43. } );
  44. var floor = new THREE.Mesh( geometry, material );
  45. floor.rotation.x = - Math.PI / 2;
  46. floor.receiveShadow = true;
  47. scene.add( floor );
  48. scene.add( new THREE.HemisphereLight( 0x808080, 0x606060 ) );
  49. var light = new THREE.DirectionalLight( 0xffffff );
  50. light.position.set( 0, 6, 0 );
  51. light.castShadow = true;
  52. light.shadow.camera.top = 2;
  53. light.shadow.camera.bottom = - 2;
  54. light.shadow.camera.right = 2;
  55. light.shadow.camera.left = - 2;
  56. light.shadow.mapSize.set( 4096, 4096 );
  57. scene.add( light );
  58. group = new THREE.Group();
  59. scene.add( group );
  60. var geometries = [
  61. new THREE.BoxBufferGeometry( 0.2, 0.2, 0.2 ),
  62. new THREE.ConeBufferGeometry( 0.2, 0.2, 64 ),
  63. new THREE.CylinderBufferGeometry( 0.2, 0.2, 0.2, 64 ),
  64. new THREE.IcosahedronBufferGeometry( 0.2, 3 ),
  65. new THREE.TorusBufferGeometry( 0.2, 0.04, 64, 32 )
  66. ];
  67. for ( var i = 0; i < 50; i ++ ) {
  68. var geometry = geometries[ Math.floor( Math.random() * geometries.length ) ];
  69. var material = new THREE.MeshStandardMaterial( {
  70. color: Math.random() * 0xffffff,
  71. roughness: 0.7,
  72. metalness: 0.0
  73. } );
  74. var object = new THREE.Mesh( geometry, material );
  75. object.position.x = Math.random() * 4 - 2;
  76. object.position.y = Math.random() * 2;
  77. object.position.z = Math.random() * 4 - 2;
  78. object.rotation.x = Math.random() * 2 * Math.PI;
  79. object.rotation.y = Math.random() * 2 * Math.PI;
  80. object.rotation.z = Math.random() * 2 * Math.PI;
  81. object.scale.setScalar( Math.random() + 0.5 );
  82. object.castShadow = true;
  83. object.receiveShadow = true;
  84. group.add( object );
  85. }
  86. //
  87. renderer = new THREE.WebGLRenderer( { antialias: true } );
  88. renderer.setPixelRatio( window.devicePixelRatio );
  89. renderer.setSize( window.innerWidth, window.innerHeight );
  90. renderer.outputEncoding = THREE.sRGBEncoding;
  91. renderer.shadowMap.enabled = true;
  92. renderer.xr.enabled = true;
  93. container.appendChild( renderer.domElement );
  94. document.body.appendChild( VRButton.createButton( renderer ) );
  95. // controllers
  96. controller1 = renderer.xr.getController( 0 );
  97. controller1.addEventListener( 'selectstart', onSelectStart );
  98. controller1.addEventListener( 'selectend', onSelectEnd );
  99. scene.add( controller1 );
  100. controller2 = renderer.xr.getController( 1 );
  101. controller2.addEventListener( 'selectstart', onSelectStart );
  102. controller2.addEventListener( 'selectend', onSelectEnd );
  103. scene.add( controller2 );
  104. var controllerModelFactory = new XRControllerModelFactory();
  105. controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  106. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  107. scene.add( controllerGrip1 );
  108. controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  109. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  110. scene.add( controllerGrip2 );
  111. //
  112. var geometry = new THREE.BufferGeometry().setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 1 ) ] );
  113. var line = new THREE.Line( geometry );
  114. line.name = 'line';
  115. line.scale.z = 5;
  116. controller1.add( line.clone() );
  117. controller2.add( line.clone() );
  118. raycaster = new THREE.Raycaster();
  119. //
  120. window.addEventListener( 'resize', onWindowResize, false );
  121. }
  122. function onWindowResize() {
  123. camera.aspect = window.innerWidth / window.innerHeight;
  124. camera.updateProjectionMatrix();
  125. renderer.setSize( window.innerWidth, window.innerHeight );
  126. }
  127. function onSelectStart( event ) {
  128. var controller = event.target;
  129. var intersections = getIntersections( controller );
  130. if ( intersections.length > 0 ) {
  131. var intersection = intersections[ 0 ];
  132. tempMatrix.getInverse( controller.matrixWorld );
  133. var object = intersection.object;
  134. object.matrix.premultiply( tempMatrix );
  135. object.matrix.decompose( object.position, object.quaternion, object.scale );
  136. object.material.emissive.b = 1;
  137. controller.add( object );
  138. controller.userData.selected = object;
  139. }
  140. }
  141. function onSelectEnd( event ) {
  142. var controller = event.target;
  143. if ( controller.userData.selected !== undefined ) {
  144. var object = controller.userData.selected;
  145. object.matrix.premultiply( controller.matrixWorld );
  146. object.matrix.decompose( object.position, object.quaternion, object.scale );
  147. object.material.emissive.b = 0;
  148. group.add( object );
  149. controller.userData.selected = undefined;
  150. }
  151. }
  152. function getIntersections( controller ) {
  153. tempMatrix.identity().extractRotation( controller.matrixWorld );
  154. raycaster.ray.origin.setFromMatrixPosition( controller.matrixWorld );
  155. raycaster.ray.direction.set( 0, 0, - 1 ).applyMatrix4( tempMatrix );
  156. return raycaster.intersectObjects( group.children );
  157. }
  158. function intersectObjects( controller ) {
  159. // Do not highlight when already selected
  160. if ( controller.userData.selected !== undefined ) return;
  161. var line = controller.getObjectByName( 'line' );
  162. var intersections = getIntersections( controller );
  163. if ( intersections.length > 0 ) {
  164. var intersection = intersections[ 0 ];
  165. var object = intersection.object;
  166. object.material.emissive.r = 1;
  167. intersected.push( object );
  168. line.scale.z = intersection.distance;
  169. } else {
  170. line.scale.z = 5;
  171. }
  172. }
  173. function cleanIntersected() {
  174. while ( intersected.length ) {
  175. var object = intersected.pop();
  176. object.material.emissive.r = 0;
  177. }
  178. }
  179. //
  180. function animate() {
  181. renderer.setAnimationLoop( render );
  182. }
  183. function render() {
  184. cleanIntersected();
  185. intersectObjects( controller1 );
  186. intersectObjects( controller2 );
  187. renderer.render( scene, camera );
  188. }
  189. </script>
  190. </body>
  191. </html>