webgl_materials_texture_manualmipmap.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - manual mipmaping</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. .lbl { color:#fff; font-size:16px; font-weight:bold; position: absolute; bottom:0px; z-index:100; text-shadow:#000 1px 1px 1px; background-color:rgba(0,0,0,0.85); padding:1em }
  10. #lbl_left { text-align:left; left:0px }
  11. #lbl_right { text-align:left; right:0px }
  12. .g { color:#aaa }
  13. .c { color:#fa0 }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="info">
  18. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - texture manual mipmapping example<br/>
  19. painting by <a href="http://en.wikipedia.org/wiki/Basket_of_Fruit_%28Caravaggio%29">Caravaggio</a>
  20. </div>
  21. <div id="lbl_left" class="lbl">
  22. Floor <span class="g">(128x128)</span><br/>
  23. mag: <span class="c">Linear</span><br/>
  24. min: <span class="c">LinearMipmapLinear</span><br/>
  25. <br/>
  26. Painting <span class="g">(748x600)</span><br/>
  27. mag: <span class="c">Linear</span><br/>
  28. min: <span class="c">Linear</span>
  29. </div>
  30. <div id="lbl_right" class="lbl">
  31. Floor <br/>
  32. mag: <span class="c">Nearest</span><br/>
  33. min: <span class="c">NearestMipmapNearestFilter</span><br/>
  34. <br/>
  35. Painting <br/>
  36. mag: <span class="c">Nearest</span><br/>
  37. min: <span class="c">Nearest</span>
  38. </div>
  39. <script type="module">
  40. import * as THREE from '../build/three.module.js';
  41. const SCREEN_WIDTH = window.innerWidth;
  42. const SCREEN_HEIGHT = window.innerHeight;
  43. let container;
  44. let camera, scene1, scene2, renderer;
  45. let mouseX = 0, mouseY = 0;
  46. const windowHalfX = window.innerWidth / 2;
  47. const windowHalfY = window.innerHeight / 2;
  48. init();
  49. animate();
  50. function init() {
  51. container = document.createElement( 'div' );
  52. document.body.appendChild( container );
  53. camera = new THREE.PerspectiveCamera( 35, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 5000 );
  54. camera.position.z = 1500;
  55. scene1 = new THREE.Scene();
  56. scene1.background = new THREE.Color( 0x000000 );
  57. scene1.fog = new THREE.Fog( 0x000000, 1500, 4000 );
  58. scene2 = new THREE.Scene();
  59. scene2.background = new THREE.Color( 0x000000 );
  60. scene2.fog = new THREE.Fog( 0x000000, 1500, 4000 );
  61. // GROUND
  62. function mipmap( size, color ) {
  63. const imageCanvas = document.createElement( "canvas" );
  64. const context = imageCanvas.getContext( "2d" );
  65. imageCanvas.width = imageCanvas.height = size;
  66. context.fillStyle = "#444";
  67. context.fillRect( 0, 0, size, size );
  68. context.fillStyle = color;
  69. context.fillRect( 0, 0, size / 2, size / 2 );
  70. context.fillRect( size / 2, size / 2, size / 2, size / 2 );
  71. return imageCanvas;
  72. }
  73. const canvas = mipmap( 128, '#f00' );
  74. const textureCanvas1 = new THREE.CanvasTexture( canvas );
  75. textureCanvas1.mipmaps[ 0 ] = canvas;
  76. textureCanvas1.mipmaps[ 1 ] = mipmap( 64, '#0f0' );
  77. textureCanvas1.mipmaps[ 2 ] = mipmap( 32, '#00f' );
  78. textureCanvas1.mipmaps[ 3 ] = mipmap( 16, '#400' );
  79. textureCanvas1.mipmaps[ 4 ] = mipmap( 8, '#040' );
  80. textureCanvas1.mipmaps[ 5 ] = mipmap( 4, '#004' );
  81. textureCanvas1.mipmaps[ 6 ] = mipmap( 2, '#044' );
  82. textureCanvas1.mipmaps[ 7 ] = mipmap( 1, '#404' );
  83. textureCanvas1.repeat.set( 1000, 1000 );
  84. textureCanvas1.wrapS = THREE.RepeatWrapping;
  85. textureCanvas1.wrapT = THREE.RepeatWrapping;
  86. const textureCanvas2 = textureCanvas1.clone();
  87. textureCanvas2.magFilter = THREE.NearestFilter;
  88. textureCanvas2.minFilter = THREE.NearestMipmapNearestFilter;
  89. const materialCanvas1 = new THREE.MeshBasicMaterial( { map: textureCanvas1 } );
  90. const materialCanvas2 = new THREE.MeshBasicMaterial( { color: 0xffccaa, map: textureCanvas2 } );
  91. const geometry = new THREE.PlaneGeometry( 100, 100 );
  92. const meshCanvas1 = new THREE.Mesh( geometry, materialCanvas1 );
  93. meshCanvas1.rotation.x = - Math.PI / 2;
  94. meshCanvas1.scale.set( 1000, 1000, 1000 );
  95. const meshCanvas2 = new THREE.Mesh( geometry, materialCanvas2 );
  96. meshCanvas2.rotation.x = - Math.PI / 2;
  97. meshCanvas2.scale.set( 1000, 1000, 1000 );
  98. // PAINTING
  99. const callbackPainting = function () {
  100. const image = texturePainting1.image;
  101. texturePainting2.image = image;
  102. texturePainting2.needsUpdate = true;
  103. scene1.add( meshCanvas1 );
  104. scene2.add( meshCanvas2 );
  105. const geometry = new THREE.PlaneGeometry( 100, 100 );
  106. const mesh1 = new THREE.Mesh( geometry, materialPainting1 );
  107. const mesh2 = new THREE.Mesh( geometry, materialPainting2 );
  108. addPainting( scene1, mesh1 );
  109. addPainting( scene2, mesh2 );
  110. function addPainting( zscene, zmesh ) {
  111. zmesh.scale.x = image.width / 100;
  112. zmesh.scale.y = image.height / 100;
  113. zscene.add( zmesh );
  114. const meshFrame = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000 } ) );
  115. meshFrame.position.z = - 10.0;
  116. meshFrame.scale.x = 1.1 * image.width / 100;
  117. meshFrame.scale.y = 1.1 * image.height / 100;
  118. zscene.add( meshFrame );
  119. const meshShadow = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000, opacity: 0.75, transparent: true } ) );
  120. meshShadow.position.y = - 1.1 * image.height / 2;
  121. meshShadow.position.z = - 1.1 * image.height / 2;
  122. meshShadow.rotation.x = - Math.PI / 2;
  123. meshShadow.scale.x = 1.1 * image.width / 100;
  124. meshShadow.scale.y = 1.1 * image.height / 100;
  125. zscene.add( meshShadow );
  126. const floorHeight = - 1.117 * image.height / 2;
  127. meshCanvas1.position.y = meshCanvas2.position.y = floorHeight;
  128. }
  129. };
  130. const texturePainting1 = new THREE.TextureLoader().load( "textures/758px-Canestra_di_frutta_(Caravaggio).jpg", callbackPainting );
  131. const texturePainting2 = new THREE.Texture();
  132. const materialPainting1 = new THREE.MeshBasicMaterial( { color: 0xffffff, map: texturePainting1 } );
  133. const materialPainting2 = new THREE.MeshBasicMaterial( { color: 0xffccaa, map: texturePainting2 } );
  134. texturePainting2.minFilter = texturePainting2.magFilter = THREE.NearestFilter;
  135. texturePainting1.minFilter = texturePainting1.magFilter = THREE.LinearFilter;
  136. texturePainting1.mapping = THREE.UVMapping;
  137. renderer = new THREE.WebGLRenderer( { antialias: true } );
  138. renderer.setPixelRatio( window.devicePixelRatio );
  139. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  140. renderer.autoClear = false;
  141. renderer.domElement.style.position = "relative";
  142. container.appendChild( renderer.domElement );
  143. document.addEventListener( 'mousemove', onDocumentMouseMove );
  144. }
  145. function onDocumentMouseMove( event ) {
  146. mouseX = ( event.clientX - windowHalfX );
  147. mouseY = ( event.clientY - windowHalfY );
  148. }
  149. function animate() {
  150. requestAnimationFrame( animate );
  151. render();
  152. }
  153. function render() {
  154. camera.position.x += ( mouseX - camera.position.x ) * .05;
  155. camera.position.y += ( - ( mouseY - 200 ) - camera.position.y ) * .05;
  156. camera.lookAt( scene1.position );
  157. renderer.clear();
  158. renderer.setScissorTest( true );
  159. renderer.setScissor( 0, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT );
  160. renderer.render( scene1, camera );
  161. renderer.setScissor( SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT );
  162. renderer.render( scene2, camera );
  163. renderer.setScissorTest( false );
  164. }
  165. </script>
  166. </body>
  167. </html>