Cube Wave Animation
# Itroduction
This examples demonstrates how to animate hundreds of cubes like waves.
# Main Code
x
1
var CUBE_COUNT = 24;
2
3
clay.application.create('#viewport', {
4
5
graphic: {
6
shadow: true
7
},
8
9
init: function (app) {
10
11
var camera = app.createCamera([6, 15, 15], [0, 0, 0]);
12
13
// Create cubes
14
var cubes = [];
15
for (var i = 0; i < CUBE_COUNT; i++) {
16
for (var j = 0; j < CUBE_COUNT; j++) {
17
var cubeMesh = app.createCube({
18
// Color can be a RGB array
19
color: [i / CUBE_COUNT, j / CUBE_COUNT, 0.5]
20
});
21
cubeMesh.position.set(i - CUBE_COUNT / 2, 0, j - CUBE_COUNT / 2);
22
cubeMesh.scale.set(0.5, 0, 0.5);
23
24
cubes.push(cubeMesh);
25
}
26
}
27
this._cubes = cubes;
28
29
// Create lights.
30
var directionalLight = app.createDirectionalLight([-1, -1, -1]);
31
directionalLight.shadowResolution = 1024;
32
this._pointLight = app.createPointLight([-30, 30, 0], 100, '#fff', 1);
33
34
35
this._control = new clay.plugin.OrbitControl({
36
target: camera,
37
domElement: app.container
38
});
39
},
40
41
loop: function (app) {
42
this._control.update(app.deltaTime);
43
44
this._cubes.forEach(function (cube, idx) {
45
var x = Math.round(idx / CUBE_COUNT);
46
var y = idx % CUBE_COUNT;
47
// cube y is animated in sin/cos wave.
48
cube.scale.y = (Math.sin(x / 3 + app.elapsedTime / 1000) * Math.cos(y / 3 + app.elapsedTime / 1000) + 1) * 2;
49
});
50
}
51
});