2
0

webgl_materials_texture_manualmipmap.html 7.7 KB

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