Coder Social home page Coder Social logo

nidi3 / simple-3d Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 0.0 2.63 MB

A simple CSG library to create models for 3D printing

Home Page: https://nidi3.github.io/simple-3d/

License: Apache License 2.0

Kotlin 99.90% Shell 0.10%
kotlin csg 3d-printing 3d-model csg-library

simple-3d's Introduction

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.

simple-3d's People

Contributors

nidi3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.