webgl_materials_blending.html 4.8 KB

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