webgpu_textures_anisotropy.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - anisotropic texture filtering</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. <style>
  9. body {
  10. color: #444;
  11. background-color: #f1f1f1;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. .lbl {
  17. color: #fff;
  18. font-size: 16px;
  19. font-weight: bold;
  20. position: absolute;
  21. bottom: 0px;
  22. z-index: 100;
  23. text-shadow: #000 1px 1px 1px;
  24. background-color: rgba(0,0,0,0.85);
  25. padding: 1em;
  26. }
  27. #lbl_left {
  28. text-align:left;
  29. left:0px;
  30. }
  31. #lbl_right {
  32. text-align:left;
  33. right:0px
  34. }
  35. .g { color:#aaa }
  36. .c { color:#fa0 }
  37. </style>
  38. </head>
  39. <body>
  40. <div id="info" class="invert">
  41. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  42. <div class="title-wrapper">
  43. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Anisotropic Texture Filtering</span>
  44. </div>
  45. <small>
  46. Anisotropic texture filtering example.
  47. </small>
  48. </div>
  49. <div id="lbl_left" class="lbl">
  50. anisotropy: <span class="c" id="val_left"></span><br/>
  51. </div>
  52. <div id="lbl_right" class="lbl">
  53. anisotropy: <span class="c" id="val_right"></span><br/>
  54. </div>
  55. <script type="importmap">
  56. {
  57. "imports": {
  58. "three": "../build/three.webgpu.js",
  59. "three/webgpu": "../build/three.webgpu.js",
  60. "three/tsl": "../build/three.tsl.js",
  61. "three/addons/": "./jsm/"
  62. }
  63. }
  64. </script>
  65. <script type="module">
  66. import * as THREE from 'three/webgpu';
  67. let container;
  68. let camera, scene1, scene2, renderer;
  69. let mouseX = 0, mouseY = 0;
  70. init();
  71. function init() {
  72. const SCREEN_WIDTH = window.innerWidth;
  73. const SCREEN_HEIGHT = window.innerHeight;
  74. container = document.createElement( 'div' );
  75. document.body.appendChild( container );
  76. renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL: false } );
  77. // RENDERER
  78. renderer.setPixelRatio( window.devicePixelRatio );
  79. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  80. renderer.setAnimationLoop( render );
  81. renderer.autoClear = false;
  82. renderer.domElement.style.position = 'relative';
  83. container.appendChild( renderer.domElement );
  84. //
  85. camera = new THREE.PerspectiveCamera( 35, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 25000 );
  86. camera.position.z = 1500;
  87. scene1 = new THREE.Scene();
  88. scene1.fog = new THREE.Fog( 0xf2f7ff, 1, 25000 );
  89. scene2 = new THREE.Scene();
  90. scene2.fog = new THREE.Fog( 0xf2f7ff, 1, 25000 );
  91. scene1.add( new THREE.AmbientLight( 0xeef0ff, 3 ) );
  92. scene2.add( new THREE.AmbientLight( 0xeef0ff, 3 ) );
  93. const light1 = new THREE.DirectionalLight( 0xffffff, 6 );
  94. light1.position.set( 1, 1, 1 );
  95. scene1.add( light1 );
  96. const light2 = new THREE.DirectionalLight( 0xffffff, 6 );
  97. light2.position.set( 1, 1, 1 );
  98. scene2.add( light2 );
  99. // GROUND
  100. const textureLoader = new THREE.TextureLoader();
  101. const maxAnisotropy = renderer.getMaxAnisotropy();
  102. const texture1 = textureLoader.load( 'textures/crate.gif' );
  103. const material1 = new THREE.MeshPhongMaterial( { color: 0xffffff, map: texture1 } );
  104. texture1.colorSpace = THREE.SRGBColorSpace;
  105. texture1.anisotropy = renderer.getMaxAnisotropy();
  106. texture1.wrapS = texture1.wrapT = THREE.RepeatWrapping;
  107. texture1.repeat.set( 512, 512 );
  108. const texture2 = textureLoader.load( 'textures/crate.gif' );
  109. const material2 = new THREE.MeshPhongMaterial( { color: 0xffffff, map: texture2 } );
  110. texture2.colorSpace = THREE.SRGBColorSpace;
  111. texture2.anisotropy = 1;
  112. texture2.wrapS = texture2.wrapT = THREE.RepeatWrapping;
  113. texture2.repeat.set( 512, 512 );
  114. if ( maxAnisotropy > 0 ) {
  115. document.getElementById( 'val_left' ).innerHTML = texture1.anisotropy;
  116. document.getElementById( 'val_right' ).innerHTML = texture2.anisotropy;
  117. } else {
  118. document.getElementById( 'val_left' ).innerHTML = 'not supported';
  119. document.getElementById( 'val_right' ).innerHTML = 'not supported';
  120. }
  121. //
  122. const geometry = new THREE.PlaneGeometry( 100, 100 );
  123. const mesh1 = new THREE.Mesh( geometry, material1 );
  124. mesh1.rotation.x = - Math.PI / 2;
  125. mesh1.scale.set( 1000, 1000, 1000 );
  126. const mesh2 = new THREE.Mesh( geometry, material2 );
  127. mesh2.rotation.x = - Math.PI / 2;
  128. mesh2.scale.set( 1000, 1000, 1000 );
  129. scene1.add( mesh1 );
  130. scene2.add( mesh2 );
  131. //
  132. document.addEventListener( 'mousemove', onDocumentMouseMove );
  133. window.addEventListener( 'resize', onWindowResize );
  134. }
  135. function onWindowResize() {
  136. camera.aspect = window.innerWidth / window.innerHeight;
  137. camera.updateProjectionMatrix();
  138. renderer.setSize( window.innerWidth, window.innerHeight );
  139. }
  140. function onDocumentMouseMove( event ) {
  141. const windowHalfX = window.innerWidth / 2;
  142. const windowHalfY = window.innerHeight / 2;
  143. mouseX = ( event.clientX - windowHalfX );
  144. mouseY = ( event.clientY - windowHalfY );
  145. }
  146. function render() {
  147. const SCREEN_WIDTH = window.innerWidth;
  148. const SCREEN_HEIGHT = window.innerHeight;
  149. camera.position.x += ( mouseX - camera.position.x ) * .05;
  150. camera.position.y = THREE.MathUtils.clamp( camera.position.y + ( - ( mouseY - 200 ) - camera.position.y ) * .05, 50, 1000 );
  151. camera.lookAt( scene1.position );
  152. renderer.clear();
  153. renderer.setScissorTest( true );
  154. renderer.setScissor( 0, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT );
  155. renderer.render( scene1, camera );
  156. renderer.setScissorTest( true );
  157. renderer.setScissor( SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT );
  158. renderer.render( scene2, camera );
  159. renderer.setScissorTest( false );
  160. }
  161. </script>
  162. </body>
  163. </html>
粤ICP备19079148号