webgl_materials_texture_compressed.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - compressed textures</title>
  5. <meta charset="utf-8">
  6. <style>
  7. body {
  8. margin: 0px;
  9. background-color: #050505;
  10. color: #fff;
  11. overflow: hidden;
  12. }
  13. a { color: #e00 }
  14. #info {
  15. position: absolute;
  16. top: 0px; width: 100%;
  17. color: #ffffff;
  18. padding: 5px;
  19. font-family:Monospace;
  20. font-size:13px;
  21. text-align:center;
  22. z-index:1000;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info">
  28. <a href="http://threejs.org" target="_blank">three.js</a> - webgl - compressed textures -
  29. leaf texture by <a href="http://opengameart.org/node/10505">lauris71</a>, explosion texture by <a href="http://opengameart.org/node/7728">bart</a>
  30. </div>
  31. <script src="../build/three.min.js"></script>
  32. <script src="js/Detector.js"></script>
  33. <script src="js/libs/stats.min.js"></script>
  34. <script>
  35. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  36. var camera, scene, renderer;
  37. var meshes = [];
  38. init();
  39. animate();
  40. function init() {
  41. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 2000 );
  42. camera.position.z = 1000;
  43. scene = new THREE.Scene();
  44. geometry = new THREE.CubeGeometry( 200, 200, 200 );
  45. /*
  46. This is how compressed textures are supposed to be used:
  47. DXT1 - RGB - opaque textures
  48. DXT3 - RGBA - transparent textures with sharp alpha transitions
  49. DXT5 - RGBA - transparent textures with full alpha range
  50. */
  51. var map1 = THREE.ImageUtils.loadCompressedTexture( 'textures/compressed/disturb_dxt1_nomip.dds' );
  52. map1.minFilter = map1.magFilter = THREE.LinearFilter;
  53. map1.anisotropy = 4;
  54. var map2 = THREE.ImageUtils.loadCompressedTexture( 'textures/compressed/disturb_dxt1_mip.dds' );
  55. map2.anisotropy = 4;
  56. var map3 = THREE.ImageUtils.loadCompressedTexture( 'textures/compressed/hepatica_dxt3_mip.dds' );
  57. map3.anisotropy = 4;
  58. var map4 = THREE.ImageUtils.loadCompressedTexture( 'textures/compressed/explosion_dxt5_mip.dds' );
  59. map4.anisotropy = 4;
  60. var material1 = new THREE.MeshBasicMaterial( { map: map1 } );
  61. var material2 = new THREE.MeshBasicMaterial( { map: map2 } );
  62. var material3 = new THREE.MeshBasicMaterial( { map: map3, alphaTest: 0.5, side: THREE.DoubleSide } );
  63. var material4 = new THREE.MeshBasicMaterial( { map: map4, side: THREE.DoubleSide, blending: THREE.AdditiveBlending, depthTest: false, transparent: true } );
  64. var mesh = new THREE.Mesh( geometry, material1 );
  65. mesh.position.x = -200;
  66. mesh.position.y = -200;
  67. scene.add( mesh );
  68. meshes.push( mesh );
  69. mesh = new THREE.Mesh( geometry, material2 );
  70. mesh.position.x = 200;
  71. mesh.position.y = -200;
  72. scene.add( mesh );
  73. meshes.push( mesh );
  74. mesh = new THREE.Mesh( geometry, material3 );
  75. mesh.position.x = 200;
  76. mesh.position.y = 200;
  77. scene.add( mesh );
  78. meshes.push( mesh );
  79. mesh = new THREE.Mesh( geometry, material4 );
  80. mesh.position.x = -200;
  81. mesh.position.y = 200;
  82. scene.add( mesh );
  83. meshes.push( mesh );
  84. renderer = new THREE.WebGLRenderer( { antialias: true } );
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. document.body.appendChild( renderer.domElement );
  87. stats = new Stats();
  88. stats.domElement.style.position = 'absolute';
  89. stats.domElement.style.top = '0px';
  90. stats.domElement.style.zIndex = 100;
  91. document.body.appendChild( stats.domElement );
  92. stats.domElement.children[ 0 ].children[ 0 ].style.color = "#777";
  93. stats.domElement.children[ 0 ].style.background = "transparent";
  94. stats.domElement.children[ 0 ].children[ 1 ].style.display = "none";
  95. window.addEventListener( 'resize', onWindowResize, false );
  96. }
  97. function onWindowResize() {
  98. camera.aspect = window.innerWidth / window.innerHeight;
  99. camera.updateProjectionMatrix();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. }
  102. function animate() {
  103. requestAnimationFrame( animate );
  104. var time = Date.now() * 0.001;
  105. for ( var i = 0; i < meshes.length; i ++ ) {
  106. var mesh = meshes[ i ];
  107. mesh.rotation.x = time;
  108. mesh.rotation.y = time;
  109. }
  110. renderer.render( scene, camera );
  111. stats.update();
  112. }
  113. </script>
  114. </body>
  115. </html>