123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <!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 type="module">
- import * as THREE from '../build/three.module.js';
- import { VRButton } from './jsm/webxr/VRButton.js';
- let camera, scene, renderer;
- let attractor, light;
- let x = 15 * Math.random();
- let y = 15 * Math.random();
- let z = 15 * Math.random();
- const scale = .02; // for reducing overall displayed size
- const speed = 5; // integer, increase for faster visualization
- const steps = 100000;
- let current = 1;
- const shown = 10000;
- const beta = 8 / 3;
- const rho = 28;
- const sigma = 10;
- const dt = .005;
- init();
- animate();
- function draw() {
- const geometry = attractor.geometry;
- geometry.attributes.position.array.copyWithin( 3 );
- geometry.attributes.color.array.copyWithin( 3 );
- if ( current < steps ) {
- const dx = sigma * ( y - x ) * dt;
- const dy = ( x * ( rho - z ) - y ) * dt;
- const 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 );
- //
- const geometry = new THREE.BufferGeometry();
- const positions = new Float32Array( 3 * shown );
- for ( let i = 0; i < positions.length; i += 3 ) {
- positions.set( [ scale * x, scale * y, scale * z ], i );
- }
- geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
- const colors = new Float32Array( 3 * shown );
- for ( let i = 0; i < positions.length; i += 3 ) {
- colors.set( [ 1, 0, 0 ], i );
- }
- geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
- const material = new THREE.LineBasicMaterial( { vertexColors: true } );
- 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 );
- const ground = new THREE.Mesh(
- new THREE.PlaneGeometry( 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 );
- //
- if ( typeof TESTING !== 'undefined' ) { for ( let i = 0; i < 200; i ++ ) { render(); } };
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- renderer.setAnimationLoop( render );
- }
- function render() {
- for ( let 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>
|