webgl_loader_texture_dds.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - compressed textures</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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - compressed textures<br/>
  12. leaf texture by <a href="http://opengameart.org/node/10505">lauris71</a>, explosion texture by <a href="http://opengameart.org/node/7728">bart</a>
  13. </div>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { DDSLoader } from 'three/addons/loaders/DDSLoader.js';
  25. let camera, scene, renderer;
  26. const meshes = [];
  27. init();
  28. function init() {
  29. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 100 );
  30. camera.position.y = - 2;
  31. camera.position.z = 16;
  32. scene = new THREE.Scene();
  33. const geometry = new THREE.BoxGeometry( 2, 2, 2 );
  34. /*
  35. This is how compressed textures are supposed to be used:
  36. DXT1 - RGB - opaque textures
  37. DXT3 - RGBA - transparent textures with sharp alpha transitions
  38. DXT5 - RGBA - transparent textures with full alpha range
  39. */
  40. const loader = new DDSLoader();
  41. const map1 = loader.load( 'textures/compressed/disturb_dxt1_nomip.dds' );
  42. map1.minFilter = map1.magFilter = THREE.LinearFilter;
  43. map1.anisotropy = 4;
  44. map1.colorSpace = THREE.SRGBColorSpace;
  45. const map2 = loader.load( 'textures/compressed/disturb_dxt1_mip.dds' );
  46. map2.anisotropy = 4;
  47. map2.colorSpace = THREE.SRGBColorSpace;
  48. const map3 = loader.load( 'textures/compressed/hepatica_dxt3_mip.dds' );
  49. map3.anisotropy = 4;
  50. map3.colorSpace = THREE.SRGBColorSpace;
  51. const map4 = loader.load( 'textures/compressed/explosion_dxt5_mip.dds' );
  52. map4.anisotropy = 4;
  53. map4.colorSpace = THREE.SRGBColorSpace;
  54. const map5 = loader.load( 'textures/compressed/disturb_argb_nomip.dds' );
  55. map5.minFilter = map5.magFilter = THREE.LinearFilter;
  56. map5.anisotropy = 4;
  57. map5.colorSpace = THREE.SRGBColorSpace;
  58. const map6 = loader.load( 'textures/compressed/disturb_argb_mip.dds' );
  59. map6.anisotropy = 4;
  60. map6.colorSpace = THREE.SRGBColorSpace;
  61. const map7 = loader.load( 'textures/compressed/disturb_dx10_bc6h_signed_nomip.dds' );
  62. map7.anisotropy = 4;
  63. const map8 = loader.load( 'textures/compressed/disturb_dx10_bc6h_signed_mip.dds' );
  64. map8.anisotropy = 4;
  65. const map9 = loader.load( 'textures/compressed/disturb_dx10_bc6h_unsigned_nomip.dds' );
  66. map9.anisotropy = 4;
  67. const map10 = loader.load( 'textures/compressed/disturb_dx10_bc6h_unsigned_mip.dds' );
  68. map10.anisotropy = 4;
  69. const map11 = loader.load( 'textures/wave_normals_24bit_uncompressed.dds' );
  70. map11.anisotropy = 4;
  71. const cubemap1 = loader.load( 'textures/compressed/Mountains.dds', function ( texture ) {
  72. texture.magFilter = THREE.LinearFilter;
  73. texture.minFilter = THREE.LinearFilter;
  74. texture.mapping = THREE.CubeReflectionMapping;
  75. texture.colorSpace = THREE.SRGBColorSpace;
  76. material1.needsUpdate = true;
  77. } );
  78. const cubemap2 = loader.load( 'textures/compressed/Mountains_argb_mip.dds', function ( texture ) {
  79. texture.magFilter = THREE.LinearFilter;
  80. texture.minFilter = THREE.LinearFilter;
  81. texture.mapping = THREE.CubeReflectionMapping;
  82. texture.colorSpace = THREE.SRGBColorSpace;
  83. material5.needsUpdate = true;
  84. } );
  85. const cubemap3 = loader.load( 'textures/compressed/Mountains_argb_nomip.dds', function ( texture ) {
  86. texture.magFilter = THREE.LinearFilter;
  87. texture.minFilter = THREE.LinearFilter;
  88. texture.mapping = THREE.CubeReflectionMapping;
  89. texture.colorSpace = THREE.SRGBColorSpace;
  90. material6.needsUpdate = true;
  91. } );
  92. const material1 = new THREE.MeshBasicMaterial( { map: map1, envMap: cubemap1 } );
  93. const material2 = new THREE.MeshBasicMaterial( { map: map2 } );
  94. const material3 = new THREE.MeshBasicMaterial( { map: map3, alphaTest: 0.5, side: THREE.DoubleSide } );
  95. const material4 = new THREE.MeshBasicMaterial( { map: map4, side: THREE.DoubleSide, blending: THREE.AdditiveBlending, depthTest: false, transparent: true } );
  96. const material5 = new THREE.MeshBasicMaterial( { envMap: cubemap2 } );
  97. const material6 = new THREE.MeshBasicMaterial( { envMap: cubemap3 } );
  98. const material7 = new THREE.MeshBasicMaterial( { map: map5 } );
  99. const material8 = new THREE.MeshBasicMaterial( { map: map6 } );
  100. const material9 = new THREE.MeshBasicMaterial( { map: map7 } );
  101. const material10 = new THREE.MeshBasicMaterial( { map: map8 } );
  102. const material11 = new THREE.MeshBasicMaterial( { map: map9 } );
  103. const material12 = new THREE.MeshBasicMaterial( { map: map10 } );
  104. const material13 = new THREE.MeshBasicMaterial( { map: map11, transparent: true } );
  105. let mesh = new THREE.Mesh( new THREE.TorusGeometry(), material1 );
  106. mesh.position.x = - 10;
  107. mesh.position.y = - 2;
  108. scene.add( mesh );
  109. meshes.push( mesh );
  110. mesh = new THREE.Mesh( geometry, material2 );
  111. mesh.position.x = - 6;
  112. mesh.position.y = - 2;
  113. scene.add( mesh );
  114. meshes.push( mesh );
  115. mesh = new THREE.Mesh( geometry, material3 );
  116. mesh.position.x = - 6;
  117. mesh.position.y = 2;
  118. scene.add( mesh );
  119. meshes.push( mesh );
  120. mesh = new THREE.Mesh( geometry, material4 );
  121. mesh.position.x = - 10;
  122. mesh.position.y = 2;
  123. scene.add( mesh );
  124. meshes.push( mesh );
  125. mesh = new THREE.Mesh( geometry, material5 );
  126. mesh.position.x = - 2;
  127. mesh.position.y = 2;
  128. scene.add( mesh );
  129. meshes.push( mesh );
  130. mesh = new THREE.Mesh( geometry, material6 );
  131. mesh.position.x = - 2;
  132. mesh.position.y = - 2;
  133. scene.add( mesh );
  134. meshes.push( mesh );
  135. mesh = new THREE.Mesh( geometry, material7 );
  136. mesh.position.x = 2;
  137. mesh.position.y = - 2;
  138. scene.add( mesh );
  139. meshes.push( mesh );
  140. mesh = new THREE.Mesh( geometry, material8 );
  141. mesh.position.x = 2;
  142. mesh.position.y = 2;
  143. scene.add( mesh );
  144. meshes.push( mesh );
  145. mesh = new THREE.Mesh( geometry, material9 );
  146. mesh.position.x = 6;
  147. mesh.position.y = - 2;
  148. scene.add( mesh );
  149. meshes.push( mesh );
  150. mesh = new THREE.Mesh( geometry, material10 );
  151. mesh.position.x = 6;
  152. mesh.position.y = 2;
  153. scene.add( mesh );
  154. meshes.push( mesh );
  155. mesh = new THREE.Mesh( geometry, material11 );
  156. mesh.position.x = 10;
  157. mesh.position.y = - 2;
  158. scene.add( mesh );
  159. meshes.push( mesh );
  160. mesh = new THREE.Mesh( geometry, material12 );
  161. mesh.position.x = 10;
  162. mesh.position.y = 2;
  163. scene.add( mesh );
  164. meshes.push( mesh );
  165. mesh = new THREE.Mesh( geometry, material13 );
  166. mesh.position.x = - 10;
  167. mesh.position.y = - 6;
  168. scene.add( mesh );
  169. meshes.push( mesh );
  170. renderer = new THREE.WebGLRenderer( { antialias: true } );
  171. renderer.setPixelRatio( window.devicePixelRatio );
  172. renderer.setSize( window.innerWidth, window.innerHeight );
  173. renderer.setAnimationLoop( animate );
  174. document.body.appendChild( renderer.domElement );
  175. window.addEventListener( 'resize', onWindowResize );
  176. }
  177. function onWindowResize() {
  178. camera.aspect = window.innerWidth / window.innerHeight;
  179. camera.updateProjectionMatrix();
  180. renderer.setSize( window.innerWidth, window.innerHeight );
  181. }
  182. function animate() {
  183. const time = Date.now() * 0.001;
  184. for ( let i = 0; i < meshes.length; i ++ ) {
  185. const mesh = meshes[ i ];
  186. mesh.rotation.x = time;
  187. mesh.rotation.y = time;
  188. }
  189. renderer.render( scene, camera );
  190. }
  191. </script>
  192. </body>
  193. </html>
粤ICP备19079148号