webgl_materials_blending_custom.html 6.9 KB

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