webgl_materials_blending.html 5.2 KB

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