webgl_effects_stereo.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { StereoEffect } from './jsm/effects/StereoEffect.js';
  26. let container, camera, scene, renderer, effect;
  27. const spheres = [];
  28. let mouseX = 0, mouseY = 0;
  29. let windowHalfX = window.innerWidth / 2;
  30. let windowHalfY = window.innerHeight / 2;
  31. document.addEventListener( 'mousemove', onDocumentMouseMove );
  32. init();
  33. animate();
  34. function init() {
  35. container = document.createElement( 'div' );
  36. document.body.appendChild( container );
  37. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 100000 );
  38. camera.position.z = 3200;
  39. scene = new THREE.Scene();
  40. scene.background = new THREE.CubeTextureLoader()
  41. .setPath( 'textures/cube/Park3Med/' )
  42. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  43. const geometry = new THREE.SphereGeometry( 100, 32, 16 );
  44. const textureCube = new THREE.CubeTextureLoader()
  45. .setPath( 'textures/cube/Park3Med/' )
  46. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  47. textureCube.mapping = THREE.CubeRefractionMapping;
  48. const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube, refractionRatio: 0.95 } );
  49. for ( let i = 0; i < 500; i ++ ) {
  50. const mesh = new THREE.Mesh( geometry, material );
  51. mesh.position.x = Math.random() * 10000 - 5000;
  52. mesh.position.y = Math.random() * 10000 - 5000;
  53. mesh.position.z = Math.random() * 10000 - 5000;
  54. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1;
  55. scene.add( mesh );
  56. spheres.push( mesh );
  57. }
  58. //
  59. renderer = new THREE.WebGLRenderer();
  60. renderer.setPixelRatio( window.devicePixelRatio );
  61. container.appendChild( renderer.domElement );
  62. effect = new StereoEffect( renderer );
  63. effect.setSize( window.innerWidth, window.innerHeight );
  64. //
  65. window.addEventListener( 'resize', onWindowResize );
  66. }
  67. function onWindowResize() {
  68. windowHalfX = window.innerWidth / 2;
  69. windowHalfY = window.innerHeight / 2;
  70. camera.aspect = window.innerWidth / window.innerHeight;
  71. camera.updateProjectionMatrix();
  72. effect.setSize( window.innerWidth, window.innerHeight );
  73. }
  74. function onDocumentMouseMove( event ) {
  75. mouseX = ( event.clientX - windowHalfX ) * 10;
  76. mouseY = ( event.clientY - windowHalfY ) * 10;
  77. }
  78. //
  79. function animate() {
  80. requestAnimationFrame( animate );
  81. render();
  82. }
  83. function render() {
  84. const timer = 0.0001 * Date.now();
  85. camera.position.x += ( mouseX - camera.position.x ) * .05;
  86. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  87. camera.lookAt( scene.position );
  88. for ( let i = 0, il = spheres.length; i < il; i ++ ) {
  89. const sphere = spheres[ i ];
  90. sphere.position.x = 5000 * Math.cos( timer + i );
  91. sphere.position.y = 5000 * Math.sin( timer + i * 1.1 );
  92. }
  93. effect.render( scene, camera );
  94. }
  95. </script>
  96. </body>
  97. </html>