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