webxr_vr_lorenzattractor.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - lorenz attractor</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="module">
  11. import * as THREE from '../build/three.module.js';
  12. import { VRButton } from './jsm/webxr/VRButton.js';
  13. let camera, scene, renderer;
  14. let attractor, light;
  15. let x = 15 * Math.random();
  16. let y = 15 * Math.random();
  17. let z = 15 * Math.random();
  18. const scale = .02; // for reducing overall displayed size
  19. const speed = 5; // integer, increase for faster visualization
  20. const steps = 100000;
  21. let current = 1;
  22. const shown = 10000;
  23. const beta = 8 / 3;
  24. const rho = 28;
  25. const sigma = 10;
  26. const dt = .005;
  27. init();
  28. animate();
  29. function draw() {
  30. const geometry = attractor.geometry;
  31. geometry.attributes.position.array.copyWithin( 3 );
  32. geometry.attributes.color.array.copyWithin( 3 );
  33. if ( current < steps ) {
  34. const dx = sigma * ( y - x ) * dt;
  35. const dy = ( x * ( rho - z ) - y ) * dt;
  36. const dz = ( x * y - beta * z ) * dt;
  37. x += dx;
  38. y += dy;
  39. z += dz;
  40. geometry.attributes.position.set( [ scale * x, scale * y, scale * z ], 0 );
  41. light.color.setHSL( current / steps, 1, .5 );
  42. geometry.attributes.color.set( light.color.toArray(), 0 );
  43. }
  44. if ( current < steps + shown ) {
  45. current ++;
  46. } else {
  47. current = 0;
  48. }
  49. }
  50. function init() {
  51. scene = new THREE.Scene();
  52. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  53. camera.position.set( 0, 1.6, 1 );
  54. //
  55. const geometry = new THREE.BufferGeometry();
  56. const positions = new Float32Array( 3 * shown );
  57. for ( let i = 0; i < positions.length; i += 3 ) {
  58. positions.set( [ scale * x, scale * y, scale * z ], i );
  59. }
  60. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  61. const colors = new Float32Array( 3 * shown );
  62. for ( let i = 0; i < positions.length; i += 3 ) {
  63. colors.set( [ 1, 0, 0 ], i );
  64. }
  65. geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  66. const material = new THREE.LineBasicMaterial( { vertexColors: true } );
  67. attractor = new THREE.Line( geometry, material );
  68. attractor.position.set( 0, 1.5, - 2 );
  69. attractor.frustumCulled = false; // critical to avoid blackouts!
  70. scene.add( attractor );
  71. //
  72. light = new THREE.PointLight( 0xffffff, 1 );
  73. light.distance = 2;
  74. attractor.add( light );
  75. const ground = new THREE.Mesh(
  76. new THREE.PlaneGeometry( 10, 10 ),
  77. new THREE.MeshPhongMaterial()
  78. );
  79. ground.geometry.rotateX( - 90 * Math.PI / 180 );
  80. scene.add( ground );
  81. //
  82. renderer = new THREE.WebGLRenderer( { antialias: true } );
  83. renderer.setPixelRatio( window.devicePixelRatio );
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. renderer.xr.enabled = true;
  86. document.body.appendChild( renderer.domElement );
  87. document.body.appendChild( VRButton.createButton( renderer ) );
  88. //
  89. window.addEventListener( 'resize', onWindowResize );
  90. //
  91. if ( typeof TESTING !== 'undefined' ) { for ( let i = 0; i < 200; i ++ ) { render(); } };
  92. }
  93. function onWindowResize() {
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. }
  98. function animate() {
  99. renderer.setAnimationLoop( render );
  100. }
  101. function render() {
  102. for ( let i = 0; i < speed; i ++ ) draw();
  103. attractor.geometry.attributes.position.needsUpdate = true;
  104. attractor.geometry.attributes.color.needsUpdate = true;
  105. attractor.rotation.z += .001;
  106. renderer.render( scene, camera );
  107. }
  108. </script>
  109. </body>
  110. </html>