|
| 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 type="importmap"> |
| 11 | + { |
| 12 | + "imports": { |
| 13 | + "three": "../build/three.webgpu.js", |
| 14 | + "three/webgpu": "../build/three.webgpu.js", |
| 15 | + "three/addons/": "./jsm/" |
| 16 | + } |
| 17 | + } |
| 18 | + </script> |
| 19 | + |
| 20 | + <script type="module"> |
| 21 | + |
| 22 | + import * as THREE from 'three'; |
| 23 | + |
| 24 | + import { |
| 25 | + RollerCoasterGeometry, |
| 26 | + RollerCoasterShadowGeometry, |
| 27 | + RollerCoasterLiftersGeometry, |
| 28 | + TreesGeometry, |
| 29 | + SkyGeometry |
| 30 | + } from 'three/addons/misc/RollerCoaster.js'; |
| 31 | + import { VRButton } from 'three/addons/webxr/VRButton.js'; |
| 32 | + |
| 33 | + let mesh, material, geometry; |
| 34 | + |
| 35 | + const renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL: true, colorBufferType: THREE.UnsignedByteType, multiview: false } ); |
| 36 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 37 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 38 | + renderer.setAnimationLoop( animate ); |
| 39 | + renderer.xr.enabled = true; |
| 40 | + renderer.xr.setReferenceSpaceType( 'local' ); |
| 41 | + document.body.appendChild( renderer.domElement ); |
| 42 | + |
| 43 | + document.body.appendChild( VRButton.createButton( renderer ) ); |
| 44 | + |
| 45 | + // |
| 46 | + |
| 47 | + const scene = new THREE.Scene(); |
| 48 | + scene.background = new THREE.Color( 0xf0f0ff ); |
| 49 | + |
| 50 | + const light = new THREE.HemisphereLight( 0xfff0f0, 0x60606, 3 ); |
| 51 | + light.position.set( 1, 1, 1 ); |
| 52 | + scene.add( light ); |
| 53 | + |
| 54 | + const train = new THREE.Object3D(); |
| 55 | + scene.add( train ); |
| 56 | + |
| 57 | + const camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 500 ); |
| 58 | + train.add( camera ); |
| 59 | + |
| 60 | + // environment |
| 61 | + |
| 62 | + geometry = new THREE.PlaneGeometry( 500, 500, 15, 15 ); |
| 63 | + geometry.rotateX( - Math.PI / 2 ); |
| 64 | + |
| 65 | + const positions = geometry.attributes.position.array; |
| 66 | + const vertex = new THREE.Vector3(); |
| 67 | + |
| 68 | + for ( let i = 0; i < positions.length; i += 3 ) { |
| 69 | + |
| 70 | + vertex.fromArray( positions, i ); |
| 71 | + |
| 72 | + vertex.x += Math.random() * 10 - 5; |
| 73 | + vertex.z += Math.random() * 10 - 5; |
| 74 | + |
| 75 | + const distance = ( vertex.distanceTo( scene.position ) / 5 ) - 25; |
| 76 | + vertex.y = Math.random() * Math.max( 0, distance ); |
| 77 | + |
| 78 | + vertex.toArray( positions, i ); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + geometry.computeVertexNormals(); |
| 83 | + |
| 84 | + material = new THREE.MeshLambertMaterial( { |
| 85 | + color: 0x407000 |
| 86 | + } ); |
| 87 | + |
| 88 | + mesh = new THREE.Mesh( geometry, material ); |
| 89 | + scene.add( mesh ); |
| 90 | + |
| 91 | + geometry = new TreesGeometry( mesh ); |
| 92 | + material = new THREE.MeshBasicMaterial( { |
| 93 | + side: THREE.DoubleSide, vertexColors: true |
| 94 | + } ); |
| 95 | + mesh = new THREE.Mesh( geometry, material ); |
| 96 | + scene.add( mesh ); |
| 97 | + |
| 98 | + geometry = new SkyGeometry(); |
| 99 | + material = new THREE.MeshBasicMaterial( { color: 0xffffff } ); |
| 100 | + mesh = new THREE.Mesh( geometry, material ); |
| 101 | + scene.add( mesh ); |
| 102 | + |
| 103 | + // |
| 104 | + |
| 105 | + const PI2 = Math.PI * 2; |
| 106 | + |
| 107 | + const curve = ( function () { |
| 108 | + |
| 109 | + const vector = new THREE.Vector3(); |
| 110 | + const vector2 = new THREE.Vector3(); |
| 111 | + |
| 112 | + return { |
| 113 | + |
| 114 | + getPointAt: function ( t ) { |
| 115 | + |
| 116 | + t = t * PI2; |
| 117 | + |
| 118 | + const x = Math.sin( t * 3 ) * Math.cos( t * 4 ) * 50; |
| 119 | + const y = Math.sin( t * 10 ) * 2 + Math.cos( t * 17 ) * 2 + 5; |
| 120 | + const z = Math.sin( t ) * Math.sin( t * 4 ) * 50; |
| 121 | + |
| 122 | + return vector.set( x, y, z ).multiplyScalar( 2 ); |
| 123 | + |
| 124 | + }, |
| 125 | + |
| 126 | + getTangentAt: function ( t ) { |
| 127 | + |
| 128 | + const delta = 0.0001; |
| 129 | + const t1 = Math.max( 0, t - delta ); |
| 130 | + const t2 = Math.min( 1, t + delta ); |
| 131 | + |
| 132 | + return vector2.copy( this.getPointAt( t2 ) ) |
| 133 | + .sub( this.getPointAt( t1 ) ).normalize(); |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + }; |
| 138 | + |
| 139 | + } )(); |
| 140 | + |
| 141 | + geometry = new RollerCoasterGeometry( curve, 1500 ); |
| 142 | + material = new THREE.MeshPhongMaterial( { |
| 143 | + vertexColors: true |
| 144 | + } ); |
| 145 | + mesh = new THREE.Mesh( geometry, material ); |
| 146 | + scene.add( mesh ); |
| 147 | + |
| 148 | + geometry = new RollerCoasterLiftersGeometry( curve, 100 ); |
| 149 | + material = new THREE.MeshPhongMaterial(); |
| 150 | + mesh = new THREE.Mesh( geometry, material ); |
| 151 | + mesh.position.y = 0.1; |
| 152 | + scene.add( mesh ); |
| 153 | + |
| 154 | + geometry = new RollerCoasterShadowGeometry( curve, 500 ); |
| 155 | + material = new THREE.MeshBasicMaterial( { |
| 156 | + color: 0x305000, depthWrite: false, transparent: true |
| 157 | + } ); |
| 158 | + mesh = new THREE.Mesh( geometry, material ); |
| 159 | + mesh.position.y = 0.1; |
| 160 | + scene.add( mesh ); |
| 161 | + |
| 162 | + const funfairs = []; |
| 163 | + |
| 164 | + // |
| 165 | + |
| 166 | + geometry = new THREE.CylinderGeometry( 10, 10, 5, 15 ); |
| 167 | + material = new THREE.MeshLambertMaterial( { |
| 168 | + color: 0xff8080 |
| 169 | + } ); |
| 170 | + mesh = new THREE.Mesh( geometry, material ); |
| 171 | + mesh.position.set( - 80, 10, - 70 ); |
| 172 | + mesh.rotation.x = Math.PI / 2; |
| 173 | + scene.add( mesh ); |
| 174 | + |
| 175 | + funfairs.push( mesh ); |
| 176 | + |
| 177 | + geometry = new THREE.CylinderGeometry( 5, 6, 4, 10 ); |
| 178 | + material = new THREE.MeshLambertMaterial( { |
| 179 | + color: 0x8080ff |
| 180 | + } ); |
| 181 | + mesh = new THREE.Mesh( geometry, material ); |
| 182 | + mesh.position.set( 50, 2, 30 ); |
| 183 | + scene.add( mesh ); |
| 184 | + |
| 185 | + funfairs.push( mesh ); |
| 186 | + |
| 187 | + // |
| 188 | + |
| 189 | + window.addEventListener( 'resize', onWindowResize ); |
| 190 | + |
| 191 | + function onWindowResize() { |
| 192 | + |
| 193 | + camera.aspect = window.innerWidth / window.innerHeight; |
| 194 | + camera.updateProjectionMatrix(); |
| 195 | + |
| 196 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 197 | + |
| 198 | + } |
| 199 | + |
| 200 | + // |
| 201 | + |
| 202 | + const position = new THREE.Vector3(); |
| 203 | + const tangent = new THREE.Vector3(); |
| 204 | + |
| 205 | + const lookAt = new THREE.Vector3(); |
| 206 | + |
| 207 | + let velocity = 0; |
| 208 | + let progress = 0; |
| 209 | + |
| 210 | + let prevTime = performance.now(); |
| 211 | + |
| 212 | + function animate() { |
| 213 | + |
| 214 | + const time = performance.now(); |
| 215 | + const delta = time - prevTime; |
| 216 | + |
| 217 | + for ( let i = 0; i < funfairs.length; i ++ ) { |
| 218 | + |
| 219 | + funfairs[ i ].rotation.y = time * 0.0004; |
| 220 | + |
| 221 | + } |
| 222 | + |
| 223 | + // |
| 224 | + |
| 225 | + progress += velocity; |
| 226 | + progress = progress % 1; |
| 227 | + |
| 228 | + position.copy( curve.getPointAt( progress ) ); |
| 229 | + position.y += 0.3; |
| 230 | + |
| 231 | + train.position.copy( position ); |
| 232 | + |
| 233 | + tangent.copy( curve.getTangentAt( progress ) ); |
| 234 | + |
| 235 | + velocity -= tangent.y * 0.0000001 * delta; |
| 236 | + velocity = Math.max( 0.00004, Math.min( 0.0002, velocity ) ); |
| 237 | + |
| 238 | + train.lookAt( lookAt.copy( position ).sub( tangent ) ); |
| 239 | + |
| 240 | + // |
| 241 | + |
| 242 | + renderer.render( scene, camera ); |
| 243 | + |
| 244 | + prevTime = time; |
| 245 | + |
| 246 | + } |
| 247 | + |
| 248 | + </script> |
| 249 | + |
| 250 | + </body> |
| 251 | +</html> |
0 commit comments