123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - materials - blending</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 {
- margin: 0px;
- background-color: #000000;
- overflow: hidden;
- }
- </style>
- </head>
- <body>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- var camera, scene, renderer;
- var mapBg;
- var textureLoader = new THREE.TextureLoader();
- init();
- animate();
- function init() {
- // CAMERA
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.z = 600;
- // SCENE
- scene = new THREE.Scene();
- // BACKGROUND
- var canvas = document.createElement( 'canvas' );
- var ctx = canvas.getContext( '2d' );
- canvas.width = canvas.height = 128;
- ctx.fillStyle = '#ddd';
- ctx.fillRect( 0, 0, 128, 128 );
- ctx.fillStyle = '#555';
- ctx.fillRect( 0, 0, 64, 64 );
- ctx.fillStyle = '#999';
- ctx.fillRect( 32, 32, 32, 32 );
- ctx.fillStyle = '#555';
- ctx.fillRect( 64, 64, 64, 64 );
- ctx.fillStyle = '#777';
- ctx.fillRect( 96, 96, 32, 32 );
- mapBg = new THREE.CanvasTexture( canvas );
- mapBg.wrapS = mapBg.wrapT = THREE.RepeatWrapping;
- mapBg.repeat.set( 128, 64 );
- /*
- var mapBg = textureLoader.load( 'textures/disturb.jpg' );
- mapBg.wrapS = mapBg.wrapT = THREE.RepeatWrapping;
- mapBg.repeat.set( 8, 4 );
- */
- var materialBg = new THREE.MeshBasicMaterial( { map: mapBg } );
- var meshBg = new THREE.Mesh( new THREE.PlaneBufferGeometry( 4000, 2000 ), materialBg );
- meshBg.position.set( 0, 0, - 1 );
- scene.add( meshBg );
- // OBJECTS
- var blendings = [
- { name: 'No', constant: THREE.NoBlending },
- { name: 'Normal', constant: THREE.NormalBlending },
- { name: 'Additive', constant: THREE.AdditiveBlending },
- { name: 'Subtractive', constant: THREE.SubtractiveBlending },
- { name: 'Multiply', constant: THREE.MultiplyBlending }
- ];
- var map0 = textureLoader.load( 'textures/uv_grid_opengl.jpg' );
- var map1 = textureLoader.load( 'textures/sprite0.jpg' );
- var map2 = textureLoader.load( 'textures/sprite0.png' );
- var map3 = textureLoader.load( 'textures/lensflare/lensflare0.png' );
- var map4 = textureLoader.load( 'textures/lensflare/lensflare0_alpha.png' );
- var geo1 = new THREE.PlaneBufferGeometry( 100, 100 );
- var geo2 = new THREE.PlaneBufferGeometry( 100, 25 );
- addImageRow( map0, 300 );
- addImageRow( map1, 150 );
- addImageRow( map2, 0 );
- addImageRow( map3, - 150 );
- addImageRow( map4, - 300 );
- function addImageRow( map, y ) {
- for ( var i = 0; i < blendings.length; i ++ ) {
- var blending = blendings[ i ];
- var material = new THREE.MeshBasicMaterial( { map: map } );
- material.transparent = true;
- material.blending = blending.constant;
- var x = ( i - blendings.length / 2 ) * 110;
- var z = 0;
- var mesh = new THREE.Mesh( geo1, material );
- mesh.position.set( x, y, z );
- scene.add( mesh );
- var mesh = new THREE.Mesh( geo2, generateLabelMaterial( blending.name ) );
- mesh.position.set( x, y - 75, z );
- scene.add( mesh );
- }
- }
- // RENDERER
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- // EVENTS
- window.addEventListener( 'resize', onWindowResize, false );
- }
- //
- function onWindowResize() {
- var SCREEN_WIDTH = window.innerWidth;
- var SCREEN_HEIGHT = window.innerHeight;
- renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
- camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
- camera.updateProjectionMatrix();
- }
- function generateLabelMaterial( text ) {
- var canvas = document.createElement( 'canvas' );
- var ctx = canvas.getContext( '2d' );
- canvas.width = 128;
- canvas.height = 32;
- ctx.fillStyle = 'rgba( 0, 0, 0, 0.95 )';
- ctx.fillRect( 0, 0, 128, 32 );
- ctx.fillStyle = 'white';
- ctx.font = '12pt arial bold';
- ctx.fillText( text, 10, 22 );
- var map = new THREE.CanvasTexture( canvas );
- var material = new THREE.MeshBasicMaterial( { map: map, transparent: true } );
- return material;
- }
- function animate() {
- requestAnimationFrame( animate );
- var time = Date.now() * 0.00025;
- var ox = ( time * - 0.01 * mapBg.repeat.x ) % 1;
- var oy = ( time * - 0.01 * mapBg.repeat.y ) % 1;
- mapBg.offset.set( ox, oy );
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|