webgl_effects_anaglyph.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - effects - anaglyph</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 - anaglyph<br/>
  12. skybox by <a href="http://ict.debevec.org/~debevec/" target="_blank" rel="noopener">Paul Debevec</a>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { AnaglyphEffect } from './jsm/effects/AnaglyphEffect.js';
  17. var container, camera, scene, renderer, effect;
  18. var spheres = [];
  19. var mouseX = 0;
  20. var mouseY = 0;
  21. var windowHalfX = window.innerWidth / 2;
  22. var windowHalfY = window.innerHeight / 2;
  23. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  24. init();
  25. animate();
  26. function init() {
  27. container = document.createElement( 'div' );
  28. document.body.appendChild( container );
  29. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.01, 100 );
  30. camera.position.z = 3;
  31. camera.focalLength = 3;
  32. var path = "textures/cube/pisa/";
  33. var format = '.png';
  34. var urls = [
  35. path + 'px' + format, path + 'nx' + format,
  36. path + 'py' + format, path + 'ny' + format,
  37. path + 'pz' + format, path + 'nz' + format
  38. ];
  39. var textureCube = new THREE.CubeTextureLoader().load( urls );
  40. scene = new THREE.Scene();
  41. scene.background = textureCube;
  42. var geometry = new THREE.SphereBufferGeometry( 0.1, 32, 16 );
  43. var material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube } );
  44. for ( var i = 0; i < 500; i ++ ) {
  45. var mesh = new THREE.Mesh( geometry, material );
  46. mesh.position.x = Math.random() * 10 - 5;
  47. mesh.position.y = Math.random() * 10 - 5;
  48. mesh.position.z = Math.random() * 10 - 5;
  49. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1;
  50. scene.add( mesh );
  51. spheres.push( mesh );
  52. }
  53. //
  54. renderer = new THREE.WebGLRenderer();
  55. renderer.setPixelRatio( window.devicePixelRatio );
  56. container.appendChild( renderer.domElement );
  57. var width = window.innerWidth || 2;
  58. var height = window.innerHeight || 2;
  59. effect = new AnaglyphEffect( renderer );
  60. effect.setSize( width, height );
  61. //
  62. window.addEventListener( 'resize', onWindowResize, false );
  63. }
  64. function onWindowResize() {
  65. windowHalfX = window.innerWidth / 2;
  66. windowHalfY = window.innerHeight / 2;
  67. camera.aspect = window.innerWidth / window.innerHeight;
  68. camera.updateProjectionMatrix();
  69. effect.setSize( window.innerWidth, window.innerHeight );
  70. }
  71. function onDocumentMouseMove( event ) {
  72. mouseX = ( event.clientX - windowHalfX ) / 100;
  73. mouseY = ( event.clientY - windowHalfY ) / 100;
  74. }
  75. //
  76. function animate() {
  77. requestAnimationFrame( animate );
  78. render();
  79. }
  80. function render() {
  81. var timer = 0.0001 * Date.now();
  82. camera.position.x += ( mouseX - camera.position.x ) * .05;
  83. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  84. camera.lookAt( scene.position );
  85. for ( var i = 0, il = spheres.length; i < il; i ++ ) {
  86. var sphere = spheres[ i ];
  87. sphere.position.x = 5 * Math.cos( timer + i );
  88. sphere.position.y = 5 * Math.sin( timer + i * 1.1 );
  89. }
  90. effect.render( scene, camera );
  91. }
  92. </script>
  93. </body>
  94. </html>