forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesh-morph.html
More file actions
173 lines (143 loc) · 7.02 KB
/
mesh-morph.html
File metadata and controls
173 lines (143 loc) · 7.02 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html>
<head>
<title>PlayCanvas Mesh Morph</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel="icon" type="image/png" href="../playcanvas-favicon.png" />
<script src="../../build/playcanvas.js"></script>
<script src="../../build/playcanvas-extras.js"></script>
<style>
body {
margin: 0;
overflow-y: hidden;
}
</style>
</head>
<body>
<!-- The canvas element -->
<canvas id="application-canvas"></canvas>
<!-- The script -->
<script>
var canvas = document.getElementById("application-canvas");
// Create the application and start the update loop
var app = new pc.Application(canvas);
app.start();
// Set the canvas to fill the window and automatically change resolution to be the same as the canvas size
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
window.addEventListener("resize", function () {
app.resizeCanvas(canvas.width, canvas.height);
});
var miniStats = new pcx.MiniStats(app);
// Create an entity with a directional light component
var light = new pc.Entity();
light.addComponent("light", {
type: "directional"
});
app.root.addChild(light);
light.setLocalEulerAngles(45, 30, 0);
// Create an entity with a camera component
var camera = new pc.Entity();
camera.addComponent("camera", {
clearColor: new pc.Color(0.1, 0.1, 0.1)
});
app.root.addChild(camera);
// helper function to return the shortest distance from point [x, y, z] to a plane defined by [a, b, c] normal
var shortestDistance = function (x, y, z, a, b, c) {
var d = Math.abs(a * x + b * y + c * z);
var e = Math.sqrt(a * a + b * b + c * c);
return d / e;
};
// helper function that creates a morph target from original positions, normals and indices, and a plane normal [nx, ny, nz]
var createMorphTarget = function (positions, normals, indices, nx, ny, nz) {
// modify vertices to separate array
var modifiedPositions = new Float32Array(positions.length);
var dist, i, displacement;
var limit = 0.2;
for (i = 0; i < positions.length; i += 3) {
// distance of the point to the specified plane
dist = shortestDistance(positions[i], positions[i + 1], positions[i + 2], nx, ny, nz);
// modify distance to displacement amoint - displace nearby points more than distant points
displacement = pc.math.clamp(dist, 0, limit);
displacement = pc.math.smoothstep(0, limit, dist);
displacement = 1 - displacement;
// generate new position by extruding vertex along normal by displacement
modifiedPositions[i] = positions[i] + normals[i] * displacement;
modifiedPositions[i + 1] = positions[i + 1] + normals[i + 1] * displacement;
modifiedPositions[i + 2] = positions[i + 2] + normals[i + 2] * displacement;
}
// generate normals based on modified positions and indices
var modifiedNormals = new Float32Array(pc.calculateNormals(modifiedPositions, indices));
// generate delta positions and normals - as morph targets store delta between base position / normal and modified position / normal
for (i = 0; i < modifiedNormals.length; i++) {
modifiedPositions[i] -= positions[i];
modifiedNormals[i] -= normals[i];
}
// create a morph target
return new pc.MorphTarget({
deltaPositions: modifiedPositions,
deltaNormals: modifiedNormals
});
};
var createMorphInstance = function (x, y, z) {
// create the base mesh - a sphere, with higher amount of vertices / triangles
var mesh = pc.createSphere(app.graphicsDevice, { latitudeBands: 200, longitudeBands: 200 });
// obtain base mesh vertex / index data
var srcPositions = [], srcNormals = [], indices = [];
mesh.getPositions(srcPositions);
mesh.getNormals(srcNormals);
mesh.getIndices(indices);
// build 3 targets by expanding a part of sphere along 3 planes, specified by the normal
var targets = [];
targets.push(createMorphTarget(srcPositions, srcNormals, indices, 1, 0, 0));
targets.push(createMorphTarget(srcPositions, srcNormals, indices, 0, 1, 0));
targets.push(createMorphTarget(srcPositions, srcNormals, indices, 0, 0, 1));
// create a morph using these 3 targets
mesh.morph = new pc.Morph(targets, app.graphicsDevice);
// Create the mesh instance
var node = new pc.GraphNode();
var material = new pc.StandardMaterial();
this.meshInstance = new pc.MeshInstance(node, mesh, material);
// Create a model and add the mesh instance to it
var model = new pc.Model();
model.graph = node;
model.meshInstances = [this.meshInstance];
// add morph instance - this is where currently set weights are stored
var morphInstance = new pc.MorphInstance(mesh.morph);
meshInstance.morphInstance = morphInstance;
model.morphInstances.push(morphInstance);
// Create Entity and add it to the scene
this.entity = new pc.Entity();
entity.setLocalPosition(x, y, z);
app.root.addChild(this.entity);
// Add a model compoonent
app.systems.model.addComponent(this.entity, {
type: 'asset'
});
this.entity.model.model = model;
return morphInstance;
}
// create 3 morph instances
var morphInstances = [];
for (var k = 0; k < 3; k++) {
morphInstances.push(createMorphInstance(Math.random() * 6 - 3, Math.random() * 6 - 3, Math.random() * 6 - 3));
}
// update function called once per frame
var self = this;
var time = 0;
app.on("update", function (dt) {
time += dt;
for (var m = 0; m < morphInstances.length; m++) {
// modify weights of all 3 morph targets along some sin curve with different frequency
morphInstances[m].setWeight(0, Math.abs(Math.sin(time + m)));
morphInstances[m].setWeight(1, Math.abs(Math.sin(time * 0.3 + m)));
morphInstances[m].setWeight(2, Math.abs(Math.sin(time * 0.7 + m)));
}
// orbit camera around
camera.setLocalPosition(16 * Math.sin(time * 0.2), 4, 16 * Math.cos(time * 0.2));
camera.lookAt(pc.Vec3.ZERO);
});
</script>
</body>
</html>