forked from xeolabs/scenejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry_morphTargets.html
184 lines (155 loc) · 7.04 KB
/
geometry_morphTargets.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>SceneJS Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
margin: 0;
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
}
#morph-target-pair, #morph-factor {
font-size: 16px;
}
</style>
<script src="../api/latest/scenejs.min.js"></script>
<link href="css/styles.css" rel="stylesheet"/>
<body>
<div id="infoDark">
<a href="http://scenejs.org">SceneJS</a> - geometry morph targets - trivial demo<br>
Current target pair: <span id="morph-target-pair">0-1</span><br>
Current interpolation factor: <span id="morph-factor">0</span>
</div>
<script>
// Point SceneJS to the bundled plugins
SceneJS.setConfigs({
pluginPath: "../api/latest/plugins"
});
// Create scene
var scene = SceneJS.createScene({
nodes: [
// Mouse-orbited camera, implemented by plugin at
// http://scenejs.org/api/latest/plugins/node/cameras/orbit.js
{
type: "cameras/orbit",
yaw: 70,
pitch: -30,
zoom: 30,
zoomSensitivity: 1.0,
nodes: [
{
type: "material",
color: { r: 0.3, g: 0.3, b: 0.9 },
nodes: [
/*------------------------------------------------------
* The morphGeometry
*
* We can morph positions and normals. In this example,
* we're morphing between three targets.
*
* The 'factor' attribute takes a value from 0.0 to 2.0
* to choose targets and interpolate between them.
*
* We'll animate that as we run the scene to drive the
* morphing.
*----------------------------------------------------*/
{
type: "morphGeometry",
id: "myMorph",
/* Start at first target
*/
factor: 0.0,
keys: [0, 1, 2],
/* Minimum of two morph targets required
*/
targets: [
/* Target 1
*/
{
positions: [ 5, 5, 5,
-5, 5, 0,
-5, -5, 5,
5, -5, 0 ],
normals: [-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0 ]
},
/* Target 2
*/
{
positions: [ 6, 6, 0,
-6, 6, 0,
-6, -6, 0,
6, -6, 0 ],
normals: [ 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1 ]
},
/* Target 3
*/
{
positions: [ 5, 5, 0,
-5, 5, 0,
-5, -5, 0,
0, 0, -5 ],
normals: [ 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1 ]
}
],
nodes: [
/*----------------------------------------------
* The geometry we're morphing.
*
* Note that the positions and normals are
* omitted because those are specified on the
* morphGeometry.
*
* We can have multiple geometries in a
* morphGeometry, perhaps to divide up the
* positions among separate indices to apply
* multiple materials to the mesh - see the
* multi-materials demo for more info on that.
*
* So the geometry can of course be nested within
* whatever other nodes we wish, except for
* another morphGeometry, because that would
* override the morphGeometry we just defined.
*---------------------------------------------*/
{
type: "geometry",
primitive: "triangles",
indices: [ 0, 1, 2, 0, 2, 3 ],
uv: [1, 1, 0, 1, 0, 0, 1, 0]
}
]
}
]
}
]
}
]
});
scene.getNode("myMorph",
function (myMorph) {
var factor = 0;
var factorInc = 0.01;
var targetPairElement = document.getElementById("morph-target-pair");
var factorElement = document.getElementById("morph-factor");
myMorph.on("frameUpdate", function(frame) {
targetPairElement.innerText = frame.key1 + "-" + frame.key2;
});
myMorph.on("update", function(frame) {
factorElement.innerText = frame.factor.toFixed(3);
});
scene.on("tick",
function () {
if (factor >= 2.0) {
factorInc *= -1;
} else if (factor < 0.0) {
factorInc *= -1;
}
factor += factorInc;
myMorph.setFactor(factor);
});
});
</script>
</body>
</html>