multiple-scenes-v1.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 - v1</title>
  8. <style>
  9. #c {
  10. position: fixed;
  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. function makeScene(elem) {
  57. const scene = new THREE.Scene();
  58. const fov = 45;
  59. const aspect = 2; // the canvas default
  60. const near = 0.1;
  61. const far = 5;
  62. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  63. camera.position.set(0, 1, 2);
  64. camera.lookAt(0, 0, 0);
  65. {
  66. const color = 0xFFFFFF;
  67. const intensity = 1;
  68. const light = new THREE.DirectionalLight(color, intensity);
  69. light.position.set(-1, 2, 4);
  70. scene.add(light);
  71. }
  72. return {scene, camera, elem};
  73. }
  74. function setupScene1() {
  75. const sceneInfo = makeScene(document.querySelector('#box'));
  76. const geometry = new THREE.BoxGeometry(1, 1, 1);
  77. const material = new THREE.MeshPhongMaterial({color: 'red'});
  78. const mesh = new THREE.Mesh(geometry, material);
  79. sceneInfo.scene.add(mesh);
  80. sceneInfo.mesh = mesh;
  81. return sceneInfo;
  82. }
  83. function setupScene2() {
  84. const sceneInfo = makeScene(document.querySelector('#pyramid'));
  85. const radius = .8;
  86. const widthSegments = 4;
  87. const heightSegments = 2;
  88. const geometry = new THREE.SphereGeometry(radius, widthSegments, heightSegments);
  89. const material = new THREE.MeshPhongMaterial({
  90. color: 'blue',
  91. flatShading: true,
  92. });
  93. const mesh = new THREE.Mesh(geometry, material);
  94. sceneInfo.scene.add(mesh);
  95. sceneInfo.mesh = mesh;
  96. return sceneInfo;
  97. }
  98. const sceneInfo1 = setupScene1();
  99. const sceneInfo2 = setupScene2();
  100. function resizeRendererToDisplaySize(renderer) {
  101. const canvas = renderer.domElement;
  102. const width = canvas.clientWidth;
  103. const height = canvas.clientHeight;
  104. const needResize = canvas.width !== width || canvas.height !== height;
  105. if (needResize) {
  106. renderer.setSize(width, height, false);
  107. }
  108. return needResize;
  109. }
  110. function renderSceneInfo(sceneInfo) {
  111. const {scene, camera, elem} = sceneInfo;
  112. // get the viewport relative position of this element
  113. const {left, right, top, bottom, width, height} =
  114. elem.getBoundingClientRect();
  115. const isOffscreen =
  116. bottom < 0 ||
  117. top > renderer.domElement.clientHeight ||
  118. right < 0 ||
  119. left > renderer.domElement.clientWidth;
  120. if (isOffscreen) {
  121. return;
  122. }
  123. camera.aspect = width / height;
  124. camera.updateProjectionMatrix();
  125. const positiveYUpBottom = renderer.domElement.clientHeight - bottom;
  126. renderer.setScissor(left, positiveYUpBottom, width, height);
  127. renderer.setViewport(left, positiveYUpBottom, width, height);
  128. renderer.render(scene, camera);
  129. }
  130. function render(time) {
  131. time *= 0.001;
  132. resizeRendererToDisplaySize(renderer);
  133. renderer.setScissorTest(false);
  134. renderer.clear(true, true);
  135. renderer.setScissorTest(true);
  136. sceneInfo1.mesh.rotation.y = time * .1;
  137. sceneInfo2.mesh.rotation.y = time * .1;
  138. renderSceneInfo(sceneInfo1);
  139. renderSceneInfo(sceneInfo2);
  140. requestAnimationFrame(render);
  141. }
  142. requestAnimationFrame(render);
  143. }
  144. main();
  145. </script>
  146. </html>