webgl_materials.html 5.7 KB

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