forked from briah1212/Mhacks-3JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitscene.html
165 lines (110 loc) · 3.75 KB
/
splitscene.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!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 class="container">
<div class="slider"></div>
</div>
<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, 3 );
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>