webgl_materials_texture_tga.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!DOCTYPE html>
  2. <!--
  3. @author Daosheng Mu / https://github.com/DaoshengMu/
  4. -->
  5. <html>
  6. <head>
  7. <title>three.js webgl - materials - tga texture</title>
  8. <meta charset="UTF-8">
  9. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  10. <style>
  11. body {
  12. color: #000;
  13. font-family:Monospace;
  14. font-size:13px;
  15. text-align:center;
  16. background-color: #fff;
  17. margin: 0px;
  18. padding: 0px;
  19. overflow: hidden;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="info">
  25. <a href="http://threejs.org" target="_blank">three.js</a> - tga texture example
  26. </div>
  27. <script src="../build/three.js"></script>
  28. <script src="js/loaders/TGALoader.js"></script>
  29. <script src="js/Detector.js"></script>
  30. <script src="js/libs/stats.min.js"></script>
  31. <script>
  32. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  33. var SCREEN_WIDTH = window.innerWidth;
  34. var SCREEN_HEIGHT = window.innerHeight;
  35. var container, stats;
  36. var camera, scene, renderer;
  37. var mouseX = 0, mouseY = 0;
  38. var windowHalfX = window.innerWidth / 2;
  39. var windowHalfY = window.innerHeight / 2;
  40. init();
  41. animate();
  42. function init() {
  43. container = document.createElement( 'div' );
  44. document.body.appendChild( container );
  45. renderer = new THREE.WebGLRenderer( { antialias: true } );
  46. camera = new THREE.PerspectiveCamera( 35, SCREEN_WIDTH / SCREEN_HEIGHT, 10, 2000 );
  47. camera.position.z = 200;
  48. scene = new THREE.Scene();
  49. scene.add( new THREE.AmbientLight( 0xffffff, 0.4 ) );
  50. var light = new THREE.DirectionalLight( 0xffffff, 1 );
  51. light.position.set( 1, 1, 1 );
  52. scene.add( light );
  53. var loader = new THREE.TGALoader();
  54. // add box 1 - grey8 texture
  55. var texture1 = loader.load( 'textures/crate_grey8.tga' );
  56. var material1 = new THREE.MeshPhongMaterial( { color: 0xffffff, map: texture1 } );
  57. var geometry = new THREE.BoxGeometry( 50, 50, 50 );
  58. var mesh1 = new THREE.Mesh( geometry, material1 );
  59. mesh1.position.x = - 50;
  60. scene.add( mesh1 );
  61. // add box 2 - tga texture
  62. var texture2 = loader.load( 'textures/crate_color8.tga' );
  63. var material2 = new THREE.MeshPhongMaterial( { color: 0xffffff, map: texture2 } );
  64. var mesh2 = new THREE.Mesh( geometry, material2 );
  65. mesh2.position.x = 50;
  66. scene.add( mesh2 );
  67. // RENDERER
  68. renderer.setClearColor( 0xf2f7ff );
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  71. renderer.domElement.style.position = "relative";
  72. container.appendChild( renderer.domElement );
  73. // STATS1
  74. stats = new Stats();
  75. container.appendChild( stats.dom );
  76. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  77. }
  78. function onDocumentMouseMove( event ) {
  79. mouseX = ( event.clientX - windowHalfX );
  80. mouseY = ( event.clientY - windowHalfY );
  81. }
  82. function animate() {
  83. requestAnimationFrame( animate );
  84. render();
  85. stats.update();
  86. }
  87. function render() {
  88. camera.position.x += ( mouseX - camera.position.x ) * .05;
  89. camera.position.y = THREE.Math.clamp( camera.position.y + ( - ( mouseY - 200 ) - camera.position.y ) * .05, 50, 1000 );
  90. camera.lookAt( scene.position );
  91. renderer.render( scene, camera );
  92. }
  93. </script>
  94. </body>
  95. </html>