-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_three_tween.html
155 lines (128 loc) · 5.41 KB
/
example_three_tween.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>three.js animation example</title>
<style>
body {
margin: 0;
}
#record {
position: fixed;
left: 2rem;
bottom: 2rem;
}
</style>
</head>
<body>
<button id="record" type="button" onclick="record()">record</button>
<!-- import ccapture.js (v1.1.0) from a CDN -->
<script src="https://unpkg.com/[email protected]/build/CCapture.all.min.js"></script>
<!-- import tween.js (v18.6.4) from CDN -->
<script src="https://unpkg.com/@tweenjs/[email protected]/dist/tween.umd.js"></script>
<script type="module">
//import the three.js library (v0.137.5) & example script from a CDN
//
// note: it seems that not all the example files can be accessed from CDNs. an alternative is to download the three.js source & include the examples folder in your project manually or use npm + browserfy/etc
//
import * as THREE from 'https://unpkg.com/[email protected]?module';
import { OrbitControls } from 'https://unpkg.com/[email protected]/examples/jsm/controls/OrbitControls.js?module';
let start, capturer, recording, width, height;
//some THREE.js "boilerplate"
const scene = new THREE.Scene();
scene.background = null;
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
const renderer = new THREE.WebGLRenderer({
preserveDrawingBuffer: true,
alpha: true,
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
document.body.appendChild(renderer.domElement);
//setting the background to blue, nice for chromakeys
renderer.setClearColor(0x0000ff, 1);
function resize() {
let w, h;
if (recording) {
w = width;
h = height;
} else {
w = window.innerWidth;
h = window.innerHeight;
}
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize(w, h);
}
window.addEventListener('resize', resize);
const orbit = new OrbitControls(camera, renderer.domElement);
//animation duration (seconds)
const duration = 3;
//--------------------- SET UP YOUR SCENE HERE ---------------------
const ambientLight = new THREE.AmbientLight(0x000000);
scene.add(ambientLight);
const lights = [];
lights[0] = new THREE.PointLight(0xffffff, 0.65, 0);
lights[1] = new THREE.PointLight(0xffffff, 0.65, 0);
lights[2] = new THREE.PointLight(0xffffff, 0.65, 0);
lights[0].position.set(0, 400, 0);
lights[1].position.set(200, 400, 400);
lights[2].position.set(-200, -400, -200);
scene.add(lights[0]);
scene.add(lights[1]);
scene.add(lights[2]);
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
});
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;
camera.position.y = 5;
camera.lookAt(0, 0, 0);
const tween = new TWEEN.Tween(cube.rotation)
.to({ y: 2 * Math.PI }, duration * 1000)
.easing(TWEEN.Easing.Quadratic.InOut)
.repeat(Infinity);
const firstTween = tween; //set this to the first tween in your chain
//-------------------------------------------------------------------
function animate() {
requestAnimationFrame(animate);
const now = (performance || Date).now();
if (start === undefined) {
firstTween.start(0); //start the tween chain at an absolute time of 0
start = now;
}
const elapsed = now - start;
//update tweens based on elapsed time
TWEEN.update(elapsed, false);
renderer.render(scene, camera);
if (capturer) capturer.capture(renderer.domElement);
}
animate();
//set up ccapture.js with webm video format
capturer = new CCapture({
format: 'webm',
framerate: 60,
timeLimit: duration, //record exactly one loop
display: true,
});
//set video dimentions
width = 1920;
height = 1080;
//record using ccapture.js when the record button is pressed
window.record = function () {
console.log('begin recording');
recording = true;
resize(); //resize scene to video dimentions
start = undefined; //reset loop timing
capturer.start();
};
</script>
</body>
</html>