123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - loader - ImageBitmap</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <style>
- body {
- font-family: Monospace;
- background-color: #000;
- color: #fff;
- margin: 0px;
- overflow: hidden;
- }
- #info {
- position: absolute;
- top: 10px;
- width: 100%;
- text-align: center;
- z-index: 100;
- display:block;
- }
- #info a, .button {
- color: #f00;
- font-weight: bold;
- text-decoration: underline;
- cursor: pointer
- }
- </style>
- </head>
- <body>
- <script src="../build/three.js"></script>
- <script src="js/Detector.js"></script>
- <div id="info">
- <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Texture loader using ImageBitmap
- </div>
- <script>
- if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
- var container;
- var camera, scene, renderer;
- var group, cubes;
- init();
- animate();
- function addImageBitmap() {
- new THREE.ImageBitmapLoader()
- .setOptions( { imageOrientation: 'none' } )
- .load( 'textures/planets/earth_atmos_2048.jpg?' + performance.now(), function ( imageBitmap ) {
- var texture = new THREE.CanvasTexture( imageBitmap );
- var material = new THREE.MeshBasicMaterial( { map: texture } );
- /* ImageBitmap should be disposed when done with it
- Can't be done until it's actually uploaded to WebGLTexture */
- // imageBitmap.close();
- addCube( material );
- }, function( p ) {
- console.log( p );
- }, function( e ) {
- console.log( e );
- } );
- }
- function addImage() {
- new THREE.ImageLoader()
- .setCrossOrigin( '*' )
- .load( 'textures/planets/earth_atmos_2048.jpg?' + performance.now(), function ( image ) {
- var texture = new THREE.CanvasTexture( image );
- var material = new THREE.MeshBasicMaterial( { color: 0xff8888, map: texture } );
- addCube( material );
- });
- }
- var geometry = new THREE.BoxBufferGeometry( 1,1,1 );
- function addCube( material ) {
- var cube = new THREE.Mesh( geometry, material );
- cube.position.set( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
- cube.rotation.set( Math.random() * 2 * Math.PI, Math.random() * 2 * Math.PI, Math.random() * 2 * Math.PI );
- cubes.add( cube );
- }
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- // CAMERA
- camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 1500 );
- camera.position.set( 0, 4, 7 );
- camera.lookAt( new THREE.Vector3() );
- // SCENE
- scene = new THREE.Scene();
- //
- group = new THREE.Group();
- scene.add( group );
- group.add( new THREE.GridHelper( 4, 12 ) );
- cubes = new THREE.Group();
- group.add( cubes );
- // RENDERER
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- container.appendChild( renderer.domElement );
- // TESTS
- setTimeout( addImage, 300 );
- setTimeout( addImage, 600 );
- setTimeout( addImage, 900 );
- setTimeout( addImageBitmap, 1300 );
- setTimeout( addImageBitmap, 1600 );
- setTimeout( addImageBitmap, 1900 );
- // EVENTS
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- group.rotation.y = performance.now() / 3000;
- renderer.render( scene, camera );
- requestAnimationFrame( animate );
- }
- </script>
- </body>
- </html>
|