1
0

webxr_vr_haptics.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 - haptics
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  16. import { VRButton } from './jsm/webxr/VRButton.js';
  17. import { XRControllerModelFactory } from './jsm/webxr/XRControllerModelFactory.js';
  18. let container;
  19. let camera, scene, renderer;
  20. let controller1, controller2;
  21. let controllerGrip1, controllerGrip2;
  22. const box = new THREE.Box3();
  23. const controllers = [];
  24. const oscillators = [];
  25. let controls, group;
  26. let audioCtx = null;
  27. init();
  28. animate();
  29. function initAudio() {
  30. if ( audioCtx !== null ) {
  31. return;
  32. }
  33. audioCtx = new ( window.AudioContext || window.webkitAudioContext )();
  34. function createOscillator() {
  35. const oscillator = audioCtx.createOscillator();
  36. const real = Array.from( { length: 8192 }, ( _, n ) => (
  37. n === 0 ?
  38. 0 :
  39. 4 / ( n * Math.PI ) * Math.sin( Math.PI * n * 0.18 )
  40. ) );
  41. const imag = real.map( () => 0 );
  42. oscillator.setPeriodicWave( audioCtx.createPeriodicWave( Float32Array.from( real ), Float32Array.from( imag ) ) );
  43. oscillator.start();
  44. return oscillator;
  45. }
  46. oscillators.push( createOscillator() );
  47. oscillators.push( createOscillator() );
  48. window.oscillators = oscillators;
  49. }
  50. function init() {
  51. container = document.createElement( 'div' );
  52. document.body.appendChild( container );
  53. scene = new THREE.Scene();
  54. scene.background = new THREE.Color( 0x808080 );
  55. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  56. camera.position.set( 0, 1.6, 3 );
  57. controls = new OrbitControls( camera, container );
  58. controls.target.set( 0, 1.6, 0 );
  59. controls.update();
  60. const floorGeometry = new THREE.PlaneGeometry( 4, 4 );
  61. const floorMaterial = new THREE.MeshStandardMaterial( {
  62. color: 0xeeeeee,
  63. roughness: 1.0,
  64. metalness: 0.0
  65. } );
  66. const floor = new THREE.Mesh( floorGeometry, floorMaterial );
  67. floor.rotation.x = - Math.PI / 2;
  68. floor.receiveShadow = true;
  69. scene.add( floor );
  70. scene.add( new THREE.HemisphereLight( 0x808080, 0x606060 ) );
  71. const light = new THREE.DirectionalLight( 0xffffff );
  72. light.position.set( 0, 6, 0 );
  73. light.castShadow = true;
  74. light.shadow.camera.top = 2;
  75. light.shadow.camera.bottom = - 2;
  76. light.shadow.camera.right = 2;
  77. light.shadow.camera.left = - 2;
  78. light.shadow.mapSize.set( 4096, 4096 );
  79. scene.add( light );
  80. group = new THREE.Group();
  81. group.position.z = - 0.5;
  82. scene.add( group );
  83. for ( let i = 0; i < 10; i ++ ) {
  84. const intensity = ( i + 1 ) / 10;
  85. const w = 0.1;
  86. const h = 0.1;
  87. const minH = 1;
  88. const geometry = new THREE.BoxGeometry( w, h * i + minH, w );
  89. const material = new THREE.MeshStandardMaterial( {
  90. color: new THREE.Color( intensity, 0.1, 0.1 ),
  91. roughness: 0.7,
  92. metalness: 0.0
  93. } );
  94. const object = new THREE.Mesh( geometry, material );
  95. object.position.x = ( i - 5 ) * ( w + 0.05 );
  96. object.castShadow = true;
  97. object.receiveShadow = true;
  98. object.userData = {
  99. index: i + 1,
  100. intensity: intensity
  101. };
  102. group.add( object );
  103. }
  104. //
  105. renderer = new THREE.WebGLRenderer( { antialias: true } );
  106. renderer.setPixelRatio( window.devicePixelRatio );
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. renderer.outputEncoding = THREE.sRGBEncoding;
  109. renderer.shadowMap.enabled = true;
  110. renderer.xr.enabled = true;
  111. container.appendChild( renderer.domElement );
  112. document.body.appendChild( VRButton.createButton( renderer ) );
  113. document.getElementById( "VRButton" ).addEventListener( "click", () => {
  114. initAudio();
  115. } );
  116. // controllers
  117. controller1 = renderer.xr.getController( 0 );
  118. scene.add( controller1 );
  119. controller2 = renderer.xr.getController( 1 );
  120. scene.add( controller2 );
  121. const controllerModelFactory = new XRControllerModelFactory();
  122. controllerGrip1 = renderer.xr.getControllerGrip( 0 );
  123. controllerGrip1.addEventListener( "connected", controllerConnected );
  124. controllerGrip1.addEventListener( "disconnected", controllerDisconnected );
  125. controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
  126. scene.add( controllerGrip1 );
  127. controllerGrip2 = renderer.xr.getControllerGrip( 1 );
  128. controllerGrip2.addEventListener( "connected", controllerConnected );
  129. controllerGrip2.addEventListener( "disconnected", controllerDisconnected );
  130. controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
  131. scene.add( controllerGrip2 );
  132. //
  133. window.addEventListener( 'resize', onWindowResize );
  134. }
  135. function controllerConnected( evt ) {
  136. controllers.push( {
  137. gamepad: evt.data.gamepad,
  138. grip: evt.target,
  139. colliding: false,
  140. playing: false
  141. } );
  142. }
  143. function controllerDisconnected( evt ) {
  144. const index = controllers.findIndex( o => o.controller === evt.target );
  145. if ( index !== - 1 ) {
  146. controllers.splice( index, 1 );
  147. }
  148. }
  149. function onWindowResize() {
  150. camera.aspect = window.innerWidth / window.innerHeight;
  151. camera.updateProjectionMatrix();
  152. renderer.setSize( window.innerWidth, window.innerHeight );
  153. }
  154. //
  155. function animate() {
  156. renderer.setAnimationLoop( render );
  157. }
  158. function handleCollisions() {
  159. for ( let g = 0; g < controllers.length; g ++ ) {
  160. controllers[ g ].colliding = false;
  161. const { grip, gamepad } = controllers[ g ];
  162. const sphere = {
  163. radius: 0.03,
  164. center: grip.position
  165. };
  166. if ( "hapticActuators" in gamepad && gamepad.hapticActuators != null && gamepad.hapticActuators.length > 0 ) {
  167. for ( let i = 0; i < group.children.length; i ++ ) {
  168. const child = group.children[ i ];
  169. box.setFromObject( child );
  170. if ( box.intersectsSphere( sphere ) ) {
  171. child.material.emissive.b = 1;
  172. const intensity = child.userData.index / group.children.length;
  173. child.scale.setScalar( 1 + Math.random() * 0.1 * intensity );
  174. gamepad.hapticActuators[ 0 ].pulse( intensity, 100 );
  175. oscillators[ g ].frequency.value = 100 + intensity * 60;
  176. controllers[ g ].colliding = true;
  177. } else {
  178. child.material.emissive.b = 0;
  179. child.scale.setScalar( 1 );
  180. }
  181. }
  182. }
  183. if ( controllers[ g ].colliding ) {
  184. if ( ! controllers[ g ].playing ) {
  185. controllers[ g ].playing = true;
  186. oscillators[ g ].connect( audioCtx.destination );
  187. }
  188. } else {
  189. if ( controllers[ g ].playing ) {
  190. controllers[ g ].playing = false;
  191. oscillators[ g ].disconnect( audioCtx.destination );
  192. }
  193. }
  194. }
  195. }
  196. function render() {
  197. handleCollisions();
  198. renderer.render( scene, camera );
  199. }
  200. </script>
  201. </body>
  202. </html>