webgpu_display_stereo.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - stereo</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. 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 } from 'three/tsl';
  25. let camera, scene, renderer, postProcessing;
  26. let mesh, dummy;
  27. let mouseX = 0, mouseY = 0;
  28. let windowHalfX = window.innerWidth / 2;
  29. let windowHalfY = window.innerHeight / 2;
  30. const position = new THREE.Vector3();
  31. init();
  32. function init() {
  33. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  34. camera.position.z = 3;
  35. scene = new THREE.Scene();
  36. scene.background = new THREE.CubeTextureLoader()
  37. .setPath( 'textures/cube/Park3Med/' )
  38. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  39. const geometry = new THREE.SphereGeometry( 0.1, 32, 16 );
  40. const textureCube = new THREE.CubeTextureLoader()
  41. .setPath( 'textures/cube/Park3Med/' )
  42. .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
  43. textureCube.mapping = THREE.CubeRefractionMapping;
  44. const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube, refractionRatio: 0.95 } );
  45. mesh = new THREE.InstancedMesh( geometry, material, 500 );
  46. mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage );
  47. dummy = new THREE.Mesh();
  48. for ( let i = 0; i < 500; i ++ ) {
  49. dummy.position.x = Math.random() * 10 - 5;
  50. dummy.position.y = Math.random() * 10 - 5;
  51. dummy.position.z = Math.random() * 10 - 5;
  52. dummy.scale.x = dummy.scale.y = dummy.scale.z = Math.random() * 3 + 1;
  53. dummy.updateMatrix();
  54. mesh.setMatrixAt( i, dummy.matrix );
  55. }
  56. scene.add( mesh );
  57. //
  58. renderer = new THREE.WebGPURenderer();
  59. renderer.setPixelRatio( window.devicePixelRatio );
  60. renderer.setSize( window.innerWidth, window.innerHeight );
  61. renderer.setAnimationLoop( animate );
  62. document.body.appendChild( renderer.domElement );
  63. postProcessing = new THREE.PostProcessing( renderer );
  64. const pass = stereoPass( scene, camera );
  65. postProcessing.outputNode = pass;
  66. //
  67. window.addEventListener( 'resize', onWindowResize );
  68. document.addEventListener( 'mousemove', onDocumentMouseMove );
  69. }
  70. function onWindowResize() {
  71. windowHalfX = window.innerWidth / 2;
  72. windowHalfY = window.innerHeight / 2;
  73. camera.aspect = window.innerWidth / window.innerHeight;
  74. camera.updateProjectionMatrix();
  75. }
  76. function onDocumentMouseMove( event ) {
  77. mouseX = ( event.clientX - windowHalfX ) * 0.01;
  78. mouseY = ( event.clientY - windowHalfY ) * 0.01;
  79. }
  80. function extractPosition( matrix, position ) {
  81. position.x = matrix.elements[ 12 ];
  82. position.y = matrix.elements[ 13 ];
  83. position.z = matrix.elements[ 14 ];
  84. }
  85. //
  86. function animate() {
  87. const timer = 0.0001 * Date.now();
  88. camera.position.x += ( mouseX - camera.position.x ) * .05;
  89. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  90. camera.lookAt( scene.position );
  91. for ( let i = 0; i < mesh.count; i ++ ) {
  92. mesh.getMatrixAt( i, dummy.matrix );
  93. extractPosition( dummy.matrix, position );
  94. position.x = 5 * Math.cos( timer + i );
  95. position.y = 5 * Math.sin( timer + i * 1.1 );
  96. dummy.matrix.setPosition( position );
  97. mesh.setMatrixAt( i, dummy.matrix );
  98. mesh.instanceMatrix.needsUpdate = true;
  99. }
  100. postProcessing.render();
  101. }
  102. </script>
  103. </body>
  104. </html>
粤ICP备19079148号