webgl_materials_blending_custom.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - custom 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. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  21. let camera, scene, renderer;
  22. let mapBg;
  23. const materials = [];
  24. const params = {
  25. blendEquation: THREE.AddEquation
  26. };
  27. const equations = { Add: THREE.AddEquation, Subtract: THREE.SubtractEquation, ReverseSubtract: THREE.ReverseSubtractEquation, Min: THREE.MinEquation, Max: THREE.MaxEquation };
  28. init();
  29. function init() {
  30. // CAMERA
  31. camera = new THREE.PerspectiveCamera( 80, window.innerWidth / window.innerHeight, 1, 1000 );
  32. camera.position.z = 700;
  33. // SCENE
  34. scene = new THREE.Scene();
  35. // BACKGROUND
  36. const canvas = document.createElement( 'canvas' );
  37. const ctx = canvas.getContext( '2d' );
  38. canvas.width = canvas.height = 128;
  39. ctx.fillStyle = '#ddd';
  40. ctx.fillRect( 0, 0, 128, 128 );
  41. ctx.fillStyle = '#555';
  42. ctx.fillRect( 0, 0, 64, 64 );
  43. ctx.fillStyle = '#999';
  44. ctx.fillRect( 32, 32, 32, 32 );
  45. ctx.fillStyle = '#555';
  46. ctx.fillRect( 64, 64, 64, 64 );
  47. ctx.fillStyle = '#777';
  48. ctx.fillRect( 96, 96, 32, 32 );
  49. mapBg = new THREE.CanvasTexture( canvas );
  50. mapBg.colorSpace = THREE.SRGBColorSpace;
  51. mapBg.wrapS = mapBg.wrapT = THREE.RepeatWrapping;
  52. mapBg.repeat.set( 64, 32 );
  53. scene.background = mapBg;
  54. // FOREGROUND OBJECTS
  55. const src = [
  56. { name: 'Zero', constant: THREE.ZeroFactor },
  57. { name: 'One', constant: THREE.OneFactor },
  58. { name: 'SrcColor', constant: THREE.SrcColorFactor },
  59. { name: 'OneMinusSrcColor', constant: THREE.OneMinusSrcColorFactor },
  60. { name: 'SrcAlpha', constant: THREE.SrcAlphaFactor },
  61. { name: 'OneMinusSrcAlpha', constant: THREE.OneMinusSrcAlphaFactor },
  62. { name: 'DstAlpha', constant: THREE.DstAlphaFactor },
  63. { name: 'OneMinusDstAlpha', constant: THREE.OneMinusDstAlphaFactor },
  64. { name: 'DstColor', constant: THREE.DstColorFactor },
  65. { name: 'OneMinusDstColor', constant: THREE.OneMinusDstColorFactor },
  66. { name: 'SrcAlphaSaturate', constant: THREE.SrcAlphaSaturateFactor }
  67. ];
  68. const dst = [
  69. { name: 'Zero', constant: THREE.ZeroFactor },
  70. { name: 'One', constant: THREE.OneFactor },
  71. { name: 'SrcColor', constant: THREE.SrcColorFactor },
  72. { name: 'OneMinusSrcColor', constant: THREE.OneMinusSrcColorFactor },
  73. { name: 'SrcAlpha', constant: THREE.SrcAlphaFactor },
  74. { name: 'OneMinusSrcAlpha', constant: THREE.OneMinusSrcAlphaFactor },
  75. { name: 'DstAlpha', constant: THREE.DstAlphaFactor },
  76. { name: 'OneMinusDstAlpha', constant: THREE.OneMinusDstAlphaFactor },
  77. { name: 'DstColor', constant: THREE.DstColorFactor },
  78. { name: 'OneMinusDstColor', constant: THREE.OneMinusDstColorFactor }
  79. ];
  80. const geo1 = new THREE.PlaneGeometry( 100, 100 );
  81. const geo2 = new THREE.PlaneGeometry( 100, 25 );
  82. const texture = new THREE.TextureLoader().load( 'textures/lensflare/lensflare0_alpha.png' );
  83. texture.colorSpace = THREE.SRGBColorSpace;
  84. for ( let i = 0; i < dst.length; i ++ ) {
  85. const blendDst = dst[ i ];
  86. for ( let j = 0; j < src.length; j ++ ) {
  87. const blendSrc = src[ j ];
  88. const material = new THREE.MeshBasicMaterial( { map: texture } );
  89. material.transparent = true;
  90. material.blending = THREE.CustomBlending;
  91. material.blendSrc = blendSrc.constant;
  92. material.blendDst = blendDst.constant;
  93. material.blendEquation = THREE.AddEquation;
  94. const x = ( j - src.length / 2 ) * 110;
  95. const z = 0;
  96. const y = ( i - dst.length / 2 ) * 110 + 50;
  97. const mesh = new THREE.Mesh( geo1, material );
  98. mesh.position.set( x, - y, z );
  99. mesh.matrixAutoUpdate = false;
  100. mesh.updateMatrix();
  101. scene.add( mesh );
  102. materials.push( material );
  103. }
  104. }
  105. for ( let j = 0; j < src.length; j ++ ) {
  106. const blendSrc = src[ j ];
  107. const x = ( j - src.length / 2 ) * 110;
  108. const z = 0;
  109. const y = ( 0 - dst.length / 2 ) * 110 + 50;
  110. const mesh = new THREE.Mesh( geo2, generateLabelMaterial( blendSrc.name, 'rgba( 0, 150, 0, 1 )' ) );
  111. mesh.position.set( x, - ( y - 70 ), z );
  112. mesh.matrixAutoUpdate = false;
  113. mesh.updateMatrix();
  114. scene.add( mesh );
  115. }
  116. for ( let i = 0; i < dst.length; i ++ ) {
  117. const blendDst = dst[ i ];
  118. const x = ( 0 - src.length / 2 ) * 110 - 125;
  119. const z = 0;
  120. const y = ( i - dst.length / 2 ) * 110 + 165;
  121. const mesh = new THREE.Mesh( geo2, generateLabelMaterial( blendDst.name, 'rgba( 150, 0, 0, 1 )' ) );
  122. mesh.position.set( x, - ( y - 120 ), z );
  123. mesh.matrixAutoUpdate = false;
  124. mesh.updateMatrix();
  125. scene.add( mesh );
  126. }
  127. // RENDERER
  128. renderer = new THREE.WebGLRenderer();
  129. renderer.setPixelRatio( window.devicePixelRatio );
  130. renderer.setSize( window.innerWidth, window.innerHeight );
  131. renderer.setAnimationLoop( animate );
  132. document.body.appendChild( renderer.domElement );
  133. // EVENTS
  134. window.addEventListener( 'resize', onWindowResize );
  135. // GUI
  136. //
  137. const gui = new GUI( { width: 300 } );
  138. gui.add( params, 'blendEquation', equations ).onChange( updateBlendEquation );
  139. gui.open();
  140. }
  141. //
  142. function onWindowResize() {
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. camera.aspect = window.innerWidth / window.innerHeight;
  145. camera.updateProjectionMatrix();
  146. }
  147. //
  148. function generateLabelMaterial( text, bg ) {
  149. const canvas = document.createElement( 'canvas' );
  150. const ctx = canvas.getContext( '2d' );
  151. canvas.width = 128;
  152. canvas.height = 32;
  153. ctx.fillStyle = bg;
  154. ctx.fillRect( 0, 0, 128, 32 );
  155. ctx.fillStyle = 'white';
  156. ctx.font = 'bold 11pt arial';
  157. ctx.fillText( text, 8, 22 );
  158. const map = new THREE.CanvasTexture( canvas );
  159. map.colorSpace = THREE.SRGBColorSpace;
  160. const material = new THREE.MeshBasicMaterial( { map: map, transparent: true } );
  161. return material;
  162. }
  163. function updateBlendEquation( value ) {
  164. for ( const material of materials ) {
  165. material.blendEquation = value;
  166. }
  167. }
  168. function animate() {
  169. const time = Date.now() * 0.00025;
  170. const ox = ( time * - 0.01 * mapBg.repeat.x ) % 1;
  171. const oy = ( time * - 0.01 * mapBg.repeat.y ) % 1;
  172. mapBg.offset.set( ox, oy );
  173. renderer.render( scene, camera );
  174. }
  175. </script>
  176. </body>
  177. </html>