webgl_multiple_elements.html 5.4 KB

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