webgl_materials_texture_manualmipmap.html 9.5 KB

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