webgl_multiple_elements.html 5.7 KB

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