webgl_loader_imagebitmap.html 3.9 KB

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