-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
208 lines (168 loc) · 6.12 KB
/
index.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import * as THREE from 'three';
import * as dat from 'dat.gui';
import Magnify3d from '../src/Magnify3d';
import { TeapotBufferGeometry } from './TeapotGeometry';
// FPS monitor
javascript:(function(){var script=document.createElement('script');script.onload=function(){var stats=new Stats();document.body.appendChild(stats.dom);requestAnimationFrame(function loop(){stats.update();requestAnimationFrame(loop)});};script.src='//mrdoob.github.io/stats.js/build/stats.min.js';document.head.appendChild(script);})()
let camera, scene, renderer, defaultTarget, boxMesh1, boxMesh2;
let magnify3d, params, gui;
let shiftDown, ctrlDown;
const MIN_ZOOM = 1;
const MAX_ZOOM = 15;
const MIN_EXP = 1;
const MAX_EXP = 100;
const MIN_RADIUS = 10;
const MAX_RADIUS = 500;
const MIN_OUTLINE_THICKNESS = 0;
const MAX_OUTLINE_THICKNESS = 50;
function initScene() {
scene = new THREE.Scene();
const texture = new THREE.TextureLoader().load( 'res/checkerboard.png');
const checkerMaterial = new THREE.MeshBasicMaterial( { map: texture } );
const normalMaterial = new THREE.MeshNormalMaterial();
const boxGeometry = new THREE.BoxGeometry(20, 20, 20);
boxMesh1 = new THREE.Mesh(boxGeometry, checkerMaterial);
boxMesh1.position.x = 50;
scene.add(boxMesh1);
boxMesh2 = new THREE.Mesh(boxGeometry, checkerMaterial);
boxMesh2.position.x = -50;
scene.add(boxMesh2);
const boxMesh3 = new THREE.Mesh(boxGeometry, normalMaterial);
boxMesh3.position.x = 100;
scene.add(boxMesh3);
const boxMesh4 = new THREE.Mesh(boxGeometry, normalMaterial);
boxMesh4.position.x = -100;
scene.add(boxMesh4);
const sphereGeometry = new THREE.SphereGeometry(10, 64, 64);
const sphereMesh = new THREE.Mesh(sphereGeometry, normalMaterial);
scene.add(sphereMesh);
const teapotGeometry = new TeapotBufferGeometry(10, 32);
const teapotMesh1 = new THREE.Mesh(teapotGeometry, normalMaterial);
teapotMesh1.position.y = 50;
scene.add(teapotMesh1);
const teapotMesh2 = new THREE.Mesh(teapotGeometry, normalMaterial);
teapotMesh2.position.y = -50;
scene.add(teapotMesh2);
}
function initCamera() {
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 1000);
camera.position.set(0.0, 40.0, 250.0);
camera.lookAt(0.0, 0.0, 0.0);
}
function initRenderer() {
const pixelRatio = window.devicePixelRatio;
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio( pixelRatio );
renderer.setSize(window.innerWidth, window.innerHeight);
const container = document.createElement('div');
container.appendChild(renderer.domElement);
document.body.appendChild(container);
defaultTarget = new THREE.WebGLRenderTarget(window.innerWidth * pixelRatio, window.innerHeight * pixelRatio);
}
function initEventListeners() {
document.addEventListener('mousemove', (e) => {
params.mouse = new THREE.Vector2(e.clientX, window.innerHeight - e.clientY);
});
document.addEventListener('touchmove', (e) => {
params.mouse = new THREE.Vector2(e.pageX, window.innerHeight - e.pageY);
e.preventDefault();
e.stopPropagation();
});
window.addEventListener('resize', (e) => {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.clientWidth = window.innerWidth;
camera.clientHeight = window.innerHeight;
camera.updateProjectionMatrix();
});
function onMouseWheel(e) {
e.preventDefault();
const delta = (e.wheelDelta && e.wheelDelta / 40) || -e.detail;
if (shiftDown) {
params.zoom = Math.min(Math.max(MIN_ZOOM, params.zoom + (delta / 10)), MAX_ZOOM);
} else if (ctrlDown) {
params.exp = Math.min(Math.max(MIN_EXP, params.exp + delta), MAX_EXP);
} else {
params.radius = Math.min(Math.max(MIN_RADIUS, params.radius + delta), MAX_RADIUS);
}
gui.updateDisplay();
}
window.addEventListener( 'mousewheel', onMouseWheel );
window.addEventListener( 'DOMMouseScroll', onMouseWheel ); // firefox
document.addEventListener('keydown', (e) => {
const key = e.keyCode;
switch (key) {
case 16:
shiftDown = true;
break;
case 17:
ctrlDown = true;
break;
default:
break;
}
});
document.addEventListener('keyup', (e) => {
const key = e.keyCode;
switch (key) {
case 16:
shiftDown = false;
break;
case 17:
ctrlDown = false;
break;
default:
break;
}
});
}
function initGUI() {
params = {
zoom: 2.0,
exp: 30.0,
radius: 110.0,
outlineThickness: 4.0,
outlineColor: 0x555555
}
gui = new dat.GUI();
gui.add(params, 'radius', MIN_RADIUS, MAX_RADIUS);
gui.add(params, 'zoom', MIN_ZOOM, MAX_ZOOM);
gui.add(params, 'exp', MIN_EXP, MAX_EXP);
gui.add(params, 'outlineThickness', MIN_OUTLINE_THICKNESS, MAX_OUTLINE_THICKNESS);
gui.addColor(params, 'outlineColor');
}
function init() {
initScene();
initCamera();
initRenderer();
initEventListeners();
initGUI();
magnify3d = new Magnify3d();
}
function renderSceneToTarget(tgt) {
renderer.render(scene, camera, tgt);
}
function render() {
renderSceneToTarget(defaultTarget); // Render original scene to target / screen (depends on defaultTarget).
magnify3d.render({
renderer,
renderSceneCB: renderSceneToTarget,
pos: params.mouse,
zoom: params.zoom,
exp: params.exp,
radius: params.radius,
outlineThickness: params.outlineThickness,
outlineColor: params.outlineColor,
antialias: true,
inputBuffer: defaultTarget,
outputBuffer: undefined
});
}
function animate() {
requestAnimationFrame(animate);
boxMesh1.rotation.y += 0.01;
boxMesh2.rotation.y -= 0.01;
render();
}
init();
animate();