2
0

webgl_materials_texture_manualmipmap.html 7.7 KB

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