webgl_materials_blending_custom.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. <style>
  8. body {
  9. margin: 0px;
  10. background-color: #111;
  11. overflow: hidden;
  12. font-family: arial;
  13. }
  14. .menu { color: #fff; font-weight: bold; font-size: 12px; z-index: 100; width: 75px; position: absolute; top: 0px; padding: 16px; }
  15. .menu img, .menu canvas { width: 75px; margin: 10px 0 }
  16. #images { background: rgba(0,0,0,0); left: 0px; }
  17. #backgrounds { background: rgba(0,0,0,0.0); left: 107px; }
  18. #labels { background: rgba(0,0,0,0.75); left: 214px; width: 100px }
  19. .lbl { color: #fff; z-index: 150; float:left; padding: 0.25em; width: 75px; display: block }
  20. #lbl_dst { background:#800; }
  21. #lbl_src { background:green; }
  22. .btn { background: darkorange; width: 100px; cursor: pointer }
  23. #btn_sub { background: transparent }
  24. #btn_rsub { background: transparent }
  25. #btn_min { background: transparent }
  26. #btn_max { background: transparent }
  27. #btn_pre { background: transparent }
  28. #btn_max, #btn_nopre { margin-bottom: 2em }
  29. </style>
  30. </head>
  31. <body>
  32. <div id="images" class="menu">
  33. Foreground
  34. <a id="img_0" href="#"><img src="textures/disturb.jpg" /></a>
  35. <a id="img_1" href="#"><img src="textures/sprite0.jpg" /></a>
  36. <a id="img_2" href="#"><img src="textures/sprite0.png" /></a>
  37. <a id="img_3" href="#"><img src="textures/lensflare/lensflare0.png" /></a>
  38. <a id="img_4" href="#"><img src="textures/lensflare/lensflare0_alpha.png" /></a>
  39. <a id="img_5" href="#"><img src="textures/sprites/ball.png" /></a>
  40. <a id="img_6" href="#"><img src="textures/sprites/snowflake7_alpha.png" /></a>
  41. </div>
  42. <div id="backgrounds" class="menu">
  43. Background
  44. <a id="bg_0" href="#"><img src="textures/disturb.jpg" /></a>
  45. <a id="bg_1" href="#"></a>
  46. <a id="bg_2" href="#"></a>
  47. <a id="bg_3" href="#"><img src="textures/crate.gif" /></a>
  48. <a id="bg_4" href="#"><img src="textures/lava/lavatile.jpg" /></a>
  49. <a id="bg_5" href="#"><img src="textures/water.jpg" /></a>
  50. <a id="bg_6" href="#"><img src="textures/lava/cloud.png" /></a>
  51. </div>
  52. <div id="labels" class="menu">
  53. Equation<br/><br/>
  54. <div class="lbl btn" id="btn_add">Add</div>
  55. <div class="lbl btn" id="btn_sub">Subtract</div>
  56. <div class="lbl btn" id="btn_rsub">ReverseSubtract</div>
  57. <div class="lbl btn" id="btn_min">Min</div>
  58. <div class="lbl btn" id="btn_max">Max</div>
  59. Premultiply alpha<br/><br/>
  60. <div class="lbl btn" id="btn_pre">On</div>
  61. <div class="lbl btn" id="btn_nopre">Off</div>
  62. Labels<br/><br/>
  63. <div class="lbl" id="lbl_src">Source</div>
  64. <div class="lbl" id="lbl_dst">Destination</div>
  65. </div>
  66. <script type="module">
  67. import * as THREE from '../build/three.module.js';
  68. var camera, scene, renderer;
  69. var materialBg;
  70. var materials = [];
  71. var mapsPre = [];
  72. var mapsNoPre = [];
  73. var currentMaps = mapsNoPre;
  74. var currentIndex = 4;
  75. init();
  76. animate();
  77. function init() {
  78. // CAMERA
  79. camera = new THREE.PerspectiveCamera( 80, window.innerWidth / window.innerHeight, 1, 1000 );
  80. camera.position.z = 700;
  81. // SCENE
  82. scene = new THREE.Scene();
  83. // TEXTURE LOADER
  84. var textureLoader = new THREE.TextureLoader();
  85. // BACKGROUND IMAGES
  86. var canvas1 = document.createElement( 'canvas' );
  87. var ctx1 = canvas1.getContext( '2d' );
  88. canvas1.width = canvas1.height = 128;
  89. ctx1.fillStyle = '#eee';
  90. ctx1.fillRect( 0, 0, 128, 128 );
  91. ctx1.fillStyle = '#999';
  92. ctx1.fillRect( 0, 0, 64, 64 );
  93. ctx1.fillStyle = '#aaa';
  94. ctx1.fillRect( 32, 32, 32, 32 );
  95. ctx1.fillStyle = '#999';
  96. ctx1.fillRect( 64, 64, 64, 64 );
  97. ctx1.fillStyle = '#bbb';
  98. ctx1.fillRect( 96, 96, 32, 32 );
  99. document.getElementById( 'bg_1' ).appendChild( canvas1 );
  100. var canvas2 = document.createElement( 'canvas' );
  101. var ctx2 = canvas2.getContext( '2d' );
  102. canvas2.width = canvas2.height = 128;
  103. ctx2.fillStyle = '#444';
  104. ctx2.fillRect( 0, 0, 128, 128 );
  105. ctx2.fillStyle = '#000';
  106. ctx2.fillRect( 0, 0, 64, 64 );
  107. ctx2.fillStyle = '#111';
  108. ctx2.fillRect( 32, 32, 32, 32 );
  109. ctx2.fillStyle = '#000';
  110. ctx2.fillRect( 64, 64, 64, 64 );
  111. ctx2.fillStyle = '#222';
  112. ctx2.fillRect( 96, 96, 32, 32 );
  113. document.getElementById( 'bg_2' ).appendChild( canvas2 );
  114. var mapBg0 = new THREE.CanvasTexture( canvas1 );
  115. mapBg0.wrapS = mapBg0.wrapT = THREE.RepeatWrapping;
  116. mapBg0.repeat.set( 128, 64 );
  117. var mapBg1 = new THREE.CanvasTexture( canvas2 );
  118. mapBg1.wrapS = mapBg1.wrapT = THREE.RepeatWrapping;
  119. mapBg1.repeat.set( 128, 64 );
  120. var mapBg2 = textureLoader.load( 'textures/disturb.jpg' );
  121. mapBg2.wrapS = mapBg2.wrapT = THREE.RepeatWrapping;
  122. mapBg2.repeat.set( 8, 4 );
  123. var mapBg3 = textureLoader.load( 'textures/crate.gif' );
  124. mapBg3.wrapS = mapBg3.wrapT = THREE.RepeatWrapping;
  125. mapBg3.repeat.set( 32, 16 );
  126. var mapBg4 = textureLoader.load( 'textures/lava/lavatile.jpg' );
  127. mapBg4.wrapS = mapBg4.wrapT = THREE.RepeatWrapping;
  128. mapBg4.repeat.set( 8, 4 );
  129. var mapBg5 = textureLoader.load( 'textures/water.jpg' );
  130. mapBg5.wrapS = mapBg5.wrapT = THREE.RepeatWrapping;
  131. mapBg5.repeat.set( 8, 4 );
  132. var mapBg6 = textureLoader.load( 'textures/lava/cloud.png' );
  133. mapBg6.wrapS = mapBg6.wrapT = THREE.RepeatWrapping;
  134. mapBg6.repeat.set( 2, 1 );
  135. // BACKGROUND
  136. materialBg = new THREE.MeshBasicMaterial( { map: mapBg1 } );
  137. var meshBg = new THREE.Mesh( new THREE.PlaneBufferGeometry( 4000, 2000 ), materialBg );
  138. meshBg.position.set( 0, 0, - 1 );
  139. scene.add( meshBg );
  140. // FOREGROUND IMAGES
  141. var images = [ 'textures/disturb.jpg',
  142. 'textures/sprite0.jpg',
  143. 'textures/sprite0.png',
  144. 'textures/lensflare/lensflare0.png',
  145. 'textures/lensflare/lensflare0_alpha.png',
  146. 'textures/sprites/ball.png',
  147. 'textures/sprites/snowflake7_alpha.png' ];
  148. for ( var i = 0; i < images.length; i ++ ) {
  149. var map = textureLoader.load( images[ i ] );
  150. mapsNoPre.push( map );
  151. var mapPre = textureLoader.load( images[ i ] );
  152. mapPre.premultiplyAlpha = true;
  153. mapPre.needsUpdate = true;
  154. mapsPre.push( mapPre );
  155. }
  156. // FOREGROUND OBJECTS
  157. var src = [
  158. { name: 'Zero', constant: THREE.ZeroFactor },
  159. { name: 'One', constant: THREE.OneFactor },
  160. { name: 'SrcColor', constant: THREE.SrcColorFactor },
  161. { name: 'OneMinusSrcColor', constant: THREE.OneMinusSrcColorFactor },
  162. { name: 'SrcAlpha', constant: THREE.SrcAlphaFactor },
  163. { name: 'OneMinusSrcAlpha', constant: THREE.OneMinusSrcAlphaFactor },
  164. { name: 'DstAlpha', constant: THREE.DstAlphaFactor },
  165. { name: 'OneMinusDstAlpha', constant: THREE.OneMinusDstAlphaFactor },
  166. { name: 'DstColor', constant: THREE.DstColorFactor },
  167. { name: 'OneMinusDstColor', constant: THREE.OneMinusDstColorFactor },
  168. { name: 'SrcAlphaSaturate', constant: THREE.SrcAlphaSaturateFactor }
  169. ];
  170. var dst = [
  171. { name: 'Zero', constant: THREE.ZeroFactor },
  172. { name: 'One', constant: THREE.OneFactor },
  173. { name: 'SrcColor', constant: THREE.SrcColorFactor },
  174. { name: 'OneMinusSrcColor', constant: THREE.OneMinusSrcColorFactor },
  175. { name: 'SrcAlpha', constant: THREE.SrcAlphaFactor },
  176. { name: 'OneMinusSrcAlpha', constant: THREE.OneMinusSrcAlphaFactor },
  177. { name: 'DstAlpha', constant: THREE.DstAlphaFactor },
  178. { name: 'OneMinusDstAlpha', constant: THREE.OneMinusDstAlphaFactor },
  179. { name: 'DstColor', constant: THREE.DstColorFactor },
  180. { name: 'OneMinusDstColor', constant: THREE.OneMinusDstColorFactor }
  181. ];
  182. var geo1 = new THREE.PlaneBufferGeometry( 100, 100 );
  183. var geo2 = new THREE.PlaneBufferGeometry( 100, 25 );
  184. for ( var i = 0; i < dst.length; i ++ ) {
  185. var blendDst = dst[ i ];
  186. for ( var j = 0; j < src.length; j ++ ) {
  187. var blendSrc = src[ j ];
  188. var material = new THREE.MeshBasicMaterial( { map: currentMaps[ currentIndex ] } );
  189. material.transparent = true;
  190. material.blending = THREE.CustomBlending;
  191. material.blendSrc = blendSrc.constant;
  192. material.blendDst = blendDst.constant;
  193. material.blendEquation = THREE.AddEquation;
  194. var x = ( j - src.length / 2 ) * 110;
  195. var z = 0;
  196. var y = ( i - dst.length / 2 ) * 110 + 50;
  197. var mesh = new THREE.Mesh( geo1, material );
  198. mesh.position.set( x, - y, z );
  199. mesh.matrixAutoUpdate = false;
  200. mesh.updateMatrix();
  201. scene.add( mesh );
  202. materials.push( material );
  203. }
  204. }
  205. for ( var j = 0; j < src.length; j ++ ) {
  206. var blendSrc = src[ j ];
  207. var x = ( j - src.length / 2 ) * 110;
  208. var z = 0;
  209. var y = ( 0 - dst.length / 2 ) * 110 + 50;
  210. var mesh = new THREE.Mesh( geo2, generateLabelMaterial( blendSrc.name, 'rgba( 0, 150, 0, 1 )' ) );
  211. mesh.position.set( x, - ( y - 70 ), z );
  212. mesh.matrixAutoUpdate = false;
  213. mesh.updateMatrix();
  214. scene.add( mesh );
  215. }
  216. for ( var i = 0; i < dst.length; i ++ ) {
  217. var blendDst = dst[ i ];
  218. var x = ( 0 - src.length / 2 ) * 110 - 125;
  219. var z = 0;
  220. var y = ( i - dst.length / 2 ) * 110 + 165;
  221. var mesh = new THREE.Mesh( geo2, generateLabelMaterial( blendDst.name, 'rgba( 150, 0, 0, 1 )' ) );
  222. mesh.position.set( x, - ( y - 120 ), z );
  223. mesh.matrixAutoUpdate = false;
  224. mesh.updateMatrix();
  225. scene.add( mesh );
  226. }
  227. // RENDERER
  228. renderer = new THREE.WebGLRenderer();
  229. renderer.setPixelRatio( window.devicePixelRatio );
  230. renderer.setSize( window.innerWidth, window.innerHeight );
  231. renderer.domElement.style.position = 'absolute';
  232. renderer.domElement.style.left = '215px';
  233. document.body.appendChild( renderer.domElement );
  234. // EVENTS
  235. window.addEventListener( 'resize', onWindowResize, false );
  236. addImgHandler( 'img_0', 0 );
  237. addImgHandler( 'img_1', 1 );
  238. addImgHandler( 'img_2', 2 );
  239. addImgHandler( 'img_3', 3 );
  240. addImgHandler( 'img_4', 4 );
  241. addImgHandler( 'img_5', 5 );
  242. addImgHandler( 'img_6', 6 );
  243. addBgHandler( 'bg_0', mapBg2 );
  244. addBgHandler( 'bg_1', mapBg0 );
  245. addBgHandler( 'bg_2', mapBg1 );
  246. addBgHandler( 'bg_3', mapBg3 );
  247. addBgHandler( 'bg_4', mapBg4 );
  248. addBgHandler( 'bg_5', mapBg5 );
  249. addBgHandler( 'bg_6', mapBg6 );
  250. addEqHandler( 'btn_add', THREE.AddEquation );
  251. addEqHandler( 'btn_sub', THREE.SubtractEquation );
  252. addEqHandler( 'btn_rsub', THREE.ReverseSubtractEquation );
  253. addEqHandler( 'btn_min', THREE.MinEquation );
  254. addEqHandler( 'btn_max', THREE.MaxEquation );
  255. addPreHandler( 'btn_pre', mapsPre );
  256. addPreHandler( 'btn_nopre', mapsNoPre );
  257. }
  258. //
  259. function addImgHandler( id, index ) {
  260. var el = document.getElementById( id );
  261. el.addEventListener( 'click', function () {
  262. for ( var i = 0; i < materials.length; i ++ ) {
  263. materials[ i ].map = currentMaps[ index ];
  264. }
  265. currentIndex = index;
  266. } );
  267. }
  268. function addEqHandler( id, eq ) {
  269. var el = document.getElementById( id );
  270. el.addEventListener( 'click', function () {
  271. for ( var i = 0; i < materials.length; i ++ ) {
  272. materials[ i ].blendEquation = eq;
  273. }
  274. document.getElementById( 'btn_add' ).style.backgroundColor = 'transparent';
  275. document.getElementById( 'btn_sub' ).style.backgroundColor = 'transparent';
  276. document.getElementById( 'btn_rsub' ).style.backgroundColor = 'transparent';
  277. document.getElementById( 'btn_min' ).style.backgroundColor = 'transparent';
  278. document.getElementById( 'btn_max' ).style.backgroundColor = 'transparent';
  279. el.style.backgroundColor = 'darkorange';
  280. } );
  281. }
  282. function addBgHandler( id, map ) {
  283. var el = document.getElementById( id );
  284. el.addEventListener( 'click', function () {
  285. materialBg.map = map;
  286. } );
  287. }
  288. function addPreHandler( id, marray ) {
  289. var el = document.getElementById( id );
  290. el.addEventListener( 'click', function () {
  291. currentMaps = marray;
  292. for ( var i = 0; i < materials.length; i ++ ) {
  293. materials[ i ].map = currentMaps[ currentIndex ];
  294. }
  295. document.getElementById( 'btn_pre' ).style.backgroundColor = 'transparent';
  296. document.getElementById( 'btn_nopre' ).style.backgroundColor = 'transparent';
  297. el.style.backgroundColor = 'darkorange';
  298. } );
  299. }
  300. //
  301. function onWindowResize() {
  302. renderer.setSize( window.innerWidth, window.innerHeight );
  303. camera.aspect = window.innerWidth / window.innerHeight;
  304. camera.updateProjectionMatrix();
  305. }
  306. //
  307. function generateLabelMaterial( text, bg ) {
  308. var canvas = document.createElement( 'canvas' );
  309. var ctx = canvas.getContext( '2d' );
  310. canvas.width = 128;
  311. canvas.height = 32;
  312. ctx.fillStyle = bg;
  313. ctx.fillRect( 0, 0, 128, 32 );
  314. ctx.fillStyle = 'white';
  315. ctx.font = '12pt arial bold';
  316. ctx.fillText( text, 8, 22 );
  317. var map = new THREE.CanvasTexture( canvas );
  318. var material = new THREE.MeshBasicMaterial( { map: map, transparent: true } );
  319. return material;
  320. }
  321. function animate() {
  322. requestAnimationFrame( animate );
  323. var time = Date.now() * 0.00025;
  324. var ox = ( time * - 0.01 * materialBg.map.repeat.x ) % 1;
  325. var oy = ( time * - 0.01 * materialBg.map.repeat.y ) % 1;
  326. materialBg.map.offset.set( ox, oy );
  327. renderer.render( scene, camera );
  328. }
  329. </script>
  330. </body>
  331. </html>