-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
executable file
·257 lines (190 loc) · 6.5 KB
/
main.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/***
* Example of how to use...
*/
import * as THREE from 'three';
import { FlipBook as Book } from './src/FlipBook';
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
import { BokehPass } from 'three/addons/postprocessing/BokehPass.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
const width = window.innerWidth;
const height = window.innerHeight;
/**
* @type {THREE.Vector3}
*/
let cameraPosition;
let zoom = 0;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x999999);
const light = new THREE.DirectionalLight( 0xffffff,1 );
light.position.set( .2, 1, 0 );
light.castShadow = true;
//Set up shadow properties for the light
light.shadow.mapSize.width = 512*8; // default
light.shadow.mapSize.height = 512*8; // default
light.shadow.camera.near = 0.5; // default
light.shadow.camera.far = 3; // default
light.shadow.bias = -0.003;
light.shadow.blurSamples=3;
scene.add( light );
const ambientLight = new THREE.AmbientLight( 0xffffff, 2);
scene.add( ambientLight );
const MAXFOV = 40;
const camera = new THREE.PerspectiveCamera( 0, width / height, 0.1, 1000 );
camera.setFocalLength(MAXFOV);
const renderer = new THREE.WebGLRenderer();
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setSize( width, height );
// //renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMapping = THREE.NoToneMapping;
document.body.appendChild( renderer.domElement );
camera.position.x = 0;
camera.position.y = 3;
camera.position.z = 1;
cameraPosition = camera.position.clone();
camera.lookAt(0,0,0);
//--------------------------------------------------------------------------------------------------------------
const postprocessing = {};
const composer = new EffectComposer( renderer );
const renderPass = new RenderPass( scene, camera );
composer.addPass( renderPass );
const bokehPass = new BokehPass( scene, camera, {
focus: 10,
aperture: 10* 0.00001,
maxblur: .008
} );
composer.addPass( bokehPass );
const outputPass = new OutputPass();
composer.addPass( outputPass );
//----------------
postprocessing.composer = composer;
postprocessing.bokeh = bokehPass;
//-----------------
const clock = new THREE.Clock();
const pmremGenerator = new THREE.PMREMGenerator( renderer );
pmremGenerator.compileEquirectangularShader();
const book = new Book({
flipDuration: .5,
yBetweenPages:.001
});
book.scale.x = 0.8;
scene.add(book);
book.setPages([
"/page1.ignore.png",
"/loremipsum.ignore.png",
"/page2.ignore.png",
"/page3.ignore.png",
"/page4.ignore.png",
"/page5.ignore.png",
]);
// setTimeout( ()=>{
// book.setPages([
// "https://placehold.co/600x400?text=Test1",
// "https://placehold.co/600x400?text=Hello+World",
// null, //blank page
// null
// ]);
// // book.dispose();
// //book.progress = 1.5;
// }, 3000 );
function onWindowResize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize( width, height );
//postprocessing.composer( width, height );
postprocessing.composer.setSize( width, height );
}
// Event listener for mouse click
function onMouseClick(event) {
// Calculate mouse coordinates in normalized device space (-1 to 1)
const mouse = new THREE.Vector2();
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
// Raycasting to check for intersections with the mesh
const raycaster = new THREE.Raycaster();
raycaster.setFromCamera(mouse, camera);
// Array to store intersected objects
const intersects = raycaster.intersectObjects(scene.children);
// // Check if the mesh is clicked
// if (intersects.length > 0 && intersects[0].object === cube) {
// console.log('Mesh clicked!');
// }
if( intersects.length )
book.flipPage(intersects[0].object.parent);
}
function onMouseMove(event)
{
const mouse = new THREE.Vector2();
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
if( zoom>0 )
{
cameraPosition.x = mouse.x/3;
cameraPosition.z = 1 + -mouse.y/3;
}
}
window.addEventListener( 'resize', onWindowResize );
renderer.domElement.addEventListener('click', onMouseClick);
renderer.domElement.addEventListener('mousemove', onMouseMove);
renderer.domElement.addEventListener('mousewheel', ev =>
{
zoom += ev.deltaY<0? 1 : -1;
if( zoom <=0 )
{
cameraPosition.x = 0;
cameraPosition.z = 1;
}
camera.setFocalLength(MAXFOV+zoom);
});
//--------------------------------------------------------------------------------------------------------------
let t = 0;
function animate() {
requestAnimationFrame( animate );
book.animate(clock.getDelta());
postprocessing.composer.render( 0.1 );
camera.position.add( cameraPosition.clone().sub(camera.position).divideScalar(5) )
}
animate();
//**************************************************************** */
const panel = new GUI( { width: 310 } );
const folder1 = panel.addFolder( 'Page deformation' );
const folder2 = panel.addFolder( 'Book' );
const folder3 = panel.addFolder( 'Scene' );
const effects = {
heightIntensity: 0.054,
curveEffectRange: 0.735,
midEffect:0.325,
intensity:1
}
folder1.add( effects, 'heightIntensity', 0, 3 ).onChange( value => {
for (const page of book) {
page.pageCurve.elevationHeight = value;
page.modifiers.apply();
}
});
folder1.add( effects, 'curveEffectRange', 0, 1 ).onChange( value => {
for (const page of book) {
page.pageCurve.effectRange = value;
page.modifiers.apply();
}
});
folder1.add( effects, 'midEffect', 0, 1 ).onChange( value => {
for (const page of book) {
page.pageCurve.effectMid = value;
page.modifiers.apply();
}
});
folder1.add( effects, 'intensity', 0, 1 ).onChange( value => {
for (const page of book) {
page.pageCurve.intensity = value;
page.modifiers.apply();
}
});
folder2.add( { progress:0 }, 'progress', 0, book.totalPages/2 ).onChange( value => {
book.progress = value ;
});
folder3.add( ambientLight, 'intensity', 0, 5 ) ;