webgl_multiple_elements.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. <meta property="og:title" content="three.js webgl - multiple elements">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_multiple_elements.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_multiple_elements.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. * {
  14. box-sizing: border-box;
  15. -moz-box-sizing: border-box;
  16. }
  17. body {
  18. background-color: #fff;
  19. color: #444;
  20. }
  21. a {
  22. color: #08f;
  23. }
  24. #content {
  25. position: absolute;
  26. top: 0; width: 100%;
  27. z-index: 1;
  28. padding: 3em 0 0 0;
  29. }
  30. #c {
  31. position: absolute;
  32. left: 0;
  33. width: 100%;
  34. height: 100%;
  35. }
  36. .list-item {
  37. display: inline-block;
  38. margin: 1em;
  39. padding: 1em;
  40. box-shadow: 1px 2px 4px 0px rgba(0,0,0,0.25);
  41. }
  42. .list-item > div:nth-child(1) {
  43. width: 200px;
  44. height: 200px;
  45. }
  46. .list-item > div:nth-child(2) {
  47. color: #888;
  48. font-family: sans-serif;
  49. font-size: large;
  50. width: 200px;
  51. margin-top: 0.5em;
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <canvas id="c"></canvas>
  57. <div id="content">
  58. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - multiple elements - webgl</div>
  59. </div>
  60. <script type="importmap">
  61. {
  62. "imports": {
  63. "three": "../build/three.module.js",
  64. "three/addons/": "./jsm/"
  65. }
  66. }
  67. </script>
  68. <script type="module">
  69. import * as THREE from 'three';
  70. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  71. let canvas, renderer;
  72. const scenes = [];
  73. init();
  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.setAnimationLoop( animate );
  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. updateSize();
  134. canvas.style.transform = `translateY(${window.scrollY}px)`;
  135. renderer.setClearColor( 0xffffff );
  136. renderer.setScissorTest( false );
  137. renderer.clear();
  138. renderer.setClearColor( 0xe0e0e0 );
  139. renderer.setScissorTest( true );
  140. scenes.forEach( function ( scene ) {
  141. // so something moves
  142. scene.children[ 0 ].rotation.y = Date.now() * 0.001;
  143. // get the element that is a place holder for where we want to
  144. // draw the scene
  145. const element = scene.userData.element;
  146. // get its position relative to the page's viewport
  147. const rect = element.getBoundingClientRect();
  148. // check if it's offscreen. If so skip it
  149. if ( rect.bottom < 0 || rect.top > renderer.domElement.clientHeight ||
  150. rect.right < 0 || rect.left > renderer.domElement.clientWidth ) {
  151. return; // it's off screen
  152. }
  153. // set the viewport
  154. const width = rect.right - rect.left;
  155. const height = rect.bottom - rect.top;
  156. const left = rect.left;
  157. const bottom = renderer.domElement.clientHeight - rect.bottom;
  158. renderer.setViewport( left, bottom, width, height );
  159. renderer.setScissor( left, bottom, width, height );
  160. const camera = scene.userData.camera;
  161. //camera.aspect = width / height; // not changing in this example
  162. //camera.updateProjectionMatrix();
  163. //scene.userData.controls.update();
  164. renderer.render( scene, camera );
  165. } );
  166. }
  167. </script>
  168. </body>
  169. </html>
粤ICP备19079148号