webgpu_materials_texture_manualmipmap.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - 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="example.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" class="logo-link"></a>
  19. <div class="title-wrapper">
  20. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Texture Manual Mipmapping</span>
  21. </div>
  22. <small>
  23. Painting by <a href="http://en.wikipedia.org/wiki/Basket_of_Fruit_%28Caravaggio%29">Caravaggio</a>.
  24. </small>
  25. </div>
  26. <div id="lbl_left" class="lbl">
  27. Floor <span class="g">(128x128)</span><br/>
  28. mag: <span class="c">Linear</span><br/>
  29. min: <span class="c">LinearMipmapLinear</span><br/>
  30. <br/>
  31. Painting <span class="g">(748x600)</span><br/>
  32. mag: <span class="c">Linear</span><br/>
  33. min: <span class="c">Linear</span>
  34. </div>
  35. <div id="lbl_right" class="lbl">
  36. Floor <br/>
  37. mag: <span class="c">Nearest</span><br/>
  38. min: <span class="c">NearestMipmapNearestFilter</span><br/>
  39. <br/>
  40. Painting <br/>
  41. mag: <span class="c">Nearest</span><br/>
  42. min: <span class="c">Nearest</span>
  43. </div>
  44. <script type="importmap">
  45. {
  46. "imports": {
  47. "three": "../build/three.webgpu.js",
  48. "three/webgpu": "../build/three.webgpu.js",
  49. "three/tsl": "../build/three.tsl.js",
  50. "three/addons/": "./jsm/"
  51. }
  52. }
  53. </script>
  54. <script type="module">
  55. import * as THREE from 'three/webgpu';
  56. const SCREEN_WIDTH = window.innerWidth;
  57. const SCREEN_HEIGHT = window.innerHeight;
  58. let container;
  59. let camera, scene1, scene2, renderer;
  60. let mouseX = 0, mouseY = 0;
  61. const windowHalfX = window.innerWidth / 2;
  62. const windowHalfY = window.innerHeight / 2;
  63. init();
  64. async function init() {
  65. container = document.createElement( 'div' );
  66. document.body.appendChild( container );
  67. camera = new THREE.PerspectiveCamera( 35, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 5000 );
  68. camera.position.z = 1500;
  69. scene1 = new THREE.Scene();
  70. scene1.background = new THREE.Color( 0x000000 );
  71. scene1.fog = new THREE.Fog( 0x000000, 1500, 4000 );
  72. scene2 = new THREE.Scene();
  73. scene2.background = new THREE.Color( 0x000000 );
  74. scene2.fog = new THREE.Fog( 0x000000, 1500, 4000 );
  75. // GROUND
  76. const canvas = mipmap( 128, '#f00' );
  77. const textureCanvas1 = new THREE.CanvasTexture( canvas );
  78. textureCanvas1.mipmaps[ 0 ] = canvas;
  79. textureCanvas1.mipmaps[ 1 ] = mipmap( 64, '#0f0' );
  80. textureCanvas1.mipmaps[ 2 ] = mipmap( 32, '#00f' );
  81. textureCanvas1.mipmaps[ 3 ] = mipmap( 16, '#400' );
  82. textureCanvas1.mipmaps[ 4 ] = mipmap( 8, '#040' );
  83. textureCanvas1.mipmaps[ 5 ] = mipmap( 4, '#004' );
  84. textureCanvas1.mipmaps[ 6 ] = mipmap( 2, '#044' );
  85. textureCanvas1.mipmaps[ 7 ] = mipmap( 1, '#404' );
  86. textureCanvas1.colorSpace = THREE.SRGBColorSpace;
  87. textureCanvas1.repeat.set( 1000, 1000 );
  88. textureCanvas1.wrapS = THREE.RepeatWrapping;
  89. textureCanvas1.wrapT = THREE.RepeatWrapping;
  90. const textureCanvas2 = textureCanvas1.clone();
  91. textureCanvas2.magFilter = THREE.NearestFilter;
  92. textureCanvas2.minFilter = THREE.NearestMipmapNearestFilter;
  93. const materialCanvas1 = new THREE.MeshBasicMaterial( { map: textureCanvas1 } );
  94. const materialCanvas2 = new THREE.MeshBasicMaterial( { color: 0xffccaa, map: textureCanvas2 } );
  95. const geometry = new THREE.PlaneGeometry( 100, 100 );
  96. const meshCanvas1 = new THREE.Mesh( geometry, materialCanvas1 );
  97. meshCanvas1.rotation.x = - Math.PI / 2;
  98. meshCanvas1.scale.set( 1000, 1000, 1000 );
  99. const meshCanvas2 = new THREE.Mesh( geometry, materialCanvas2 );
  100. meshCanvas2.rotation.x = - Math.PI / 2;
  101. meshCanvas2.scale.set( 1000, 1000, 1000 );
  102. scene1.add( meshCanvas1 );
  103. scene2.add( meshCanvas2 );
  104. // PAINTING
  105. const texturePainting1 = await new THREE.TextureLoader().loadAsync( 'textures/758px-Canestra_di_frutta_(Caravaggio).jpg' );
  106. const texturePainting2 = texturePainting1.clone();
  107. texturePainting1.colorSpace = THREE.SRGBColorSpace;
  108. texturePainting2.colorSpace = THREE.SRGBColorSpace;
  109. texturePainting1.minFilter = texturePainting1.magFilter = THREE.LinearFilter;
  110. texturePainting2.minFilter = texturePainting2.magFilter = THREE.NearestFilter;
  111. const materialPainting1 = new THREE.MeshBasicMaterial( { color: 0xffffff, map: texturePainting1 } );
  112. const materialPainting2 = new THREE.MeshBasicMaterial( { color: 0xffccaa, map: texturePainting2 } );
  113. const geometryPainting = new THREE.PlaneGeometry( 100, 100 );
  114. const mesh1 = new THREE.Mesh( geometryPainting, materialPainting1 );
  115. const mesh2 = new THREE.Mesh( geometryPainting, materialPainting2 );
  116. addPainting( scene1, mesh1 );
  117. addPainting( scene2, mesh2 );
  118. function addPainting( zscene, zmesh ) {
  119. const image = texturePainting1.image;
  120. zmesh.scale.x = image.width / 100;
  121. zmesh.scale.y = image.height / 100;
  122. zscene.add( zmesh );
  123. const meshFrame = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000 } ) );
  124. meshFrame.position.z = - 10.0;
  125. meshFrame.scale.x = 1.1 * image.width / 100;
  126. meshFrame.scale.y = 1.1 * image.height / 100;
  127. zscene.add( meshFrame );
  128. const meshShadow = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000, opacity: 0.75, transparent: true } ) );
  129. meshShadow.position.y = - 1.1 * image.height / 2;
  130. meshShadow.position.z = - 1.1 * image.height / 2;
  131. meshShadow.rotation.x = - Math.PI / 2;
  132. meshShadow.scale.x = 1.1 * image.width / 100;
  133. meshShadow.scale.y = 1.1 * image.height / 100;
  134. zscene.add( meshShadow );
  135. const floorHeight = - 1.117 * image.height / 2;
  136. meshCanvas1.position.y = meshCanvas2.position.y = floorHeight;
  137. }
  138. renderer = new THREE.WebGPURenderer( { antialias: true } );
  139. renderer.setPixelRatio( window.devicePixelRatio );
  140. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  141. renderer.setAnimationLoop( animate );
  142. renderer.autoClear = false;
  143. renderer.domElement.style.position = 'relative';
  144. container.appendChild( renderer.domElement );
  145. document.addEventListener( 'mousemove', onDocumentMouseMove );
  146. }
  147. function mipmap( size, color ) {
  148. const imageCanvas = document.createElement( 'canvas' );
  149. const context = imageCanvas.getContext( '2d' );
  150. imageCanvas.width = imageCanvas.height = size;
  151. context.fillStyle = '#444';
  152. context.fillRect( 0, 0, size, size );
  153. context.fillStyle = color;
  154. context.fillRect( 0, 0, size / 2, size / 2 );
  155. context.fillRect( size / 2, size / 2, size / 2, size / 2 );
  156. return imageCanvas;
  157. }
  158. function onDocumentMouseMove( event ) {
  159. mouseX = ( event.clientX - windowHalfX );
  160. mouseY = ( event.clientY - windowHalfY );
  161. }
  162. function animate() {
  163. camera.position.x += ( mouseX - camera.position.x ) * .05;
  164. camera.position.y += ( - ( mouseY - 200 ) - camera.position.y ) * .05;
  165. camera.lookAt( scene1.position );
  166. renderer.clear();
  167. renderer.setScissorTest( true );
  168. renderer.setScissor( 0, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT );
  169. renderer.render( scene1, camera );
  170. renderer.setScissor( SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT );
  171. renderer.render( scene2, camera );
  172. renderer.setScissorTest( false );
  173. }
  174. </script>
  175. </body>
  176. </html>
粤ICP备19079148号