forked from Soft8Soft/verge3d-code-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl_buffergeometry_morphtargets.html
158 lines (106 loc) · 4.7 KB
/
webgl_buffergeometry_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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Verge3D webgl - buffergeometry - morph targets</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="container"></div>
<div id="info">
<a href="https://www.soft8soft.com/verge3d" target="_blank" rel="noopener">Verge3D</a> - buffergeometry - morph targets<br/>
by <a href="https://discoverthreejs.com/" target="_blank" rel="noopener">Discover Verge3D</a>
</div>
<script type="module">
import * as v3d from '../build/v3d.module.js';
import { GUI } from './jsm/libs/dat.gui.module.js';
import { OrbitControls } from './jsm/controls/OrbitControls.js';
let container, camera, scene, renderer, mesh;
function init() {
scene = new v3d.Scene();
scene.background = new v3d.Color(0x8FBCD4);
scene.add(new v3d.AmbientLight(0x8FBCD4, 0.4));
container = document.getElementById('container');
camera = new v3d.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 20);
camera.position.z = 10;
scene.add(camera);
const pointLight = new v3d.PointLight(0xffffff, 1);
camera.add(pointLight);
const geometry = createGeometry();
const material = new v3d.MeshPhongMaterial({
color: 0xff0000,
flatShading: true,
morphTargets: true
});
mesh = new v3d.Mesh(geometry, material);
scene.add(mesh);
initGUI();
renderer = new v3d.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setAnimationLoop(function() {
renderer.render(scene, camera);
});
container.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableZoom = false;
window.addEventListener('resize', onWindowResize, false);
}
function createGeometry() {
const geometry = new v3d.BoxBufferGeometry(2, 2, 2, 32, 32, 32);
// create an empty array to hold targets for the attribute we want to morph
// morphing positions and normals is supported
geometry.morphAttributes.position = [];
// the original positions of the cube's vertices
const positions = geometry.attributes.position.array;
// for the first morph target we'll move the cube's vertices onto the surface of a sphere
const spherePositions = [];
// for the second morph target, we'll twist the cubes vertices
const twistPositions = [];
const direction = new v3d.Vector3(1, 0, 0).normalize();
const vertex = new v3d.Vector3();
for (let i = 0; i < positions.length; i += 3) {
const x = positions[i];
const y = positions[i + 1];
const z = positions[i + 2];
spherePositions.push(
x * Math.sqrt(1 - (y * y / 2) - (z * z / 2) + (y * y * z * z / 3)),
y * Math.sqrt(1 - (z * z / 2) - (x * x / 2) + (z * z * x * x / 3)),
z * Math.sqrt(1 - (x * x / 2) - (y * y / 2) + (x * x * y * y / 3))
);
// stretch along the x-axis so we can see the twist better
vertex.set(x * 2, y, z);
vertex.applyAxisAngle(direction, Math.PI * x / 2).toArray(twistPositions, twistPositions.length);
}
// add the spherical positions as the first morph target
geometry.morphAttributes.position[0] = new v3d.Float32BufferAttribute(spherePositions, 3);
// add the twisted positions as the second morph target
geometry.morphAttributes.position[1] = new v3d.Float32BufferAttribute(twistPositions, 3);
return geometry;
}
function initGUI() {
// Set up dat.GUI to control targets
const params = {
Spherify: 0,
Twist: 0,
};
const gui = new GUI();
const folder = gui.addFolder('Morph Targets');
folder.add(params, 'Spherify', 0, 1).step(0.01).onChange(function(value) {
mesh.morphTargetInfluences[0] = value;
});
folder.add(params, 'Twist', 0, 1).step(0.01).onChange(function(value) {
mesh.morphTargetInfluences[1] = value;
});
folder.open();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
init();
</script>
</body>
</html>