Skip to content

Latest commit

 

History

History
111 lines (91 loc) · 3.92 KB

README.md

File metadata and controls

111 lines (91 loc) · 3.92 KB

simple-3d

Build Status codecov License Maven Central

To see the models described here, go here.

The github project is here.

A simple CSG library to create models for 3D printing. Originally, the code is based on csg.js.

This is a kotlin multi platform project, so it can be used from both javascript and kotlin. The javascript part is currently unpolished, but for an example what can be done, see here.

To use it with kotlin, add it as a dependency:

<dependency>
    <groupId>guru.nidi.simple-3d</groupId>
    <artifactId>simple-3d-jvm</artifactId>
    <version>0.0.1</version>
</dependency>

The basic element is the model. Objects are added to a model. The model will be saved under the given file name, .stl and .obj formats are supported.

model(
    File("examples/simple.stl"),
    cube(length = v(1, 2, 3)).rotateX(45.deg),
    sphere(center = v(3, 0, 0))
)
<script src="https://embed.github.com/view/3d/nidi3/simple-3d/master/examples/simple.stl"></script>

Objects can be combined with the union / +, subtract / - and intersect / * operators. They can be translated, rotated and scaled.

model(File("examples/csg.stl")) {
    val a = cube(center = v(1, 0, 0))
    val b = sphere()

    add(
        (a union b).translate(3, 0, 0),
        (a subtract b).translate(6, 0, 0),
        (a intersect b).translate(0, 0, 0)
    )
}
<script src="https://embed.github.com/view/3d/nidi3/simple-3d/master/examples/csg.stl"></script>

Transformations can be applied before adding objects to the model.

model(File("examples/transform.stl")) {
    val cross = cube(length = v(1, 1, 2)) + cube(length = v(1, 2, 1))
    fun f(level: Int) {
        if (level > 0) {
            add(cross)
            scale(.25, .25, .25).apply {
                translate(0, 3, 3).apply { f(level - 1) }
                translate(0, -3, 3).apply { f(level - 1) }
                translate(0, 3, -3).apply { f(level - 1) }
                translate(0, -3, -3).apply { f(level - 1) }
            }
        }
    }
    f(3)
}
<script src="https://embed.github.com/view/3d/nidi3/simple-3d/master/examples/transform.stl"></script>

When using the .obj format, materials can be assigned to objects.

model(File("examples/material.obj")) {
    val red = material("red", Color(1.0, 0.0, 0.0))
    val green = material("green", Color(0.0, 1.0, 0.0))
    add(cube().material(red) - cube(center = v(.5, .5, 0)).material(green))
}
<script src="https://embed.github.com/view/3d/nidi3/simple-3d/master/examples/material.obj"></script>

Unfortunately, the viewer does not support materials :(


There is also a simple vectorizer which can be used to create models from images.

model(File("examples/dinosaur.stl")) {
    val img = Image.fromClasspath("brontosaurus-pattern.gif")
    val c = outline(img) { rgb -> rgb < 0xffffff }
        .simplify(2.0)
        .map { it.toVector() / 10.0 }
    add(prism(10, c))
}

More examples can be found here.