webgpu_display_stereo.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - stereo effects</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> - stereo effects. skybox by <a href="http://www.zfight.com/" target="_blank" rel="noopener">Jochum Skoglund</a>
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/webgpu": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.tsl.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { stereoPass } from 'three/addons/tsl/display/StereoPassNode.js';
  26. import { anaglyphPass } from 'three/addons/tsl/display/AnaglyphPassNode.js';
  27. import { parallaxBarrierPass } from 'three/addons/tsl/display/ParallaxBarrierPassNode.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { Timer } from 'three/addons/misc/Timer.js';
  30. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  31. let camera, scene, renderer, postProcessing;
  32. let stereo, anaglyph, parallaxBarrier;
  33. let mesh, dummy, timer;
  34. const position = new THREE.Vector3();
  35. const params = {
  36. effect: 'stereo',
  37. eyeSep: 0.064,
  38. };
  39. const effects = { Stereo: 'stereo', Anaglyph: 'anaglyph', ParallaxBarrier: 'parallaxBarrier' };
  40. init();
  41. function init() {
  42. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  43. camera.position.z = 3;
  44. scene = new THREE.Scene();
  45. scene.background = new THREE.CubeTextureLoader()
  46. .setPath( 'textures/cube/Park3Med/' )
  47. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  48. timer = new Timer();
  49. timer.connect( document );
  50. const geometry = new THREE.SphereGeometry( 0.1, 32, 16 );
  51. const textureCube = new THREE.CubeTextureLoader()
  52. .setPath( 'textures/cube/Park3Med/' )
  53. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  54. const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube } );
  55. mesh = new THREE.InstancedMesh( geometry, material, 500 );
  56. mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage );
  57. dummy = new THREE.Mesh();
  58. for ( let i = 0; i < 500; i ++ ) {
  59. dummy.position.x = Math.random() * 10 - 5;
  60. dummy.position.y = Math.random() * 10 - 5;
  61. dummy.position.z = Math.random() * 10 - 5;
  62. dummy.scale.x = dummy.scale.y = dummy.scale.z = Math.random() * 3 + 1;
  63. dummy.updateMatrix();
  64. mesh.setMatrixAt( i, dummy.matrix );
  65. }
  66. scene.add( mesh );
  67. //
  68. renderer = new THREE.WebGPURenderer();
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. renderer.setAnimationLoop( animate );
  72. document.body.appendChild( renderer.domElement );
  73. postProcessing = new THREE.PostProcessing( renderer );
  74. stereo = stereoPass( scene, camera );
  75. anaglyph = anaglyphPass( scene, camera );
  76. parallaxBarrier = parallaxBarrierPass( scene, camera );
  77. postProcessing.outputNode = stereo;
  78. const gui = new GUI();
  79. gui.add( params, 'effect', effects ).onChange( update );
  80. gui.add( params, 'eyeSep', 0.001, 0.15, 0.001 ).onChange( function ( value ) {
  81. stereo.stereo.eyeSep = value;
  82. anaglyph.stereo.eyeSep = value;
  83. parallaxBarrier.stereo.eyeSep = value;
  84. } );
  85. window.addEventListener( 'resize', onWindowResize );
  86. const controls = new OrbitControls( camera, renderer.domElement );
  87. controls.minDistance = 1;
  88. controls.maxDistance = 25;
  89. }
  90. function update( value ) {
  91. if ( value === 'stereo' ) {
  92. postProcessing.outputNode = stereo;
  93. } else if ( value === 'anaglyph' ) {
  94. postProcessing.outputNode = anaglyph;
  95. } else if ( value === 'parallaxBarrier' ) {
  96. postProcessing.outputNode = parallaxBarrier;
  97. }
  98. postProcessing.needsUpdate = true;
  99. }
  100. function onWindowResize() {
  101. camera.aspect = window.innerWidth / window.innerHeight;
  102. camera.updateProjectionMatrix();
  103. renderer.setSize( window.innerWidth, window.innerHeight );
  104. }
  105. function extractPosition( matrix, position ) {
  106. position.x = matrix.elements[ 12 ];
  107. position.y = matrix.elements[ 13 ];
  108. position.z = matrix.elements[ 14 ];
  109. }
  110. function animate() {
  111. timer.update();
  112. const elapsedTime = timer.getElapsed() * 0.1;
  113. for ( let i = 0; i < mesh.count; i ++ ) {
  114. mesh.getMatrixAt( i, dummy.matrix );
  115. extractPosition( dummy.matrix, position );
  116. position.x = 5 * Math.cos( elapsedTime + i );
  117. position.y = 5 * Math.sin( elapsedTime + i * 1.1 );
  118. dummy.matrix.setPosition( position );
  119. mesh.setMatrixAt( i, dummy.matrix );
  120. mesh.instanceMatrix.needsUpdate = true;
  121. }
  122. postProcessing.render();
  123. }
  124. </script>
  125. </body>
  126. </html>
粤ICP备19079148号