webgpu_xr_cubes.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js xr - cubes</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <meta property="og:title" content="three.js xr - cubes">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_xr_cubes.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_xr_cubes.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> xr - interactive cubes
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.webgpu.js",
  21. "three/webgpu": "../build/three.webgpu.js",
  22. "three/tsl": "../build/three.tsl.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three/webgpu';
  29. import { BoxLineGeometry } from 'three/addons/geometries/BoxLineGeometry.js';
  30. import { XRButton } from 'three/addons/webxr/XRButton.js';
  31. import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js';
  32. const timer = new THREE.Timer();
  33. timer.connect( document );
  34. let container;
  35. let camera, scene, raycaster, renderer;
  36. let room;
  37. let controller, controllerGrip;
  38. let INTERSECTED;
  39. init();
  40. function init() {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. scene = new THREE.Scene();
  44. scene.background = new THREE.Color( 0x505050 );
  45. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  46. camera.position.set( 0, 1.6, 3 );
  47. scene.add( camera );
  48. room = new THREE.LineSegments(
  49. new BoxLineGeometry( 6, 6, 6, 10, 10, 10 ).translate( 0, 3, 0 ),
  50. new THREE.LineBasicMaterial( { color: 0xbcbcbc } )
  51. );
  52. scene.add( room );
  53. scene.add( new THREE.HemisphereLight( 0xa5a5a5, 0x898989, 3 ) );
  54. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  55. light.position.set( 1, 1, 1 ).normalize();
  56. scene.add( light );
  57. const geometry = new THREE.BoxGeometry( 0.15, 0.15, 0.15 );
  58. for ( let i = 0; i < 200; i ++ ) {
  59. const object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  60. object.position.x = Math.random() * 4 - 2;
  61. object.position.y = Math.random() * 4;
  62. object.position.z = Math.random() * 4 - 2;
  63. object.rotation.x = Math.random() * 2 * Math.PI;
  64. object.rotation.y = Math.random() * 2 * Math.PI;
  65. object.rotation.z = Math.random() * 2 * Math.PI;
  66. object.scale.x = Math.random() + 0.5;
  67. object.scale.y = Math.random() + 0.5;
  68. object.scale.z = Math.random() + 0.5;
  69. object.userData.velocity = new THREE.Vector3();
  70. object.userData.velocity.x = Math.random() * 0.01 - 0.005;
  71. object.userData.velocity.y = Math.random() * 0.01 - 0.005;
  72. object.userData.velocity.z = Math.random() * 0.01 - 0.005;
  73. room.add( object );
  74. }
  75. raycaster = new THREE.Raycaster();
  76. renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL: true, outputBufferType: THREE.UnsignedByteType, multiview: true } );
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. renderer.setAnimationLoop( animate );
  80. renderer.xr.enabled = true;
  81. container.appendChild( renderer.domElement );
  82. //
  83. function onSelectStart() {
  84. this.userData.isSelecting = true;
  85. }
  86. function onSelectEnd() {
  87. this.userData.isSelecting = false;
  88. }
  89. controller = renderer.xr.getController( 0 );
  90. controller.addEventListener( 'selectstart', onSelectStart );
  91. controller.addEventListener( 'selectend', onSelectEnd );
  92. controller.addEventListener( 'connected', function ( event ) {
  93. const targetRayMode = event.data.targetRayMode;
  94. if ( targetRayMode === 'tracked-pointer' || targetRayMode === 'gaze' ) {
  95. this.add( buildController( event.data ) );
  96. }
  97. } );
  98. controller.addEventListener( 'disconnected', function () {
  99. this.remove( this.children[ 0 ] );
  100. } );
  101. scene.add( controller );
  102. const controllerModelFactory = new XRControllerModelFactory();
  103. controllerGrip = renderer.xr.getControllerGrip( 0 );
  104. controllerGrip.add( controllerModelFactory.createControllerModel( controllerGrip ) );
  105. scene.add( controllerGrip );
  106. window.addEventListener( 'resize', onWindowResize );
  107. //
  108. document.body.appendChild( XRButton.createButton( renderer ) );
  109. }
  110. function buildController( data ) {
  111. let geometry, material;
  112. switch ( data.targetRayMode ) {
  113. case 'tracked-pointer':
  114. geometry = new THREE.BufferGeometry();
  115. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 1 ], 3 ) );
  116. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( [ 0.5, 0.5, 0.5, 0, 0, 0 ], 3 ) );
  117. material = new THREE.LineBasicMaterial( { vertexColors: true, blending: THREE.AdditiveBlending } );
  118. return new THREE.Line( geometry, material );
  119. case 'gaze':
  120. geometry = new THREE.RingGeometry( 0.02, 0.04, 32 ).translate( 0, 0, - 1 );
  121. material = new THREE.MeshBasicMaterial( { opacity: 0.5, transparent: true } );
  122. return new THREE.Mesh( geometry, material );
  123. }
  124. }
  125. function onWindowResize() {
  126. camera.aspect = window.innerWidth / window.innerHeight;
  127. camera.updateProjectionMatrix();
  128. renderer.setSize( window.innerWidth, window.innerHeight );
  129. }
  130. //
  131. function animate() {
  132. timer.update();
  133. const delta = timer.getDelta() * 60;
  134. if ( controller.userData.isSelecting === true ) {
  135. const cube = room.children[ 0 ];
  136. room.remove( cube );
  137. cube.position.copy( controller.position );
  138. cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02 * delta;
  139. cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02 * delta;
  140. cube.userData.velocity.z = ( Math.random() * 0.01 - 0.05 ) * delta;
  141. cube.userData.velocity.applyQuaternion( controller.quaternion );
  142. room.add( cube );
  143. }
  144. // find intersections
  145. raycaster.setFromXRController( controller );
  146. const intersects = raycaster.intersectObjects( room.children, false );
  147. if ( intersects.length > 0 ) {
  148. if ( INTERSECTED != intersects[ 0 ].object ) {
  149. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  150. INTERSECTED = intersects[ 0 ].object;
  151. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  152. INTERSECTED.material.emissive.setHex( 0xff0000 );
  153. }
  154. } else {
  155. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  156. INTERSECTED = undefined;
  157. }
  158. // Keep cubes inside room
  159. for ( let i = 0; i < room.children.length; i ++ ) {
  160. const cube = room.children[ i ];
  161. cube.userData.velocity.multiplyScalar( 1 - ( 0.001 * delta ) );
  162. cube.position.add( cube.userData.velocity );
  163. if ( cube.position.x < - 3 || cube.position.x > 3 ) {
  164. cube.position.x = THREE.MathUtils.clamp( cube.position.x, - 3, 3 );
  165. cube.userData.velocity.x = - cube.userData.velocity.x;
  166. }
  167. if ( cube.position.y < 0 || cube.position.y > 6 ) {
  168. cube.position.y = THREE.MathUtils.clamp( cube.position.y, 0, 6 );
  169. cube.userData.velocity.y = - cube.userData.velocity.y;
  170. }
  171. if ( cube.position.z < - 3 || cube.position.z > 3 ) {
  172. cube.position.z = THREE.MathUtils.clamp( cube.position.z, - 3, 3 );
  173. cube.userData.velocity.z = - cube.userData.velocity.z;
  174. }
  175. cube.rotation.x += cube.userData.velocity.x * 2 * delta;
  176. cube.rotation.y += cube.userData.velocity.y * 2 * delta;
  177. cube.rotation.z += cube.userData.velocity.z * 2 * delta;
  178. }
  179. renderer.render( scene, camera );
  180. }
  181. </script>
  182. </body>
  183. </html>
粤ICP备19079148号