123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js vr - lorenz attractor</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <script src="js/vr/HelioWebXRPolyfill.js"></script>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { VRButton } from './jsm/webxr/VRButton.js';
- var camera, scene, renderer;
- var attractor, light;
- var x = 15 * Math.random();
- var y = 15 * Math.random();
- var z = 15 * Math.random();
- var scale = .02; // for reducing overall displayed size
- var speed = 5; // integer, increase for faster visualization
- var steps = 100000;
- var current = 1;
- var shown = 10000;
- var beta = 8 / 3;
- var rho = 28;
- var sigma = 10;
- var dt = .005;
- init();
- animate();
- function draw() {
- var geometry = attractor.geometry;
- geometry.attributes.position.array.copyWithin( 3 );
- geometry.attributes.color.array.copyWithin( 3 );
- if ( current < steps ) {
- var dx = sigma * ( y - x ) * dt;
- var dy = ( x * ( rho - z ) - y ) * dt;
- var dz = ( x * y - beta * z ) * dt;
- x += dx;
- y += dy;
- z += dz;
- geometry.attributes.position.set( [ scale * x, scale * y, scale * z ], 0 );
- light.color.setHSL( current / steps, 1, .5 );
- geometry.attributes.color.set( light.color.toArray(), 0 );
- }
- if ( current < steps + shown ) {
- current ++;
- } else {
- current = 0;
- }
- }
- function init() {
- scene = new THREE.Scene();
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
- camera.position.set( 0, 1.6, 1 );
- //
- var geometry = new THREE.BufferGeometry();
- var positions = new Float32Array( 3 * shown );
- for ( var i = 0; i < positions.length; i += 3 ) {
- positions.set( [ scale * x, scale * y, scale * z ], i );
- }
- geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
- var colors = new Float32Array( 3 * shown );
- for ( var i = 0; i < positions.length; i += 3 ) {
- colors.set( [ 1, 0, 0 ], i );
- }
- geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
- var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } );
- attractor = new THREE.Line( geometry, material );
- attractor.position.set( 0, 1.5, - 2 );
- attractor.frustumCulled = false; // critical to avoid blackouts!
- scene.add( attractor );
- //
- light = new THREE.PointLight( 0xffffff, 1 );
- light.distance = 2;
- attractor.add( light );
- var ground = new THREE.Mesh(
- new THREE.PlaneBufferGeometry( 10, 10 ),
- new THREE.MeshPhongMaterial()
- );
- ground.geometry.rotateX( - 90 * Math.PI / 180 );
- scene.add( ground );
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.xr.enabled = true;
- document.body.appendChild( renderer.domElement );
- document.body.appendChild( VRButton.createButton( renderer ) );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- renderer.setAnimationLoop( render );
- }
- function render() {
- for ( var i = 0; i < speed; i ++ ) draw();
- attractor.geometry.attributes.position.needsUpdate = true;
- attractor.geometry.attributes.color.needsUpdate = true;
- attractor.rotation.z += .001;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|