webgpu_display_stereo.html 4.8 KB

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