webgl_loader_imagebitmap.html 3.4 KB

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