webxr_vr_haptics.html 7.9 KB

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