three实现一个vr三维过山车动画效果代码
代码语言:html
所属分类:三维
代码描述:three实现一个vr三维过山车动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> </head> <body> <!-- Import maps polyfill --> <!-- Remove this when import maps will be widely supported --> <script async type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/es-module-shims.1.3.6.js"></script> <script type="importmap"> { "imports": { "three": "//repo.bfw.wiki/bfwtool/build/three.module.js" } } </script> <script type="module"> import * as THREE from 'three'; import Stats from '//repo.bfw.wiki/bfwtool/examples/jsm/libs/stats.module.js'; import { RollerCoasterGeometry, RollerCoasterShadowGeometry, RollerCoasterLiftersGeometry, TreesGeometry, SkyGeometry } from '//repo.bfw.wiki/bfwtool/examples/jsm/misc/RollerCoaster.js'; import { VRButton } from '//repo.bfw.wiki/bfwtool/examples/jsm/webxr/VRButton.js'; let mesh, material, geometry; const renderer = new THREE.WebGLRenderer( { antialias: true } ); renderer.setPixelRatio( window.devicePixelRatio ); renderer.setSize( window.innerWidth, window.innerHeight ); renderer.xr.enabled = true; renderer.xr.setReferenceSpaceType( 'local' ); document.body.appendChild( renderer.domElement ); document.body.appendChild( VRButton.createButton( renderer ) ); // const scene = new THREE.Scene(); scene.background = new THREE.Color( 0xf0f0ff ); const light = new THREE.HemisphereLight( 0xfff0f0, 0x606066 ); light.position.set( 1, 1, 1 ); scene.add( light ); const train = new THREE.Object3D(); scene.add( train ); const camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 500 ); train.add( camera ); // environment geometry = new THREE.PlaneGeometry( 500, 500, 15, 15 ); geometry.rotateX( - Math.PI / 2 ); const positions = geometry.attributes.position.array; const vertex = new THREE.Vector3(); for ( let i = 0; i < positions.length; i += 3 ) { vertex.fromArray( positions, i ); vertex.x += Math.random() * 10 - 5; vertex.z += Math.random() * 10 - 5; const distance = ( vertex.distanceTo( scene.position ) / 5 ) - 25; vertex.y = Math.random() * Math.max( 0, distance ); vertex.toArray( positions, i ); } geometry.computeVertexNormals(); material = new THREE.MeshLambertMaterial( { color: 0x407000 } ); mesh = new THREE.Mesh( geometry, material ); scene.add( mesh ); geometry = new TreesGeometry( mesh ); material = new THREE.MeshBasicMaterial( { side: THREE.DoubleSide, vertexColors: true } ); mesh = new THREE.Mesh( geometry, material ); scene.add( mesh ); geometry = new SkyGeometry(); material = new THREE.MeshBasicMaterial( { color: 0xffffff } ); mesh = new THREE.Mesh( geometry, material ); scene.add( mesh ); // const PI2 = Math.PI * 2; const curve = ( function.........完整代码请登录后点击上方下载按钮下载查看
网友评论0