multiple-scenes-controls.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Multiple Scenes - Controls</title>
  8. <style>
  9. #c {
  10. position: absolute;
  11. left: 0;
  12. top: 0;
  13. width: 100%;
  14. height: 100%;
  15. display: block;
  16. z-index: -1;
  17. }
  18. *[data-diagram] {
  19. display: inline-block;
  20. width: 5em;
  21. height: 3em;
  22. }
  23. .left {
  24. float: left;
  25. margin-right: .25em;
  26. }
  27. .right {
  28. float: right;
  29. margin-left: .25em;
  30. }
  31. p {
  32. margin: 1em auto;
  33. max-width: 500px;
  34. font-size: xx-large;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <canvas id="c"></canvas>
  40. <p>
  41. <span data-diagram="box" class="left"></span>
  42. I love boxes. Presents come in boxes.
  43. When I find a new box I'm always excited to find out what's inside.
  44. </p>
  45. <p>
  46. <span data-diagram="pyramid" class="right"></span>
  47. When I was a kid I dreamed of going on an expedition inside a pyramid
  48. and finding a undiscovered tomb full of mummies and treasure.
  49. </p>
  50. </body>
  51. <!-- Import maps polyfill -->
  52. <!-- Remove this when import maps will be widely supported -->
  53. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  54. <script type="importmap">
  55. {
  56. "imports": {
  57. "three": "../../build/three.module.js"
  58. }
  59. }
  60. </script>
  61. <script type="module">
  62. import * as THREE from 'three';
  63. import {TrackballControls} from '../../examples/jsm/controls/TrackballControls.js';
  64. function main() {
  65. const canvas = document.querySelector('#c');
  66. const renderer = new THREE.WebGLRenderer({canvas, alpha: true});
  67. const sceneElements = [];
  68. function addScene(elem, fn) {
  69. sceneElements.push({elem, fn});
  70. }
  71. function makeScene(elem) {
  72. const scene = new THREE.Scene();
  73. const fov = 45;
  74. const aspect = 2; // the canvas default
  75. const near = 0.1;
  76. const far = 5;
  77. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  78. camera.position.set(0, 1, 2);
  79. camera.lookAt(0, 0, 0);
  80. scene.add(camera);
  81. const controls = new TrackballControls(camera, elem);
  82. controls.noZoom = true;
  83. controls.noPan = true;
  84. {
  85. const color = 0xFFFFFF;
  86. const intensity = 1;
  87. const light = new THREE.DirectionalLight(color, intensity);
  88. light.position.set(-1, 2, 4);
  89. camera.add(light);
  90. }
  91. return {scene, camera, controls};
  92. }
  93. const sceneInitFunctionsByName = {
  94. 'box': (elem) => {
  95. const {scene, camera, controls} = makeScene(elem);
  96. const geometry = new THREE.BoxGeometry(1, 1, 1);
  97. const material = new THREE.MeshPhongMaterial({color: 'red'});
  98. const mesh = new THREE.Mesh(geometry, material);
  99. scene.add(mesh);
  100. return (time, rect) => {
  101. mesh.rotation.y = time * .1;
  102. camera.aspect = rect.width / rect.height;
  103. camera.updateProjectionMatrix();
  104. controls.handleResize();
  105. controls.update();
  106. renderer.render(scene, camera);
  107. };
  108. },
  109. 'pyramid': (elem) => {
  110. const {scene, camera, controls} = makeScene(elem);
  111. const radius = .8;
  112. const widthSegments = 4;
  113. const heightSegments = 2;
  114. const geometry = new THREE.SphereGeometry(radius, widthSegments, heightSegments);
  115. const material = new THREE.MeshPhongMaterial({
  116. color: 'blue',
  117. flatShading: true,
  118. });
  119. const mesh = new THREE.Mesh(geometry, material);
  120. scene.add(mesh);
  121. return (time, rect) => {
  122. mesh.rotation.y = time * .1;
  123. camera.aspect = rect.width / rect.height;
  124. camera.updateProjectionMatrix();
  125. controls.handleResize();
  126. controls.update();
  127. renderer.render(scene, camera);
  128. };
  129. },
  130. };
  131. document.querySelectorAll('[data-diagram]').forEach((elem) => {
  132. const sceneName = elem.dataset.diagram;
  133. const sceneInitFunction = sceneInitFunctionsByName[sceneName];
  134. const sceneRenderFunction = sceneInitFunction(elem);
  135. addScene(elem, sceneRenderFunction);
  136. });
  137. function resizeRendererToDisplaySize(renderer) {
  138. const canvas = renderer.domElement;
  139. const width = canvas.clientWidth;
  140. const height = canvas.clientHeight;
  141. const needResize = canvas.width !== width || canvas.height !== height;
  142. if (needResize) {
  143. renderer.setSize(width, height, false);
  144. }
  145. return needResize;
  146. }
  147. const clearColor = new THREE.Color('#000');
  148. function render(time) {
  149. time *= 0.001;
  150. resizeRendererToDisplaySize(renderer);
  151. renderer.setScissorTest(false);
  152. renderer.setClearColor(clearColor, 0);
  153. renderer.clear(true, true);
  154. renderer.setScissorTest(true);
  155. const transform = `translateY(${window.scrollY}px)`;
  156. renderer.domElement.style.transform = transform;
  157. for (const {elem, fn} of sceneElements) {
  158. // get the viewport relative position of this element
  159. const rect = elem.getBoundingClientRect();
  160. const {left, right, top, bottom, width, height} = rect;
  161. const isOffscreen =
  162. bottom < 0 ||
  163. top > renderer.domElement.clientHeight ||
  164. right < 0 ||
  165. left > renderer.domElement.clientWidth;
  166. if (!isOffscreen) {
  167. const positiveYUpBottom = renderer.domElement.clientHeight - bottom;
  168. renderer.setScissor(left, positiveYUpBottom, width, height);
  169. renderer.setViewport(left, positiveYUpBottom, width, height);
  170. fn(time, rect);
  171. }
  172. }
  173. requestAnimationFrame(render);
  174. }
  175. requestAnimationFrame(render);
  176. }
  177. main();
  178. </script>
  179. </html>