threejs-multiple-scenes-generic.html 4.7 KB

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