webgl_multiple_canvases_grid.html 6.6 KB

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