webgl_materials_texture_manualmipmap.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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="importmap">
  40. {
  41. "imports": {
  42. "three": "../build/three.module.js",
  43. "three/addons/": "./jsm/"
  44. }
  45. }
  46. </script>
  47. <script type="module">
  48. import * as THREE from 'three';
  49. const SCREEN_WIDTH = window.innerWidth;
  50. const SCREEN_HEIGHT = window.innerHeight;
  51. let container;
  52. let camera, scene1, scene2, renderer;
  53. let mouseX = 0, mouseY = 0;
  54. const windowHalfX = window.innerWidth / 2;
  55. const windowHalfY = window.innerHeight / 2;
  56. init();
  57. function init() {
  58. container = document.createElement( 'div' );
  59. document.body.appendChild( container );
  60. camera = new THREE.PerspectiveCamera( 35, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 5000 );
  61. camera.position.z = 1500;
  62. scene1 = new THREE.Scene();
  63. scene1.background = new THREE.Color( 0x000000 );
  64. scene1.fog = new THREE.Fog( 0x000000, 1500, 4000 );
  65. scene2 = new THREE.Scene();
  66. scene2.background = new THREE.Color( 0x000000 );
  67. scene2.fog = new THREE.Fog( 0x000000, 1500, 4000 );
  68. // GROUND
  69. function mipmap( size, color ) {
  70. const imageCanvas = document.createElement( 'canvas' );
  71. const context = imageCanvas.getContext( '2d' );
  72. imageCanvas.width = imageCanvas.height = size;
  73. context.fillStyle = '#444';
  74. context.fillRect( 0, 0, size, size );
  75. context.fillStyle = color;
  76. context.fillRect( 0, 0, size / 2, size / 2 );
  77. context.fillRect( size / 2, size / 2, size / 2, size / 2 );
  78. return imageCanvas;
  79. }
  80. const canvas = mipmap( 128, '#f00' );
  81. const textureCanvas1 = new THREE.CanvasTexture( canvas );
  82. textureCanvas1.mipmaps[ 0 ] = canvas;
  83. textureCanvas1.mipmaps[ 1 ] = mipmap( 64, '#0f0' );
  84. textureCanvas1.mipmaps[ 2 ] = mipmap( 32, '#00f' );
  85. textureCanvas1.mipmaps[ 3 ] = mipmap( 16, '#400' );
  86. textureCanvas1.mipmaps[ 4 ] = mipmap( 8, '#040' );
  87. textureCanvas1.mipmaps[ 5 ] = mipmap( 4, '#004' );
  88. textureCanvas1.mipmaps[ 6 ] = mipmap( 2, '#044' );
  89. textureCanvas1.mipmaps[ 7 ] = mipmap( 1, '#404' );
  90. textureCanvas1.colorSpace = THREE.SRGBColorSpace;
  91. textureCanvas1.repeat.set( 1000, 1000 );
  92. textureCanvas1.wrapS = THREE.RepeatWrapping;
  93. textureCanvas1.wrapT = THREE.RepeatWrapping;
  94. const textureCanvas2 = textureCanvas1.clone();
  95. textureCanvas2.magFilter = THREE.NearestFilter;
  96. textureCanvas2.minFilter = THREE.NearestMipmapNearestFilter;
  97. const materialCanvas1 = new THREE.MeshBasicMaterial( { map: textureCanvas1 } );
  98. const materialCanvas2 = new THREE.MeshBasicMaterial( { color: 0xffccaa, map: textureCanvas2 } );
  99. const geometry = new THREE.PlaneGeometry( 100, 100 );
  100. const meshCanvas1 = new THREE.Mesh( geometry, materialCanvas1 );
  101. meshCanvas1.rotation.x = - Math.PI / 2;
  102. meshCanvas1.scale.set( 1000, 1000, 1000 );
  103. const meshCanvas2 = new THREE.Mesh( geometry, materialCanvas2 );
  104. meshCanvas2.rotation.x = - Math.PI / 2;
  105. meshCanvas2.scale.set( 1000, 1000, 1000 );
  106. // PAINTING
  107. const callbackPainting = function () {
  108. const image = texturePainting1.image;
  109. texturePainting2.image = image;
  110. texturePainting2.needsUpdate = true;
  111. scene1.add( meshCanvas1 );
  112. scene2.add( meshCanvas2 );
  113. const geometry = new THREE.PlaneGeometry( 100, 100 );
  114. const mesh1 = new THREE.Mesh( geometry, materialPainting1 );
  115. const mesh2 = new THREE.Mesh( geometry, materialPainting2 );
  116. addPainting( scene1, mesh1 );
  117. addPainting( scene2, mesh2 );
  118. function addPainting( zscene, zmesh ) {
  119. zmesh.scale.x = image.width / 100;
  120. zmesh.scale.y = image.height / 100;
  121. zscene.add( zmesh );
  122. const meshFrame = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000 } ) );
  123. meshFrame.position.z = - 10.0;
  124. meshFrame.scale.x = 1.1 * image.width / 100;
  125. meshFrame.scale.y = 1.1 * image.height / 100;
  126. zscene.add( meshFrame );
  127. const meshShadow = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000, opacity: 0.75, transparent: true } ) );
  128. meshShadow.position.y = - 1.1 * image.height / 2;
  129. meshShadow.position.z = - 1.1 * image.height / 2;
  130. meshShadow.rotation.x = - Math.PI / 2;
  131. meshShadow.scale.x = 1.1 * image.width / 100;
  132. meshShadow.scale.y = 1.1 * image.height / 100;
  133. zscene.add( meshShadow );
  134. const floorHeight = - 1.117 * image.height / 2;
  135. meshCanvas1.position.y = meshCanvas2.position.y = floorHeight;
  136. }
  137. };
  138. const texturePainting1 = new THREE.TextureLoader().load( 'textures/758px-Canestra_di_frutta_(Caravaggio).jpg', callbackPainting );
  139. const texturePainting2 = new THREE.Texture();
  140. const materialPainting1 = new THREE.MeshBasicMaterial( { color: 0xffffff, map: texturePainting1 } );
  141. const materialPainting2 = new THREE.MeshBasicMaterial( { color: 0xffccaa, map: texturePainting2 } );
  142. texturePainting1.colorSpace = THREE.SRGBColorSpace;
  143. texturePainting2.colorSpace = THREE.SRGBColorSpace;
  144. texturePainting2.minFilter = texturePainting2.magFilter = THREE.NearestFilter;
  145. texturePainting1.minFilter = texturePainting1.magFilter = THREE.LinearFilter;
  146. texturePainting1.mapping = THREE.UVMapping;
  147. renderer = new THREE.WebGLRenderer( { antialias: true } );
  148. renderer.setPixelRatio( window.devicePixelRatio );
  149. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  150. renderer.setAnimationLoop( animate );
  151. renderer.autoClear = false;
  152. renderer.domElement.style.position = 'relative';
  153. container.appendChild( renderer.domElement );
  154. document.addEventListener( 'mousemove', onDocumentMouseMove );
  155. }
  156. function onDocumentMouseMove( event ) {
  157. mouseX = ( event.clientX - windowHalfX );
  158. mouseY = ( event.clientY - windowHalfY );
  159. }
  160. function animate() {
  161. camera.position.x += ( mouseX - camera.position.x ) * .05;
  162. camera.position.y += ( - ( mouseY - 200 ) - camera.position.y ) * .05;
  163. camera.lookAt( scene1.position );
  164. renderer.clear();
  165. renderer.setScissorTest( true );
  166. renderer.setScissor( 0, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT );
  167. renderer.render( scene1, camera );
  168. renderer.setScissor( SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT );
  169. renderer.render( scene2, camera );
  170. renderer.setScissorTest( false );
  171. }
  172. </script>
  173. </body>
  174. </html>