webgl_materials_texture_anisotropy.html 5.5 KB

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