123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - scene - multiple - compare</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- .container {
- position: absolute;
- width: 100%;
- height: 100%;
- }
- .slider {
- position: absolute;
- cursor: ew-resize;
- width: 40px;
- height: 40px;
- background-color: #2196F3;
- opacity: 0.7;
- border-radius: 50%;
- top: calc(50% - 20px);
- left: calc(50% - 20px);
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - multiple scenes comparison<br />
- </div>
- <div class="container">
- <div class="slider"></div>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- var container, camera, renderer, controls;
- var sceneL, sceneR;
- var sliderPos = window.innerWidth / 2;
- var sliderMoved = false;
- function init() {
- container = document.querySelector( '.container' );
- sceneL = new THREE.Scene();
- sceneL.background = new THREE.Color( 0xff00ff );
- sceneR = new THREE.Scene();
- sceneR.background = new THREE.Color( 0x8FBCD4 );
- camera = new THREE.PerspectiveCamera( 35, container.clientWidth / container.clientHeight, 0.1, 10 );
- camera.position.set( 2, 4, 7 );
- controls = new OrbitControls( camera, container );
- initMeshes();
- initLights();
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setSize( container.clientWidth, container.clientHeight );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setScissorTest( true );
- container.appendChild( renderer.domElement );
- renderer.setAnimationLoop( function () {
- render();
- } );
- initComparisons();
- }
- function initMeshes() {
- var geoB = new THREE.BoxBufferGeometry( 2, 2, 2 );
- var matB = new THREE.MeshStandardMaterial( { color: 0x0000ff } );
- var meshB = new THREE.Mesh( geoB, matB );
- sceneL.add( meshB );
- var geoA = new THREE.IcosahedronBufferGeometry( 1, 0 );
- var matA = new THREE.MeshStandardMaterial( { color: 0xff0000 } );
- var meshA = new THREE.Mesh( geoA, matA );
- sceneR.add( meshA );
- }
- function initLights() {
- var light1 = new THREE.DirectionalLight();
- light1.position.set( 20, 20, 20 );
- sceneL.add( light1 );
- sceneR.add( light1.clone() );
- var light2 = new THREE.DirectionalLight();
- light2.position.set( - 20, 20, 20 );
- sceneL.add( light2 );
- sceneR.add( light2.clone() );
- }
- function render() {
- renderer.setScissor( 0, 0, sliderPos, window.innerHeight );
- renderer.render( sceneL, camera );
- renderer.setScissor( sliderPos, 0, window.innerWidth, window.innerHeight );
- renderer.render( sceneR, camera );
- }
- function onWindowResize() {
- camera.aspect = container.clientWidth / container.clientHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( container.clientWidth, container.clientHeight );
- if ( ! sliderMoved ) sliderPos = window.innerWidth / 2;
- }
- window.addEventListener( 'resize', onWindowResize );
- function initComparisons() {
- var slider = document.querySelector( '.slider' );
- var clicked = false;
- function slideReady() {
- clicked = true;
- controls.enabled = false;
- }
- function slideFinish() {
- clicked = false;
- controls.enabled = true;
- }
- function slideMove( e ) {
- if ( ! clicked ) return false;
- sliderMoved = true;
- sliderPos = e.pageX || e.touches[ 0 ].pageX;
- //prevent the slider from being positioned outside the window bounds
- if ( sliderPos < 0 ) sliderPos = 0;
- if ( sliderPos > window.innerWidth ) sliderPos = window.innerWidth;
- slider.style.left = sliderPos - ( slider.offsetWidth / 2 ) + "px";
- }
- slider.addEventListener( 'mousedown', slideReady );
- slider.addEventListener( 'touchstart', slideReady );
- window.addEventListener( 'mouseup', slideFinish );
- window.addEventListener( 'touchend', slideFinish );
- window.addEventListener( 'mousemove', slideMove );
- window.addEventListener( 'touchmove', slideMove );
- }
- init();
- </script>
- </body>
- </html>
|