webgl_multiple_elements.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. * {
  10. box-sizing: border-box;
  11. -moz-box-sizing: border-box;
  12. }
  13. body {
  14. background-color: #fff;
  15. color: #444;
  16. }
  17. a {
  18. color: #08f;
  19. }
  20. #content {
  21. position: absolute;
  22. top: 0; width: 100%;
  23. z-index: 1;
  24. padding: 3em 0 0 0;
  25. }
  26. #c {
  27. position: absolute;
  28. left: 0;
  29. width: 100%;
  30. height: 100%;
  31. }
  32. .list-item {
  33. display: inline-block;
  34. margin: 1em;
  35. padding: 1em;
  36. box-shadow: 1px 2px 4px 0px rgba(0,0,0,0.25);
  37. }
  38. .list-item .scene {
  39. width: 200px;
  40. height: 200px;
  41. }
  42. .list-item .description {
  43. color: #888;
  44. font-family: sans-serif;
  45. font-size: large;
  46. width: 200px;
  47. margin-top: 0.5em;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <canvas id="c"></canvas>
  53. <div id="content">
  54. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - multiple elements - webgl</div>
  55. </div>
  56. <script id="template" type="notjs">
  57. <div class="scene"></div>
  58. <div class="description">Scene $</div>
  59. </script>
  60. <script type="module">
  61. import * as THREE from '../build/three.module.js';
  62. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  63. var canvas;
  64. var scenes = [], renderer;
  65. init();
  66. animate();
  67. function init() {
  68. canvas = document.getElementById( "c" );
  69. var geometries = [
  70. new THREE.BoxBufferGeometry( 1, 1, 1 ),
  71. new THREE.SphereBufferGeometry( 0.5, 12, 8 ),
  72. new THREE.DodecahedronBufferGeometry( 0.5 ),
  73. new THREE.CylinderBufferGeometry( 0.5, 0.5, 1, 12 )
  74. ];
  75. var template = document.getElementById( "template" ).text;
  76. var content = document.getElementById( "content" );
  77. for ( var i = 0; i < 40; i ++ ) {
  78. var scene = new THREE.Scene();
  79. // make a list item
  80. var element = document.createElement( "div" );
  81. element.className = "list-item";
  82. element.innerHTML = template.replace( '$', i + 1 );
  83. // Look up the element that represents the area
  84. // we want to render the scene
  85. scene.userData.element = element.querySelector( ".scene" );
  86. content.appendChild( element );
  87. var camera = new THREE.PerspectiveCamera( 50, 1, 1, 10 );
  88. camera.position.z = 2;
  89. scene.userData.camera = camera;
  90. var controls = new OrbitControls( scene.userData.camera, scene.userData.element );
  91. controls.minDistance = 2;
  92. controls.maxDistance = 5;
  93. controls.enablePan = false;
  94. controls.enableZoom = false;
  95. scene.userData.controls = controls;
  96. // add one random mesh to each scene
  97. var geometry = geometries[ geometries.length * Math.random() | 0 ];
  98. var material = new THREE.MeshStandardMaterial( {
  99. color: new THREE.Color().setHSL( Math.random(), 1, 0.75 ),
  100. roughness: 0.5,
  101. metalness: 0,
  102. flatShading: true
  103. } );
  104. scene.add( new THREE.Mesh( geometry, material ) );
  105. scene.add( new THREE.HemisphereLight( 0xaaaaaa, 0x444444 ) );
  106. var light = new THREE.DirectionalLight( 0xffffff, 0.5 );
  107. light.position.set( 1, 1, 1 );
  108. scene.add( light );
  109. scenes.push( scene );
  110. }
  111. renderer = new THREE.WebGLRenderer( { canvas: canvas, antialias: true } );
  112. renderer.setClearColor( 0xffffff, 1 );
  113. renderer.setPixelRatio( window.devicePixelRatio );
  114. }
  115. function updateSize() {
  116. var width = canvas.clientWidth;
  117. var height = canvas.clientHeight;
  118. if ( canvas.width !== width || canvas.height !== height ) {
  119. renderer.setSize( width, height, false );
  120. }
  121. }
  122. function animate() {
  123. render();
  124. requestAnimationFrame( animate );
  125. }
  126. function render() {
  127. updateSize();
  128. canvas.style.transform = `translateY(${window.scrollY}px)`;
  129. renderer.setClearColor( 0xffffff );
  130. renderer.setScissorTest( false );
  131. renderer.clear();
  132. renderer.setClearColor( 0xe0e0e0 );
  133. renderer.setScissorTest( true );
  134. scenes.forEach( function ( scene ) {
  135. // so something moves
  136. scene.children[ 0 ].rotation.y = Date.now() * 0.001;
  137. // get the element that is a place holder for where we want to
  138. // draw the scene
  139. var element = scene.userData.element;
  140. // get its position relative to the page's viewport
  141. var rect = element.getBoundingClientRect();
  142. // check if it's offscreen. If so skip it
  143. if ( rect.bottom < 0 || rect.top > renderer.domElement.clientHeight ||
  144. rect.right < 0 || rect.left > renderer.domElement.clientWidth ) {
  145. return; // it's off screen
  146. }
  147. // set the viewport
  148. var width = rect.right - rect.left;
  149. var height = rect.bottom - rect.top;
  150. var left = rect.left;
  151. var bottom = renderer.domElement.clientHeight - rect.bottom;
  152. renderer.setViewport( left, bottom, width, height );
  153. renderer.setScissor( left, bottom, width, height );
  154. var camera = scene.userData.camera;
  155. //camera.aspect = width / height; // not changing in this example
  156. //camera.updateProjectionMatrix();
  157. //scene.userData.controls.update();
  158. renderer.render( scene, camera );
  159. } );
  160. }
  161. </script>
  162. </body>
  163. </html>