webgl_effects_parallaxbarrier.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - effects - parallax barrier</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 - parallax barrier">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_effects_parallaxbarrier.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_effects_parallaxbarrier.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 - parallax barrier<br/>
  16. skybox by <a href="https://www.pauldebevec.com/" target="_blank" rel="noopener">Paul Debevec</a>
  17. </div>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { ParallaxBarrierEffect } from 'three/addons/effects/ParallaxBarrierEffect.js';
  29. let container, camera, scene, renderer, effect;
  30. const spheres = [];
  31. let mouseX = 0;
  32. let mouseY = 0;
  33. let windowHalfX = window.innerWidth / 2;
  34. let windowHalfY = window.innerHeight / 2;
  35. document.addEventListener( 'mousemove', onDocumentMouseMove );
  36. init();
  37. function init() {
  38. container = document.createElement( 'div' );
  39. document.body.appendChild( container );
  40. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 100 );
  41. camera.position.z = 3;
  42. const path = 'textures/cube/pisa/';
  43. const format = '.png';
  44. const urls = [
  45. path + 'px' + format, path + 'nx' + format,
  46. path + 'py' + format, path + 'ny' + format,
  47. path + 'pz' + format, path + 'nz' + format
  48. ];
  49. const textureCube = new THREE.CubeTextureLoader().load( urls );
  50. scene = new THREE.Scene();
  51. scene.background = textureCube;
  52. const geometry = new THREE.SphereGeometry( 0.1, 32, 16 );
  53. const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube } );
  54. for ( let i = 0; i < 500; i ++ ) {
  55. const mesh = new THREE.Mesh( geometry, material );
  56. mesh.position.x = Math.random() * 10 - 5;
  57. mesh.position.y = Math.random() * 10 - 5;
  58. mesh.position.z = Math.random() * 10 - 5;
  59. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1;
  60. scene.add( mesh );
  61. spheres.push( mesh );
  62. }
  63. //
  64. renderer = new THREE.WebGLRenderer();
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setAnimationLoop( animate );
  67. container.appendChild( renderer.domElement );
  68. const width = window.innerWidth || 2;
  69. const height = window.innerHeight || 2;
  70. effect = new ParallaxBarrierEffect( renderer );
  71. effect.setSize( width, height );
  72. //
  73. window.addEventListener( 'resize', onWindowResize );
  74. }
  75. function onWindowResize() {
  76. windowHalfX = window.innerWidth / 2;
  77. windowHalfY = window.innerHeight / 2;
  78. camera.aspect = window.innerWidth / window.innerHeight;
  79. camera.updateProjectionMatrix();
  80. effect.setSize( window.innerWidth, window.innerHeight );
  81. }
  82. function onDocumentMouseMove( event ) {
  83. mouseX = ( event.clientX - windowHalfX ) / 100;
  84. mouseY = ( event.clientY - windowHalfY ) / 100;
  85. }
  86. //
  87. function animate() {
  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 = 5 * Math.cos( timer + i );
  95. sphere.position.y = 5 * Math.sin( timer + i * 1.1 );
  96. }
  97. effect.render( scene, camera );
  98. }
  99. </script>
  100. </body>
  101. </html>
粤ICP备19079148号