webgl_loader_imagebitmap.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 10px;
  18. width: 100%;
  19. text-align: center;
  20. z-index: 100;
  21. display:block;
  22. }
  23. #info a, .button {
  24. color: #f00;
  25. font-weight: bold;
  26. text-decoration: underline;
  27. cursor: pointer
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <script src="../build/three.js"></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/loaders/ImageBitmapLoader.js"></script>
  35. <div id="info">
  36. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Texture loader using ImageBitmap
  37. </div>
  38. <script>
  39. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  40. var container;
  41. var camera, scene, renderer;
  42. var group, cubes;
  43. init();
  44. animate();
  45. function addImageBitmap() {
  46. new THREE.ImageBitmapLoader()
  47. .setOptions( { imageOrientation: 'none' } )
  48. .load( 'textures/planets/earth_atmos_2048.jpg?' + performance.now(), function ( imageBitmap ) {
  49. var texture = new THREE.CanvasTexture( imageBitmap );
  50. var material = new THREE.MeshBasicMaterial( { map: texture } );
  51. /* ImageBitmap should be disposed when done with it
  52. Can't be done until it's actually uploaded to WebGLTexture */
  53. // imageBitmap.close();
  54. addCube( material );
  55. }, function( p ) {
  56. console.log( p );
  57. }, function( e ) {
  58. console.log( e );
  59. } );
  60. }
  61. function addImage() {
  62. new THREE.ImageLoader()
  63. .setCrossOrigin( '*' )
  64. .load( 'textures/planets/earth_atmos_2048.jpg?' + performance.now(), function ( image ) {
  65. var texture = new THREE.CanvasTexture( image );
  66. var material = new THREE.MeshBasicMaterial( { color: 0xff8888, map: texture } );
  67. addCube( material );
  68. });
  69. }
  70. var geometry = new THREE.BoxBufferGeometry( 1,1,1 );
  71. function addCube( material ) {
  72. var cube = new THREE.Mesh( geometry, material );
  73. cube.position.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  74. cube.rotation.set( Math.random() * 2 * Math.PI, Math.random() * 2 * Math.PI, Math.random() * 2 * Math.PI );
  75. cubes.add( cube );
  76. }
  77. function init() {
  78. container = document.createElement( 'div' );
  79. document.body.appendChild( container );
  80. // CAMERA
  81. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1500 );
  82. camera.position.set( 0, 4, 7 );
  83. camera.lookAt( new THREE.Vector3() );
  84. // SCENE
  85. scene = new THREE.Scene();
  86. //
  87. group = new THREE.Group();
  88. scene.add( group );
  89. group.add( new THREE.GridHelper( 4, 12 ) );
  90. cubes = new THREE.Group();
  91. group.add( cubes );
  92. // RENDERER
  93. renderer = new THREE.WebGLRenderer( { antialias: true } );
  94. renderer.setPixelRatio( window.devicePixelRatio );
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. container.appendChild( renderer.domElement );
  97. // TESTS
  98. setTimeout( addImage, 300 );
  99. setTimeout( addImage, 600 );
  100. setTimeout( addImage, 900 );
  101. setTimeout( addImageBitmap, 1300 );
  102. setTimeout( addImageBitmap, 1600 );
  103. setTimeout( addImageBitmap, 1900 );
  104. // EVENTS
  105. window.addEventListener( 'resize', onWindowResize, false );
  106. }
  107. function onWindowResize() {
  108. camera.aspect = window.innerWidth / window.innerHeight;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. }
  112. function animate() {
  113. group.rotation.y = performance.now() / 3000;
  114. renderer.render( scene, camera );
  115. requestAnimationFrame( animate );
  116. }
  117. </script>
  118. </body>
  119. </html>