webgl_materials_blending.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - blending</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. margin: 0px;
  10. background-color: #000000;
  11. overflow: hidden;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <script type="module">
  17. import * as THREE from '../build/three.module.js';
  18. var camera, scene, renderer;
  19. var mapBg;
  20. var textureLoader = new THREE.TextureLoader();
  21. init();
  22. animate();
  23. function init() {
  24. // CAMERA
  25. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  26. camera.position.z = 600;
  27. // SCENE
  28. scene = new THREE.Scene();
  29. // BACKGROUND
  30. var canvas = document.createElement( 'canvas' );
  31. var ctx = canvas.getContext( '2d' );
  32. canvas.width = canvas.height = 128;
  33. ctx.fillStyle = '#ddd';
  34. ctx.fillRect( 0, 0, 128, 128 );
  35. ctx.fillStyle = '#555';
  36. ctx.fillRect( 0, 0, 64, 64 );
  37. ctx.fillStyle = '#999';
  38. ctx.fillRect( 32, 32, 32, 32 );
  39. ctx.fillStyle = '#555';
  40. ctx.fillRect( 64, 64, 64, 64 );
  41. ctx.fillStyle = '#777';
  42. ctx.fillRect( 96, 96, 32, 32 );
  43. mapBg = new THREE.CanvasTexture( canvas );
  44. mapBg.wrapS = mapBg.wrapT = THREE.RepeatWrapping;
  45. mapBg.repeat.set( 128, 64 );
  46. /*
  47. var mapBg = textureLoader.load( 'textures/disturb.jpg' );
  48. mapBg.wrapS = mapBg.wrapT = THREE.RepeatWrapping;
  49. mapBg.repeat.set( 8, 4 );
  50. */
  51. var materialBg = new THREE.MeshBasicMaterial( { map: mapBg } );
  52. var meshBg = new THREE.Mesh( new THREE.PlaneBufferGeometry( 4000, 2000 ), materialBg );
  53. meshBg.position.set( 0, 0, - 1 );
  54. scene.add( meshBg );
  55. // OBJECTS
  56. var blendings = [
  57. { name: 'No', constant: THREE.NoBlending },
  58. { name: 'Normal', constant: THREE.NormalBlending },
  59. { name: 'Additive', constant: THREE.AdditiveBlending },
  60. { name: 'Subtractive', constant: THREE.SubtractiveBlending },
  61. { name: 'Multiply', constant: THREE.MultiplyBlending }
  62. ];
  63. var map0 = textureLoader.load( 'textures/uv_grid_opengl.jpg' );
  64. var map1 = textureLoader.load( 'textures/sprite0.jpg' );
  65. var map2 = textureLoader.load( 'textures/sprite0.png' );
  66. var map3 = textureLoader.load( 'textures/lensflare/lensflare0.png' );
  67. var map4 = textureLoader.load( 'textures/lensflare/lensflare0_alpha.png' );
  68. var geo1 = new THREE.PlaneBufferGeometry( 100, 100 );
  69. var geo2 = new THREE.PlaneBufferGeometry( 100, 25 );
  70. addImageRow( map0, 300 );
  71. addImageRow( map1, 150 );
  72. addImageRow( map2, 0 );
  73. addImageRow( map3, - 150 );
  74. addImageRow( map4, - 300 );
  75. function addImageRow( map, y ) {
  76. for ( var i = 0; i < blendings.length; i ++ ) {
  77. var blending = blendings[ i ];
  78. var material = new THREE.MeshBasicMaterial( { map: map } );
  79. material.transparent = true;
  80. material.blending = blending.constant;
  81. var x = ( i - blendings.length / 2 ) * 110;
  82. var z = 0;
  83. var mesh = new THREE.Mesh( geo1, material );
  84. mesh.position.set( x, y, z );
  85. scene.add( mesh );
  86. var mesh = new THREE.Mesh( geo2, generateLabelMaterial( blending.name ) );
  87. mesh.position.set( x, y - 75, z );
  88. scene.add( mesh );
  89. }
  90. }
  91. // RENDERER
  92. renderer = new THREE.WebGLRenderer();
  93. renderer.setPixelRatio( window.devicePixelRatio );
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. document.body.appendChild( renderer.domElement );
  96. // EVENTS
  97. window.addEventListener( 'resize', onWindowResize, false );
  98. }
  99. //
  100. function onWindowResize() {
  101. var SCREEN_WIDTH = window.innerWidth;
  102. var SCREEN_HEIGHT = window.innerHeight;
  103. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  104. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  105. camera.updateProjectionMatrix();
  106. }
  107. function generateLabelMaterial( text ) {
  108. var canvas = document.createElement( 'canvas' );
  109. var ctx = canvas.getContext( '2d' );
  110. canvas.width = 128;
  111. canvas.height = 32;
  112. ctx.fillStyle = 'rgba( 0, 0, 0, 0.95 )';
  113. ctx.fillRect( 0, 0, 128, 32 );
  114. ctx.fillStyle = 'white';
  115. ctx.font = '12pt arial bold';
  116. ctx.fillText( text, 10, 22 );
  117. var map = new THREE.CanvasTexture( canvas );
  118. var material = new THREE.MeshBasicMaterial( { map: map, transparent: true } );
  119. return material;
  120. }
  121. function animate() {
  122. requestAnimationFrame( animate );
  123. var time = Date.now() * 0.00025;
  124. var ox = ( time * - 0.01 * mapBg.repeat.x ) % 1;
  125. var oy = ( time * - 0.01 * mapBg.repeat.y ) % 1;
  126. mapBg.offset.set( ox, oy );
  127. renderer.render( scene, camera );
  128. }
  129. </script>
  130. </body>
  131. </html>