webgl_loader_texture_dds.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { DDSLoader } from 'three/addons/loaders/DDSLoader.js';
  28. let camera, scene, renderer;
  29. const meshes = [];
  30. init();
  31. animate();
  32. function init() {
  33. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
  34. camera.position.z = 1000;
  35. scene = new THREE.Scene();
  36. const geometry = new THREE.BoxGeometry( 200, 200, 200 );
  37. /*
  38. This is how compressed textures are supposed to be used:
  39. DXT1 - RGB - opaque textures
  40. DXT3 - RGBA - transparent textures with sharp alpha transitions
  41. DXT5 - RGBA - transparent textures with full alpha range
  42. */
  43. const loader = new DDSLoader();
  44. const map1 = loader.load( 'textures/compressed/disturb_dxt1_nomip.dds' );
  45. map1.minFilter = map1.magFilter = THREE.LinearFilter;
  46. map1.anisotropy = 4;
  47. map1.colorSpace = THREE.SRGBColorSpace;
  48. const map2 = loader.load( 'textures/compressed/disturb_dxt1_mip.dds' );
  49. map2.anisotropy = 4;
  50. map2.colorSpace = THREE.SRGBColorSpace;
  51. const map3 = loader.load( 'textures/compressed/hepatica_dxt3_mip.dds' );
  52. map3.anisotropy = 4;
  53. map3.colorSpace = THREE.SRGBColorSpace;
  54. const map4 = loader.load( 'textures/compressed/explosion_dxt5_mip.dds' );
  55. map4.anisotropy = 4;
  56. map4.colorSpace = THREE.SRGBColorSpace;
  57. const map5 = loader.load( 'textures/compressed/disturb_argb_nomip.dds' );
  58. map5.minFilter = map5.magFilter = THREE.LinearFilter;
  59. map5.anisotropy = 4;
  60. map5.colorSpace = THREE.SRGBColorSpace;
  61. const map6 = loader.load( 'textures/compressed/disturb_argb_mip.dds' );
  62. map6.anisotropy = 4;
  63. map6.colorSpace = THREE.SRGBColorSpace;
  64. const cubemap1 = loader.load( 'textures/compressed/Mountains.dds', function ( texture ) {
  65. texture.magFilter = THREE.LinearFilter;
  66. texture.minFilter = THREE.LinearFilter;
  67. texture.mapping = THREE.CubeReflectionMapping;
  68. texture.colorSpace = THREE.SRGBColorSpace;
  69. material1.needsUpdate = true;
  70. } );
  71. const cubemap2 = loader.load( 'textures/compressed/Mountains_argb_mip.dds', function ( texture ) {
  72. texture.magFilter = THREE.LinearFilter;
  73. texture.minFilter = THREE.LinearFilter;
  74. texture.mapping = THREE.CubeReflectionMapping;
  75. texture.colorSpace = THREE.SRGBColorSpace;
  76. material5.needsUpdate = true;
  77. } );
  78. const cubemap3 = loader.load( 'textures/compressed/Mountains_argb_nomip.dds', function ( texture ) {
  79. texture.magFilter = THREE.LinearFilter;
  80. texture.minFilter = THREE.LinearFilter;
  81. texture.mapping = THREE.CubeReflectionMapping;
  82. texture.colorSpace = THREE.SRGBColorSpace;
  83. material6.needsUpdate = true;
  84. } );
  85. const material1 = new THREE.MeshBasicMaterial( { map: map1, envMap: cubemap1 } );
  86. const material2 = new THREE.MeshBasicMaterial( { map: map2 } );
  87. const material3 = new THREE.MeshBasicMaterial( { map: map3, alphaTest: 0.5, side: THREE.DoubleSide } );
  88. const material4 = new THREE.MeshBasicMaterial( { map: map4, side: THREE.DoubleSide, blending: THREE.AdditiveBlending, depthTest: false, transparent: true } );
  89. const material5 = new THREE.MeshBasicMaterial( { envMap: cubemap2 } );
  90. const material6 = new THREE.MeshBasicMaterial( { envMap: cubemap3 } );
  91. const material7 = new THREE.MeshBasicMaterial( { map: map5 } );
  92. const material8 = new THREE.MeshBasicMaterial( { map: map6 } );
  93. let mesh = new THREE.Mesh( new THREE.TorusGeometry( 100, 50, 32, 16 ), material1 );
  94. mesh.position.x = - 600;
  95. mesh.position.y = - 200;
  96. scene.add( mesh );
  97. meshes.push( mesh );
  98. mesh = new THREE.Mesh( geometry, material2 );
  99. mesh.position.x = - 200;
  100. mesh.position.y = - 200;
  101. scene.add( mesh );
  102. meshes.push( mesh );
  103. mesh = new THREE.Mesh( geometry, material3 );
  104. mesh.position.x = - 200;
  105. mesh.position.y = 200;
  106. scene.add( mesh );
  107. meshes.push( mesh );
  108. mesh = new THREE.Mesh( geometry, material4 );
  109. mesh.position.x = - 600;
  110. mesh.position.y = 200;
  111. scene.add( mesh );
  112. meshes.push( mesh );
  113. mesh = new THREE.Mesh( geometry, material5 );
  114. mesh.position.x = 200;
  115. mesh.position.y = 200;
  116. scene.add( mesh );
  117. meshes.push( mesh );
  118. mesh = new THREE.Mesh( geometry, material6 );
  119. mesh.position.x = 200;
  120. mesh.position.y = - 200;
  121. scene.add( mesh );
  122. meshes.push( mesh );
  123. mesh = new THREE.Mesh( geometry, material7 );
  124. mesh.position.x = 600;
  125. mesh.position.y = - 200;
  126. scene.add( mesh );
  127. meshes.push( mesh );
  128. mesh = new THREE.Mesh( geometry, material8 );
  129. mesh.position.x = 600;
  130. mesh.position.y = 200;
  131. scene.add( mesh );
  132. meshes.push( mesh );
  133. renderer = new THREE.WebGLRenderer( { antialias: true } );
  134. renderer.setPixelRatio( window.devicePixelRatio );
  135. renderer.setSize( window.innerWidth, window.innerHeight );
  136. document.body.appendChild( renderer.domElement );
  137. window.addEventListener( 'resize', onWindowResize );
  138. }
  139. function onWindowResize() {
  140. camera.aspect = window.innerWidth / window.innerHeight;
  141. camera.updateProjectionMatrix();
  142. renderer.setSize( window.innerWidth, window.innerHeight );
  143. }
  144. function animate() {
  145. requestAnimationFrame( animate );
  146. const time = Date.now() * 0.001;
  147. for ( let i = 0; i < meshes.length; i ++ ) {
  148. const mesh = meshes[ i ];
  149. mesh.rotation.x = time;
  150. mesh.rotation.y = time;
  151. }
  152. renderer.render( scene, camera );
  153. }
  154. </script>
  155. </body>
  156. </html>