webgl_effects_anaglyph.html 3.6 KB

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