webgl_multiple_elements.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 > div:nth-child(1) {
  39. width: 200px;
  40. height: 200px;
  41. }
  42. .list-item > div:nth-child(2) {
  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 type="module">
  57. import * as THREE from '../build/three.module.js';
  58. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  59. let canvas, renderer;
  60. const scenes = [];
  61. init();
  62. animate();
  63. function init() {
  64. canvas = document.getElementById( "c" );
  65. const geometries = [
  66. new THREE.BoxGeometry( 1, 1, 1 ),
  67. new THREE.SphereGeometry( 0.5, 12, 8 ),
  68. new THREE.DodecahedronGeometry( 0.5 ),
  69. new THREE.CylinderGeometry( 0.5, 0.5, 1, 12 )
  70. ];
  71. const content = document.getElementById( 'content' );
  72. for ( let i = 0; i < 40; i ++ ) {
  73. const scene = new THREE.Scene();
  74. // make a list item
  75. const element = document.createElement( 'div' );
  76. element.className = 'list-item';
  77. const sceneElement = document.createElement( 'div' );
  78. element.appendChild( sceneElement );
  79. const descriptionElement = document.createElement( 'div' );
  80. descriptionElement.innerText = 'Scene ' + ( i + 1 );
  81. element.appendChild( descriptionElement );
  82. // the element that represents the area we want to render the scene
  83. scene.userData.element = sceneElement;
  84. content.appendChild( element );
  85. const camera = new THREE.PerspectiveCamera( 50, 1, 1, 10 );
  86. camera.position.z = 2;
  87. scene.userData.camera = camera;
  88. const controls = new OrbitControls( scene.userData.camera, scene.userData.element );
  89. controls.minDistance = 2;
  90. controls.maxDistance = 5;
  91. controls.enablePan = false;
  92. controls.enableZoom = false;
  93. scene.userData.controls = controls;
  94. // add one random mesh to each scene
  95. const geometry = geometries[ geometries.length * Math.random() | 0 ];
  96. const material = new THREE.MeshStandardMaterial( {
  97. color: new THREE.Color().setHSL( Math.random(), 1, 0.75 ),
  98. roughness: 0.5,
  99. metalness: 0,
  100. flatShading: true
  101. } );
  102. scene.add( new THREE.Mesh( geometry, material ) );
  103. scene.add( new THREE.HemisphereLight( 0xaaaaaa, 0x444444 ) );
  104. const light = new THREE.DirectionalLight( 0xffffff, 0.5 );
  105. light.position.set( 1, 1, 1 );
  106. scene.add( light );
  107. scenes.push( scene );
  108. }
  109. renderer = new THREE.WebGLRenderer( { canvas: canvas, antialias: true } );
  110. renderer.setClearColor( 0xffffff, 1 );
  111. renderer.setPixelRatio( window.devicePixelRatio );
  112. }
  113. function updateSize() {
  114. const width = canvas.clientWidth;
  115. const height = canvas.clientHeight;
  116. if ( canvas.width !== width || canvas.height !== height ) {
  117. renderer.setSize( width, height, false );
  118. }
  119. }
  120. function animate() {
  121. render();
  122. requestAnimationFrame( animate );
  123. }
  124. function render() {
  125. updateSize();
  126. canvas.style.transform = `translateY(${window.scrollY}px)`;
  127. renderer.setClearColor( 0xffffff );
  128. renderer.setScissorTest( false );
  129. renderer.clear();
  130. renderer.setClearColor( 0xe0e0e0 );
  131. renderer.setScissorTest( true );
  132. scenes.forEach( function ( scene ) {
  133. // so something moves
  134. scene.children[ 0 ].rotation.y = Date.now() * 0.001;
  135. // get the element that is a place holder for where we want to
  136. // draw the scene
  137. const element = scene.userData.element;
  138. // get its position relative to the page's viewport
  139. const rect = element.getBoundingClientRect();
  140. // check if it's offscreen. If so skip it
  141. if ( rect.bottom < 0 || rect.top > renderer.domElement.clientHeight ||
  142. rect.right < 0 || rect.left > renderer.domElement.clientWidth ) {
  143. return; // it's off screen
  144. }
  145. // set the viewport
  146. const width = rect.right - rect.left;
  147. const height = rect.bottom - rect.top;
  148. const left = rect.left;
  149. const bottom = renderer.domElement.clientHeight - rect.bottom;
  150. renderer.setViewport( left, bottom, width, height );
  151. renderer.setScissor( left, bottom, width, height );
  152. const camera = scene.userData.camera;
  153. //camera.aspect = width / height; // not changing in this example
  154. //camera.updateProjectionMatrix();
  155. //scene.userData.controls.update();
  156. renderer.render( scene, camera );
  157. } );
  158. }
  159. </script>
  160. </body>
  161. </html>