webgl_effects_stereo.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - effects - stereo</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - effects - stereo. skybox by <a href="http://www.zfight.com/" target="_blank" rel="noopener">Jochum Skoglund</a>
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { StereoEffect } from 'three/addons/effects/StereoEffect.js';
  24. let container, camera, scene, renderer, effect;
  25. const spheres = [];
  26. let mouseX = 0, mouseY = 0;
  27. let windowHalfX = window.innerWidth / 2;
  28. let windowHalfY = window.innerHeight / 2;
  29. document.addEventListener( 'mousemove', onDocumentMouseMove );
  30. init();
  31. function init() {
  32. container = document.createElement( 'div' );
  33. document.body.appendChild( container );
  34. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 100000 );
  35. camera.position.z = 3200;
  36. scene = new THREE.Scene();
  37. scene.background = new THREE.CubeTextureLoader()
  38. .setPath( 'textures/cube/Park3Med/' )
  39. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  40. const geometry = new THREE.SphereGeometry( 100, 32, 16 );
  41. const textureCube = new THREE.CubeTextureLoader()
  42. .setPath( 'textures/cube/Park3Med/' )
  43. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  44. textureCube.mapping = THREE.CubeRefractionMapping;
  45. const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube, refractionRatio: 0.95 } );
  46. for ( let i = 0; i < 500; i ++ ) {
  47. const mesh = new THREE.Mesh( geometry, material );
  48. mesh.position.x = Math.random() * 10000 - 5000;
  49. mesh.position.y = Math.random() * 10000 - 5000;
  50. mesh.position.z = Math.random() * 10000 - 5000;
  51. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1;
  52. scene.add( mesh );
  53. spheres.push( mesh );
  54. }
  55. //
  56. renderer = new THREE.WebGLRenderer();
  57. renderer.setPixelRatio( window.devicePixelRatio );
  58. renderer.setAnimationLoop( animate );
  59. container.appendChild( renderer.domElement );
  60. effect = new StereoEffect( renderer );
  61. effect.setSize( window.innerWidth, window.innerHeight );
  62. //
  63. window.addEventListener( 'resize', onWindowResize );
  64. }
  65. function onWindowResize() {
  66. windowHalfX = window.innerWidth / 2;
  67. windowHalfY = window.innerHeight / 2;
  68. camera.aspect = window.innerWidth / window.innerHeight;
  69. camera.updateProjectionMatrix();
  70. effect.setSize( window.innerWidth, window.innerHeight );
  71. }
  72. function onDocumentMouseMove( event ) {
  73. mouseX = ( event.clientX - windowHalfX ) * 10;
  74. mouseY = ( event.clientY - windowHalfY ) * 10;
  75. }
  76. //
  77. function animate() {
  78. const timer = 0.0001 * Date.now();
  79. camera.position.x += ( mouseX - camera.position.x ) * .05;
  80. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  81. camera.lookAt( scene.position );
  82. for ( let i = 0, il = spheres.length; i < il; i ++ ) {
  83. const sphere = spheres[ i ];
  84. sphere.position.x = 5000 * Math.cos( timer + i );
  85. sphere.position.y = 5000 * Math.sin( timer + i * 1.1 );
  86. }
  87. effect.render( scene, camera );
  88. }
  89. </script>
  90. </body>
  91. </html>