webgl_multiple_elements.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - multiple elements</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. * {
  9. box-sizing: border-box;
  10. -moz-box-sizing: border-box;
  11. }
  12. body {
  13. color: #000;
  14. font-family:Monospace;
  15. font-size:13px;
  16. background-color: #fff;
  17. margin: 0px;
  18. }
  19. #info {
  20. position: absolute;
  21. top: 0px; width: 100%;
  22. padding: 5px;
  23. text-align:center;
  24. }
  25. #content {
  26. position: absolute;
  27. top: 0px; width: 100%;
  28. z-index: 1;
  29. padding: 2em;
  30. }
  31. a {
  32. color: #0080ff;
  33. }
  34. #c {
  35. position: fixed;
  36. left: 0px;
  37. width: 100%;
  38. height: 100%;
  39. }
  40. .list-item {
  41. margin: 1em;
  42. padding: 2em;
  43. display: -webkit-flex;
  44. display: flex;
  45. flex-direction: row;
  46. -webkit-flex-direction: row;
  47. }
  48. .list-item .scene {
  49. width: 200px;
  50. height: 200px;
  51. flex: 0 0 auto;
  52. -webkit-flex: 0 0 auto;
  53. }
  54. .list-item .description {
  55. font-family: sans-serif;
  56. font-size: large;
  57. padding-left: 2em;
  58. flex: 1 1 auto;
  59. -webkit-flex: 1 1 auto;
  60. }
  61. @media only screen and (max-width : 600px) {
  62. #content {
  63. width: 100%;
  64. }
  65. .list-item {
  66. margin: 0.5em;
  67. padding: 0.5em;
  68. flex-direction: column;
  69. -webkit-flex-direction: column;
  70. }
  71. .list-item .description {
  72. padding-left: 0em;
  73. }
  74. }
  75. </style>
  76. </head>
  77. <body>
  78. <canvas id="c"></canvas>
  79. <div id="content">
  80. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - multiple elements - webgl</div>
  81. </div>
  82. <script src="../build/three.min.js"></script>
  83. <script src="js/Detector.js"></script>
  84. <script id="template" type="notjs">
  85. <div class="scene"></div>
  86. <div class="description">some random text about this object, scene, whatever</div>
  87. </script>
  88. <script>
  89. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  90. var canvas;
  91. var scenes = [], camera, renderer, emptyScene;
  92. init();
  93. animate();
  94. function init() {
  95. canvas = document.getElementById( "c" );
  96. camera = new THREE.PerspectiveCamera( 75, 1, 0.1, 100 );
  97. camera.position.z = 1.5;
  98. var geometries = [
  99. new THREE.BoxGeometry( 1, 1, 1 ),
  100. new THREE.SphereGeometry( 0.5, 12, 12 ),
  101. new THREE.DodecahedronGeometry( 0.5 ),
  102. new THREE.CylinderGeometry( 0.5, 0.5, 1, 12 ),
  103. ];
  104. var template = document.getElementById("template").text;
  105. var content = document.getElementById("content");
  106. var emptyScene = new THREE.Scene();
  107. var numScenes = 100;
  108. for ( var ii = 0; ii < numScenes; ++ii ) {
  109. var scene = new THREE.Scene();
  110. // make a list item.
  111. var element = document.createElement( "div" );
  112. element.innerHTML = template;
  113. element.className = "list-item";
  114. // Look up the element that represents the area
  115. // we want to render the scene
  116. scene.element = element.querySelector(".scene");
  117. content.appendChild(element);
  118. // add one random mesh to each scene
  119. var geometry = geometries[ geometries.length * Math.random() | 0 ];
  120. var material = new THREE.MeshLambertMaterial( { color: randColor() } );
  121. scene.add( new THREE.Mesh( geometry, material ) );
  122. light = new THREE.DirectionalLight( 0xffffff );
  123. light.position.set( 0.5, 0.8, 1 );
  124. scene.add( light );
  125. light = new THREE.DirectionalLight( 0xffffff );
  126. light.position.set( -0.5, -0.8, -1 );
  127. scene.add( light );
  128. scenes.push( scene );
  129. }
  130. renderer = new THREE.WebGLRenderer( { canvas: canvas, antialias: true } );
  131. renderer.setClearColor( 0xFFFFFF );
  132. }
  133. function updateSize() {
  134. var width = canvas.clientWidth;
  135. var height = canvas.clientHeight;
  136. if ( canvas.width !== width || canvas.height != height ) {
  137. renderer.setSize ( width, height, false );
  138. }
  139. }
  140. function animate() {
  141. render();
  142. requestAnimationFrame( animate );
  143. }
  144. function render() {
  145. updateSize();
  146. renderer.setClearColor( 0xFFFFFF );
  147. renderer.clear( true );
  148. renderer.setClearColor( 0xE0E0E0 );
  149. renderer.enableScissorTest( true );
  150. scenes.forEach( function( scene ) {
  151. // so something moves
  152. scene.children[0].rotation.x = Date.now() * 0.00111;
  153. scene.children[0].rotation.z = Date.now() * 0.001;
  154. // get the element that is a place holder for where we want to
  155. // draw the scene
  156. var element = scene.element;
  157. // get its position relative to the page's viewport
  158. var rect = element.getBoundingClientRect();
  159. // check if it's offscreen. If so skip it
  160. if ( rect.bottom < 0 || rect.top > renderer.domElement.clientHeight ||
  161. rect.right < 0 || rect.left > renderer.domElement.clientWidth ) {
  162. return; // it's off screen
  163. }
  164. // set the viewport
  165. var width = rect.right - rect.left;
  166. var height = rect.bottom - rect.top;
  167. var left = rect.left;
  168. var bottom = renderer.domElement.clientHeight - rect.bottom;
  169. camera.aspect = width / height;
  170. camera.updateProjectionMatrix();
  171. renderer.setViewport( left, bottom, width, height );
  172. renderer.setScissor( left, bottom, width, height );
  173. renderer.render( scene, camera );
  174. } );
  175. renderer.enableScissorTest( false );
  176. }
  177. function rand( min, max ) {
  178. if ( max == undefined ) {
  179. max = min;
  180. min = 0;
  181. }
  182. return Math.random() * ( max - min ) + min;
  183. }
  184. function randColor() {
  185. var colors = [ rand( 256 ), rand ( 256 ), rand( 256 ) ];
  186. colors[ Math.random() * 3 | 0 ] = 255;
  187. return ( colors[0] << 16 ) |
  188. ( colors[1] << 8 ) |
  189. ( colors[2] << 0 ) ;
  190. }
  191. </script>
  192. </body>
  193. </html>