webgl_materials_blending.html 4.6 KB

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