webgl_materials.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials</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. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl materials
  12. </div>
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. let stats;
  28. let camera, scene, renderer;
  29. let pointLight;
  30. const objects = [], materials = [];
  31. init();
  32. animate();
  33. function init() {
  34. const container = document.createElement( 'div' );
  35. document.body.appendChild( container );
  36. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  37. camera.position.set( 0, 200, 800 );
  38. scene = new THREE.Scene();
  39. // Grid
  40. const helper = new THREE.GridHelper( 1000, 40, 0x303030, 0x303030 );
  41. helper.position.y = - 75;
  42. scene.add( helper );
  43. // Materials
  44. const texture = new THREE.Texture( generateTexture() );
  45. texture.colorSpace = THREE.SRGBColorSpace;
  46. texture.needsUpdate = true;
  47. materials.push( new THREE.MeshLambertMaterial( { map: texture, transparent: true } ) );
  48. materials.push( new THREE.MeshLambertMaterial( { color: 0xdddddd } ) );
  49. materials.push( new THREE.MeshPhongMaterial( { color: 0xdddddd, specular: 0x009900, shininess: 30, flatShading: true } ) );
  50. materials.push( new THREE.MeshNormalMaterial() );
  51. materials.push( new THREE.MeshBasicMaterial( { color: 0xffaa00, transparent: true, blending: THREE.AdditiveBlending } ) );
  52. materials.push( new THREE.MeshLambertMaterial( { color: 0xdddddd } ) );
  53. materials.push( new THREE.MeshPhongMaterial( { color: 0xdddddd, specular: 0x009900, shininess: 30, map: texture, transparent: true } ) );
  54. materials.push( new THREE.MeshNormalMaterial( { flatShading: true } ) );
  55. materials.push( new THREE.MeshBasicMaterial( { color: 0xffaa00, wireframe: true } ) );
  56. materials.push( new THREE.MeshDepthMaterial() );
  57. materials.push( new THREE.MeshLambertMaterial( { color: 0x666666, emissive: 0xff0000 } ) );
  58. materials.push( new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x666666, emissive: 0xff0000, shininess: 10, opacity: 0.9, transparent: true } ) );
  59. materials.push( new THREE.MeshBasicMaterial( { map: texture, transparent: true } ) );
  60. // Spheres geometry
  61. const geometry = new THREE.SphereGeometry( 70, 32, 16 );
  62. for ( let i = 0, l = materials.length; i < l; i ++ ) {
  63. addMesh( geometry, materials[ i ] );
  64. }
  65. // Lights
  66. scene.add( new THREE.AmbientLight( 0x111111 ) );
  67. const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.125 );
  68. directionalLight.position.x = Math.random() - 0.5;
  69. directionalLight.position.y = Math.random() - 0.5;
  70. directionalLight.position.z = Math.random() - 0.5;
  71. directionalLight.position.normalize();
  72. scene.add( directionalLight );
  73. pointLight = new THREE.PointLight( 0xffffff, 1 );
  74. scene.add( pointLight );
  75. pointLight.add( new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) ) );
  76. //
  77. renderer = new THREE.WebGLRenderer( { antialias: true } );
  78. renderer.setPixelRatio( window.devicePixelRatio );
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. container.appendChild( renderer.domElement );
  81. //
  82. stats = new Stats();
  83. container.appendChild( stats.dom );
  84. //
  85. window.addEventListener( 'resize', onWindowResize );
  86. }
  87. function addMesh( geometry, material ) {
  88. const mesh = new THREE.Mesh( geometry, material );
  89. mesh.position.x = ( objects.length % 4 ) * 200 - 400;
  90. mesh.position.z = Math.floor( objects.length / 4 ) * 200 - 200;
  91. mesh.rotation.x = Math.random() * 200 - 100;
  92. mesh.rotation.y = Math.random() * 200 - 100;
  93. mesh.rotation.z = Math.random() * 200 - 100;
  94. objects.push( mesh );
  95. scene.add( mesh );
  96. }
  97. function onWindowResize() {
  98. camera.aspect = window.innerWidth / window.innerHeight;
  99. camera.updateProjectionMatrix();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. }
  102. function generateTexture() {
  103. const canvas = document.createElement( 'canvas' );
  104. canvas.width = 256;
  105. canvas.height = 256;
  106. const context = canvas.getContext( '2d' );
  107. const image = context.getImageData( 0, 0, 256, 256 );
  108. let x = 0, y = 0;
  109. for ( let i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
  110. x = j % 256;
  111. y = ( x === 0 ) ? y + 1 : y;
  112. image.data[ i ] = 255;
  113. image.data[ i + 1 ] = 255;
  114. image.data[ i + 2 ] = 255;
  115. image.data[ i + 3 ] = Math.floor( x ^ y );
  116. }
  117. context.putImageData( image, 0, 0 );
  118. return canvas;
  119. }
  120. //
  121. function animate() {
  122. requestAnimationFrame( animate );
  123. render();
  124. stats.update();
  125. }
  126. function render() {
  127. const timer = 0.0001 * Date.now();
  128. camera.position.x = Math.cos( timer ) * 1000;
  129. camera.position.z = Math.sin( timer ) * 1000;
  130. camera.lookAt( scene.position );
  131. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  132. const object = objects[ i ];
  133. object.rotation.x += 0.01;
  134. object.rotation.y += 0.005;
  135. }
  136. materials[ materials.length - 2 ].emissive.setHSL( 0.54, 1, 0.35 * ( 0.5 + 0.5 * Math.sin( 35 * timer ) ) );
  137. materials[ materials.length - 3 ].emissive.setHSL( 0.04, 1, 0.35 * ( 0.5 + 0.5 * Math.cos( 35 * timer ) ) );
  138. pointLight.position.x = Math.sin( timer * 7 ) * 300;
  139. pointLight.position.y = Math.cos( timer * 5 ) * 400;
  140. pointLight.position.z = Math.cos( timer * 3 ) * 300;
  141. renderer.render( scene, camera );
  142. }
  143. </script>
  144. </body>
  145. </html>