webgl_effects_parallaxbarrier.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. textureCube.colorSpace = THREE.SRGBColorSpace;
  52. scene = new THREE.Scene();
  53. scene.background = textureCube;
  54. const geometry = new THREE.SphereGeometry( 0.1, 32, 16 );
  55. const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube } );
  56. for ( let i = 0; i < 500; i ++ ) {
  57. const mesh = new THREE.Mesh( geometry, material );
  58. mesh.position.x = Math.random() * 10 - 5;
  59. mesh.position.y = Math.random() * 10 - 5;
  60. mesh.position.z = Math.random() * 10 - 5;
  61. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1;
  62. scene.add( mesh );
  63. spheres.push( mesh );
  64. }
  65. //
  66. renderer = new THREE.WebGLRenderer();
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. container.appendChild( renderer.domElement );
  69. const width = window.innerWidth || 2;
  70. const height = window.innerHeight || 2;
  71. effect = new ParallaxBarrierEffect( renderer );
  72. effect.setSize( width, height );
  73. //
  74. window.addEventListener( 'resize', onWindowResize );
  75. }
  76. function onWindowResize() {
  77. windowHalfX = window.innerWidth / 2;
  78. windowHalfY = window.innerHeight / 2;
  79. camera.aspect = window.innerWidth / window.innerHeight;
  80. camera.updateProjectionMatrix();
  81. effect.setSize( window.innerWidth, window.innerHeight );
  82. }
  83. function onDocumentMouseMove( event ) {
  84. mouseX = ( event.clientX - windowHalfX ) / 100;
  85. mouseY = ( event.clientY - windowHalfY ) / 100;
  86. }
  87. //
  88. function animate() {
  89. requestAnimationFrame( animate );
  90. render();
  91. }
  92. function render() {
  93. const timer = 0.0001 * Date.now();
  94. camera.position.x += ( mouseX - camera.position.x ) * .05;
  95. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  96. camera.lookAt( scene.position );
  97. for ( let i = 0, il = spheres.length; i < il; i ++ ) {
  98. const sphere = spheres[ i ];
  99. sphere.position.x = 5 * Math.cos( timer + i );
  100. sphere.position.y = 5 * Math.sin( timer + i * 1.1 );
  101. }
  102. effect.render( scene, camera );
  103. }
  104. </script>
  105. </body>
  106. </html>