webgl_effects_stereo.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. animate();
  32. function init() {
  33. container = document.createElement( 'div' );
  34. document.body.appendChild( container );
  35. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 100000 );
  36. camera.position.z = 3200;
  37. scene = new THREE.Scene();
  38. scene.background = new THREE.CubeTextureLoader()
  39. .setPath( 'textures/cube/Park3Med/' )
  40. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  41. const geometry = new THREE.SphereGeometry( 100, 32, 16 );
  42. const textureCube = new THREE.CubeTextureLoader()
  43. .setPath( 'textures/cube/Park3Med/' )
  44. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  45. textureCube.mapping = THREE.CubeRefractionMapping;
  46. const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube, refractionRatio: 0.95 } );
  47. for ( let i = 0; i < 500; i ++ ) {
  48. const mesh = new THREE.Mesh( geometry, material );
  49. mesh.position.x = Math.random() * 10000 - 5000;
  50. mesh.position.y = Math.random() * 10000 - 5000;
  51. mesh.position.z = Math.random() * 10000 - 5000;
  52. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1;
  53. scene.add( mesh );
  54. spheres.push( mesh );
  55. }
  56. //
  57. renderer = new THREE.WebGLRenderer();
  58. renderer.setPixelRatio( window.devicePixelRatio );
  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. requestAnimationFrame( animate );
  79. render();
  80. }
  81. function render() {
  82. const timer = 0.0001 * Date.now();
  83. camera.position.x += ( mouseX - camera.position.x ) * .05;
  84. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  85. camera.lookAt( scene.position );
  86. for ( let i = 0, il = spheres.length; i < il; i ++ ) {
  87. const sphere = spheres[ i ];
  88. sphere.position.x = 5000 * Math.cos( timer + i );
  89. sphere.position.y = 5000 * Math.sin( timer + i * 1.1 );
  90. }
  91. effect.render( scene, camera );
  92. }
  93. </script>
  94. </body>
  95. </html>