webgl_loader_imagebitmap.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - loader - ImageBitmap</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> - Texture loader using ImageBitmap
  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. let camera, scene, renderer;
  27. let group, cubes;
  28. init();
  29. animate();
  30. function addImageBitmap() {
  31. new THREE.ImageBitmapLoader()
  32. .load( 'textures/planets/earth_atmos_2048.jpg?' + performance.now(), function ( imageBitmap ) {
  33. const texture = new THREE.CanvasTexture( imageBitmap );
  34. texture.colorSpace = THREE.SRGBColorSpace;
  35. const material = new THREE.MeshBasicMaterial( { map: texture } );
  36. /* ImageBitmap should be disposed when done with it
  37. Can't be done until it's actually uploaded to WebGLTexture */
  38. // imageBitmap.close();
  39. addCube( material );
  40. }, function ( p ) {
  41. console.log( p );
  42. }, function ( e ) {
  43. console.log( e );
  44. } );
  45. }
  46. function addImage() {
  47. new THREE.ImageLoader()
  48. .setCrossOrigin( '*' )
  49. .load( 'textures/planets/earth_atmos_2048.jpg?' + performance.now(), function ( image ) {
  50. const texture = new THREE.CanvasTexture( image );
  51. texture.colorSpace = THREE.SRGBColorSpace;
  52. const material = new THREE.MeshBasicMaterial( { color: 0xff8888, map: texture } );
  53. addCube( material );
  54. } );
  55. }
  56. const geometry = new THREE.BoxGeometry();
  57. function addCube( material ) {
  58. const cube = new THREE.Mesh( geometry, material );
  59. cube.position.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  60. cube.rotation.set( Math.random() * 2 * Math.PI, Math.random() * 2 * Math.PI, Math.random() * 2 * Math.PI );
  61. cubes.add( cube );
  62. }
  63. function init() {
  64. const container = document.createElement( 'div' );
  65. document.body.appendChild( container );
  66. // CAMERA
  67. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1500 );
  68. camera.position.set( 0, 4, 7 );
  69. camera.lookAt( 0, 0, 0 );
  70. // SCENE
  71. scene = new THREE.Scene();
  72. //
  73. group = new THREE.Group();
  74. scene.add( group );
  75. group.add( new THREE.GridHelper( 4, 12, 0x888888, 0x444444 ) );
  76. cubes = new THREE.Group();
  77. group.add( cubes );
  78. // RENDERER
  79. renderer = new THREE.WebGLRenderer( { antialias: true } );
  80. renderer.setPixelRatio( window.devicePixelRatio );
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. renderer.useLegacyLights = false;
  83. container.appendChild( renderer.domElement );
  84. // TESTS
  85. setTimeout( addImage, 300 );
  86. setTimeout( addImage, 600 );
  87. setTimeout( addImage, 900 );
  88. setTimeout( addImageBitmap, 1300 );
  89. setTimeout( addImageBitmap, 1600 );
  90. setTimeout( addImageBitmap, 1900 );
  91. // EVENTS
  92. window.addEventListener( 'resize', onWindowResize );
  93. }
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. }
  99. function animate() {
  100. group.rotation.y = performance.now() / 3000;
  101. renderer.render( scene, camera );
  102. requestAnimationFrame( animate );
  103. }
  104. </script>
  105. </body>
  106. </html>