multiple-scenes-generic.html 4.6 KB

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