You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

173 lines
4.1 KiB

<!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: #F32196;
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>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
let container, camera, renderer, controls;
let sceneL, sceneR;
let sliderPos = window.innerWidth / 2;
init();
function init() {
container = document.querySelector( '.container' );
sceneL = new THREE.Scene();
sceneL.background = new THREE.Color( 0xBCD48F );
sceneR = new THREE.Scene();
sceneR.background = new THREE.Color( 0x8FBCD4 );
camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 100 );
camera.position.z = 6;
controls = new OrbitControls( camera, container );
const light = new THREE.HemisphereLight( 0xffffff, 0x444444, 1 );
light.position.set( - 2, 2, 2 );
sceneL.add( light.clone() );
sceneR.add( light.clone() );
initMeshes();
initSlider();
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setScissorTest( true );
renderer.setAnimationLoop( render );
container.appendChild( renderer.domElement );
window.addEventListener( 'resize', onWindowResize );
}
function initMeshes() {
const geometry = new THREE.IcosahedronGeometry( 1, 3 );
const meshL = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial() );
sceneL.add( meshL );
const meshR = new THREE.Mesh( geometry, new THREE.MeshStandardMaterial( { wireframe: true } ) );
sceneR.add( meshR );
}
function initSlider() {
const slider = document.querySelector( '.slider' );
function onPointerDown() {
if ( event.isPrimary === false ) return;
controls.enabled = false;
window.addEventListener( 'pointermove', onPointerMove );
window.addEventListener( 'pointerup', onPointerUp );
}
function onPointerUp() {
controls.enabled = true;
window.removeEventListener( 'pointermove', onPointerMove );
window.removeEventListener( 'pointerup', onPointerUp );
}
function onPointerMove( e ) {
if ( event.isPrimary === false ) return;
sliderPos = Math.max( 0, Math.min( window.innerWidth, e.pageX ) );
slider.style.left = sliderPos - ( slider.offsetWidth / 2 ) + 'px';
}
slider.style.touchAction = 'none'; // disable touch scroll
slider.addEventListener( 'pointerdown', onPointerDown );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
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 );
}
</script>
</body>
</html>