123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - materials - manual mipmaping</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- .lbl { color:#fff; font-size:16px; font-weight:bold; position: absolute; bottom:0px; z-index:100; text-shadow:#000 1px 1px 1px; background-color:rgba(0,0,0,0.85); padding:1em }
- #lbl_left { text-align:left; left:0px }
- #lbl_right { text-align:left; right:0px }
- .g { color:#aaa }
- .c { color:#fa0 }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - texture manual mipmapping example<br/>
- painting by <a href="http://en.wikipedia.org/wiki/Basket_of_Fruit_%28Caravaggio%29">Caravaggio</a>
- </div>
- <div id="lbl_left" class="lbl">
- Floor <span class="g">(128x128)</span><br/>
- mag: <span class="c">Linear</span><br/>
- min: <span class="c">LinearMipmapLinear</span><br/>
- <br/>
- Painting <span class="g">(748x600)</span><br/>
- mag: <span class="c">Linear</span><br/>
- min: <span class="c">Linear</span>
- </div>
- <div id="lbl_right" class="lbl">
- Floor <br/>
- mag: <span class="c">Nearest</span><br/>
- min: <span class="c">NearestMipmapNearestFilter</span><br/>
- <br/>
- Painting <br/>
- mag: <span class="c">Nearest</span><br/>
- min: <span class="c">Nearest</span>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- var SCREEN_WIDTH = window.innerWidth;
- var SCREEN_HEIGHT = window.innerHeight;
- var container;
- var camera, scene1, scene2, renderer;
- var mouseX = 0, mouseY = 0;
- var windowHalfX = window.innerWidth / 2;
- var windowHalfY = window.innerHeight / 2;
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 35, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 5000 );
- camera.position.z = 1500;
- scene1 = new THREE.Scene();
- scene1.background = new THREE.Color( 0x000000 );
- scene1.fog = new THREE.Fog( 0x000000, 1500, 4000 );
- scene2 = new THREE.Scene();
- scene2.background = new THREE.Color( 0x000000 );
- scene2.fog = new THREE.Fog( 0x000000, 1500, 4000 );
- // GROUND
- function mipmap( size, color ) {
- var imageCanvas = document.createElement( "canvas" ),
- context = imageCanvas.getContext( "2d" );
- imageCanvas.width = imageCanvas.height = size;
- context.fillStyle = "#444";
- context.fillRect( 0, 0, size, size );
- context.fillStyle = color;
- context.fillRect( 0, 0, size / 2, size / 2 );
- context.fillRect( size / 2, size / 2, size / 2, size / 2 );
- return imageCanvas;
- }
- var canvas = mipmap( 128, '#f00' );
- var textureCanvas1 = new THREE.CanvasTexture( canvas );
- textureCanvas1.mipmaps[ 0 ] = canvas;
- textureCanvas1.mipmaps[ 1 ] = mipmap( 64, '#0f0' );
- textureCanvas1.mipmaps[ 2 ] = mipmap( 32, '#00f' );
- textureCanvas1.mipmaps[ 3 ] = mipmap( 16, '#400' );
- textureCanvas1.mipmaps[ 4 ] = mipmap( 8, '#040' );
- textureCanvas1.mipmaps[ 5 ] = mipmap( 4, '#004' );
- textureCanvas1.mipmaps[ 6 ] = mipmap( 2, '#044' );
- textureCanvas1.mipmaps[ 7 ] = mipmap( 1, '#404' );
- textureCanvas1.repeat.set( 1000, 1000 );
- textureCanvas1.wrapS = THREE.RepeatWrapping;
- textureCanvas1.wrapT = THREE.RepeatWrapping;
- var textureCanvas2 = textureCanvas1.clone();
- textureCanvas2.magFilter = THREE.NearestFilter;
- textureCanvas2.minFilter = THREE.NearestMipmapNearestFilter;
- var materialCanvas1 = new THREE.MeshBasicMaterial( { map: textureCanvas1 } );
- var materialCanvas2 = new THREE.MeshBasicMaterial( { color: 0xffccaa, map: textureCanvas2 } );
- var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
- var meshCanvas1 = new THREE.Mesh( geometry, materialCanvas1 );
- meshCanvas1.rotation.x = - Math.PI / 2;
- meshCanvas1.scale.set( 1000, 1000, 1000 );
- var meshCanvas2 = new THREE.Mesh( geometry, materialCanvas2 );
- meshCanvas2.rotation.x = - Math.PI / 2;
- meshCanvas2.scale.set( 1000, 1000, 1000 );
- // PAINTING
- var callbackPainting = function () {
- var image = texturePainting1.image;
- texturePainting2.image = image;
- texturePainting2.needsUpdate = true;
- scene1.add( meshCanvas1 );
- scene2.add( meshCanvas2 );
- var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
- var mesh1 = new THREE.Mesh( geometry, materialPainting1 );
- var mesh2 = new THREE.Mesh( geometry, materialPainting2 );
- addPainting( scene1, mesh1 );
- addPainting( scene2, mesh2 );
- function addPainting( zscene, zmesh ) {
- zmesh.scale.x = image.width / 100;
- zmesh.scale.y = image.height / 100;
- zscene.add( zmesh );
- var meshFrame = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000 } ) );
- meshFrame.position.z = - 10.0;
- meshFrame.scale.x = 1.1 * image.width / 100;
- meshFrame.scale.y = 1.1 * image.height / 100;
- zscene.add( meshFrame );
- var meshShadow = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: 0x000000, opacity: 0.75, transparent: true } ) );
- meshShadow.position.y = - 1.1 * image.height / 2;
- meshShadow.position.z = - 1.1 * image.height / 2;
- meshShadow.rotation.x = - Math.PI / 2;
- meshShadow.scale.x = 1.1 * image.width / 100;
- meshShadow.scale.y = 1.1 * image.height / 100;
- zscene.add( meshShadow );
- var floorHeight = - 1.117 * image.height / 2;
- meshCanvas1.position.y = meshCanvas2.position.y = floorHeight;
- }
- };
- var texturePainting1 = new THREE.TextureLoader().load( "textures/758px-Canestra_di_frutta_(Caravaggio).jpg", callbackPainting );
- var texturePainting2 = new THREE.Texture();
- var materialPainting1 = new THREE.MeshBasicMaterial( { color: 0xffffff, map: texturePainting1 } );
- var materialPainting2 = new THREE.MeshBasicMaterial( { color: 0xffccaa, map: texturePainting2 } );
- texturePainting2.minFilter = texturePainting2.magFilter = THREE.NearestFilter;
- texturePainting1.minFilter = texturePainting1.magFilter = THREE.LinearFilter;
- texturePainting1.mapping = THREE.UVMapping;
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
- renderer.autoClear = false;
- renderer.domElement.style.position = "relative";
- container.appendChild( renderer.domElement );
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- }
- function onDocumentMouseMove( event ) {
- mouseX = ( event.clientX - windowHalfX );
- mouseY = ( event.clientY - windowHalfY );
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- }
- function render() {
- camera.position.x += ( mouseX - camera.position.x ) * .05;
- camera.position.y += ( - ( mouseY - 200 ) - camera.position.y ) * .05;
- camera.lookAt( scene1.position );
- renderer.clear();
- renderer.setScissorTest( true );
- renderer.setScissor( 0, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT );
- renderer.render( scene1, camera );
- renderer.setScissor( SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2 - 2, SCREEN_HEIGHT );
- renderer.render( scene2, camera );
- renderer.setScissorTest( false );
- }
- </script>
- </body>
- </html>
|