webgl_materials_blending.html 4.9 KB

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