openrndr / orx

A growing library of assorted data structures, algorithms and utilities for OPENRNDR
https://openrndr.org
BSD 2-Clause "Simplified" License
119 stars 35 forks source link

Improve GeneratorBuffer / meshGenerator {} #261

Closed edwinRNDR closed 1 year ago

edwinRNDR commented 1 year ago

Improve GeneratorBuffer by replacing the transform() function with a drawer-like isolated {}, translate, rotate interface.

This also adds support for colored meshes.

hamoid commented 1 year ago

Do we want a ShapeContour.toPath3D()?

I made a DemoExtrude04.kt with it:

import org.openrndr.WindowMultisample
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.DrawPrimitive
import org.openrndr.draw.shadeStyle
import org.openrndr.extra.camera.Orbital
import org.openrndr.extra.meshgenerators.buildTriangleMesh
import org.openrndr.extra.meshgenerators.extrudeContourSteps
import org.openrndr.math.Vector2
import org.openrndr.math.Vector3
import org.openrndr.shape.*

private fun ShapeContour.toPath3D() = Path3D(segments.map { seg ->
    Segment3D(
        seg.start.xy0,
        seg.control.map { it.xy0 }.toTypedArray(),
        seg.end.xy0
    )
}, closed)

fun main() {
    application {
        configure {
            width = 800
            height = 800
            multisample = WindowMultisample.SampleCount(8)
        }
        program {
            val m = buildTriangleMesh {
                color = ColorRGBa.PINK

                val path = Circle(Vector2.ZERO, 4.0).contour.toPath3D()

                translate(-1.0, 0.0, 0.0)

                for (i in 0 until 7) {
                    isolated {
                        rotate(Vector3.UNIT_Z, i * 360.0 / 7)
                        translate(2.0, 0.0, -1.0)
                        rotate(Vector3.UNIT_X, 20.0)
                        rotate(Vector3.UNIT_Y, 20.0)
                        extrudeContourSteps(
                            Circle(0.0, 0.0, 0.2).contour,
                            path,
                            160,
                            Vector3.UNIT_Y,
                            contourDistanceTolerance = 0.02,
                            pathDistanceTolerance = 0.001
                        )
                    }
                }

            }

            extend(Orbital()) {
                this.eye = Vector3(0.0, 3.0, 7.0)
                this.lookAt = Vector3(0.0, 0.0, 0.0)
            }

            extend {
                drawer.shadeStyle = shadeStyle {
                    fragmentTransform = """
                        x_fill = va_color;
                        x_fill.rgb *= v_viewNormal.z;
                    """.trimIndent()
                }

                drawer.vertexBuffer(m, DrawPrimitive.TRIANGLES)
            }
        }
    }
}
hamoid commented 1 year ago

Is it possible to have error messages with the require in Frames.kt? I was only seeing Failed Requirement and initially didn't know why.

hamoid commented 1 year ago

If we add fun path3D() = Path3D(listOf(this), false) to Segment3D.kt in openrndr then it's easier to create a Path3D between 2 points (useful for extrudeContourSteps). Somewhat equivalent to LineSegment.contour.

Update: I just figured out one can use Path3D.fromPoints().

hamoid commented 1 year ago

I have continued work on this at https://github.com/hamoid/orx/tree/improved-mesh-generator

My branch is based on current master. I implemented the suggestions above.

edwinRNDR commented 1 year ago

This PR went stale, @hamoid's efforts to revive the PR can be found in https://github.com/openrndr/orx/pull/301 and have recently been merged into master.