-
Notifications
You must be signed in to change notification settings - Fork 558
Expand file tree
/
Copy pathdemo-cli.js
More file actions
117 lines (104 loc) · 3.41 KB
/
demo-cli.js
File metadata and controls
117 lines (104 loc) · 3.41 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
const { writeContextToFile } = require('@jscad/img-utils')
const { prepareRender, drawCommands, cameras, entitiesFromSolids } = require('./src') // replace this with the correct import
// setup demo solids data
const demoSolids = (parameters) => {
const { colorize } = require('@jscad/modeling').colors
const { cube, cuboid, line, sphere, star } = require('@jscad/modeling').primitives
const { intersect, subtract } = require('@jscad/modeling').booleans
const logo = [
colorize([1.0, 0.4, 1.0], subtract(
cube({ size: 300 }),
sphere({ radius: 200 })
)),
colorize([1.0, 1.0, 0], intersect(
sphere({ radius: 130 }),
cube({ size: 210 })
))
]
const transpCube = colorize([1, 0, 0, 0.75], cuboid({ size: [100 * parameters.scale, 100, 210 + (200 * parameters.scale)] }))
const star2D = star({ vertices: 8, innerRadius: 300, outerRadius: 400 })
const line2D = colorize([1.0, 0, 0], line([[260, 260], [-260, 260], [-260, -260], [260, -260], [260, 260]]))
// some colors are intentionally without alpfa channel to test geom2ToGeometries will add alpha channel
const colorChange = [
[1, 0, 0, 1],
[1, 0.5, 0],
[1, 0, 1],
[0, 1, 0],
[0, 0, 0.7]
]
star2D.sides.forEach((side, i) => {
if (i >= 2) side.color = colorChange[i % colorChange.length]
})
return [transpCube, star2D, line2D, ...logo]
}
const params = {
width: 640,
height: 480
}
const { width, height } = params
// create webgl context
const gl = require('gl')(width, height)
// process entities and inject extras
const entities = entitiesFromSolids({}, demoSolids({ scale: 1 }))
// prepare the camera
const perspectiveCamera = cameras.perspective
const camera = Object.assign({}, perspectiveCamera.defaults)
perspectiveCamera.setProjection(camera, camera, { width, height })
perspectiveCamera.update(camera, camera)
const options = {
glOptions: { gl },
camera,
drawCommands: {
// draw commands bootstrap themselves the first time they are run
drawAxis: drawCommands.drawAxis,
drawGrid: drawCommands.drawGrid,
drawLines: drawCommands.drawLines,
drawMesh: drawCommands.drawMesh
},
rendering: {
background: [1, 1, 1, 1],
meshColor: [1, 0.5, 0.5, 1], // use as default face color for csgs, color for cags
lightColor: [1, 1, 1, 1], // note: for now there is a single preset light, not an entity
lightDirection: [0.2, 0.2, 1],
lightPosition: [100, 200, 100],
ambientLightAmount: 0.3,
diffuseLightAmount: 0.89,
specularLightAmount: 0.16,
materialShininess: 8.0
},
// next few are for solids / csg / cags specifically
overrideOriginalColors: false, // for csg/cag conversion: do not use the original (csg) color, use meshColor instead
smoothNormals: true,
// data
entities: [
{ // grid data
// the choice of what draw command to use is also data based
visuals: {
drawCmd: 'drawGrid',
show: true
},
size: [500, 500],
ticks: [25, 5]
// color: [0, 0, 1, 1],
// subColor: [0, 0, 1, 0.5]
},
{
visuals: {
drawCmd: 'drawAxis',
show: true
},
size: 300
// alwaysVisible: false,
// xColor: [0, 0, 1, 1],
// yColor: [1, 0, 1, 1],
// zColor: [0, 0, 0, 1]
},
...entities
]
}
// prepare
const render = prepareRender(options)
// do the actual render
render(options)
// output to file
writeContextToFile(gl, width, height, 4, './test.png')