webgl_loader_texture_tga.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - TGA texture</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> - TGA texture example by <a href="https://github.com/DaoshengMu/" target="_blank" rel="noopener">Daosheng Mu</a>
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import Stats from 'three/addons/libs/stats.module.js';
  24. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  25. import { TGALoader } from 'three/addons/loaders/TGALoader.js';
  26. let camera, scene, renderer, stats;
  27. init();
  28. function init() {
  29. const container = document.createElement( 'div' );
  30. document.body.appendChild( container );
  31. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  32. camera.position.set( 0, 1, 5 );
  33. scene = new THREE.Scene();
  34. //
  35. const loader = new TGALoader();
  36. const geometry = new THREE.BoxGeometry();
  37. // add box 1 - grey8 texture
  38. const texture1 = loader.load( 'textures/crate_grey8.tga' );
  39. texture1.colorSpace = THREE.SRGBColorSpace;
  40. const material1 = new THREE.MeshPhongMaterial( { color: 0xffffff, map: texture1 } );
  41. const mesh1 = new THREE.Mesh( geometry, material1 );
  42. mesh1.position.x = - 1;
  43. scene.add( mesh1 );
  44. // add box 2 - tga texture
  45. const texture2 = loader.load( 'textures/crate_color8.tga' );
  46. texture2.colorSpace = THREE.SRGBColorSpace;
  47. const material2 = new THREE.MeshPhongMaterial( { color: 0xffffff, map: texture2 } );
  48. const mesh2 = new THREE.Mesh( geometry, material2 );
  49. mesh2.position.x = 1;
  50. scene.add( mesh2 );
  51. //
  52. const ambientLight = new THREE.AmbientLight( 0xffffff, 1.5 );
  53. scene.add( ambientLight );
  54. const light = new THREE.DirectionalLight( 0xffffff, 2.5 );
  55. light.position.set( 1, 1, 1 );
  56. scene.add( light );
  57. //
  58. renderer = new THREE.WebGLRenderer( { antialias: true } );
  59. renderer.setPixelRatio( window.devicePixelRatio );
  60. renderer.setSize( window.innerWidth, window.innerHeight );
  61. renderer.setAnimationLoop( animate );
  62. container.appendChild( renderer.domElement );
  63. //
  64. const controls = new OrbitControls( camera, renderer.domElement );
  65. controls.enableZoom = false;
  66. //
  67. stats = new Stats();
  68. container.appendChild( stats.dom );
  69. //
  70. window.addEventListener( 'resize', onWindowResize );
  71. }
  72. function onWindowResize() {
  73. camera.aspect = window.innerWidth / window.innerHeight;
  74. camera.updateProjectionMatrix();
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. }
  77. function animate() {
  78. renderer.render( scene, camera );
  79. stats.update();
  80. }
  81. </script>
  82. </body>
  83. </html>