webgl_multiple_scenes_comparison.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - scene - multiple - compare</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. .container {
  10. position: absolute;
  11. width: 100%;
  12. height: 100%;
  13. }
  14. .slider {
  15. position: absolute;
  16. cursor: ew-resize;
  17. width: 40px;
  18. height: 40px;
  19. background-color: #2196F3;
  20. opacity: 0.7;
  21. border-radius: 50%;
  22. top: calc(50% - 20px);
  23. left: calc(50% - 20px);
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="info">
  29. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - multiple scenes comparison<br />
  30. </div>
  31. <div class="container">
  32. <div class="slider"></div>
  33. </div>
  34. <script type="module">
  35. import * as THREE from '../build/three.module.js';
  36. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  37. var container, camera, renderer, controls;
  38. var sceneL, sceneR;
  39. var sliderPos = window.innerWidth / 2;
  40. var sliderMoved = false;
  41. function init() {
  42. container = document.querySelector( '.container' );
  43. sceneL = new THREE.Scene();
  44. sceneL.background = new THREE.Color( 0xff00ff );
  45. sceneR = new THREE.Scene();
  46. sceneR.background = new THREE.Color( 0x8FBCD4 );
  47. camera = new THREE.PerspectiveCamera( 35, container.clientWidth / container.clientHeight, 0.1, 10 );
  48. camera.position.set( 2, 4, 7 );
  49. controls = new OrbitControls( camera, container );
  50. initMeshes();
  51. initLights();
  52. renderer = new THREE.WebGLRenderer( { antialias: true } );
  53. renderer.setSize( container.clientWidth, container.clientHeight );
  54. renderer.setPixelRatio( window.devicePixelRatio );
  55. renderer.setScissorTest( true );
  56. container.appendChild( renderer.domElement );
  57. renderer.setAnimationLoop( function () {
  58. render();
  59. } );
  60. initComparisons();
  61. }
  62. function initMeshes() {
  63. var geoB = new THREE.BoxBufferGeometry( 2, 2, 2 );
  64. var matB = new THREE.MeshStandardMaterial( { color: 0x0000ff } );
  65. var meshB = new THREE.Mesh( geoB, matB );
  66. sceneL.add( meshB );
  67. var geoA = new THREE.IcosahedronBufferGeometry( 1, 0 );
  68. var matA = new THREE.MeshStandardMaterial( { color: 0xff0000 } );
  69. var meshA = new THREE.Mesh( geoA, matA );
  70. sceneR.add( meshA );
  71. }
  72. function initLights() {
  73. var light1 = new THREE.DirectionalLight();
  74. light1.position.set( 20, 20, 20 );
  75. sceneL.add( light1 );
  76. sceneR.add( light1.clone() );
  77. var light2 = new THREE.DirectionalLight();
  78. light2.position.set( - 20, 20, 20 );
  79. sceneL.add( light2 );
  80. sceneR.add( light2.clone() );
  81. }
  82. function render() {
  83. renderer.setScissor( 0, 0, sliderPos, window.innerHeight );
  84. renderer.render( sceneL, camera );
  85. renderer.setScissor( sliderPos, 0, window.innerWidth, window.innerHeight );
  86. renderer.render( sceneR, camera );
  87. }
  88. function onWindowResize() {
  89. camera.aspect = container.clientWidth / container.clientHeight;
  90. camera.updateProjectionMatrix();
  91. renderer.setSize( container.clientWidth, container.clientHeight );
  92. if ( ! sliderMoved ) sliderPos = window.innerWidth / 2;
  93. }
  94. window.addEventListener( 'resize', onWindowResize );
  95. function initComparisons() {
  96. var slider = document.querySelector( '.slider' );
  97. var clicked = false;
  98. function slideReady() {
  99. clicked = true;
  100. controls.enabled = false;
  101. }
  102. function slideFinish() {
  103. clicked = false;
  104. controls.enabled = true;
  105. }
  106. function slideMove( e ) {
  107. if ( ! clicked ) return false;
  108. sliderMoved = true;
  109. sliderPos = e.pageX || e.touches[ 0 ].pageX;
  110. //prevent the slider from being positioned outside the window bounds
  111. if ( sliderPos < 0 ) sliderPos = 0;
  112. if ( sliderPos > window.innerWidth ) sliderPos = window.innerWidth;
  113. slider.style.left = sliderPos - ( slider.offsetWidth / 2 ) + "px";
  114. }
  115. slider.addEventListener( 'mousedown', slideReady );
  116. slider.addEventListener( 'touchstart', slideReady );
  117. window.addEventListener( 'mouseup', slideFinish );
  118. window.addEventListener( 'touchend', slideFinish );
  119. window.addEventListener( 'mousemove', slideMove );
  120. window.addEventListener( 'touchmove', slideMove );
  121. }
  122. init();
  123. </script>
  124. </body>
  125. </html>