Improve mesh generator (#301)

Co-authored-by: Edwin Jakobs <edwin@rndr.studio>
This commit is contained in:
Abe Pazos
2023-04-19 10:34:55 +02:00
committed by GitHub
parent a8c2f7217c
commit 03bf971ff5
31 changed files with 2639 additions and 1164 deletions

View File

@@ -0,0 +1,69 @@
import org.openrndr.WindowMultisample
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.*
import org.openrndr.extra.meshgenerators.*
import org.openrndr.extra.shapes.grid
import org.openrndr.math.Vector2
import org.openrndr.math.Vector3
import org.openrndr.shape.Rectangle
fun main() {
application {
configure {
multisample = WindowMultisample.SampleCount(8)
}
program {
val meshes = listOf(
boxMesh(1.0, 1.0, 1.0),
sphereMesh(radius = 0.5),
dodecahedronMesh(0.5),
cylinderMesh(radius = 0.5, length = 1.0, center = true),
planeMesh(Vector3.ZERO, Vector3.UNIT_X, Vector3.UNIT_Y),
capMesh(
15, 0.5,
listOf(Vector2.ZERO, Vector2(0.5, 0.2), Vector2.UNIT_X)
),
revolveMesh(5, 0.5)
)
val texture = colorBuffer(256, 256)
val s = texture.shadow
for (y in 0 until 256) {
for (x in 0 until 256) {
s[x, y] = ColorRGBa(x / 256.0, y / 256.0, 0.0, 1.0)
}
}
s.upload()
val positions = Rectangle.fromCenter(Vector2.ZERO, width * 0.01, height * 0.01)
.grid(4, 2).flatten().map {
it.center.vector3(z = -5.0)
}
extend {
drawer.clear(ColorRGBa.PINK)
drawer.perspective(60.0, width * 1.0 / height, 0.01, 1000.0)
drawer.depthWrite = true
drawer.depthTestPass = DepthTestPass.LESS_OR_EQUAL
drawer.shadeStyle = shadeStyle {
fragmentTransform = """
float light = dot(v_worldNormal, p_light) * 0.5 + 0.5;
x_fill = texture(p_texture, va_texCoord0.xy);
x_fill.rgb *= light;
""".trimIndent()
parameter("texture", texture)
parameter("light", Vector3(1.0).normalized)
}
meshes.forEachIndexed { i, mesh ->
drawer.isolated {
translate(positions[i])
rotate(Vector3.UNIT_Y, seconds * 12)
rotate(Vector3.UNIT_X, seconds * 25)
vertexBuffer(mesh, DrawPrimitive.TRIANGLES)
}
}
}
}
}
}

View File

@@ -0,0 +1,45 @@
import org.openrndr.WindowMultisample
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.CullTestPass
import org.openrndr.draw.DrawPrimitive
import org.openrndr.draw.colorBuffer
import org.openrndr.draw.shadeStyle
import org.openrndr.extra.camera.Orbital
import org.openrndr.extra.meshgenerators.boxMesh
import org.openrndr.math.Vector3
fun main() {
application {
configure {
multisample = WindowMultisample.SampleCount(8)
}
program {
val box = boxMesh(1.0, 1.0, 1.0)
val texture = colorBuffer(256, 256)
val s = texture.shadow
for (y in 0 until 256) {
for (x in 0 until 256) {
s[x, y] = ColorRGBa(x/256.0, y/256.0, 0.0, 1.0)
}
}
s.upload()
extend(Orbital()) {
eye = Vector3(1.0, 1.0, 1.0)
}
extend {
drawer.clear(ColorRGBa.PINK)
drawer.shadeStyle = shadeStyle {
fragmentTransform = """
x_fill = texture(p_texture, va_texCoord0.xy);
""".trimIndent()
parameter("texture", texture)
}
drawer.drawStyle.cullTestPass = CullTestPass.FRONT
drawer.vertexBuffer(box, DrawPrimitive.TRIANGLES)
}
}
}
}

View File

@@ -0,0 +1,46 @@
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.box
import org.openrndr.extra.meshgenerators.buildTriangleMesh
import org.openrndr.extra.meshgenerators.sphere
import org.openrndr.math.Vector3
fun main() {
application {
configure {
width = 800
height = 800
multisample = WindowMultisample.SampleCount(8)
}
program {
val m = buildTriangleMesh {
color = ColorRGBa.PINK
sphere(32, 32, 1.0)
color = ColorRGBa.WHITE
translate(0.0, -2.0, 0.0)
box(4.0, 4.0, 4.0)
}
extend(Orbital()) {
this.eye = Vector3(0.0, 3.0, 7.0)
this.lookAt = Vector3(0.0, 2.0, 0.0)
}
extend {
drawer.shadeStyle = shadeStyle {
fragmentTransform = """
x_fill = va_color;
x_fill.rgb *= v_viewNormal.z;
""".trimIndent()
}
drawer.vertexBuffer(m, DrawPrimitive.TRIANGLES)
}
}
}
}

View File

@@ -0,0 +1,67 @@
import org.openrndr.WindowMultisample
import org.openrndr.application
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.cylinder
import org.openrndr.extra.meshgenerators.hemisphere
import org.openrndr.math.Vector3
fun main() {
application {
configure {
width = 800
height = 800
multisample = WindowMultisample.SampleCount(8)
}
program {
extend(Orbital()) {
this.eye = Vector3(0.0, 10.0, 20.0)
this.lookAt = Vector3(0.0, 5.0, 0.0)
}
val m = buildTriangleMesh {
isolated {
translate(0.0, 12.0, 0.0)
hemisphere(32, 16, 5.0)
}
isolated {
translate(0.0, 9.0, 0.0)
rotate(Vector3.UNIT_X, 90.0)
cylinder(32, 1, 5.0, 6.0, center = true)
}
isolated {
translate(0.0, 6.0, 0.0)
rotate(Vector3.UNIT_X, 180.0)
hemisphere(32, 16, 5.0)
}
isolated {
val legCount = 12
val baseRadius = 3.0
val legRadius = 0.05
val legLength = 4.0
for (i in 0 until legCount) {
isolated {
val dphi = 360.0 / legCount
rotate(Vector3.UNIT_Y, dphi * i)
translate(baseRadius, 0.0, 0.0)
rotate(Vector3.UNIT_Z, -15.0)
translate(0.0, legLength / 2.0, 0.0)
rotate(Vector3.UNIT_X, 90.0)
cylinder(32, 1, legRadius, legLength, center = true)
}
}
}
}
extend {
drawer.shadeStyle = shadeStyle {
fragmentTransform = """
x_fill.rgb *= v_viewNormal.z;
""".trimIndent()
}
drawer.vertexBuffer(m, DrawPrimitive.TRIANGLES)
}
}
}
}

View File

@@ -0,0 +1,83 @@
import org.openrndr.WindowMultisample
import org.openrndr.application
import org.openrndr.draw.DrawPrimitive
import org.openrndr.draw.shadeStyle
import org.openrndr.extra.camera.Orbital
import org.openrndr.extra.meshgenerators.*
import org.openrndr.math.Vector3
fun main() {
application {
configure {
width = 800
height = 800
multisample = WindowMultisample.SampleCount(8)
}
program {
extend(Orbital()) {
this.eye = Vector3(0.0, 10.0, 20.0)
this.lookAt = Vector3(0.0, 5.0, 0.0)
}
val m = buildTriangleMesh {
isolated {
translate(0.0, 12.0, 0.0)
hemisphere(32, 16, 5.0)
}
val ridges = 5
val midLength = 6.0
val ridgeLength = midLength / ridges
val ridgeRadius = 5.5
for (r in 0 until ridges) {
isolated {
translate(0.0,
ridgeLength/4.0 + r * ridgeLength + 6.0,
0.0)
rotate(Vector3.UNIT_X, 270.0)
taperedCylinder(32, 1, 5.0, ridgeRadius, ridgeLength/ 2.0, center = true)
}
isolated {
translate(0.0,
ridgeLength/4.0 + ridgeLength/2.0 + r * ridgeLength + 6.0,
0.0)
rotate(Vector3.UNIT_X, 270.0)
taperedCylinder(32, 1, ridgeRadius, 5.0, ridgeLength/2.0, center = true)
}
}
isolated {
translate(0.0, 6.0, 0.0)
rotate(Vector3.UNIT_X, 180.0)
hemisphere(32, 16, 5.0)
}
isolated {
val legCount = 12
val baseRadius = 3.0
val legRadius = 0.05
val legLength = 4.0
for (i in 0 until legCount) {
isolated {
val dphi = 360.0 / legCount
rotate(Vector3.UNIT_Y, dphi * i)
translate(baseRadius, 0.0, 0.0)
rotate(Vector3.UNIT_Z, -15.0)
translate(0.0, legLength/2.0, 0.0)
rotate(Vector3.UNIT_X, 90.0)
cylinder(32, 1, legRadius, legLength, center = true)
}
}
}
}
extend {
drawer.shadeStyle = shadeStyle {
fragmentTransform = """
x_fill.rgb *= v_viewNormal.z;
""".trimIndent()
}
drawer.vertexBuffer(m, DrawPrimitive.TRIANGLES)
}
}
}
}

View File

@@ -0,0 +1,104 @@
import org.openrndr.WindowMultisample
import org.openrndr.application
import org.openrndr.draw.DrawPrimitive
import org.openrndr.draw.shadeStyle
import org.openrndr.extra.camera.Orbital
import org.openrndr.extra.meshgenerators.*
import org.openrndr.math.Vector2
import org.openrndr.math.Vector3
fun main() {
application {
configure {
width = 800
height = 800
multisample = WindowMultisample.SampleCount(8)
}
program {
extend(Orbital()) {
this.eye = Vector3(0.0, 15.0, 15.0)
}
val m = buildTriangleMesh {
val sides = 12
isolated {
translate(0.0, 12.0, 0.0)
cap(sides, 5.0, listOf(
Vector2(0.0, 1.0),
Vector2(0.5, 1.0),
Vector2(0.5, 0.5),
Vector2(0.9, 0.5),
Vector2(1.0, 0.0))
)
}
val ridges = 5
val midLength = 6.0
val ridgeLength = midLength / ridges
val ridgeRadius = 5.5
for (r in 0 until ridges) {
isolated {
translate(
0.0,
ridgeLength / 6.0 + r * ridgeLength + 6.0,
0.0
)
rotate(Vector3.UNIT_X, 270.0)
taperedCylinder(sides, 1, 5.0, ridgeRadius, ridgeLength / 3.0, center = true)
}
isolated {
translate(
0.0,
ridgeLength / 6.0 + ridgeLength / 3.0 + r * ridgeLength + 6.0,
0.0
)
rotate(Vector3.UNIT_X, 270.0)
taperedCylinder(sides, 1, ridgeRadius, ridgeRadius, ridgeLength / 3.0, center = true)
}
isolated {
translate(
0.0,
ridgeLength / 6.0 + 2 * ridgeLength / 3.0 + r * ridgeLength + 6.0,
0.0
)
rotate(Vector3.UNIT_X, 270.0)
taperedCylinder(sides, 1, ridgeRadius, 5.0, ridgeLength / 3.0, center = true)
}
}
isolated {
translate(0.0, 6.0, 0.0)
rotate(Vector3.UNIT_X, 180.0)
cap(sides, 5.0, listOf(Vector2(0.0, 0.0), Vector2(1.0, 0.0)))
}
isolated {
val legCount = 12
val baseRadius = 4.5
val legRadius = 0.05
val legLength = 7.0
for (i in 0 until legCount) {
isolated {
val dphi = 360.0 / legCount
rotate(Vector3.UNIT_Y, dphi * i)
translate(baseRadius, 0.0, 0.0)
translate(0.0, legLength / 2.0, 0.0)
rotate(Vector3.UNIT_X, 90.0)
cylinder(sides, 1, legRadius, legLength, center = true)
}
}
}
}
extend {
drawer.shadeStyle = shadeStyle {
fragmentTransform = """
x_fill.rgb *= v_viewNormal.z;
""".trimIndent()
}
drawer.vertexBuffer(m, DrawPrimitive.TRIANGLES)
}
}
}
}

View File

@@ -0,0 +1,45 @@
import org.openrndr.WindowMultisample
import org.openrndr.application
import org.openrndr.draw.CullTestPass
import org.openrndr.draw.DrawPrimitive
import org.openrndr.draw.shadeStyle
import org.openrndr.extra.camera.Orbital
import org.openrndr.extra.meshgenerators.*
import org.openrndr.math.Vector3
import org.openrndr.shape.Circle
fun main() {
application {
configure {
width = 800
height = 800
multisample = WindowMultisample.SampleCount(8)
}
program {
extend(Orbital()) {
this.eye = Vector3(0.0, 30.0, 50.0)
}
val m = buildTriangleMesh {
grid(5,5, 5) { u, v, w ->
isolated {
translate(u * 20.0, v * 20.0, w * 20.0)
extrudeShape(Circle(0.0, 0.0, 50.0).shape, 4.0, scale = 0.1)
}
}
twist(360.0/200.0, 0.0)
twist(360.0/200.0, 0.0, Vector3.UNIT_X)
twist(360.0/200.0, 0.0, Vector3.UNIT_Z)
}
extend {
drawer.shadeStyle = shadeStyle {
fragmentTransform = """
x_fill.rgb *= v_viewNormal.z;
""".trimIndent()
}
drawer.drawStyle.cullTestPass = CullTestPass.FRONT
drawer.vertexBuffer(m, DrawPrimitive.TRIANGLES)
}
}
}
}

View File

@@ -0,0 +1,67 @@
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.Vector3
import org.openrndr.math.catmullRom
import org.openrndr.shape.Circle
import org.openrndr.shape.toPath3D
fun main() {
application {
configure {
width = 800
height = 800
multisample = WindowMultisample.SampleCount(8)
}
program {
val m = buildTriangleMesh {
color = ColorRGBa.PINK
val path = listOf(
Vector3(0.0, 0.0, 0.0),
Vector3(-2.0, 2.0, 2.0),
Vector3(2.0, -4.0, 4.0),
Vector3(0.0, 0.0, 8.0)
).catmullRom(0.5, closed = false).toPath3D()
translate(-1.0, 0.0, 0.0)
for (i in 0 until 3) {
extrudeContourSteps(
Circle(0.0, 0.0, 0.5).contour,
path,
160,
Vector3.UNIT_Y,
contourDistanceTolerance = 0.02,
pathDistanceTolerance = 0.001
)
translate(1.0, 0.0, 0.0)
}
}
extend(Orbital()) {
this.eye = Vector3(0.0, 3.0, 7.0)
this.lookAt = Vector3(0.0, 2.0, 0.0)
}
extend {
drawer.shadeStyle = shadeStyle {
fragmentTransform = """
x_fill = va_color;
x_fill.rgb *= v_viewNormal.z;
""".trimIndent()
}
drawer.vertexBuffer(m, DrawPrimitive.TRIANGLES)
}
}
}
}

View File

@@ -0,0 +1,71 @@
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.extrudeShapeSteps
import org.openrndr.math.Vector3
import org.openrndr.math.catmullRom
import org.openrndr.shape.Circle
import org.openrndr.shape.Shape
import org.openrndr.shape.toPath3D
fun main() {
application {
configure {
width = 800
height = 800
multisample = WindowMultisample.SampleCount(8)
}
program {
val m = buildTriangleMesh {
color = ColorRGBa.PINK
val path = listOf(
Vector3(0.0, 0.0, 0.0),
Vector3(-2.0, 2.0, 2.0),
Vector3(2.0, -4.0, 4.0),
Vector3(0.0, 0.0, 8.0)
).catmullRom(0.5, closed = false).toPath3D()
translate(-5.0, 0.0, 0.0)
val ring = Shape(listOf(Circle(0.0, 0.0, 0.5).contour, Circle(0.0, 0.0, 0.25).contour.reversed))
for (i in 0 until 5) {
extrudeShapeSteps(
ring,
path,
160,
Vector3.UNIT_Y,
contourDistanceTolerance = 0.02,
pathDistanceTolerance = 0.001
)
translate(2.0, 0.0, 0.0)
}
}
extend(Orbital()) {
this.eye = Vector3(0.0, 3.0, 7.0)
this.lookAt = Vector3(0.0, 2.0, 0.0)
}
extend {
drawer.shadeStyle = shadeStyle {
fragmentTransform = """
x_fill = va_color;
x_fill.rgb *= v_viewNormal.z;
""".trimIndent()
}
drawer.vertexBuffer(m, DrawPrimitive.TRIANGLES)
}
}
}
}

View File

@@ -0,0 +1,96 @@
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.extrudeContourAdaptive
import org.openrndr.math.Polar
import org.openrndr.math.Vector3
import org.openrndr.math.asDegrees
import org.openrndr.math.asRadians
import org.openrndr.shape.Circle
import org.openrndr.shape.Path3D
import kotlin.math.PI
import kotlin.math.exp
fun main() {
application {
configure {
width = 800
height = 800
multisample = WindowMultisample.SampleCount(8)
}
program {
fun spiralPath(a: Double, k: Double, cycles: Double, steps: Int, direction:Double = 1.0): Path3D {
val points = (0 until steps).map {
val theta = ((PI * 2.0 * cycles) / steps) * it
val radius = a * exp(k * theta)
val c = Polar(theta.asDegrees, radius).cartesian
c.xy0
}
return Path3D.fromPoints(points, false)
}
val spiral = buildTriangleMesh {
for (i in -1..1 step 2) {
val p = spiralPath(0.2 * i, 0.25, 4.0, 400)
extrudeContourAdaptive(
Circle(0.0, 0.0, 0.1).contour,
p,
Vector3.UNIT_Z,
contourDistanceTolerance = 0.02,
pathDistanceTolerance = 0.001
)
}
isolated {
color = ColorRGBa.YELLOW
rotate(Vector3.UNIT_X, 90.0)
//rotate(Vector3.UNIT_Y, 45.0)
for (j in 0 until 1) {
for (i in -1..1 step 2) {
val rotationDegrees = j * 180.0 / 1.0
val rotation = rotationDegrees.asRadians
val scale = exp(rotation * 0.25)
val p = spiralPath(0.2 * i * scale, 0.25, 4.0, 400)
extrudeContourAdaptive(
Circle(0.0, 0.0, 0.1).contour,
p,
Vector3.UNIT_Z,
contourDistanceTolerance = 0.02,
pathDistanceTolerance = 0.001
)
}
rotate(Vector3.UNIT_Y, 180.0 / 1.0)
}
}
}
extend(Orbital())
extend {
drawer.shadeStyle = shadeStyle {
fragmentTransform = """
x_fill = va_color;
x_fill.rgb *= v_viewNormal.z;
""".trimIndent()
}
drawer.rotate(Vector3.UNIT_X, seconds*20.0)
drawer.vertexBuffer(spiral, DrawPrimitive.TRIANGLES)
}
}
}
}