webgl_effects_anaglyph.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. <meta property="og:title" content="three.js webgl - effects - anaglyph">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_effects_anaglyph.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_effects_anaglyph.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - effects - anaglyph<br/>
  16. skybox by <a href="https://www.pauldebevec.com/" target="_blank" rel="noopener">Paul Debevec</a>
  17. </div>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import { AnaglyphEffect } from 'three/addons/effects/AnaglyphEffect.js';
  29. let container, camera, scene, renderer, effect;
  30. const spheres = [];
  31. let mouseX = 0;
  32. let mouseY = 0;
  33. let windowHalfX = window.innerWidth / 2;
  34. let windowHalfY = window.innerHeight / 2;
  35. document.addEventListener( 'mousemove', onDocumentMouseMove );
  36. init();
  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. const path = 'textures/cube/pisa/';
  43. const format = '.png';
  44. const urls = [
  45. path + 'px' + format, path + 'nx' + format,
  46. path + 'py' + format, path + 'ny' + format,
  47. path + 'pz' + format, path + 'nz' + format
  48. ];
  49. const textureCube = new THREE.CubeTextureLoader().load( urls );
  50. scene = new THREE.Scene();
  51. scene.background = textureCube;
  52. const geometry = new THREE.SphereGeometry( 0.1, 32, 16 );
  53. const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube } );
  54. for ( let i = 0; i < 500; i ++ ) {
  55. const mesh = new THREE.Mesh( geometry, material );
  56. mesh.position.x = Math.random() * 10 - 5;
  57. mesh.position.y = Math.random() * 10 - 5;
  58. mesh.position.z = Math.random() * 10 - 5;
  59. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 3 + 1;
  60. scene.add( mesh );
  61. spheres.push( mesh );
  62. }
  63. //
  64. renderer = new THREE.WebGLRenderer();
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setAnimationLoop( animate );
  67. container.appendChild( renderer.domElement );
  68. const width = window.innerWidth || 2;
  69. const height = window.innerHeight || 2;
  70. effect = new AnaglyphEffect( renderer );
  71. effect.setSize( width, height );
  72. // Configure stereo parameters for physically-correct rendering
  73. // eyeSep: interpupillary distance (default 0.064m / 64mm for humans)
  74. // planeDistance: distance to the zero-parallax plane (objects here appear at screen depth)
  75. effect.eyeSep = 0.064;
  76. effect.planeDistance = 3; // Match camera distance to origin for zero parallax at scene center
  77. //
  78. window.addEventListener( 'resize', onWindowResize );
  79. }
  80. function onWindowResize() {
  81. windowHalfX = window.innerWidth / 2;
  82. windowHalfY = window.innerHeight / 2;
  83. camera.aspect = window.innerWidth / window.innerHeight;
  84. camera.updateProjectionMatrix();
  85. effect.setSize( window.innerWidth, window.innerHeight );
  86. }
  87. function onDocumentMouseMove( event ) {
  88. mouseX = ( event.clientX - windowHalfX ) / 100;
  89. mouseY = ( event.clientY - windowHalfY ) / 100;
  90. }
  91. //
  92. function animate() {
  93. render();
  94. }
  95. function render() {
  96. const timer = 0.0001 * Date.now();
  97. camera.position.x += ( mouseX - camera.position.x ) * .05;
  98. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  99. camera.lookAt( scene.position );
  100. for ( let i = 0, il = spheres.length; i < il; i ++ ) {
  101. const sphere = spheres[ i ];
  102. sphere.position.x = 5 * Math.cos( timer + i );
  103. sphere.position.y = 5 * Math.sin( timer + i * 1.1 );
  104. }
  105. effect.render( scene, camera );
  106. }
  107. </script>
  108. </body>
  109. </html>
粤ICP备19079148号