webgl_multiple_canvases_grid.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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/Stats.js"></script>
  72. <script>
  73. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  74. var apps = [];
  75. init();
  76. animate();
  77. function init() {
  78. var w = 300;
  79. var h = 200;
  80. var fullWidth = w * 2;
  81. var fullHeight = h * 2;
  82. apps.push( new App( 'container1', fullWidth, fullHeight, w * 0, h * 0, w, h ) );
  83. apps.push( new App( 'container2', fullWidth, fullHeight, w * 1, h * 0, w, h ) );
  84. apps.push( new App( 'container3', fullWidth, fullHeight, w * 0, h * 1, w, h ) );
  85. apps.push( new App( 'container4', fullWidth, fullHeight, w * 1, h * 1, w, h ) );
  86. }
  87. function animate() {
  88. for ( var i = 0; i < apps.length; ++i ) {
  89. apps[ i ].animate();
  90. }
  91. requestAnimationFrame( animate );
  92. }
  93. function App( containerId, fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight ) {
  94. var container, stats;
  95. var camera, scene, renderer;
  96. var mesh, group1, group2, group3, light;
  97. var mouseX = 0, mouseY = 0;
  98. var windowHalfX = window.innerWidth / 2;
  99. var windowHalfY = window.innerHeight / 2;
  100. init();
  101. function init() {
  102. container = document.getElementById( containerId );
  103. scene = new THREE.Scene();
  104. camera = new THREE.PerspectiveCamera( 20, container.clientWidth / container.clientHeight, 1, 10000 );
  105. camera.setViewOffset( fullWidth, fullHeight, viewX, viewY, viewWidth, viewHeight );
  106. camera.position.z = 1800;
  107. scene.add( camera );
  108. light = new THREE.DirectionalLight( 0xffffff );
  109. light.position.set( 0, 0, 1 ).normalize();
  110. scene.add( light );
  111. var shadowMaterial = new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'textures/shadow.png' ) } );
  112. var shadowGeo = new THREE.PlaneGeometry( 300, 300, 1, 1 );
  113. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  114. mesh.position.y = - 250;
  115. mesh.rotation.x = - 90 * Math.PI / 180;
  116. scene.add( mesh );
  117. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  118. mesh.position.y = - 250;
  119. mesh.position.x = - 400;
  120. mesh.rotation.x = - 90 * Math.PI / 180;
  121. scene.add( mesh );
  122. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  123. mesh.position.y = - 250;
  124. mesh.position.x = 400;
  125. mesh.rotation.x = - 90 * Math.PI / 180;
  126. scene.add( mesh );
  127. var faceIndices = [ 'a', 'b', 'c', 'd' ];
  128. var color, f1, f2, f3, p, n, vertexIndex,
  129. geometry1 = new THREE.IcosahedronGeometry( 1 ),
  130. geometry2 = new THREE.IcosahedronGeometry( 1 ),
  131. geometry3 = new THREE.IcosahedronGeometry( 1 );
  132. for ( var i = 0; i < geometry1.faces.length; i ++ ) {
  133. f1 = geometry1.faces[ i ];
  134. f2 = geometry2.faces[ i ];
  135. f3 = geometry3.faces[ i ];
  136. n = ( f1 instanceof THREE.Face3 ) ? 3 : 4;
  137. for( var j = 0; j < n; j ++ ) {
  138. vertexIndex = f1[ faceIndices[ j ] ];
  139. p = geometry1.vertices[ vertexIndex ].position;
  140. color = new THREE.Color( 0xffffff );
  141. color.setHSV( ( p.y + 1 ) / 2, 1.0, 1.0 );
  142. f1.vertexColors[ j ] = color;
  143. color = new THREE.Color( 0xffffff );
  144. color.setHSV( 0.0, ( p.y + 1 ) / 2, 1.0 );
  145. f2.vertexColors[ j ] = color;
  146. color = new THREE.Color( 0xffffff );
  147. color.setHSV( 0.125 * vertexIndex / geometry1.vertices.length, 1.0, 1.0 );
  148. f3.vertexColors[ j ] = color;
  149. }
  150. }
  151. var materials = [
  152. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } ),
  153. new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true, transparent: true } )
  154. ];
  155. var scaleMatrix = new THREE.Matrix4().setScale( 200, 200, 200 );
  156. geometry1.applyMatrix( scaleMatrix );
  157. geometry2.applyMatrix( scaleMatrix );
  158. geometry3.applyMatrix( scaleMatrix );
  159. group1 = THREE.SceneUtils.createMultiMaterialObject( geometry1, materials );
  160. group1.position.x = -400;
  161. group1.rotation.x = -1.87;
  162. scene.add( group1 );
  163. group2 = THREE.SceneUtils.createMultiMaterialObject( geometry2, materials );
  164. group2.position.x = 400;
  165. group2.rotation.x = 0;
  166. scene.add( group2 );
  167. group3 = THREE.SceneUtils.createMultiMaterialObject( geometry3, materials );
  168. group3.position.x = 0;
  169. group3.rotation.x = 0;
  170. scene.add( group3 );
  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. camera.lookAt( scene.position );
  193. renderer.render( scene, camera );
  194. }
  195. }
  196. </script>
  197. </body>
  198. </html>