webxr_vr_haptics.html 8.0 KB

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