2
0

multiple-scenes-controls.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. "three/addons/": "../../examples/jsm/"
  59. }
  60. }
  61. </script>
  62. <script type="module">
  63. import * as THREE from 'three';
  64. import {TrackballControls} from 'three/addons/controls/TrackballControls.js';
  65. function main() {
  66. const canvas = document.querySelector('#c');
  67. const renderer = new THREE.WebGLRenderer({antialias: true, canvas, alpha: true});
  68. const sceneElements = [];
  69. function addScene(elem, fn) {
  70. sceneElements.push({elem, fn});
  71. }
  72. function makeScene(elem) {
  73. const scene = new THREE.Scene();
  74. const fov = 45;
  75. const aspect = 2; // the canvas default
  76. const near = 0.1;
  77. const far = 5;
  78. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  79. camera.position.set(0, 1, 2);
  80. camera.lookAt(0, 0, 0);
  81. scene.add(camera);
  82. const controls = new TrackballControls(camera, elem);
  83. controls.noZoom = true;
  84. controls.noPan = true;
  85. {
  86. const color = 0xFFFFFF;
  87. const intensity = 1;
  88. const light = new THREE.DirectionalLight(color, intensity);
  89. light.position.set(-1, 2, 4);
  90. camera.add(light);
  91. }
  92. return {scene, camera, controls};
  93. }
  94. const sceneInitFunctionsByName = {
  95. 'box': (elem) => {
  96. const {scene, camera, controls} = makeScene(elem);
  97. const geometry = new THREE.BoxGeometry(1, 1, 1);
  98. const material = new THREE.MeshPhongMaterial({color: 'red'});
  99. const mesh = new THREE.Mesh(geometry, material);
  100. scene.add(mesh);
  101. return (time, rect) => {
  102. mesh.rotation.y = time * .1;
  103. camera.aspect = rect.width / rect.height;
  104. camera.updateProjectionMatrix();
  105. controls.handleResize();
  106. controls.update();
  107. renderer.render(scene, camera);
  108. };
  109. },
  110. 'pyramid': (elem) => {
  111. const {scene, camera, controls} = makeScene(elem);
  112. const radius = .8;
  113. const widthSegments = 4;
  114. const heightSegments = 2;
  115. const geometry = new THREE.SphereGeometry(radius, widthSegments, heightSegments);
  116. const material = new THREE.MeshPhongMaterial({
  117. color: 'blue',
  118. flatShading: true,
  119. });
  120. const mesh = new THREE.Mesh(geometry, material);
  121. scene.add(mesh);
  122. return (time, rect) => {
  123. mesh.rotation.y = time * .1;
  124. camera.aspect = rect.width / rect.height;
  125. camera.updateProjectionMatrix();
  126. controls.handleResize();
  127. controls.update();
  128. renderer.render(scene, camera);
  129. };
  130. },
  131. };
  132. document.querySelectorAll('[data-diagram]').forEach((elem) => {
  133. const sceneName = elem.dataset.diagram;
  134. const sceneInitFunction = sceneInitFunctionsByName[sceneName];
  135. const sceneRenderFunction = sceneInitFunction(elem);
  136. addScene(elem, sceneRenderFunction);
  137. });
  138. function resizeRendererToDisplaySize(renderer) {
  139. const canvas = renderer.domElement;
  140. const width = canvas.clientWidth;
  141. const height = canvas.clientHeight;
  142. const needResize = canvas.width !== width || canvas.height !== height;
  143. if (needResize) {
  144. renderer.setSize(width, height, false);
  145. }
  146. return needResize;
  147. }
  148. const clearColor = new THREE.Color('#000');
  149. function render(time) {
  150. time *= 0.001;
  151. resizeRendererToDisplaySize(renderer);
  152. renderer.setScissorTest(false);
  153. renderer.setClearColor(clearColor, 0);
  154. renderer.clear(true, true);
  155. renderer.setScissorTest(true);
  156. const transform = `translateY(${window.scrollY}px)`;
  157. renderer.domElement.style.transform = transform;
  158. for (const {elem, fn} of sceneElements) {
  159. // get the viewport relative position of this element
  160. const rect = elem.getBoundingClientRect();
  161. const {left, right, top, bottom, width, height} = rect;
  162. const isOffscreen =
  163. bottom < 0 ||
  164. top > renderer.domElement.clientHeight ||
  165. right < 0 ||
  166. left > renderer.domElement.clientWidth;
  167. if (!isOffscreen) {
  168. const positiveYUpBottom = renderer.domElement.clientHeight - bottom;
  169. renderer.setScissor(left, positiveYUpBottom, width, height);
  170. renderer.setViewport(left, positiveYUpBottom, width, height);
  171. fn(time, rect);
  172. }
  173. }
  174. requestAnimationFrame(render);
  175. }
  176. requestAnimationFrame(render);
  177. }
  178. main();
  179. </script>
  180. </html>