webgl_multiple_canvases_complex.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - multiple canvases - complex</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. color: #808080;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #fff;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. #container1, #container2, #container3 {
  23. position: relative;
  24. border: 1px solid red;
  25. }
  26. #container1 {
  27. width: 300px;
  28. height: 200px;
  29. }
  30. #container2 {
  31. width: 400px;
  32. height: 100px;
  33. left: 150px;
  34. }
  35. #container3 {
  36. width: 200px;
  37. height: 300px;
  38. left: 75px;
  39. }
  40. a {
  41. color: #0080ff;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <div id="container">
  47. <div id="container1"></div>
  48. <div id="container2"></div>
  49. <div id="container3"></div>
  50. </div>
  51. <div id="info"><a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> webgl - multiple canvases - complex</div>
  52. <script src="../build/Three.js"></script>
  53. <script src="js/Detector.js"></script>
  54. <script src="js/RequestAnimationFrame.js"></script>
  55. <script src="js/Stats.js"></script>
  56. <script>
  57. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  58. var apps = [];
  59. init();
  60. animate();
  61. function init() {
  62. var container1 = document.getElementById( 'container1' );
  63. var container2 = document.getElementById( 'container2' );
  64. var container3 = document.getElementById( 'container3' );
  65. var fullWidth = 550;
  66. var fullHeight = 600;
  67. apps.push( new App( 'container1', fullWidth, fullHeight, 0, 0, container1.clientWidth, container1.clientHeight ) );
  68. apps.push( new App( 'container2', fullWidth, fullHeight, 150, 200, container2.clientWidth, container2.clientHeight ) );
  69. apps.push( new App( 'container3', fullWidth, fullHeight, 75, 300, container3.clientWidth, container3.clientHeight ) );
  70. }
  71. function animate() {
  72. for ( var i = 0; i < apps.length; ++i ) {
  73. apps[ i ].animate();
  74. }
  75. requestAnimationFrame( animate );
  76. }
  77. function App( containerId, fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight ) {
  78. var container, stats;
  79. var camera, scene, renderer;
  80. var mesh, mesh2, mesh3, light;
  81. var mouseX = 0, mouseY = 0;
  82. var windowHalfX = window.innerWidth / 2;
  83. var windowHalfY = window.innerHeight / 2;
  84. init();
  85. function init() {
  86. container = document.getElementById( containerId );
  87. camera = new THREE.PerspectiveCamera( 20, container.clientWidth / container.clientHeight, 1, 10000 );
  88. camera.setViewOffset( fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight );
  89. camera.position.z = 1800;
  90. scene = new THREE.Scene();
  91. light = new THREE.DirectionalLight( 0xffffff );
  92. light.position.set( 0, 0, 1 ).normalize();
  93. scene.add( 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.add( 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.add( 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.add( 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.add( 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.add( 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.add( 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. camera.lookAt( scene.position );
  175. renderer.render( scene, camera );
  176. }
  177. }
  178. </script>
  179. </body>
  180. </html>