webgl_loader_imagebitmap.html 4.6 KB

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