webxr_vr_rollercoaster.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - roller coaster</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. <script src="js/vr/HelioWebXRPolyfill.js"></script>
  11. <script type="module">
  12. import * as THREE from '../build/three.module.js';
  13. import {
  14. RollerCoasterGeometry,
  15. RollerCoasterShadowGeometry,
  16. RollerCoasterLiftersGeometry,
  17. TreesGeometry,
  18. SkyGeometry
  19. } from './jsm/misc/RollerCoaster.js';
  20. import { VRButton } from './jsm/webxr/VRButton.js';
  21. var renderer = new THREE.WebGLRenderer( { antialias: true } );
  22. renderer.setPixelRatio( window.devicePixelRatio );
  23. renderer.setSize( window.innerWidth, window.innerHeight );
  24. renderer.xr.enabled = true;
  25. document.body.appendChild( renderer.domElement );
  26. document.body.appendChild( VRButton.createButton( renderer, { referenceSpaceType: 'local' } ) );
  27. //
  28. var scene = new THREE.Scene();
  29. scene.background = new THREE.Color( 0xf0f0ff );
  30. var light = new THREE.HemisphereLight( 0xfff0f0, 0x606066 );
  31. light.position.set( 1, 1, 1 );
  32. scene.add( light );
  33. var train = new THREE.Object3D();
  34. scene.add( train );
  35. var camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 500 );
  36. train.add( camera );
  37. // environment
  38. var geometry = new THREE.PlaneBufferGeometry( 500, 500, 15, 15 );
  39. geometry.rotateX( - Math.PI / 2 );
  40. var positions = geometry.attributes.position.array;
  41. var vertex = new THREE.Vector3();
  42. for ( var i = 0; i < positions.length; i += 3 ) {
  43. vertex.fromArray( positions, i );
  44. vertex.x += Math.random() * 10 - 5;
  45. vertex.z += Math.random() * 10 - 5;
  46. var distance = ( vertex.distanceTo( scene.position ) / 5 ) - 25;
  47. vertex.y = Math.random() * Math.max( 0, distance );
  48. vertex.toArray( positions, i );
  49. }
  50. geometry.computeVertexNormals();
  51. var material = new THREE.MeshLambertMaterial( {
  52. color: 0x407000
  53. } );
  54. var mesh = new THREE.Mesh( geometry, material );
  55. scene.add( mesh );
  56. var geometry = new TreesGeometry( mesh );
  57. var material = new THREE.MeshBasicMaterial( {
  58. side: THREE.DoubleSide, vertexColors: THREE.VertexColors
  59. } );
  60. var mesh = new THREE.Mesh( geometry, material );
  61. scene.add( mesh );
  62. var geometry = new SkyGeometry();
  63. var material = new THREE.MeshBasicMaterial( { color: 0xffffff } );
  64. var mesh = new THREE.Mesh( geometry, material );
  65. scene.add( mesh );
  66. //
  67. var PI2 = Math.PI * 2;
  68. var curve = ( function () {
  69. var vector = new THREE.Vector3();
  70. var vector2 = new THREE.Vector3();
  71. return {
  72. getPointAt: function ( t ) {
  73. t = t * PI2;
  74. var x = Math.sin( t * 3 ) * Math.cos( t * 4 ) * 50;
  75. var y = Math.sin( t * 10 ) * 2 + Math.cos( t * 17 ) * 2 + 5;
  76. var z = Math.sin( t ) * Math.sin( t * 4 ) * 50;
  77. return vector.set( x, y, z ).multiplyScalar( 2 );
  78. },
  79. getTangentAt: function ( t ) {
  80. var delta = 0.0001;
  81. var t1 = Math.max( 0, t - delta );
  82. var t2 = Math.min( 1, t + delta );
  83. return vector2.copy( this.getPointAt( t2 ) )
  84. .sub( this.getPointAt( t1 ) ).normalize();
  85. }
  86. };
  87. } )();
  88. var geometry = new RollerCoasterGeometry( curve, 1500 );
  89. var material = new THREE.MeshPhongMaterial( {
  90. vertexColors: THREE.VertexColors
  91. } );
  92. var mesh = new THREE.Mesh( geometry, material );
  93. scene.add( mesh );
  94. var geometry = new RollerCoasterLiftersGeometry( curve, 100 );
  95. var material = new THREE.MeshPhongMaterial();
  96. var mesh = new THREE.Mesh( geometry, material );
  97. mesh.position.y = 0.1;
  98. scene.add( mesh );
  99. var geometry = new RollerCoasterShadowGeometry( curve, 500 );
  100. var material = new THREE.MeshBasicMaterial( {
  101. color: 0x305000, depthWrite: false, transparent: true
  102. } );
  103. var mesh = new THREE.Mesh( geometry, material );
  104. mesh.position.y = 0.1;
  105. scene.add( mesh );
  106. var funfairs = [];
  107. //
  108. var geometry = new THREE.CylinderBufferGeometry( 10, 10, 5, 15 );
  109. var material = new THREE.MeshLambertMaterial( {
  110. color: 0xff8080,
  111. //flatShading: true // Lambert does not support flat shading
  112. } );
  113. var mesh = new THREE.Mesh( geometry, material );
  114. mesh.position.set( - 80, 10, - 70 );
  115. mesh.rotation.x = Math.PI / 2;
  116. scene.add( mesh );
  117. funfairs.push( mesh );
  118. var geometry = new THREE.CylinderBufferGeometry( 5, 6, 4, 10 );
  119. var material = new THREE.MeshLambertMaterial( {
  120. color: 0x8080ff,
  121. //flatShading: true // Lambert does not support flat shading
  122. } );
  123. var mesh = new THREE.Mesh( geometry, material );
  124. mesh.position.set( 50, 2, 30 );
  125. scene.add( mesh );
  126. funfairs.push( mesh );
  127. //
  128. window.addEventListener( 'resize', onWindowResize, false );
  129. function onWindowResize() {
  130. camera.aspect = window.innerWidth / window.innerHeight;
  131. camera.updateProjectionMatrix();
  132. renderer.setSize( window.innerWidth, window.innerHeight );
  133. }
  134. //
  135. var position = new THREE.Vector3();
  136. var tangent = new THREE.Vector3();
  137. var lookAt = new THREE.Vector3();
  138. var velocity = 0;
  139. var progress = 0;
  140. var prevTime = performance.now();
  141. function render() {
  142. var time = performance.now();
  143. var delta = time - prevTime;
  144. for ( var i = 0; i < funfairs.length; i ++ ) {
  145. funfairs[ i ].rotation.y = time * 0.0004;
  146. }
  147. //
  148. progress += velocity;
  149. progress = progress % 1;
  150. position.copy( curve.getPointAt( progress ) );
  151. position.y += 0.3;
  152. train.position.copy( position );
  153. tangent.copy( curve.getTangentAt( progress ) );
  154. velocity -= tangent.y * 0.0000001 * delta;
  155. velocity = Math.max( 0.00004, Math.min( 0.0002, velocity ) );
  156. train.lookAt( lookAt.copy( position ).sub( tangent ) );
  157. //
  158. renderer.render( scene, camera );
  159. prevTime = time;
  160. }
  161. renderer.setAnimationLoop( render );
  162. </script>
  163. </body>
  164. </html>