1
0

webgpu_display_stereo.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Stereo Effects</span>
  14. </div>
  15. <small>
  16. Collection of Stereo, Anaglyph and Parallax Barrier effects.
  17. </small>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.webgpu.js",
  23. "three/webgpu": "../build/three.webgpu.js",
  24. "three/tsl": "../build/three.tsl.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three/webgpu';
  31. import { stereoPass } from 'three/addons/tsl/display/StereoPassNode.js';
  32. import { anaglyphPass, AnaglyphAlgorithm, AnaglyphColorMode } from 'three/addons/tsl/display/AnaglyphPassNode.js';
  33. import { parallaxBarrierPass } from 'three/addons/tsl/display/ParallaxBarrierPassNode.js';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. import { Inspector } from 'three/addons/inspector/Inspector.js';
  36. let camera, scene, renderer, renderPipeline;
  37. let stereo, anaglyph, parallaxBarrier;
  38. let mesh, dummy, timer;
  39. let anaglyphFolder;
  40. const position = new THREE.Vector3();
  41. const params = {
  42. effect: 'stereo',
  43. eyeSep: 0.064,
  44. planeDistance: 3,
  45. anaglyphAlgorithm: 'dubois',
  46. anaglyphColorMode: 'redCyan'
  47. };
  48. const effects = { Stereo: 'stereo', Anaglyph: 'anaglyph', ParallaxBarrier: 'parallaxBarrier' };
  49. const anaglyphAlgorithms = {
  50. 'True': AnaglyphAlgorithm.TRUE,
  51. 'Grey': AnaglyphAlgorithm.GREY,
  52. 'Colour': AnaglyphAlgorithm.COLOUR,
  53. 'Half-Colour': AnaglyphAlgorithm.HALF_COLOUR,
  54. 'Dubois': AnaglyphAlgorithm.DUBOIS,
  55. 'Optimised': AnaglyphAlgorithm.OPTIMISED,
  56. 'Compromise': AnaglyphAlgorithm.COMPROMISE
  57. };
  58. const anaglyphColorModes = {
  59. 'Red / Cyan': AnaglyphColorMode.RED_CYAN,
  60. 'Magenta / Cyan': AnaglyphColorMode.MAGENTA_CYAN,
  61. 'Magenta / Green': AnaglyphColorMode.MAGENTA_GREEN
  62. };
  63. init();
  64. function init() {
  65. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  66. camera.position.z = 3;
  67. scene = new THREE.Scene();
  68. scene.background = new THREE.CubeTextureLoader()
  69. .setPath( 'textures/cube/Park3Med/' )
  70. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  71. timer = new THREE.Timer();
  72. timer.connect( document );
  73. const geometry = new THREE.SphereGeometry( 0.1, 32, 16 );
  74. const textureCube = new THREE.CubeTextureLoader()
  75. .setPath( 'textures/cube/Park3Med/' )
  76. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  77. const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube } );
  78. mesh = new THREE.InstancedMesh( geometry, material, 500 );
  79. mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage );
  80. dummy = new THREE.Mesh();
  81. for ( let i = 0; i < 500; i ++ ) {
  82. dummy.position.x = Math.random() * 10 - 5;
  83. dummy.position.y = Math.random() * 10 - 5;
  84. dummy.position.z = Math.random() * 10 - 5;
  85. dummy.scale.x = dummy.scale.y = dummy.scale.z = Math.random() * 3 + 1;
  86. dummy.updateMatrix();
  87. mesh.setMatrixAt( i, dummy.matrix );
  88. }
  89. scene.add( mesh );
  90. //
  91. renderer = new THREE.WebGPURenderer();
  92. renderer.setPixelRatio( window.devicePixelRatio );
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. renderer.setAnimationLoop( animate );
  95. renderer.inspector = new Inspector();
  96. document.body.appendChild( renderer.domElement );
  97. renderPipeline = new THREE.RenderPipeline( renderer );
  98. stereo = stereoPass( scene, camera );
  99. anaglyph = anaglyphPass( scene, camera );
  100. parallaxBarrier = parallaxBarrierPass( scene, camera );
  101. // Configure anaglyph for physically-correct stereo with zero parallax at scene center
  102. anaglyph.eyeSep = params.eyeSep;
  103. anaglyph.planeDistance = params.planeDistance;
  104. renderPipeline.outputNode = stereo;
  105. const gui = renderer.inspector.createParameters( 'Stereo Settings' );
  106. gui.add( params, 'effect', effects ).onChange( update );
  107. gui.add( params, 'eyeSep', 0.001, 0.15, 0.001 ).onChange( function ( value ) {
  108. stereo.stereo.eyeSep = value;
  109. anaglyph.eyeSep = value; // Anaglyph has direct eyeSep property
  110. parallaxBarrier.stereo.eyeSep = value;
  111. } );
  112. // Anaglyph-specific settings folder
  113. anaglyphFolder = gui.addFolder( 'Anaglyph Options' );
  114. anaglyphFolder.add( params, 'anaglyphAlgorithm', anaglyphAlgorithms ).name( 'Algorithm' ).onChange( function ( value ) {
  115. anaglyph.algorithm = value;
  116. } );
  117. anaglyphFolder.add( params, 'anaglyphColorMode', anaglyphColorModes ).name( 'Color Mode' ).onChange( function ( value ) {
  118. anaglyph.colorMode = value;
  119. } );
  120. anaglyphFolder.add( params, 'planeDistance', 0.5, 10, 0.1 ).name( 'Plane Distance' ).onChange( function ( value ) {
  121. anaglyph.planeDistance = value;
  122. } );
  123. anaglyphFolder.paramList.domElement.style.display = 'none';
  124. window.addEventListener( 'resize', onWindowResize );
  125. const controls = new OrbitControls( camera, renderer.domElement );
  126. controls.minDistance = 1;
  127. controls.maxDistance = 25;
  128. }
  129. function update( value ) {
  130. if ( value === 'stereo' ) {
  131. renderPipeline.outputNode = stereo;
  132. anaglyphFolder.paramList.domElement.style.display = 'none';
  133. } else if ( value === 'anaglyph' ) {
  134. renderPipeline.outputNode = anaglyph;
  135. anaglyphFolder.paramList.domElement.style.display = '';
  136. } else if ( value === 'parallaxBarrier' ) {
  137. renderPipeline.outputNode = parallaxBarrier;
  138. anaglyphFolder.paramList.domElement.style.display = 'none';
  139. }
  140. renderPipeline.needsUpdate = true;
  141. }
  142. function onWindowResize() {
  143. camera.aspect = window.innerWidth / window.innerHeight;
  144. camera.updateProjectionMatrix();
  145. renderer.setSize( window.innerWidth, window.innerHeight );
  146. }
  147. function extractPosition( matrix, position ) {
  148. position.x = matrix.elements[ 12 ];
  149. position.y = matrix.elements[ 13 ];
  150. position.z = matrix.elements[ 14 ];
  151. }
  152. function animate() {
  153. timer.update();
  154. const elapsedTime = timer.getElapsed() * 0.1;
  155. for ( let i = 0; i < mesh.count; i ++ ) {
  156. mesh.getMatrixAt( i, dummy.matrix );
  157. extractPosition( dummy.matrix, position );
  158. position.x = 5 * Math.cos( elapsedTime + i );
  159. position.y = 5 * Math.sin( elapsedTime + i * 1.1 );
  160. dummy.matrix.setPosition( position );
  161. mesh.setMatrixAt( i, dummy.matrix );
  162. mesh.instanceMatrix.needsUpdate = true;
  163. }
  164. renderPipeline.render();
  165. }
  166. </script>
  167. </body>
  168. </html>
粤ICP备19079148号