-
Notifications
You must be signed in to change notification settings - Fork 558
Expand file tree
/
Copy pathentitiesFromSolids.js
More file actions
67 lines (60 loc) · 2.07 KB
/
entitiesFromSolids.js
File metadata and controls
67 lines (60 loc) · 2.07 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
const { flatten, toArray } = require('@jscad/array-utils')
const { meshColor } = require('../rendering/renderDefaults')
const geom2ToGeometries = require('./geom2ToGeometries')
const geom3ToGeometries = require('./geom3ToGeometries')
const path2ToGeometries = require('./path2ToGeometries')
/*
* Assemble a set of renderable entities from the given geometries.
*/
const assembleEntities = (geometries) => {
const entities = geometries.map((geometry) => {
const visuals = {
drawCmd: geometry.type === '2d' ? 'drawLines' : 'drawMesh',
show: true,
transparent: geometry.isTransparent,
useVertexColors: true
}
const entity = {
geometry,
visuals
}
return entity
})
return entities
}
/**
* Convert the given solids into renderable entities.
* Each 'solid' (V2 geometry) is converted to a WEBGL renderable 'geometry'.
* The resulting entities are passed as properities to the render.
* @param {Object} options - options for construction
* @param {Array} [options.color] - color for rendering, if the solid does not provide a color
* @param {Boolean} [options.smoothNormals=true] - smooth the normals of 3d solids, rendering a smooth surface
* @returns {Array} an array of renderable entities
*/
const entitiesFromSolids = (options, ...solids) => {
const defaults = {
color: meshColor,
smoothNormals: true
}
const { color, smoothNormals } = Object.assign({}, defaults, options)
solids = flatten(toArray(solids))
solids = solids.filter((solid) => solid && (solid instanceof Object))
const entities = []
solids.forEach((solid) => {
let geometries = []
if ('sides' in solid) {
geometries = geom2ToGeometries({ color }, solid)
} else if ('points' in solid) {
geometries = path2ToGeometries({ color }, solid)
} else if ('polygons' in solid) {
geometries = geom3ToGeometries({
smoothLighting: smoothNormals,
normalThreshold: 0.3,
color
}, solid)
}
entities.push(...assembleEntities(geometries))
})
return entities
}
module.exports = entitiesFromSolids