webgl_materials_blending.html 4.9 KB

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