-
Notifications
You must be signed in to change notification settings - Fork 0
/
createBunnyGeometry.js
33 lines (26 loc) · 918 Bytes
/
createBunnyGeometry.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
// Simple 3D geometry
const data = require('three-buffer-vertex-data');
const unindex = require('unindex-mesh');
// grab a simplicial complex
const primitive = require('bunny');
module.exports = function (opt = {}) {
// Set up our geometry
const geometry = new THREE.BufferGeometry();
// Sharing normals gives us a smooth look, splitting
// them gives us a faceted look
if (opt.flat) {
data.attr(geometry, 'position', unindex(primitive));
} else {
data.index(geometry, primitive.cells);
data.attr(geometry, 'position', primitive.positions);
}
// This is a ThreeJS utility to position the vertices
// into world center [ 0, 0, 0 ]
geometry.center();
// Another utility to scale all the vertices
geometry.scale(0.2, 0.2, 0.2);
// Now compute a normal for each vertex
// This will add a new attribute called 'normal'
geometry.computeVertexNormals();
return geometry;
};