webgl_loader_texture_dds.html 5.6 KB

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