-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_three.html
More file actions
150 lines (123 loc) · 5.26 KB
/
Copy pathexample_three.html
File metadata and controls
150 lines (123 loc) · 5.26 KB
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
<!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/ccapture.js@1.1.0/build/CCapture.all.min.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/three@0.137.5?module';
import { OrbitControls } from 'https://unpkg.com/three@0.137.5/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);
//--------------------- 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);
//-------------------------------------------------------------------
//animation duration (seconds)
const duration = 5;
function animate() {
requestAnimationFrame(animate);
const now = (performance || Date).now();
if (start === undefined) start = now;
const elapsed = now - start;
//t = loop position in the range 0 - 1
const t = (elapsed % (duration * 1000)) / (duration * 1000);
//--------------------- ANIMATE YOUR SCENE HERE ---------------------
//use the value of t to drive your animation
//DO NOT incriment values every frame, i.e. cube.rotation.y += 0.01
cube.rotation.y = 2 * Math.PI * t;
//-------------------------------------------------------------------
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');
start = undefined; //reset loop timing
recording = true;
resize(); //resize scene to video dimentions
capturer.start();
};
</script>
</body>
</html>