webgl_effects_stereo.html 4.0 KB

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