webgl_multiple_canvases_complex.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/Stats.js"></script>
  55. <script>
  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, group1, group2, group3, 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. scene = new THREE.Scene();
  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.add( camera );
  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 = - Math.PI / 2;
  99. scene.add( mesh );
  100. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  101. mesh.position.x = - 400;
  102. mesh.position.y = - 250;
  103. mesh.rotation.x = - Math.PI / 2;
  104. scene.add( mesh );
  105. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  106. mesh.position.x = 400;
  107. mesh.position.y = - 250;
  108. mesh.rotation.x = - Math.PI / 2;
  109. scene.add( mesh );
  110. var faceIndices = [ 'a', 'b', 'c', 'd' ];
  111. var color, f1, f2, f3, p, n, vertexIndex,
  112. radius = 200,
  113. geometry1 = new THREE.IcosahedronGeometry( radius, 1 ),
  114. geometry2 = new THREE.IcosahedronGeometry( radius, 1 ),
  115. geometry3 = new THREE.IcosahedronGeometry( radius, 1 );
  116. for ( var i = 0; i < geometry1.faces.length; i ++ ) {
  117. f1 = geometry1.faces[ i ];
  118. f2 = geometry2.faces[ i ];
  119. f3 = geometry3.faces[ i ];
  120. n = ( f1 instanceof THREE.Face3 ) ? 3 : 4;
  121. for( var j = 0; j < n; j ++ ) {
  122. vertexIndex = f1[ faceIndices[ j ] ];
  123. p = geometry1.vertices[ vertexIndex ];
  124. color = new THREE.Color( 0xffffff );
  125. color.setHSV( ( p.y / radius + 1 ) / 2, 1.0, 1.0 );
  126. f1.vertexColors[ j ] = color;
  127. color = new THREE.Color( 0xffffff );
  128. color.setHSV( 0.0, ( p.y/ radius + 1 ) / 2, 1.0 );
  129. f2.vertexColors[ j ] = color;
  130. color = new THREE.Color( 0xffffff );
  131. color.setHSV( 0.125 * vertexIndex / geometry1.vertices.length, 1.0, 1.0 );
  132. f3.vertexColors[ j ] = color;
  133. }
  134. }
  135. var materials = [
  136. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } ),
  137. new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true, transparent: true } )
  138. ];
  139. group1 = THREE.SceneUtils.createMultiMaterialObject( geometry1, materials );
  140. group1.position.x = -400;
  141. group1.rotation.x = -1.87;
  142. scene.add( group1 );
  143. group2 = THREE.SceneUtils.createMultiMaterialObject( geometry2, materials );
  144. group2.position.x = 400;
  145. group2.rotation.x = 0;
  146. scene.add( group2 );
  147. group3 = THREE.SceneUtils.createMultiMaterialObject( geometry3, materials );
  148. group3.position.x = 0;
  149. group3.rotation.x = 0;
  150. scene.add( group3 );
  151. renderer = new THREE.WebGLRenderer( { antialias: true } );
  152. renderer.setSize( container.clientWidth, container.clientHeight );
  153. container.appendChild( renderer.domElement );
  154. stats = new Stats();
  155. stats.domElement.style.position = 'absolute';
  156. stats.domElement.style.top = '0px';
  157. container.appendChild( stats.domElement );
  158. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  159. }
  160. function onDocumentMouseMove( event ) {
  161. mouseX = ( event.clientX - windowHalfX );
  162. mouseY = ( event.clientY - windowHalfY );
  163. }
  164. //
  165. this.animate = function() {
  166. render();
  167. stats.update();
  168. };
  169. function render() {
  170. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  171. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  172. camera.lookAt( scene.position );
  173. renderer.render( scene, camera );
  174. }
  175. }
  176. </script>
  177. </body>
  178. </html>