webgl_effects_stereo.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. <meta property="og:title" content="three.js webgl - effects - stereo">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_effects_stereo.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_effects_stereo.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <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>
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { StereoEffect } from 'three/addons/effects/StereoEffect.js';
  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. function init() {
  36. container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  39. camera.position.z = 3;
  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. const geometry = new THREE.SphereGeometry( 0.1, 32, 16 );
  45. const textureCube = new THREE.CubeTextureLoader()
  46. .setPath( 'textures/cube/Park3Med/' )
  47. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  48. textureCube.mapping = THREE.CubeRefractionMapping;
  49. const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube, refractionRatio: 0.95 } );
  50. for ( let i = 0; i < 500; i ++ ) {
  51. const mesh = new THREE.Mesh( geometry, material );
  52. mesh.position.x = Math.random() * 10 - 5;
  53. mesh.position.y = Math.random() * 10 - 5;
  54. mesh.position.z = Math.random() * 10 - 5;
  55. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1;
  56. scene.add( mesh );
  57. spheres.push( mesh );
  58. }
  59. //
  60. renderer = new THREE.WebGLRenderer();
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setAnimationLoop( animate );
  63. container.appendChild( renderer.domElement );
  64. effect = new StereoEffect( renderer );
  65. effect.setSize( window.innerWidth, window.innerHeight );
  66. //
  67. window.addEventListener( 'resize', onWindowResize );
  68. }
  69. function onWindowResize() {
  70. windowHalfX = window.innerWidth / 2;
  71. windowHalfY = window.innerHeight / 2;
  72. camera.aspect = window.innerWidth / window.innerHeight;
  73. camera.updateProjectionMatrix();
  74. effect.setSize( window.innerWidth, window.innerHeight );
  75. }
  76. function onDocumentMouseMove( event ) {
  77. mouseX = ( event.clientX - windowHalfX ) * 0.01;
  78. mouseY = ( event.clientY - windowHalfY ) * 0.01;
  79. }
  80. //
  81. function animate() {
  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 = 5 * Math.cos( timer + i );
  89. sphere.position.y = 5 * Math.sin( timer + i * 1.1 );
  90. }
  91. effect.render( scene, camera );
  92. }
  93. </script>
  94. </body>
  95. </html>
粤ICP备19079148号