webgl_effects_stereo.html 3.9 KB

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