webgl_materials2.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="../build/Three.js"></script>
  18. <script src="js/Detector.js"></script>
  19. <script src="js/Stats.js"></script>
  20. <script>
  21. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  22. var container, stats;
  23. var camera, scene, renderer, objects;
  24. var particleLight, pointLight;
  25. init();
  26. animate();
  27. function init() {
  28. container = document.createElement( 'div' );
  29. document.body.appendChild( container );
  30. scene = new THREE.Scene();
  31. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  32. camera.position.set( 0, 200, 0 );
  33. scene.add( camera );
  34. // Materials
  35. var imgTexture2 = THREE.ImageUtils.loadTexture( "textures/planets/moon_1024.jpg" );
  36. var imgTexture = THREE.ImageUtils.loadTexture( "textures/lava/lavatile.jpg" );
  37. imgTexture.repeat.set( 4, 2 );
  38. imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
  39. var shininess = 15, shading = THREE.SmoothShading;
  40. var materials = [];
  41. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture, color: 0xffffff, ambient: 0x777777, specular: 0x999999, shininess: shininess, shading: shading } ) );
  42. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture, color: 0x00ff00, ambient: 0x777777, specular: 0x999999, shininess: shininess, shading: shading } ) );
  43. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture, color: 0x00ff00, ambient: 0x007700, specular: 0x999999, shininess: shininess, shading: shading } ) );
  44. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture, color: 0x000000, ambient: 0x00ff00, specular: 0x999999, shininess: shininess, shading: shading } ) );
  45. materials.push( new THREE.MeshLambertMaterial( { map: imgTexture, color: 0xffffff, ambient: 0x777777, shading: shading } ) );
  46. materials.push( new THREE.MeshLambertMaterial( { map: imgTexture, color: 0xff0000, ambient: 0x777777, shading: shading } ) );
  47. materials.push( new THREE.MeshLambertMaterial( { map: imgTexture, color: 0xff0000, ambient: 0x770000, shading: shading } ) );
  48. materials.push( new THREE.MeshLambertMaterial( { map: imgTexture, color: 0x000000, ambient: 0xff0000, shading: shading } ) );
  49. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture2, color: 0x000000, ambient: 0x000000, specular: 0xffaa00, shininess: shininess, metal: true, shading: shading } ) );
  50. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture2, color: 0x000000, ambient: 0x000000, specular: 0xaaff00, shininess: shininess, metal: true, shading: shading } ) );
  51. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture2, color: 0x000000, ambient: 0x000000, specular: 0x00ffaa, shininess: shininess, metal: true, shading: shading } ) );
  52. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture2, color: 0x000000, ambient: 0x000000, specular: 0x00aaff, shininess: shininess, metal: true, shading: shading } ) );
  53. // Spheres geometry
  54. var geometry_smooth = new THREE.SphereGeometry( 70, 32, 16 );
  55. var geometry_flat = new THREE.SphereGeometry( 70, 32, 16 );
  56. objects = [];
  57. var sphere, geometry, material;
  58. for ( var i = 0, l = materials.length; i < l; i ++ ) {
  59. material = materials[ i ];
  60. geometry = material.shading == THREE.FlatShading ? geometry_flat : geometry_smooth;
  61. sphere = new THREE.Mesh( geometry, material );
  62. sphere.position.x = ( i % 4 ) * 200 - 200;
  63. sphere.position.z = Math.floor( i / 4 ) * 200 - 200;
  64. objects.push( sphere );
  65. scene.add( sphere );
  66. }
  67. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  68. scene.add( particleLight );
  69. // Lights
  70. scene.add( new THREE.AmbientLight( 0x444444 ) );
  71. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  72. directionalLight.position.set( 1, 1, 1 ).normalize();
  73. scene.add( directionalLight );
  74. pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
  75. scene.add( pointLight );
  76. particleLight.material.color = pointLight.color;
  77. pointLight.position = particleLight.position;
  78. //
  79. renderer = new THREE.WebGLRenderer( { clearColor: 0x0a0a0a, clearAlpha: 1, antialias: true } );
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. renderer.sortObjects = true;
  82. container.appendChild( renderer.domElement );
  83. renderer.gammaInput = true;
  84. renderer.gammaOutput = true;
  85. //renderer.physicallyBasedShading = true;
  86. //
  87. stats = new Stats();
  88. stats.domElement.style.position = 'absolute';
  89. stats.domElement.style.top = '0px';
  90. container.appendChild( stats.domElement );
  91. }
  92. //
  93. function animate() {
  94. requestAnimationFrame( animate );
  95. render();
  96. stats.update();
  97. }
  98. function render() {
  99. var timer = Date.now() * 0.00025;
  100. camera.position.x = Math.cos( timer ) * 800;
  101. camera.position.z = Math.sin( timer ) * 800;
  102. camera.lookAt( scene.position );
  103. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  104. var object = objects[ i ];
  105. object.rotation.y += 0.005;
  106. }
  107. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  108. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  109. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  110. renderer.render( scene, camera );
  111. }
  112. </script>
  113. </body>
  114. </html>