webgl_materials_texture_compressed.html 6.1 KB

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