-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhair.js
85 lines (73 loc) · 2.61 KB
/
hair.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
/*!
* a-aviator <https://github.com/rlamana/a-aviator>
*
* Copyright (c) 2017, Ramon Lamana.
* Based on Karim Maaloul's code for 'The Aviator':
* <https://github.com/yakudoo/TheAviator>
*
* Released under the MIT License.
*/
/**
* Cool animated hair as conceived by Karim Maaloul.
*/
AFRAME.registerComponent('hair', {
init() {
this._angleHairs = 0;
this._hairsTop = null;
},
update() {
const mesh = this.el.getOrCreateObject3D('mesh', THREE.Mesh);
const hairGeom = new THREE.BoxGeometry(5, 5, 5);
const hair = new THREE.Mesh(hairGeom, mesh.material);
// Align the shape of the hair to its bottom boundary, that will make it easier to scale.
hair.geometry.applyMatrix(new THREE.Matrix4().makeTranslation(0,2,0));
// Create a container for the hair.
const hairs = new THREE.Object3D();
// Create a container for the hairs at the top of the head (the ones that
// will be animated).
this._hairsTop = new THREE.Object3D();
// Create the hairs at the top of the head and position them on a 3 x 4 grid.
for (let i = 0; i < 12; i++) {
const h = hair.clone();
const col = i % 3;
const row = Math.floor(i / 3);
const startPosZ = -4;
const startPosX = -4;
h.position.set(startPosX + row * 4, 0, startPosZ + col * 4);
this._hairsTop.add(h);
}
hairs.add(this._hairsTop);
// Create the hairs at the side of the face.
const hairSideGeom = new THREE.BoxGeometry(12, 4, 2);
hairSideGeom.applyMatrix(new THREE.Matrix4().makeTranslation(-6, 0, 0));
const hairSideR = new THREE.Mesh(hairSideGeom, mesh.material);
const hairSideL = hairSideR.clone();
hairSideR.position.set(8, -2, 6);
hairSideL.position.set(8, -2, -6);
hairs.add(hairSideR);
hairs.add(hairSideL);
// Create the hairs at the back of the head.
var hairBackGeom = new THREE.BoxGeometry(2, 8, 10);
var hairBack = new THREE.Mesh(hairBackGeom, mesh.material);
hairBack.position.set(-1, -4 ,0)
hairs.add(hairBack);
hairs.position.set(-5, 5, 0);
mesh.add(hairs);
},
tick(time, timeDelta) {
this._updateHairs(timeDelta);
},
_updateHairs(timeDelta) {
// Get the hair.
const hairs = this._hairsTop.children;
// Update them according to the angle angleHairs.
for (let i = 0; i < hairs.length; i++) {
const h = hairs[i];
// Each hair element will scale on cyclical basis between 75% and 100% of
// its original size.
h.scale.y = .75 + Math.cos(this._angleHairs + i / 3) * .25;
}
// Increment the angle for the next frame.
this._angleHairs += timeDelta * .010;
}
});