webgl_loader_texture_dds.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. <style>
  8. body {
  9. margin: 0px;
  10. background-color: #050505;
  11. color: #fff;
  12. overflow: hidden;
  13. }
  14. a { color: #e00 }
  15. #info {
  16. position: absolute;
  17. top: 0px; width: 100%;
  18. color: #ffffff;
  19. padding: 5px;
  20. font-family:Monospace;
  21. font-size:13px;
  22. text-align:center;
  23. z-index:1000;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="info">
  29. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl - compressed textures -
  30. leaf texture by <a href="http://opengameart.org/node/10505">lauris71</a>, explosion texture by <a href="http://opengameart.org/node/7728">bart</a>
  31. </div>
  32. <script src="../build/three.js"></script>
  33. <script src="js/loaders/DDSLoader.js"></script>
  34. <script src="js/Detector.js"></script>
  35. <script>
  36. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  37. var camera, scene, renderer;
  38. var meshes = [];
  39. init();
  40. animate();
  41. function init() {
  42. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
  43. camera.position.z = 1000;
  44. scene = new THREE.Scene();
  45. var geometry = new THREE.BoxGeometry( 200, 200, 200 );
  46. /*
  47. This is how compressed textures are supposed to be used:
  48. DXT1 - RGB - opaque textures
  49. DXT3 - RGBA - transparent textures with sharp alpha transitions
  50. DXT5 - RGBA - transparent textures with full alpha range
  51. */
  52. var loader = new THREE.DDSLoader();
  53. var map1 = loader.load( 'textures/compressed/disturb_dxt1_nomip.dds' );
  54. map1.minFilter = map1.magFilter = THREE.LinearFilter;
  55. map1.anisotropy = 4;
  56. var map2 = loader.load( 'textures/compressed/disturb_dxt1_mip.dds' );
  57. map2.anisotropy = 4;
  58. var map3 = loader.load( 'textures/compressed/hepatica_dxt3_mip.dds' );
  59. map3.anisotropy = 4;
  60. var map4 = loader.load( 'textures/compressed/explosion_dxt5_mip.dds' );
  61. map4.anisotropy = 4;
  62. var map5 = loader.load( 'textures/compressed/disturb_argb_nomip.dds' );
  63. map5.minFilter = map5.magFilter = THREE.LinearFilter;
  64. map5.anisotropy = 4;
  65. var map6 = loader.load( 'textures/compressed/disturb_argb_mip.dds' );
  66. map6.anisotropy = 4;
  67. var cubemap1 = loader.load( 'textures/compressed/Mountains.dds', function ( texture ) {
  68. texture.magFilter = THREE.LinearFilter;
  69. texture.minFilter = THREE.LinearFilter;
  70. texture.mapping = THREE.CubeReflectionMapping;
  71. material1.needsUpdate = true;
  72. } );
  73. var cubemap2 = loader.load( 'textures/compressed/Mountains_argb_mip.dds', function ( texture ) {
  74. texture.magFilter = THREE.LinearFilter;
  75. texture.minFilter = THREE.LinearFilter;
  76. texture.mapping = THREE.CubeReflectionMapping;
  77. material5.needsUpdate = true;
  78. } );
  79. var cubemap3 = loader.load( 'textures/compressed/Mountains_argb_nomip.dds', function ( texture ) {
  80. texture.magFilter = THREE.LinearFilter;
  81. texture.minFilter = THREE.LinearFilter;
  82. texture.mapping = THREE.CubeReflectionMapping;
  83. material6.needsUpdate = true;
  84. } );
  85. var material1 = new THREE.MeshBasicMaterial( { map: map1, envMap: cubemap1 } );
  86. var material2 = new THREE.MeshBasicMaterial( { map: map2 } );
  87. var material3 = new THREE.MeshBasicMaterial( { map: map3, alphaTest: 0.5, side: THREE.DoubleSide } );
  88. var material4 = new THREE.MeshBasicMaterial( { map: map4, side: THREE.DoubleSide, blending: THREE.AdditiveBlending, depthTest: false, transparent: true } );
  89. var material5 = new THREE.MeshBasicMaterial( { envMap: cubemap2 } );
  90. var material6 = new THREE.MeshBasicMaterial( { envMap: cubemap3 } );
  91. var material7 = new THREE.MeshBasicMaterial( { map: map5 } );
  92. var material8 = new THREE.MeshBasicMaterial( { map: map6 } );
  93. var 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, false );
  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. var time = Date.now() * 0.001;
  147. for ( var i = 0; i < meshes.length; i ++ ) {
  148. var 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>