webgl_materials_texture_manualmipmap.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.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.CanvasTexture( canvas );
  91. textureCanvas1.mipmaps[ 0 ] = canvas;
  92. textureCanvas1.mipmaps[ 1 ] = mipmap( 64, '#0f0' );
  93. textureCanvas1.mipmaps[ 2 ] = mipmap( 32, '#00f' );
  94. textureCanvas1.mipmaps[ 3 ] = mipmap( 16, '#400' );
  95. textureCanvas1.mipmaps[ 4 ] = mipmap( 8, '#040' );
  96. textureCanvas1.mipmaps[ 5 ] = mipmap( 4, '#004' );
  97. textureCanvas1.mipmaps[ 6 ] = mipmap( 2, '#044' );
  98. textureCanvas1.mipmaps[ 7 ] = mipmap( 1, '#404' );
  99. textureCanvas1.repeat.set( 1000, 1000 );
  100. textureCanvas1.wrapS = THREE.RepeatWrapping;
  101. textureCanvas1.wrapT = THREE.RepeatWrapping;
  102. var textureCanvas2 = textureCanvas1.clone();
  103. textureCanvas2.magFilter = THREE.NearestFilter;
  104. textureCanvas2.minFilter = THREE.NearestMipMapNearestFilter;
  105. materialCanvas1 = new THREE.MeshBasicMaterial( { map: textureCanvas1 } );
  106. materialCanvas2 = new THREE.MeshBasicMaterial( { color: 0xffccaa, map: textureCanvas2 } );
  107. var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
  108. var meshCanvas1 = new THREE.Mesh( geometry, materialCanvas1 );
  109. meshCanvas1.rotation.x = -Math.PI / 2;
  110. meshCanvas1.scale.set(1000, 1000, 1000);
  111. var meshCanvas2 = new THREE.Mesh( geometry, materialCanvas2 );
  112. meshCanvas2.rotation.x = -Math.PI / 2;
  113. meshCanvas2.scale.set( 1000, 1000, 1000 );
  114. // PAINTING
  115. var callbackPainting = function () {
  116. var image = texturePainting1.image;
  117. texturePainting2.image = image;
  118. texturePainting2.needsUpdate = true;
  119. scene1.add( meshCanvas1 );
  120. scene2.add( meshCanvas2 );
  121. var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
  122. var mesh1 = new THREE.Mesh( geometry, materialPainting1 );
  123. var mesh2 = new THREE.Mesh( geometry, materialPainting2 );
  124. addPainting( scene1, mesh1 );
  125. addPainting( scene2, mesh2 );
  126. function addPainting( zscene, zmesh ) {
  127. zmesh.scale.x = image.width / 100;
  128. zmesh.scale.y = image.height / 100;
  129. zscene.add( zmesh );
  130. var meshFrame = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000, polygonOffset: true, polygonOffsetFactor: 1, polygonOffsetUnits: 5 } ) );
  131. meshFrame.scale.x = 1.1 * image.width / 100;
  132. meshFrame.scale.y = 1.1 * image.height / 100;
  133. zscene.add( meshFrame );
  134. var meshShadow = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000, opacity: 0.75, transparent: true } ) );
  135. meshShadow.position.y = -1.1 * image.height / 2;
  136. meshShadow.position.z = -1.1 * image.height / 2;
  137. meshShadow.rotation.x = -Math.PI / 2;
  138. meshShadow.scale.x = 1.1 * image.width / 100;
  139. meshShadow.scale.y = 1.1 * image.height / 100;
  140. zscene.add( meshShadow );
  141. var floorHeight = -1.117 * image.height / 2;
  142. meshCanvas1.position.y = meshCanvas2.position.y = floorHeight;
  143. }
  144. };
  145. var texturePainting1 = new THREE.TextureLoader().load( "textures/758px-Canestra_di_frutta_(Caravaggio).jpg", callbackPainting );
  146. var texturePainting2 = new THREE.Texture();
  147. var materialPainting1 = new THREE.MeshBasicMaterial( { color: 0xffffff, map: texturePainting1 } );
  148. var materialPainting2 = new THREE.MeshBasicMaterial( { color: 0xffccaa, map: texturePainting2 } );
  149. texturePainting2.minFilter = texturePainting2.magFilter = THREE.NearestFilter;
  150. texturePainting1.minFilter = texturePainting1.magFilter = THREE.LinearFilter;
  151. texturePainting1.mapping = THREE.UVMapping;
  152. renderer = new THREE.WebGLRenderer( { antialias: true } );
  153. renderer.setClearColor( scene1.fog.color );
  154. renderer.setPixelRatio( window.devicePixelRatio );
  155. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  156. renderer.autoClear = false;
  157. renderer.domElement.style.position = "relative";
  158. container.appendChild( renderer.domElement );
  159. stats = new Stats();
  160. stats.domElement.style.position = 'absolute';
  161. stats.domElement.style.top = '0px';
  162. stats.domElement.style.zIndex = 100;
  163. //container.appendChild( stats.domElement );
  164. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  165. }
  166. function onDocumentMouseMove( event ) {
  167. mouseX = ( event.clientX - windowHalfX );
  168. mouseY = ( event.clientY - windowHalfY );
  169. }
  170. function animate() {
  171. requestAnimationFrame( animate );
  172. stats.begin();
  173. render();
  174. stats.end();
  175. }
  176. function render() {
  177. camera.position.x += ( mouseX - camera.position.x ) * .05;
  178. camera.position.y += ( -( mouseY - 200 ) - camera.position.y ) * .05;
  179. camera.lookAt( scene1.position );
  180. renderer.clear();
  181. renderer.setScissorTest( true );
  182. renderer.setScissor( 0, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT );
  183. renderer.render( scene1, camera );
  184. renderer.setScissor( SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT );
  185. renderer.render( scene2, camera );
  186. renderer.setScissorTest( false );
  187. }
  188. </script>
  189. </body>
  190. </html>