webgl_loader_imagebitmap.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. <div id="info">
  35. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Texture loader using ImageBitmap
  36. </div>
  37. <script>
  38. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  39. var container;
  40. var camera, scene, renderer;
  41. var group, cubes;
  42. init();
  43. animate();
  44. function addImageBitmap() {
  45. new THREE.ImageBitmapLoader()
  46. .setOptions( { imageOrientation: 'none' } )
  47. .load( 'textures/planets/earth_atmos_2048.jpg?' + performance.now(), function ( imageBitmap ) {
  48. var texture = new THREE.CanvasTexture( imageBitmap );
  49. var material = new THREE.MeshBasicMaterial( { map: texture } );
  50. /* ImageBitmap should be disposed when done with it
  51. Can't be done until it's actually uploaded to WebGLTexture */
  52. // imageBitmap.close();
  53. addCube( material );
  54. }, function( p ) {
  55. console.log( p );
  56. }, function( e ) {
  57. console.log( e );
  58. } );
  59. }
  60. function addImage() {
  61. new THREE.ImageLoader()
  62. .setCrossOrigin( '*' )
  63. .load( 'textures/planets/earth_atmos_2048.jpg?' + performance.now(), function ( image ) {
  64. var texture = new THREE.CanvasTexture( image );
  65. var material = new THREE.MeshBasicMaterial( { color: 0xff8888, map: texture } );
  66. addCube( material );
  67. });
  68. }
  69. var geometry = new THREE.BoxBufferGeometry( 1,1,1 );
  70. function addCube( material ) {
  71. var cube = new THREE.Mesh( geometry, material );
  72. cube.position.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  73. cube.rotation.set( Math.random() * 2 * Math.PI, Math.random() * 2 * Math.PI, Math.random() * 2 * Math.PI );
  74. cubes.add( cube );
  75. }
  76. function init() {
  77. container = document.createElement( 'div' );
  78. document.body.appendChild( container );
  79. // CAMERA
  80. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1500 );
  81. camera.position.set( 0, 4, 7 );
  82. camera.lookAt( new THREE.Vector3() );
  83. // SCENE
  84. scene = new THREE.Scene();
  85. //
  86. group = new THREE.Group();
  87. scene.add( group );
  88. group.add( new THREE.GridHelper( 4, 12 ) );
  89. cubes = new THREE.Group();
  90. group.add( cubes );
  91. // RENDERER
  92. renderer = new THREE.WebGLRenderer( { antialias: true } );
  93. renderer.setPixelRatio( window.devicePixelRatio );
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. container.appendChild( renderer.domElement );
  96. // TESTS
  97. setTimeout( addImage, 300 );
  98. setTimeout( addImage, 600 );
  99. setTimeout( addImage, 900 );
  100. setTimeout( addImageBitmap, 1300 );
  101. setTimeout( addImageBitmap, 1600 );
  102. setTimeout( addImageBitmap, 1900 );
  103. // EVENTS
  104. window.addEventListener( 'resize', onWindowResize, false );
  105. }
  106. function onWindowResize() {
  107. camera.aspect = window.innerWidth / window.innerHeight;
  108. camera.updateProjectionMatrix();
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. }
  111. function animate() {
  112. group.rotation.y = performance.now() / 3000;
  113. renderer.render( scene, camera );
  114. requestAnimationFrame( animate );
  115. }
  116. </script>
  117. </body>
  118. </html>