multiple-scenes-copy-canvas.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 - Copy</title>
  8. <style>
  9. canvas {
  10. width: 100%;
  11. height: 100%;
  12. display: block;
  13. }
  14. *[data-diagram] {
  15. display: inline-block;
  16. width: 5em;
  17. height: 3em;
  18. }
  19. .left {
  20. float: left;
  21. margin-right: .25em;
  22. }
  23. .right {
  24. float: right;
  25. margin-left: .25em;
  26. }
  27. p {
  28. margin: 1em auto;
  29. max-width: 500px;
  30. font-size: xx-large;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <p>
  36. <span data-diagram="box" class="left"></span>
  37. I love boxes. Presents come in boxes.
  38. When I find a new box I'm always excited to find out what's inside.
  39. </p>
  40. <p>
  41. <span data-diagram="pyramid" class="right"></span>
  42. When I was a kid I dreamed of going on an expedition inside a pyramid
  43. and finding a undiscovered tomb full of mummies and treasure.
  44. </p>
  45. </body>
  46. <!-- Import maps polyfill -->
  47. <!-- Remove this when import maps will be widely supported -->
  48. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  49. <script type="importmap">
  50. {
  51. "imports": {
  52. "three": "../../build/three.module.js",
  53. "three/addons/": "../../examples/jsm/"
  54. }
  55. }
  56. </script>
  57. <script type="module">
  58. import * as THREE from 'three';
  59. import { TrackballControls } from 'three/addons/controls/TrackballControls.js';
  60. function main() {
  61. const canvas = document.createElement( 'canvas' );
  62. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas, alpha: true } );
  63. renderer.useLegacyLights = false;
  64. renderer.setScissorTest( true );
  65. const sceneElements = [];
  66. function addScene( elem, fn ) {
  67. const ctx = document.createElement( 'canvas' ).getContext( '2d' );
  68. elem.appendChild( ctx.canvas );
  69. sceneElements.push( { elem, ctx, fn } );
  70. }
  71. function makeScene( elem ) {
  72. const scene = new THREE.Scene();
  73. const fov = 45;
  74. const aspect = 2; // the canvas default
  75. const near = 0.1;
  76. const far = 5;
  77. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  78. camera.position.set( 0, 1, 2 );
  79. camera.lookAt( 0, 0, 0 );
  80. scene.add( camera );
  81. const controls = new TrackballControls( camera, elem );
  82. controls.noZoom = true;
  83. controls.noPan = true;
  84. {
  85. const color = 0xFFFFFF;
  86. const intensity = 3;
  87. const light = new THREE.DirectionalLight( color, intensity );
  88. light.position.set( - 1, 2, 4 );
  89. camera.add( light );
  90. }
  91. return { scene, camera, controls };
  92. }
  93. const sceneInitFunctionsByName = {
  94. 'box': ( elem ) => {
  95. const { scene, camera, controls } = makeScene( elem );
  96. const geometry = new THREE.BoxGeometry( 1, 1, 1 );
  97. const material = new THREE.MeshPhongMaterial( { color: 'red' } );
  98. const mesh = new THREE.Mesh( geometry, material );
  99. scene.add( mesh );
  100. return ( time, rect ) => {
  101. mesh.rotation.y = time * .1;
  102. camera.aspect = rect.width / rect.height;
  103. camera.updateProjectionMatrix();
  104. controls.handleResize();
  105. controls.update();
  106. renderer.render( scene, camera );
  107. };
  108. },
  109. 'pyramid': ( elem ) => {
  110. const { scene, camera, controls } = makeScene( elem );
  111. const radius = .8;
  112. const widthSegments = 4;
  113. const heightSegments = 2;
  114. const geometry = new THREE.SphereGeometry( radius, widthSegments, heightSegments );
  115. const material = new THREE.MeshPhongMaterial( {
  116. color: 'blue',
  117. flatShading: true,
  118. } );
  119. const mesh = new THREE.Mesh( geometry, material );
  120. scene.add( mesh );
  121. return ( time, rect ) => {
  122. mesh.rotation.y = time * .1;
  123. camera.aspect = rect.width / rect.height;
  124. camera.updateProjectionMatrix();
  125. controls.handleResize();
  126. controls.update();
  127. renderer.render( scene, camera );
  128. };
  129. },
  130. };
  131. document.querySelectorAll( '[data-diagram]' ).forEach( ( elem ) => {
  132. const sceneName = elem.dataset.diagram;
  133. const sceneInitFunction = sceneInitFunctionsByName[ sceneName ];
  134. const sceneRenderFunction = sceneInitFunction( elem );
  135. addScene( elem, sceneRenderFunction );
  136. } );
  137. function render( time ) {
  138. time *= 0.001;
  139. for ( const { elem, fn, ctx } of sceneElements ) {
  140. // get the viewport relative position of this element
  141. const rect = elem.getBoundingClientRect();
  142. const { left, right, top, bottom, width, height } = rect;
  143. const rendererCanvas = renderer.domElement;
  144. const isOffscreen =
  145. bottom < 0 ||
  146. top > window.innerHeight ||
  147. right < 0 ||
  148. left > window.innerWidth;
  149. if ( ! isOffscreen ) {
  150. // make sure the renderer's canvas is big enough
  151. if ( rendererCanvas.width < width || rendererCanvas.height < height ) {
  152. renderer.setSize( width, height, false );
  153. }
  154. // make sure the canvas for this area is the same size as the area
  155. if ( ctx.canvas.width !== width || ctx.canvas.height !== height ) {
  156. ctx.canvas.width = width;
  157. ctx.canvas.height = height;
  158. }
  159. renderer.setScissor( 0, 0, width, height );
  160. renderer.setViewport( 0, 0, width, height );
  161. fn( time, rect );
  162. // copy the rendered scene to this element's canvas
  163. ctx.globalCompositeOperation = 'copy';
  164. ctx.drawImage(
  165. rendererCanvas,
  166. 0, rendererCanvas.height - height, width, height, // src rect
  167. 0, 0, width, height ); // dst rect
  168. }
  169. }
  170. requestAnimationFrame( render );
  171. }
  172. requestAnimationFrame( render );
  173. }
  174. main();
  175. </script>
  176. </html>