webgl_multiple_canvases_complex.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - multiple canvases - complex</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. color: #808080;
  9. font-family:Monospace;
  10. font-size:13px;
  11. text-align:center;
  12. background-color: #fff;
  13. margin: 0px;
  14. overflow: hidden;
  15. }
  16. #info {
  17. position: absolute;
  18. top: 0px; width: 100%;
  19. padding: 5px;
  20. }
  21. #container1, #container2, #container3 {
  22. position: relative;
  23. border: 1px solid red;
  24. }
  25. #container1 {
  26. width: 300px;
  27. height: 200px;
  28. }
  29. #container2 {
  30. width: 400px;
  31. height: 100px;
  32. left: 150px;
  33. }
  34. #container3 {
  35. width: 200px;
  36. height: 300px;
  37. left: 75px;
  38. }
  39. a {
  40. color: #0080ff;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <div id="container">
  46. <div id="container1"></div>
  47. <div id="container2"></div>
  48. <div id="container3"></div>
  49. </div>
  50. <div id="info"><a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> webgl - multiple canvases - complex</div>
  51. <script type="text/javascript" src="../build/Three.js"></script>
  52. <script type="text/javascript" src="js/Detector.js"></script>
  53. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  54. <script type="text/javascript" src="js/Stats.js"></script>
  55. <script type="text/javascript">
  56. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  57. var apps = [];
  58. init();
  59. animate();
  60. function init() {
  61. var container1 = document.getElementById( 'container1' );
  62. var container2 = document.getElementById( 'container2' );
  63. var container3 = document.getElementById( 'container3' );
  64. var fullWidth = 550;
  65. var fullHeight = 600;
  66. apps.push( new App( 'container1', fullWidth, fullHeight, 0, 0, container1.clientWidth, container1.clientHeight ) );
  67. apps.push( new App( 'container2', fullWidth, fullHeight, 150, 200, container2.clientWidth, container2.clientHeight ) );
  68. apps.push( new App( 'container3', fullWidth, fullHeight, 75, 300, container3.clientWidth, container3.clientHeight ) );
  69. }
  70. function animate() {
  71. for ( var i = 0; i < apps.length; ++i ) {
  72. apps[ i ].animate();
  73. }
  74. requestAnimationFrame( animate );
  75. }
  76. function App( containerId, fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight ) {
  77. var container, stats;
  78. var camera, scene, renderer;
  79. var mesh, mesh2, mesh3, light;
  80. var mouseX = 0, mouseY = 0;
  81. var windowHalfX = window.innerWidth / 2;
  82. var windowHalfY = window.innerHeight / 2;
  83. init();
  84. function init() {
  85. container = document.getElementById( containerId );
  86. camera = new THREE.Camera( 20, container.clientWidth / container.clientHeight, 1, 10000 );
  87. camera.setViewOffset(fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight);
  88. camera.position.z = 1800;
  89. scene = new THREE.Scene();
  90. light = new THREE.DirectionalLight( 0xffffff );
  91. light.position.set( 0, 0, 1 );
  92. light.position.normalize();
  93. scene.addLight( light );
  94. var shadowMaterial = new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'textures/shadow.png' ) } );
  95. var shadowGeo = new THREE.PlaneGeometry( 300, 300, 1, 1 );
  96. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  97. mesh.position.y = - 250;
  98. mesh.rotation.x = - 90 * Math.PI / 180;
  99. scene.addObject( mesh );
  100. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  101. mesh.position.y = - 250;
  102. mesh.position.x = - 400;
  103. mesh.rotation.x = - 90 * Math.PI / 180;
  104. scene.addObject( mesh );
  105. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  106. mesh.position.y = - 250;
  107. mesh.position.x = 400;
  108. mesh.rotation.x = - 90 * Math.PI / 180;
  109. scene.addObject( mesh );
  110. var faceIndices = [ 'a', 'b', 'c', 'd' ];
  111. var color, f, f2, f3, p, n, vertexIndex,
  112. geometry = new THREE.IcosahedronGeometry( 1 ),
  113. geometry2 = new THREE.IcosahedronGeometry( 1 ),
  114. geometry3 = new THREE.IcosahedronGeometry( 1 );
  115. for ( var i = 0; i < geometry.faces.length; i++ ) {
  116. f = geometry.faces[ i ];
  117. f2 = geometry2.faces[ i ];
  118. f3 = geometry3.faces[ i ];
  119. n = ( f instanceof THREE.Face3 ) ? 3 : 4;
  120. for( var j = 0; j < n; j++ ) {
  121. vertexIndex = f[ faceIndices[ j ] ];
  122. p = geometry.vertices[ vertexIndex ].position;
  123. color = new THREE.Color( 0xffffff );
  124. color.setHSV( ( p.y + 1 ) / 2, 1.0, 1.0 );
  125. f.vertexColors[ j ] = color;
  126. color = new THREE.Color( 0xffffff );
  127. color.setHSV( 0.0, ( p.y + 1 ) / 2, 1.0 );
  128. f2.vertexColors[ j ] = color;
  129. color = new THREE.Color( 0xffffff );
  130. color.setHSV( 0.125 * vertexIndex/geometry.vertices.length, 1.0, 1.0 );
  131. f3.vertexColors[ j ] = color;
  132. }
  133. }
  134. var materials = [
  135. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } ),
  136. new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true } )
  137. ];
  138. mesh = new THREE.Mesh( geometry, materials );
  139. mesh.position.x = -400;
  140. mesh.scale.x = mesh.scale.y = mesh.scale.z = 200;
  141. mesh.rotation.x = -1.87;
  142. scene.addObject( mesh );
  143. mesh2 = new THREE.Mesh( geometry2, materials );
  144. mesh2.position.x = 400;
  145. mesh2.rotation.x = 0;
  146. mesh2.scale = mesh.scale;
  147. scene.addObject( mesh2 );
  148. mesh3 = new THREE.Mesh( geometry3, materials );
  149. mesh3.position.x = 0;
  150. mesh3.rotation.x = 0;
  151. mesh3.scale = mesh.scale;
  152. scene.addObject( mesh3 );
  153. renderer = new THREE.WebGLRenderer( { antialias: true } );
  154. renderer.setSize( container.clientWidth, container.clientHeight );
  155. container.appendChild( renderer.domElement );
  156. stats = new Stats();
  157. stats.domElement.style.position = 'absolute';
  158. stats.domElement.style.top = '0px';
  159. container.appendChild( stats.domElement );
  160. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  161. }
  162. function onDocumentMouseMove( event ) {
  163. mouseX = ( event.clientX - windowHalfX );
  164. mouseY = ( event.clientY - windowHalfY );
  165. }
  166. //
  167. this.animate = function() {
  168. render();
  169. stats.update();
  170. };
  171. function render() {
  172. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  173. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  174. renderer.render( scene, camera );
  175. }
  176. }
  177. </script>
  178. </body>
  179. </html>