webgl_materials_texture_manualmipmap.html 7.5 KB

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