vr.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import {
  2. DebugTilesRenderer as TilesRenderer,
  3. NONE,
  4. SCREEN_ERROR,
  5. GEOMETRIC_ERROR,
  6. DISTANCE,
  7. DEPTH,
  8. RELATIVE_DEPTH,
  9. IS_LEAF,
  10. RANDOM_COLOR,
  11. } from '../src/index.js';
  12. import {
  13. Scene,
  14. DirectionalLight,
  15. AmbientLight,
  16. WebGLRenderer,
  17. PerspectiveCamera,
  18. Box3,
  19. Raycaster,
  20. Mesh,
  21. MeshBasicMaterial,
  22. Group,
  23. TorusBufferGeometry,
  24. sRGBEncoding,
  25. GridHelper,
  26. BufferGeometry,
  27. Float32BufferAttribute,
  28. LineBasicMaterial,
  29. AdditiveBlending,
  30. Line,
  31. Vector3,
  32. RingBufferGeometry,
  33. Sphere,
  34. } from 'three';
  35. import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js';
  36. import { VRButton } from 'three/examples/jsm/webxr/VRButton.js';
  37. import { XRControllerModelFactory } from 'three/examples/jsm/webxr/XRControllerModelFactory.js';
  38. let camera, scene, renderer, tiles;
  39. let workspace;
  40. let box, sphere, grid;
  41. let raycaster, fwdVector, intersectRing;
  42. let offsetParent;
  43. let controller, controllerGrip;
  44. let xrSession = null;
  45. const tasks = [];
  46. const upVector = new Vector3( 0, 1, 0 );
  47. const params = {
  48. 'displayBoxBounds': false,
  49. 'colorMode': 0,
  50. 'displayGrid': true,
  51. };
  52. init();
  53. function init() {
  54. scene = new Scene();
  55. // primary camera view
  56. renderer = new WebGLRenderer( { antialias: true } );
  57. renderer.setPixelRatio( window.devicePixelRatio );
  58. renderer.setSize( window.innerWidth, window.innerHeight );
  59. renderer.setClearColor( 0xbbbbbb );
  60. renderer.outputEncoding = sRGBEncoding;
  61. renderer.xr.enabled = true;
  62. document.body.appendChild( renderer.domElement );
  63. renderer.domElement.tabIndex = 1;
  64. renderer.setAnimationLoop( animate );
  65. // create workspace
  66. workspace = new Group();
  67. scene.add( workspace );
  68. grid = new GridHelper( 10, 10, 0xffffff, 0xffffff );
  69. grid.material.transparent = true;
  70. grid.material.opacity = 0.5;
  71. grid.material.depthWrite = false;
  72. workspace.add( grid );
  73. camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 4000 );
  74. camera.position.set( 0, 1, 0 );
  75. workspace.add( camera );
  76. // lights
  77. const dirLight = new DirectionalLight( 0xffffff );
  78. dirLight.position.set( 1, 2, 3 );
  79. scene.add( dirLight );
  80. const ambLight = new AmbientLight( 0xffffff, 0.2 );
  81. scene.add( ambLight );
  82. // tile set
  83. box = new Box3();
  84. sphere = new Sphere();
  85. // parent for centering the tileset
  86. offsetParent = new Group();
  87. offsetParent.rotation.x = Math.PI / 2;
  88. offsetParent.position.y = 32;
  89. scene.add( offsetParent );
  90. tiles = new TilesRenderer( 'https://raw.githubusercontent.com/NASA-AMMOS/3DTilesSampleData/master/msl-dingo-gap/0528_0260184_to_s64o256_colorize/scene-tileset.json' );
  91. offsetParent.add( tiles.group );
  92. // We set camera for tileset
  93. tiles.setCamera( camera );
  94. tiles.setResolutionFromRenderer( camera, renderer );
  95. // We define a custom scheduling callback to handle also active WebXR sessions
  96. const tilesSchedulingCB = func => {
  97. tasks.push( func );
  98. };
  99. // We set our scheduling callback for tiles downloading and parsing
  100. tiles.downloadQueue.schedulingCallback = tilesSchedulingCB;
  101. tiles.parseQueue.schedulingCallback = tilesSchedulingCB;
  102. tiles.lruCache.maxSize = 1200;
  103. tiles.lruCache.minSize = 900;
  104. // Raycasting init
  105. raycaster = new Raycaster();
  106. fwdVector = new Vector3( 0, 0, 1 );
  107. const rayIntersectMat = new MeshBasicMaterial( { color: 0xb2dfdb } );
  108. intersectRing = new Mesh( new TorusBufferGeometry( 1.5, 0.2, 16, 100 ), rayIntersectMat );
  109. intersectRing.visible = false;
  110. scene.add( intersectRing );
  111. // vr setup
  112. document.body.appendChild( VRButton.createButton( renderer ) );
  113. controller = renderer.xr.getController( 0 );
  114. controller.addEventListener( 'selectstart', () => {
  115. if ( intersectRing.visible ) {
  116. workspace.position.copy( intersectRing.position );
  117. }
  118. } );
  119. controller.addEventListener( 'connected', function ( event ) {
  120. this.controllerActive = true;
  121. this.add( buildController( event.data ) );
  122. } );
  123. controller.addEventListener( 'disconnected', function () {
  124. this.controllerActive = false;
  125. this.remove( this.children[ 0 ] );
  126. } );
  127. workspace.add( controller );
  128. // controller models
  129. const controllerModelFactory = new XRControllerModelFactory();
  130. controllerGrip = renderer.xr.getControllerGrip( 0 );
  131. controllerGrip.add( controllerModelFactory.createControllerModel( controllerGrip ) );
  132. workspace.add( controllerGrip );
  133. onWindowResize();
  134. window.addEventListener( 'resize', onWindowResize, false );
  135. // GUI
  136. const gui = new GUI();
  137. gui.width = 300;
  138. gui.add( params, 'displayGrid' );
  139. gui.add( params, 'displayBoxBounds' );
  140. gui.add( params, 'colorMode', {
  141. NONE,
  142. SCREEN_ERROR,
  143. GEOMETRIC_ERROR,
  144. DISTANCE,
  145. DEPTH,
  146. RELATIVE_DEPTH,
  147. IS_LEAF,
  148. RANDOM_COLOR,
  149. } );
  150. gui.open();
  151. }
  152. function buildController( data ) {
  153. let geometry, material;
  154. switch ( data.targetRayMode ) {
  155. case 'tracked-pointer':
  156. geometry = new BufferGeometry();
  157. geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 1 ], 3 ) );
  158. geometry.setAttribute( 'color', new Float32BufferAttribute( [ 0.5, 0.5, 0.5, 0, 0, 0 ], 3 ) );
  159. material = new LineBasicMaterial( {
  160. vertexColors: true,
  161. blending: AdditiveBlending,
  162. depthWrite: false,
  163. transparent: true,
  164. } );
  165. return new Line( geometry, material );
  166. case 'gaze':
  167. geometry = new RingBufferGeometry( 0.02, 0.04, 32 ).translate( 0, 0, - 1 );
  168. material = new MeshBasicMaterial( { opacity: 0.5, transparent: true } );
  169. return new Mesh( geometry, material );
  170. }
  171. }
  172. function onWindowResize() {
  173. camera.updateProjectionMatrix();
  174. renderer.setPixelRatio( window.devicePixelRatio );
  175. renderer.setSize( window.innerWidth, window.innerHeight );
  176. camera.aspect = window.innerWidth / window.innerHeight;
  177. camera.updateProjectionMatrix();
  178. }
  179. function handleCamera() {
  180. // get the XR camera with a combined frustum for culling
  181. if ( renderer.xr.isPresenting ) {
  182. if ( xrSession === null ) { // We setup XR camera once
  183. const xrCamera = renderer.xr.getCamera( camera );
  184. // remove all cameras so we can use the VR camera instead
  185. tiles.cameras.forEach( c => tiles.deleteCamera( c ) );
  186. tiles.setCamera( xrCamera );
  187. const leftCam = xrCamera.cameras[ 0 ];
  188. if ( leftCam ) {
  189. tiles.setResolution( xrCamera, leftCam.viewport.z, leftCam.viewport.w );
  190. }
  191. xrSession = renderer.xr.getSession();
  192. }
  193. } else {
  194. // Reset default camera (exiting WebXR session)
  195. if ( xrSession !== null ) {
  196. tiles.cameras.forEach( c => tiles.deleteCamera( c ) );
  197. tiles.setCamera( camera );
  198. tiles.setResolutionFromRenderer( camera, renderer );
  199. camera.position.set( 0, 1, 0 );
  200. xrSession = null;
  201. }
  202. }
  203. }
  204. function handleTasks() {
  205. for ( let t = 0, l = tasks.length; t < l; t ++ ) {
  206. tasks[ t ]();
  207. }
  208. tasks.length = 0;
  209. }
  210. function animate() {
  211. grid.visible = params.displayGrid;
  212. // update options
  213. tiles.displayBoxBounds = params.displayBoxBounds;
  214. tiles.colorMode = parseFloat( params.colorMode );
  215. // update tiles center
  216. if ( tiles.getBounds( box ) ) {
  217. box.getCenter( tiles.group.position );
  218. tiles.group.position.multiplyScalar( - 1 );
  219. } else if ( tiles.getBoundingSphere( sphere ) ) {
  220. tiles.group.position.copy( sphere.center );
  221. tiles.group.position.multiplyScalar( - 1 );
  222. }
  223. // We check for tiles camera setup (default and XR sessions)
  224. handleCamera();
  225. // We handle pending tasks
  226. handleTasks();
  227. tiles.update();
  228. if ( controller.controllerActive ) {
  229. const { ray } = raycaster;
  230. raycaster.firstHitOnly = true;
  231. // get the controller ray
  232. ray.origin
  233. .copy( controller.position )
  234. .applyMatrix4( workspace.matrixWorld );
  235. controller
  236. .getWorldDirection( ray.direction )
  237. .multiplyScalar( - 1 );
  238. const results = raycaster.intersectObject( tiles.group, true );
  239. if ( results.length ) {
  240. const hit = results[ 0 ];
  241. hit.face.normal.transformDirection( tiles.group.matrixWorld );
  242. intersectRing.position.copy( hit.point );
  243. intersectRing.quaternion.setFromUnitVectors(
  244. fwdVector,
  245. hit.face.normal,
  246. );
  247. intersectRing.visible = true;
  248. // scale ring based on distance
  249. const scale = workspace.position.distanceTo( intersectRing.position ) * camera.fov / 4000;
  250. intersectRing.scale.setScalar( scale );
  251. // only teleport to mostly horizontal surfaces
  252. if ( hit.face.normal.dot( upVector ) < 0.15 ) {
  253. intersectRing.visible = false;
  254. }
  255. } else {
  256. intersectRing.visible = false;
  257. }
  258. } else {
  259. intersectRing.visible = false;
  260. }
  261. // render primary view
  262. renderer.render( scene, camera );
  263. }