webgl_effects_parallaxbarrier.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. #info {
  14. color: #000;
  15. position: absolute;
  16. top: 10px;
  17. width: 100%;
  18. text-align: center;
  19. z-index: 100;
  20. display:block;
  21. }
  22. #info a {
  23. color: #046;
  24. font-weight: bold;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="info">
  30. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - effects - parallax barrier<br />
  31. Texture by <a href="http://www.humus.name/index.php?page=Textures" target="_blank" rel="noopener">Humus</a>
  32. Ferrari F50 model by <a href="http://artist-3d.com/free_3d_models/dnm/model_disp.php?uid=1687" target="_blank" rel="noopener">Daniel Sathya</a>
  33. </div>
  34. <script src="../build/three.js"></script>
  35. <script src="js/effects/ParallaxBarrierEffect.js"></script>
  36. <script src="js/loaders/GLTFLoader.js"></script>
  37. <script src="js/controls/OrbitControls.js"></script>
  38. <script src="js/Detector.js"></script>
  39. <script src="js/libs/stats.min.js"></script>
  40. <script>
  41. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  42. var stats, camera, scene, renderer, effect;
  43. function init() {
  44. var container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  47. camera.position.set( 200, 100, 200 );
  48. controls = new THREE.OrbitControls( camera );
  49. // controls.target.set( 0, 100, 0 );
  50. controls.maxPolarAngle = Math.PI / 2.25;
  51. controls.minDistance = 100;
  52. controls.maxDistance = 500;
  53. var textureCube = new THREE.CubeTextureLoader()
  54. .setPath( 'textures/cube/Bridge2/')
  55. .load( [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ] );
  56. scene = new THREE.Scene();
  57. scene.background = new THREE.Color( 0xa0a0a0 );
  58. scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );
  59. var hemi = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  60. hemi.position.set( 0, 200, 0 );
  61. scene.add( hemi );
  62. var direct = new THREE.DirectionalLight( 0xffffff, 1.5 );
  63. direct.position.set( 200, 300, 200 );
  64. scene.add( direct );
  65. // ground
  66. var mesh = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  67. mesh.rotation.x = - Math.PI / 2;
  68. scene.add( mesh );
  69. var grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
  70. grid.material.opacity = 0.2;
  71. grid.material.transparent = true;
  72. scene.add( grid );
  73. var width = window.innerWidth || 2;
  74. var height = window.innerHeight || 2;
  75. var loader = new THREE.GLTFLoader();
  76. loader.load( 'models/gltf/f50/f50.glb', function ( gltf ) {
  77. var object = gltf.scene;
  78. object.traverse( function ( child ) {
  79. if ( child.isMesh ) {
  80. child.material.envMap = textureCube;
  81. child.material.envMapIntensity = 1;
  82. child.material.metalness = 0.6;
  83. child.material.roughness = 0.1;
  84. child.material.needsUpdate = true;
  85. }
  86. } );
  87. scene.add( object )
  88. } );
  89. renderer = new THREE.WebGLRenderer();
  90. renderer.setPixelRatio( window.devicePixelRatio );
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. container.appendChild( renderer.domElement );
  93. effect = new THREE.ParallaxBarrierEffect( renderer );
  94. effect.setSize( width, height );
  95. window.addEventListener( 'resize', onWindowResize, false );
  96. stats = new Stats();
  97. container.appendChild( stats.dom );
  98. }
  99. function onWindowResize() {
  100. windowHalfX = window.innerWidth / 2;
  101. windowHalfY = window.innerHeight / 2;
  102. camera.aspect = window.innerWidth / window.innerHeight;
  103. camera.updateProjectionMatrix();
  104. effect.setSize( window.innerWidth, window.innerHeight );
  105. }
  106. function animate() {
  107. requestAnimationFrame( animate );
  108. render();
  109. stats.update();
  110. }
  111. function render() {
  112. effect.render( scene, camera );
  113. }
  114. init();
  115. animate();
  116. </script>
  117. </body>
  118. </html>