webgl_loader_texture_tga.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. import { TGALoader } from 'three/addons/loaders/TGALoader.js';
  29. let camera, scene, renderer, stats;
  30. init();
  31. animate();
  32. function init() {
  33. const container = document.createElement( 'div' );
  34. document.body.appendChild( container );
  35. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
  36. camera.position.set( 0, 50, 250 );
  37. scene = new THREE.Scene();
  38. //
  39. const loader = new TGALoader();
  40. const geometry = new THREE.BoxGeometry( 50, 50, 50 );
  41. // add box 1 - grey8 texture
  42. const texture1 = loader.load( 'textures/crate_grey8.tga' );
  43. const material1 = new THREE.MeshPhongMaterial( { color: 0xffffff, map: texture1 } );
  44. const mesh1 = new THREE.Mesh( geometry, material1 );
  45. mesh1.position.x = - 50;
  46. scene.add( mesh1 );
  47. // add box 2 - tga texture
  48. const texture2 = loader.load( 'textures/crate_color8.tga' );
  49. const material2 = new THREE.MeshPhongMaterial( { color: 0xffffff, map: texture2 } );
  50. const mesh2 = new THREE.Mesh( geometry, material2 );
  51. mesh2.position.x = 50;
  52. scene.add( mesh2 );
  53. //
  54. const ambientLight = new THREE.AmbientLight( 0xffffff, 0.4 );
  55. scene.add( ambientLight );
  56. const light = new THREE.DirectionalLight( 0xffffff, 1 );
  57. light.position.set( 1, 1, 1 );
  58. scene.add( light );
  59. //
  60. renderer = new THREE.WebGLRenderer( { antialias: true } );
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. container.appendChild( renderer.domElement );
  64. //
  65. const controls = new OrbitControls( camera, renderer.domElement );
  66. controls.enableZoom = false;
  67. //
  68. stats = new Stats();
  69. container.appendChild( stats.dom );
  70. //
  71. window.addEventListener( 'resize', onWindowResize );
  72. }
  73. function onWindowResize() {
  74. camera.aspect = window.innerWidth / window.innerHeight;
  75. camera.updateProjectionMatrix();
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. }
  78. function animate() {
  79. requestAnimationFrame( animate );
  80. render();
  81. stats.update();
  82. }
  83. function render() {
  84. renderer.render( scene, camera );
  85. }
  86. </script>
  87. </body>
  88. </html>