webgl_materials2.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.min.js"></script>
  18. <script src="js/Detector.js"></script>
  19. <script src="js/libs/stats.min.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. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
  31. camera.position.set( 0, 200, 0 );
  32. scene = new THREE.Scene();
  33. // Materials
  34. var imgTexture2 = THREE.ImageUtils.loadTexture( "textures/planets/moon_1024.jpg" );
  35. imgTexture2.wrapS = imgTexture2.wrapT = THREE.RepeatWrapping;
  36. imgTexture2.anisotropy = 16;
  37. var imgTexture = THREE.ImageUtils.loadTexture( "textures/lava/lavatile.jpg" );
  38. imgTexture.repeat.set( 4, 2 );
  39. imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;
  40. imgTexture.anisotropy = 16;
  41. var shininess = 50, specular = 0x333333, bumpScale = 1, shading = THREE.SmoothShading;
  42. var materials = [];
  43. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture, bumpMap: imgTexture, bumpScale: bumpScale, color: 0xffffff, ambient: 0x777777, specular: specular, shininess: shininess, shading: shading } ) );
  44. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture, bumpMap: imgTexture, bumpScale: bumpScale, color: 0x00ff00, ambient: 0x777777, specular: specular, shininess: shininess, shading: shading } ) );
  45. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture, bumpMap: imgTexture, bumpScale: bumpScale, color: 0x00ff00, ambient: 0x007700, specular: specular, shininess: shininess, shading: shading } ) );
  46. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture, bumpMap: imgTexture, bumpScale: bumpScale, color: 0x000000, ambient: 0x00ff00, specular: specular, shininess: shininess, shading: shading } ) );
  47. materials.push( new THREE.MeshLambertMaterial( { map: imgTexture, color: 0xffffff, ambient: 0x777777, shading: shading } ) );
  48. materials.push( new THREE.MeshLambertMaterial( { map: imgTexture, color: 0xff0000, ambient: 0x777777, shading: shading } ) );
  49. materials.push( new THREE.MeshLambertMaterial( { map: imgTexture, color: 0xff0000, ambient: 0x770000, shading: shading } ) );
  50. materials.push( new THREE.MeshLambertMaterial( { map: imgTexture, color: 0x000000, ambient: 0xff0000, shading: shading } ) );
  51. shininess = 15;
  52. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture2, bumpMap: imgTexture2, bumpScale: bumpScale, color: 0x000000, ambient: 0x000000, specular: 0xffaa00, shininess: shininess, metal: true, shading: shading } ) );
  53. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture2, bumpMap: imgTexture2, bumpScale: bumpScale, color: 0x000000, ambient: 0x000000, specular: 0xaaff00, shininess: shininess, metal: true, shading: shading } ) );
  54. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture2, bumpMap: imgTexture2, bumpScale: bumpScale, color: 0x000000, ambient: 0x000000, specular: 0x00ffaa, shininess: shininess, metal: true, shading: shading } ) );
  55. materials.push( new THREE.MeshPhongMaterial( { map: imgTexture2, bumpMap: imgTexture2, bumpScale: bumpScale, color: 0x000000, ambient: 0x000000, specular: 0x00aaff, shininess: shininess, metal: true, shading: shading } ) );
  56. // Spheres geometry
  57. var geometry_smooth = new THREE.SphereGeometry( 70, 32, 16 );
  58. var geometry_flat = new THREE.SphereGeometry( 70, 32, 16 );
  59. objects = [];
  60. var sphere, geometry, material;
  61. for ( var i = 0, l = materials.length; i < l; i ++ ) {
  62. material = materials[ i ];
  63. geometry = material.shading == THREE.FlatShading ? geometry_flat : geometry_smooth;
  64. sphere = new THREE.Mesh( geometry, material );
  65. sphere.position.x = ( i % 4 ) * 200 - 200;
  66. sphere.position.z = Math.floor( i / 4 ) * 200 - 200;
  67. objects.push( sphere );
  68. scene.add( sphere );
  69. }
  70. particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
  71. scene.add( particleLight );
  72. // Lights
  73. scene.add( new THREE.AmbientLight( 0x444444 ) );
  74. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  75. directionalLight.position.set( 1, 1, 1 ).normalize();
  76. scene.add( directionalLight );
  77. pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
  78. scene.add( pointLight );
  79. particleLight.material.color = pointLight.color;
  80. pointLight.position = particleLight.position;
  81. //
  82. renderer = new THREE.WebGLRenderer( { antialias: true } );
  83. renderer.setClearColor( 0x0a0a0a, 1 );
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. renderer.sortObjects = true;
  86. container.appendChild( renderer.domElement );
  87. renderer.gammaInput = true;
  88. renderer.gammaOutput = true;
  89. //
  90. stats = new Stats();
  91. stats.domElement.style.position = 'absolute';
  92. stats.domElement.style.top = '0px';
  93. container.appendChild( stats.domElement );
  94. //
  95. window.addEventListener( 'resize', onWindowResize, false );
  96. }
  97. function onWindowResize() {
  98. camera.aspect = window.innerWidth / window.innerHeight;
  99. camera.updateProjectionMatrix();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. }
  102. //
  103. function animate() {
  104. requestAnimationFrame( animate );
  105. render();
  106. stats.update();
  107. }
  108. function render() {
  109. var timer = Date.now() * 0.00025;
  110. camera.position.x = Math.cos( timer ) * 800;
  111. camera.position.z = Math.sin( timer ) * 800;
  112. camera.lookAt( scene.position );
  113. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  114. var object = objects[ i ];
  115. object.rotation.y += 0.005;
  116. }
  117. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  118. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  119. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  120. renderer.render( scene, camera );
  121. }
  122. </script>
  123. </body>
  124. </html>