webgpu_display_stereo.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. const geometry = new THREE.SphereGeometry( 0.1, 32, 16 );
  50. const textureCube = new THREE.CubeTextureLoader()
  51. .setPath( 'textures/cube/Park3Med/' )
  52. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  53. const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube } );
  54. mesh = new THREE.InstancedMesh( geometry, material, 500 );
  55. mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage );
  56. dummy = new THREE.Mesh();
  57. for ( let i = 0; i < 500; i ++ ) {
  58. dummy.position.x = Math.random() * 10 - 5;
  59. dummy.position.y = Math.random() * 10 - 5;
  60. dummy.position.z = Math.random() * 10 - 5;
  61. dummy.scale.x = dummy.scale.y = dummy.scale.z = Math.random() * 3 + 1;
  62. dummy.updateMatrix();
  63. mesh.setMatrixAt( i, dummy.matrix );
  64. }
  65. scene.add( mesh );
  66. //
  67. renderer = new THREE.WebGPURenderer();
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. renderer.setAnimationLoop( animate );
  71. document.body.appendChild( renderer.domElement );
  72. postProcessing = new THREE.PostProcessing( renderer );
  73. stereo = stereoPass( scene, camera );
  74. anaglyph = anaglyphPass( scene, camera );
  75. parallaxBarrier = parallaxBarrierPass( scene, camera );
  76. postProcessing.outputNode = stereo;
  77. const gui = new GUI();
  78. gui.add( params, 'effect', effects ).onChange( update );
  79. gui.add( params, 'eyeSep', 0.001, 0.15, 0.001 ).onChange( function ( value ) {
  80. stereo.stereo.eyeSep = value;
  81. anaglyph.stereo.eyeSep = value;
  82. parallaxBarrier.stereo.eyeSep = value;
  83. } );
  84. window.addEventListener( 'resize', onWindowResize );
  85. const controls = new OrbitControls( camera, renderer.domElement );
  86. controls.minDistance = 1;
  87. controls.maxDistance = 25;
  88. }
  89. function update( value ) {
  90. if ( value === 'stereo' ) {
  91. postProcessing.outputNode = stereo;
  92. } else if ( value === 'anaglyph' ) {
  93. postProcessing.outputNode = anaglyph;
  94. } else if ( value === 'parallaxBarrier' ) {
  95. postProcessing.outputNode = parallaxBarrier;
  96. }
  97. postProcessing.needsUpdate = true;
  98. }
  99. function onWindowResize() {
  100. camera.aspect = window.innerWidth / window.innerHeight;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. }
  104. function extractPosition( matrix, position ) {
  105. position.x = matrix.elements[ 12 ];
  106. position.y = matrix.elements[ 13 ];
  107. position.z = matrix.elements[ 14 ];
  108. }
  109. function animate() {
  110. timer.update();
  111. const elapsedTime = timer.getElapsed() * 0.1;
  112. for ( let i = 0; i < mesh.count; i ++ ) {
  113. mesh.getMatrixAt( i, dummy.matrix );
  114. extractPosition( dummy.matrix, position );
  115. position.x = 5 * Math.cos( elapsedTime + i );
  116. position.y = 5 * Math.sin( elapsedTime + i * 1.1 );
  117. dummy.matrix.setPosition( position );
  118. mesh.setMatrixAt( i, dummy.matrix );
  119. mesh.instanceMatrix.needsUpdate = true;
  120. }
  121. postProcessing.render();
  122. }
  123. </script>
  124. </body>
  125. </html>
粤ICP备19079148号