webgl_materials_blending.html 4.6 KB

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