webgl_loader_texture_tga.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. texture1.colorSpace = THREE.SRGBColorSpace;
  44. const material1 = new THREE.MeshPhongMaterial( { color: 0xffffff, map: texture1 } );
  45. const mesh1 = new THREE.Mesh( geometry, material1 );
  46. mesh1.position.x = - 50;
  47. scene.add( mesh1 );
  48. // add box 2 - tga texture
  49. const texture2 = loader.load( 'textures/crate_color8.tga' );
  50. texture2.colorSpace = THREE.SRGBColorSpace;
  51. const material2 = new THREE.MeshPhongMaterial( { color: 0xffffff, map: texture2 } );
  52. const mesh2 = new THREE.Mesh( geometry, material2 );
  53. mesh2.position.x = 50;
  54. scene.add( mesh2 );
  55. //
  56. const ambientLight = new THREE.AmbientLight( 0xffffff, 0.4 );
  57. scene.add( ambientLight );
  58. const light = new THREE.DirectionalLight( 0xffffff, 1 );
  59. light.position.set( 1, 1, 1 );
  60. scene.add( light );
  61. //
  62. renderer = new THREE.WebGLRenderer( { antialias: true } );
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. container.appendChild( renderer.domElement );
  66. //
  67. const controls = new OrbitControls( camera, renderer.domElement );
  68. controls.enableZoom = false;
  69. //
  70. stats = new Stats();
  71. container.appendChild( stats.dom );
  72. //
  73. window.addEventListener( 'resize', onWindowResize );
  74. }
  75. function onWindowResize() {
  76. camera.aspect = window.innerWidth / window.innerHeight;
  77. camera.updateProjectionMatrix();
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. }
  80. function animate() {
  81. requestAnimationFrame( animate );
  82. render();
  83. stats.update();
  84. }
  85. function render() {
  86. renderer.render( scene, camera );
  87. }
  88. </script>
  89. </body>
  90. </html>