Monorepository holding a React library, and CLI tool for CSG modeling.
The math is provided by the @jscad/csg package, please see it for more details and to understand the features/limitations of the approach. 3D output is intended for WebGL display or for STL export.
Main library, a custom React reconciler, designed to work with react-three-fiber
and Three.js. Uses @jscad/csg under the hood.
npm install --save modeler-csg
const React = require("react");
const ReactDOM = require("react-dom");
const { Canvas } = require("react-three-fiber");
const { Model } = require("modeler-csg");
ReactDOM.render(
<Canvas>
<ambientLight color={0x888888} />
<spotLight position={[0, 10, 10]} />
<Model>
<subtract>
<cube />
<sphere radius={1.3} />
</subtract>
</Model>
</Canvas>,
document.getElementById("root")
);
API follows (for the most part) @jscad/csg docs
<Model>
props:{children}
- shapes and operationsmodelMaterial
- THREE material for the model (defaultMeshStandardMaterial({ roughness: 1.0, metalness: 0.0, color: 0x333333 })
)partsMaterial
- THREE material for the parts (defaultMeshBasicMaterial({ wireframe: true, color: 0x888888 })
)showModel
-true/false
- show or hide model itself (defaulttrue
)showParts
-true/false
- show or hide parts that made the model (defaultfalse
)
<sphere/>
- props:center
,radius
,resolution
<cube/>
- props:center
,radius
<roundedCube/>
- props:center
,radius
,roundradius
,resolution
<cylinder/>
- props:start
,end
,radius
,resolution
<roundedCylinder/>
- props:start
,end
,radius
,resolution
<ellipticCylinder/>
- props:start
,end
,radius
,radiusStart
,radiusEnd
,resolution
Each shape also supports a color
prop that accepts [0..1, 0..1, 0..1]
RGB values.
<union>...</union>
<subtract>...</subtract>
<intersect>...</intersect>
Helper utility for quick sketching.
npm install -g modeler-cli
or npm install --save modler-cli
for local usage
- create a
model.js
file:
const React = require("react");
const { Model } = require("modeler-csg");
module.exports = () => (
<Model>
<subtract>
<cube />
<sphere radius={1.3} />
</subtract>
</Model>
);
- run
modeler-cli preview -m model.js
This will start local server on port 3000
, and file watcher on the model.js
file - any changes are live updated in the web app.
The web app has a simple camera handler, and provides a grid (main grid lines are by 1cm
, secondary grid lines by 1mm
).
modeler-cli
additional can compile provided model file to STL offline: modler-cli export -m model.js -o model.stl
.
preview
:-m
,--model
- input model file-p
,--port
- specify port
export
:-m
,--model
- input model file-o
,--out
- output STL file