webgl_materials.html 5.9 KB

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