materials_cubemap.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - cube map WebGL</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. background:#000;
  9. color:#fff;
  10. padding:0;
  11. margin:0;
  12. overflow:hidden;
  13. font-family:georgia;
  14. text-align:center;
  15. }
  16. a { color: #ff0080; text-decoration: none; }
  17. a:hover { color: #0080ff; }
  18. canvas { pointer-events:none; z-index:10; position:relative; }
  19. #log { position:absolute; top:50px; text-align:left; display:block; z-index:100 }
  20. #d { text-align:center; margin:1em 0 -7.5em 0; z-index:1000; position:relative; display:block }
  21. .button { background:orange; color:#fff; padding:0.2em 0.5em; cursor:pointer }
  22. .inactive { background:#999; color:#eee }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="d">
  27. <p><a href="http://github.com/mrdoob/three.js">Three.js</a> cube mapping demo
  28. <p>Walt Disney head by <a href="http://www.davidoreilly.com/2009/01/walt-disneys-head-on-a-plate" target="_blank">David OReilly</a>
  29. <p>Texture by <a href="http://www.humus.name/index.php?page=Textures" target="_blank">Humus</a>
  30. </div>
  31. <pre id="log"></pre>
  32. <script type="text/javascript" src="../build/Three.js"></script>
  33. <script type="text/javascript" src="../src/extras/primitives/Sphere.js"></script>
  34. <script type="text/javascript" src="../src/extras/primitives/Plane.js"></script>
  35. <script type="text/javascript" src="../src/extras/io/Loader.js"></script>
  36. <script type="text/javascript" src="js/Stats.js"></script>
  37. <script type="text/javascript">
  38. var SCREEN_WIDTH = window.innerWidth;
  39. var SCREEN_HEIGHT = window.innerHeight;
  40. var FLOOR = -250;
  41. var container;
  42. var stats;
  43. var camera;
  44. var scene;
  45. var webglRenderer;
  46. var mesh, zmesh, lightMesh, geometry;
  47. var directionalLight, pointLight;
  48. var mouseX = 0;
  49. var mouseY = 0;
  50. var windowHalfX = window.innerWidth >> 1;
  51. var windowHalfY = window.innerHeight >> 1;
  52. document.addEventListener('mousemove', onDocumentMouseMove, false);
  53. init();
  54. loop();
  55. setInterval(loop, 1000/60);
  56. function addMesh( geometry, scale, x, y, z, rx, ry, rz, material ) {
  57. mesh = new THREE.Mesh( geometry, material );
  58. mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
  59. mesh.position.x = x;
  60. mesh.position.y = y;
  61. mesh.position.z = z;
  62. mesh.rotation.x = rx;
  63. mesh.rotation.y = ry;
  64. mesh.rotation.z = rz;
  65. mesh.overdraw = true;
  66. mesh.updateMatrix();
  67. scene.addObject(mesh);
  68. }
  69. function init() {
  70. container = document.createElement('div');
  71. document.body.appendChild(container);
  72. camera = new THREE.Camera( 50, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 100000 );
  73. camera.position.z = 2000;
  74. camera.updateMatrix();
  75. scene = new THREE.Scene();
  76. // LIGHTS
  77. var ambient = new THREE.AmbientLight( 0x101010 );
  78. scene.addLight( ambient );
  79. directionalLight = new THREE.DirectionalLight( 0xffffff, 1.0 );
  80. directionalLight.position.x = 1;
  81. directionalLight.position.y = 1;
  82. directionalLight.position.z = 2;
  83. directionalLight.position.normalize();
  84. scene.addLight( directionalLight );
  85. pointLight = new THREE.PointLight( 0xffffff );
  86. pointLight.position.x = 0;
  87. pointLight.position.y = 0;
  88. pointLight.position.z = 0;
  89. scene.addLight( pointLight );
  90. // light representation
  91. sphere = new Sphere( 100, 16, 8, 1 );
  92. lightMesh = new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color:0xffaa00 } ) );
  93. lightMesh.scale.x = lightMesh.scale.y = lightMesh.scale.z = 0.05;
  94. lightMesh.position = pointLight.position;
  95. lightMesh.overdraw = true;
  96. lightMesh.updateMatrix();
  97. scene.addObject(lightMesh);
  98. // material samples
  99. var r = "textures/cube/SwedishRoyalCastle/";
  100. var urls = [ r + "px.jpg", r + "nx.jpg",
  101. r + "py.jpg", r + "ny.jpg",
  102. r + "pz.jpg", r + "nz.jpg" ];
  103. var images = loadImageArray( urls );
  104. var cubeMaterial3 = new THREE.MeshLambertMaterial( { color: 0xffaa00, env_map: new THREE.TextureCube( images ) } );
  105. var cubeMaterial2 = new THREE.MeshLambertMaterial( { color: 0xffee00, env_map: new THREE.TextureCube( images ) } );
  106. var cubeMaterial1 = new THREE.MeshLambertMaterial( { color: 0xffffff, env_map: new THREE.TextureCube( images ) } )
  107. var plane = new Plane( 100, 100 );
  108. addMesh( plane, 40, 0, 0, -2000, 0, 0,0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[5] ) } ) );
  109. addMesh( plane, 40, -2000, 0, 0, 0, 1.57,0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[0] ) } ) );
  110. addMesh( plane, 40, 2000, 0, 0, 0,-1.57,0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[1] ) } ) );
  111. addMesh( plane, 40, 0, 2000, 0, 1.57, 0,0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[2] ) } ) );
  112. addMesh( plane, 40, 0, -2000, 0, -1.57,0,0, new THREE.MeshBasicMaterial( { map: new THREE.Texture( images[3] ) } ) );
  113. webglRenderer = new THREE.WebGLRenderer( scene );
  114. webglRenderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  115. container.appendChild( webglRenderer.domElement );
  116. stats = new Stats();
  117. stats.domElement.style.position = 'absolute';
  118. stats.domElement.style.top = '0px';
  119. stats.domElement.style.zIndex = 100;
  120. container.appendChild( stats.domElement );
  121. var loader = new THREE.Loader();
  122. loader.loadBinary( "obj/walt/WaltHead_bin.js", function( geometry ) { createScene( geometry, cubeMaterial1, cubeMaterial2, cubeMaterial3 ) }, "obj/walt" );
  123. }
  124. function createScene( geometry, m1, m2, m3 ) {
  125. var s = 15;
  126. addMesh( geometry, s, 0, 0, -100, 0,0,0, m1 );
  127. addMesh( geometry, s, -900, 0, -100, 0,0,0, m2 );
  128. addMesh( geometry, s, 900, 0, -100, 0,0,0, m3 );
  129. }
  130. function loadImageArray( urls ) {
  131. var i, images = [];
  132. images.loadCount = 0;
  133. for ( i = 0; i < urls.length; ++i ) {
  134. images[i] = new Image();
  135. images[i].loaded = 0;
  136. images[i].onload = function() { images.loadCount += 1; this.loaded = 1;/*log( images.loadCount );*/ }
  137. images[i].src = urls[i];
  138. }
  139. return images;
  140. }
  141. function onDocumentMouseMove(event) {
  142. mouseX = ( event.clientX - windowHalfX );
  143. mouseY = ( event.clientY - windowHalfY );
  144. }
  145. var r = 0;
  146. function loop() {
  147. camera.position.x += ( mouseX - camera.position.x ) * .05;
  148. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  149. camera.updateMatrix();
  150. lightMesh.position.x = 1500*Math.cos(r);
  151. lightMesh.position.z = 1500*Math.sin(r);
  152. lightMesh.updateMatrix();
  153. r += 0.01;
  154. webglRenderer.render( scene, camera );
  155. stats.update();
  156. }
  157. function log(text) {
  158. var e = document.getElementById("log");
  159. e.innerHTML = text + "<br/>" + e.innerHTML;
  160. }
  161. </script>
  162. </body>
  163. </html>