2
0

webgl_multiple_scenes_comparison.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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: #F32196;
  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. <!-- Import maps polyfill -->
  35. <!-- Remove this when import maps will be widely supported -->
  36. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  37. <script type="importmap">
  38. {
  39. "imports": {
  40. "three": "../build/three.module.js"
  41. }
  42. }
  43. </script>
  44. <script type="module">
  45. import * as THREE from 'three';
  46. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  47. let container, camera, renderer, controls;
  48. let sceneL, sceneR;
  49. let sliderPos = window.innerWidth / 2;
  50. init();
  51. function init() {
  52. container = document.querySelector( '.container' );
  53. sceneL = new THREE.Scene();
  54. sceneL.background = new THREE.Color( 0xBCD48F );
  55. sceneR = new THREE.Scene();
  56. sceneR.background = new THREE.Color( 0x8FBCD4 );
  57. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 100 );
  58. camera.position.z = 6;
  59. controls = new OrbitControls( camera, container );
  60. const light = new THREE.HemisphereLight( 0xffffff, 0x444444, 1 );
  61. light.position.set( - 2, 2, 2 );
  62. sceneL.add( light.clone() );
  63. sceneR.add( light.clone() );
  64. initMeshes();
  65. initSlider();
  66. renderer = new THREE.WebGLRenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. renderer.setScissorTest( true );
  70. renderer.setAnimationLoop( render );
  71. container.appendChild( renderer.domElement );
  72. window.addEventListener( 'resize', onWindowResize );
  73. }
  74. function initMeshes() {
  75. const geometry = new THREE.IcosahedronGeometry( 1, 3 );
  76. const meshL = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
  77. sceneL.add( meshL );
  78. const meshR = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial( { wireframe: true } ) );
  79. sceneR.add( meshR );
  80. }
  81. function initSlider() {
  82. const slider = document.querySelector( '.slider' );
  83. function onPointerDown() {
  84. if ( event.isPrimary === false ) return;
  85. controls.enabled = false;
  86. window.addEventListener( 'pointermove', onPointerMove );
  87. window.addEventListener( 'pointerup', onPointerUp );
  88. }
  89. function onPointerUp() {
  90. controls.enabled = true;
  91. window.removeEventListener( 'pointermove', onPointerMove );
  92. window.removeEventListener( 'pointerup', onPointerUp );
  93. }
  94. function onPointerMove( e ) {
  95. if ( event.isPrimary === false ) return;
  96. sliderPos = Math.max( 0, Math.min( window.innerWidth, e.pageX ) );
  97. slider.style.left = sliderPos - ( slider.offsetWidth / 2 ) + "px";
  98. }
  99. slider.style.touchAction = 'none'; // disable touch scroll
  100. slider.addEventListener( 'pointerdown', onPointerDown );
  101. }
  102. function onWindowResize() {
  103. camera.aspect = window.innerWidth / window.innerHeight;
  104. camera.updateProjectionMatrix();
  105. renderer.setSize( window.innerWidth, window.innerHeight );
  106. }
  107. function render() {
  108. renderer.setScissor( 0, 0, sliderPos, window.innerHeight );
  109. renderer.render( sceneL, camera );
  110. renderer.setScissor( sliderPos, 0, window.innerWidth, window.innerHeight );
  111. renderer.render( sceneR, camera );
  112. }
  113. </script>
  114. </body>
  115. </html>