webxr_vr_lorenzattractor.html 3.8 KB

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