threejs-multiple-scenes-controls.html 5.4 KB

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