webgl_loader_imagebitmap.html 3.4 KB

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