webgl_loader_texture_tga.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. <meta property="og:title" content="three.js webgl - materials - TGA texture">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_loader_texture_tga.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_loader_texture_tga.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <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>
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { TGALoader } from 'three/addons/loaders/TGALoader.js';
  30. let camera, scene, renderer, stats;
  31. init();
  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, 100 );
  36. camera.position.set( 0, 1, 5 );
  37. scene = new THREE.Scene();
  38. //
  39. const loader = new TGALoader();
  40. const geometry = new THREE.BoxGeometry();
  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 = - 1;
  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 = 1;
  54. scene.add( mesh2 );
  55. //
  56. const ambientLight = new THREE.AmbientLight( 0xffffff, 1.5 );
  57. scene.add( ambientLight );
  58. const light = new THREE.DirectionalLight( 0xffffff, 2.5 );
  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. renderer.setAnimationLoop( animate );
  66. container.appendChild( renderer.domElement );
  67. //
  68. const controls = new OrbitControls( camera, renderer.domElement );
  69. controls.enableZoom = false;
  70. //
  71. stats = new Stats();
  72. container.appendChild( stats.dom );
  73. //
  74. window.addEventListener( 'resize', onWindowResize );
  75. }
  76. function onWindowResize() {
  77. camera.aspect = window.innerWidth / window.innerHeight;
  78. camera.updateProjectionMatrix();
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. }
  81. function animate() {
  82. renderer.render( scene, camera );
  83. stats.update();
  84. }
  85. </script>
  86. </body>
  87. </html>
粤ICP备19079148号