webgl_loader_imagebitmap.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. {
  10. font-family: Monospace;
  11. background-color: #000;
  12. color: #fff;
  13. margin: 0px;
  14. overflow: hidden;
  15. }
  16. #info
  17. {
  18. position: absolute;
  19. top: 10px;
  20. width: 100%;
  21. text-align: center;
  22. z-index: 100;
  23. display:block;
  24. }
  25. #info a, .button
  26. {
  27. color: #f00;
  28. font-weight: bold;
  29. text-decoration: underline;
  30. cursor: pointer
  31. }
  32. .actions{
  33. padding: 2em;
  34. line-height: 0;
  35. }
  36. .actions span{
  37. line-height: 4em;
  38. padding: 1em;
  39. border: 1px solid white;
  40. cursor: pointer;
  41. white-space: nowrap;
  42. user-select: none;
  43. }
  44. .actions span:hover{
  45. color: red;
  46. border-color: red;
  47. }
  48. .actions span:active{
  49. color: black;
  50. background-color: red;
  51. border-color: red;
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <script src="../build/three.js"></script>
  57. <script src="js/Detector.js"></script>
  58. <script src="js/loaders/ImageBitmapLoader.js"></script>
  59. <div id="info">
  60. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Texture loader using ImageBitmap
  61. <div class="actions">
  62. <span id="add_ib_btn" >Press to add ImageBitmap</span>
  63. <span id="add_i_btn">Press to add Image</span>
  64. <span id="clear_btn" >Clear</span>
  65. </div>
  66. </div>
  67. <script>
  68. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  69. THREE.Cache.enabled = true;
  70. var container;
  71. var camera, scene, renderer;
  72. var group, cubes;
  73. var autoAddImageBitmap = false;
  74. var autoAddImage = false;
  75. init();
  76. animate();
  77. function addImageBitmap () {
  78. new THREE.ImageBitmapLoader()
  79. .setOptions( { imageOrientation: 'none' } )
  80. .load ( 'textures/planets/earth_atmos_2048.jpg', function( imageBitmap ) {
  81. var tex = new THREE.Texture ( imageBitmap );
  82. tex.needsUpdate = true;
  83. /* ImageBitmap should be disposed when done with it
  84. Can't be done until it's actually uploaded to WebGLTexture */
  85. // imageBitmap.close();
  86. addCube( tex );
  87. }, function( p ) {
  88. console.log( p );
  89. }, function( e ) {
  90. console.log( e );
  91. } );
  92. }
  93. function addImage () {
  94. new THREE.TextureLoader().load( 'textures/planets/earth_atmos_2048.jpg?' + performance.now(), function( tex ) {
  95. addCube( tex );
  96. });
  97. }
  98. function addCube ( texture ) {
  99. var cube = new THREE.Mesh(
  100. new THREE.BoxBufferGeometry( 1,1,1 ),
  101. new THREE.MeshBasicMaterial( {
  102. map: texture
  103. })
  104. );
  105. cube.position.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  106. cube.rotation.set( Math.random() * 2 * Math.PI, Math.random() * 2 * Math.PI, Math.random() * 2 * Math.PI );
  107. cubes.add( cube );
  108. }
  109. function init() {
  110. container = document.createElement( 'div' );
  111. document.body.appendChild( container );
  112. // CAMERA
  113. camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1500 );
  114. camera.position.set( 0, 4, 7 );
  115. camera.lookAt( new THREE.Vector3() );
  116. // SCENE
  117. scene = new THREE.Scene();
  118. //
  119. group = new THREE.Group();
  120. scene.add( group );
  121. group.add( new THREE.GridHelper( 4, 12 ) );
  122. cubes = new THREE.Group();
  123. group.add( cubes );
  124. addImageBitmap();
  125. addImage();
  126. var ibBtn = document.getElementById( 'add_ib_btn' );
  127. ibBtn.addEventListener( 'click', function( e ) {
  128. addImageBitmap();
  129. e.preventDefault();
  130. } );
  131. var iBtn = document.getElementById( 'add_i_btn' );
  132. iBtn.addEventListener( 'click', function( e ) {
  133. addImage();
  134. e.preventDefault();
  135. } );
  136. var clearBtn = document.getElementById( 'clear_btn' );
  137. clearBtn.addEventListener( 'click', function( e ) {
  138. while( cubes.children.length ) {
  139. cubes.remove( cubes.children[ 0 ] );
  140. }
  141. })
  142. // RENDERER
  143. renderer = new THREE.WebGLRenderer( { antialias: true } );
  144. renderer.setPixelRatio( window.devicePixelRatio );
  145. renderer.setSize( window.innerWidth, window.innerHeight );
  146. container.appendChild( renderer.domElement );
  147. // EVENTS
  148. window.addEventListener( 'resize', onWindowResize, false );
  149. }
  150. function onWindowResize() {
  151. camera.aspect = window.innerWidth / window.innerHeight;
  152. camera.updateProjectionMatrix();
  153. renderer.setSize( window.innerWidth, window.innerHeight );
  154. }
  155. function animate() {
  156. requestAnimationFrame( animate );
  157. group.rotation.y = performance.now() / 2000;
  158. renderer.render( scene, camera );
  159. }
  160. </script>
  161. </body>
  162. </html>