webgl_effects_parallaxbarrier.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. <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 - parallax barrier<br/>
  12. skybox by <a href="https://www.pauldebevec.com/" target="_blank" rel="noopener">Paul Debevec</a>
  13. </div>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  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 { ParallaxBarrierEffect } from 'three/addons/effects/ParallaxBarrierEffect.js';
  28. let container, camera, scene, renderer, effect;
  29. const spheres = [];
  30. let mouseX = 0;
  31. let mouseY = 0;
  32. let windowHalfX = window.innerWidth / 2;
  33. let windowHalfY = window.innerHeight / 2;
  34. document.addEventListener( 'mousemove', onDocumentMouseMove );
  35. init();
  36. animate();
  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. camera.focalLength = 3;
  43. const path = 'textures/cube/pisa/';
  44. const format = '.png';
  45. const urls = [
  46. path + 'px' + format, path + 'nx' + format,
  47. path + 'py' + format, path + 'ny' + format,
  48. path + 'pz' + format, path + 'nz' + format
  49. ];
  50. const textureCube = new THREE.CubeTextureLoader().load( urls );
  51. scene = new THREE.Scene();
  52. scene.background = textureCube;
  53. const geometry = new THREE.SphereGeometry( 0.1, 32, 16 );
  54. const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube } );
  55. for ( let i = 0; i < 500; i ++ ) {
  56. const mesh = new THREE.Mesh( geometry, material );
  57. mesh.position.x = Math.random() * 10 - 5;
  58. mesh.position.y = Math.random() * 10 - 5;
  59. mesh.position.z = Math.random() * 10 - 5;
  60. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1;
  61. scene.add( mesh );
  62. spheres.push( mesh );
  63. }
  64. //
  65. renderer = new THREE.WebGLRenderer();
  66. renderer.setPixelRatio( window.devicePixelRatio );
  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. requestAnimationFrame( animate );
  89. render();
  90. }
  91. function render() {
  92. const timer = 0.0001 * Date.now();
  93. camera.position.x += ( mouseX - camera.position.x ) * .05;
  94. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  95. camera.lookAt( scene.position );
  96. for ( let i = 0, il = spheres.length; i < il; i ++ ) {
  97. const sphere = spheres[ i ];
  98. sphere.position.x = 5 * Math.cos( timer + i );
  99. sphere.position.y = 5 * Math.sin( timer + i * 1.1 );
  100. }
  101. effect.render( scene, camera );
  102. }
  103. </script>
  104. </body>
  105. </html>