webgl_materials_texture_manualmipmap.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. <style>
  8. body {
  9. color: #aaa;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #000;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. z-index:100;
  22. }
  23. .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 }
  24. #lbl_left { text-align:left; left:0px }
  25. #lbl_right { text-align:left; right:0px }
  26. .g { color:#aaa }
  27. .c { color:#fa0 }
  28. a { color:red }
  29. </style>
  30. </head>
  31. <body>
  32. <div id="info">
  33. <a href="http://threejs.org" target="_blank">three.js</a> - texture manual mipmapping example
  34. - painting by <a href="http://en.wikipedia.org/wiki/Basket_of_Fruit_%28Caravaggio%29">Caravaggio</a>
  35. </div>
  36. <div id="lbl_left" class="lbl">
  37. Floor <span class="g">(128x128)</span><br/>
  38. mag: <span class="c">Linear</span><br/>
  39. min: <span class="c">LinearMipMapLinear</span><br/>
  40. <br/>
  41. Painting <span class="g">(748x600)</span><br/>
  42. mag: <span class="c">Linear</span><br/>
  43. min: <span class="c">Linear</span>
  44. </div>
  45. <div id="lbl_right" class="lbl">
  46. Floor <br/>
  47. mag: <span class="c">Nearest</span><br/>
  48. min: <span class="c">NearestMipMapNearestFilter</span><br/>
  49. <br/>
  50. Painting <br/>
  51. mag: <span class="c">Nearest</span><br/>
  52. min: <span class="c">Nearest</span>
  53. </div>
  54. <script src="../build/three.min.js"></script>
  55. <script src="js/Detector.js"></script>
  56. <script src="js/libs/stats.min.js"></script>
  57. <script>
  58. if ( !Detector.webgl ) Detector.addGetWebGLMessage();
  59. var SCREEN_WIDTH = window.innerWidth;
  60. var SCREEN_HEIGHT = window.innerHeight;
  61. var container, stats;
  62. var camera, scene1, scene2, renderer;
  63. var mouseX = 0, mouseY = 0;
  64. var windowHalfX = window.innerWidth / 2;
  65. var windowHalfY = window.innerHeight / 2;
  66. init();
  67. animate();
  68. function init() {
  69. container = document.createElement( 'div' );
  70. document.body.appendChild( container );
  71. camera = new THREE.PerspectiveCamera( 35, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 5000 );
  72. camera.position.z = 1500;
  73. scene1 = new THREE.Scene();
  74. scene1.fog = new THREE.Fog( 0x000000, 1500, 4000 );
  75. scene2 = new THREE.Scene();
  76. scene2.fog = scene1.fog;
  77. // GROUND
  78. function mipmap( size, color ) {
  79. var imageCanvas = document.createElement( "canvas" ),
  80. context = imageCanvas.getContext( "2d" );
  81. imageCanvas.width = imageCanvas.height = size;
  82. context.fillStyle = "#444";
  83. context.fillRect( 0, 0, size, size );
  84. context.fillStyle = color;
  85. context.fillRect( 0, 0, size / 2, size / 2 );
  86. context.fillRect( size / 2, size / 2, size / 2, size / 2 );
  87. return imageCanvas;
  88. }
  89. var canvas = mipmap( 128, '#f00' );
  90. var textureCanvas1 = new THREE.Texture( canvas, THREE.UVMapping, THREE.RepeatWrapping, THREE.RepeatWrapping );
  91. textureCanvas1.repeat.set( 1000, 1000 );
  92. textureCanvas1.mipmaps = [];
  93. textureCanvas1.mipmaps[ 0 ] = canvas;
  94. textureCanvas1.mipmaps[ 1 ] = mipmap( 64, '#0f0' );
  95. textureCanvas1.mipmaps[ 2 ] = mipmap( 32, '#00f' );
  96. textureCanvas1.mipmaps[ 3 ] = mipmap( 16, '#400' );
  97. textureCanvas1.mipmaps[ 4 ] = mipmap( 8, '#040' );
  98. textureCanvas1.mipmaps[ 5 ] = mipmap( 4, '#004' );
  99. textureCanvas1.mipmaps[ 6 ] = mipmap( 2, '#044' );
  100. textureCanvas1.mipmaps[ 7 ] = mipmap( 1, '#404' );
  101. textureCanvas1.needsUpdate = true;
  102. materialCanvas1 = new THREE.MeshBasicMaterial( { map: textureCanvas1 } );
  103. var textureCanvas2 = textureCanvas1.clone();
  104. textureCanvas2.magFilter = THREE.NearestFilter;
  105. textureCanvas2.minFilter = THREE.NearestMipMapNearestFilter;
  106. textureCanvas2.needsUpdate = true;
  107. materialCanvas2 = new THREE.MeshBasicMaterial( { color: 0xffccaa, map: textureCanvas2 } );
  108. var geometry = new THREE.PlaneGeometry( 100, 100 );
  109. var meshCanvas1 = new THREE.Mesh( geometry, materialCanvas1 );
  110. meshCanvas1.rotation.x = -Math.PI / 2;
  111. meshCanvas1.scale.set(1000, 1000, 1000);
  112. var meshCanvas2 = new THREE.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 THREE.PlaneGeometry( 100, 100 );
  123. var mesh1 = new THREE.Mesh( geometry, materialPainting1 );
  124. var mesh2 = new THREE.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 THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000, polygonOffset: true, polygonOffsetFactor: 1, polygonOffsetUnits: 5 } ) );
  132. meshFrame.scale.x = 1.1 * image.width / 100;
  133. meshFrame.scale.y = 1.1 * image.height / 100;
  134. zscene.add( meshFrame );
  135. var meshShadow = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000, opacity: 0.75, transparent: true } ) );
  136. meshShadow.position.y = -1.1 * image.height / 2;
  137. meshShadow.position.z = -1.1 * image.height / 2;
  138. meshShadow.rotation.x = -Math.PI / 2;
  139. meshShadow.scale.x = 1.1 * image.width / 100;
  140. meshShadow.scale.y = 1.1 * image.height / 100;
  141. zscene.add( meshShadow );
  142. var floorHeight = -1.117 * image.height / 2;
  143. meshCanvas1.position.y = meshCanvas2.position.y = floorHeight;
  144. }
  145. };
  146. var texturePainting1 = THREE.ImageUtils.loadTexture( "textures/758px-Canestra_di_frutta_(Caravaggio).jpg", THREE.UVMapping, callbackPainting ),
  147. texturePainting2 = new THREE.Texture(),
  148. materialPainting1 = new THREE.MeshBasicMaterial( { color: 0xffffff, map: texturePainting1 } ),
  149. materialPainting2 = new THREE.MeshBasicMaterial( { color: 0xffccaa, map: texturePainting2 } );
  150. texturePainting2.minFilter = texturePainting2.magFilter = THREE.NearestFilter;
  151. texturePainting1.minFilter = texturePainting1.magFilter = THREE.LinearFilter;
  152. renderer = new THREE.WebGLRenderer( { antialias: true } );
  153. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  154. renderer.setClearColor( scene1.fog.color, 1 );
  155. renderer.autoClear = false;
  156. renderer.domElement.style.position = "relative";
  157. container.appendChild( renderer.domElement );
  158. stats = new Stats();
  159. stats.domElement.style.position = 'absolute';
  160. stats.domElement.style.top = '0px';
  161. stats.domElement.style.zIndex = 100;
  162. //container.appendChild( stats.domElement );
  163. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  164. }
  165. function onDocumentMouseMove( event ) {
  166. mouseX = ( event.clientX - windowHalfX );
  167. mouseY = ( event.clientY - windowHalfY );
  168. }
  169. function animate() {
  170. requestAnimationFrame( animate );
  171. render();
  172. stats.update();
  173. }
  174. function render() {
  175. camera.position.x += ( mouseX - camera.position.x ) * .05;
  176. camera.position.y += ( -( mouseY - 200 ) - camera.position.y ) * .05;
  177. camera.lookAt( scene1.position );
  178. renderer.enableScissorTest( false );
  179. renderer.clear();
  180. renderer.enableScissorTest( true );
  181. //renderer.setViewport( 0, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT );
  182. renderer.setScissor( 0, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT );
  183. renderer.render( scene1, camera );
  184. //renderer.setViewport( SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT );
  185. renderer.setScissor( SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT );
  186. renderer.render( scene2, camera );
  187. }
  188. </script>
  189. </body>
  190. </html>