Created
September 27, 2020 05:50
-
-
Save ylegall/821f413f958982a02e3de64af04c2c33 to your computer and use it in GitHub Desktop.
circle pack 3D code for https://www.instagram.com/p/CFlLlfOH4FI/?utm_source=ig_web_copy_link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.ygl.openrndr.demos | |
import org.openrndr.application | |
import org.openrndr.color.ColorRGBa | |
import org.openrndr.draw.DrawPrimitive | |
import org.openrndr.draw.VertexElementType | |
import org.openrndr.draw.renderTarget | |
import org.openrndr.draw.shadeStyle | |
import org.openrndr.draw.vertexBuffer | |
import org.openrndr.draw.vertexFormat | |
import org.openrndr.extra.compositor.compose | |
import org.openrndr.extra.compositor.draw | |
import org.openrndr.extra.compositor.post | |
import org.openrndr.extra.dnk3.post.SegmentContours | |
import org.openrndr.extra.fx.blur.FrameBlur | |
import org.openrndr.extra.gui.GUI | |
import org.openrndr.extras.camera.OrbitalCamera | |
import org.openrndr.extras.camera.OrbitalControls | |
import org.openrndr.extras.camera.applyTo | |
import org.openrndr.extras.meshgenerators.sphereMesh | |
import org.openrndr.ffmpeg.VideoWriter | |
import org.openrndr.math.Vector3 | |
import org.openrndr.math.mod | |
import org.openrndr.math.transforms.transform | |
import org.openrndr.shape.Rectangle | |
import org.ygl.fastnoise.FastNoise | |
import org.ygl.openrndr.demos.util.Dialate | |
import org.ygl.openrndr.demos.util.Invert | |
import org.ygl.openrndr.utils.BokehDepthBlur | |
import org.ygl.openrndr.utils.CirclePackParams | |
import org.ygl.openrndr.utils.CirclePacker2 | |
import org.ygl.openrndr.utils.isolatedWithTarget | |
import org.ygl.openrndr.utils.randomColor | |
import kotlin.math.PI | |
import kotlin.math.cos | |
import kotlin.math.sin | |
import kotlin.random.Random | |
private const val WIDTH = 920 | |
private const val HEIGHT = 920 | |
private const val TOTAL_FRAMES = 360 * 6 | |
private const val LOOPS = 1 | |
private const val DELAY_FRAMES = 60 | |
private const val RECORDING = true | |
fun main() = application { | |
configure { | |
width = WIDTH | |
height = HEIGHT | |
} | |
program { | |
var time = 0.0 | |
val rng = Random(0) | |
val noise = FastNoise() | |
val circleBounds = Rectangle(0.0, 0.0, 1000.0, 1000.0) | |
val camera = OrbitalCamera( | |
//eye = Vector3(0.0, 100.0, -circleBounds.height/2.0) | |
eye = Vector3(x=-386.94, y=310.535, z=381.758), | |
lookAt = Vector3(x=-45.70, y=-212.51, z=51.504), | |
far = 2000.0, | |
fov = 50.0 | |
) | |
val circlePacker = CirclePacker2(CirclePackParams( | |
circleBounds, | |
minRadius = 3.0, | |
maxRadius = 36.0, | |
wrap = true, | |
padding = 0.0, | |
targetFillRatio = 0.863 | |
//circleLimit = numCircles | |
)) | |
val circles = circlePacker.loadOrGenerate("data/iso-circles-3.txt") | |
val dialateFilter = Dialate() | |
val blur = BokehDepthBlur() | |
val blurTarget = renderTarget(width, height) { colorBuffer(); depthBuffer() } | |
val videoTarget = renderTarget(width, height) { colorBuffer() } | |
val videoWriter = VideoWriter.create() | |
.size(width, height) | |
.frameRate(60) | |
.output("video/CirclePack3D.mp4") | |
if (RECORDING) { | |
videoWriter.start() | |
} | |
val sphereMesh = if (RECORDING) sphereMesh(sides = 24, segments = 24) else sphereMesh(sides = 24, segments = 24) | |
val transforms = vertexBuffer(vertexFormat { | |
attribute("transform", VertexElementType.MATRIX44_FLOAT32) | |
attribute("color", VertexElementType.VECTOR4_FLOAT32) | |
}, circles.size) | |
val colors = List(circles.size) { randomColor(rng) } | |
fun update() { | |
val noiseAngle = 2 * PI * time | |
val offset = circleBounds.dimensions * time | |
transforms.put { | |
circles.forEachIndexed { index, (center, radius) -> | |
val newCenter = (center + offset) | |
.mod(circleBounds.dimensions) | |
val dy = 10 * noise.getSimplex( | |
0.5 * newCenter.x + 10 * cos(noiseAngle), | |
0.5 * newCenter.y + 10 * sin(noiseAngle), | |
) | |
val pos = newCenter | |
.minus(circleBounds.center) | |
.let { | |
Vector3(it.x, dy, it.y) | |
} | |
write(transform { | |
translate(pos) | |
scale(radius) | |
}) | |
write(colors[index]) | |
} | |
} | |
} | |
val composite = compose { | |
draw { | |
drawer.isolatedWithTarget(blurTarget) { | |
camera.applyTo(drawer) | |
drawer.clear(ColorRGBa.BLACK) | |
drawer.shadeStyle = shadeStyle { | |
vertexTransform = "x_modelMatrix *= i_transform;" | |
fragmentTransform = "x_fill = vi_color;" | |
} | |
drawer.vertexBufferInstances( | |
listOf(sphereMesh), | |
listOf(transforms), | |
DrawPrimitive.TRIANGLES, | |
transforms.vertexCount | |
) | |
} | |
drawer.image(blurTarget.colorBuffer(0)) | |
} | |
post(SegmentContours()) | |
post(dialateFilter) { | |
threshold = 0.1 | |
spread = 1.2 | |
window = 1 | |
} | |
post(Invert()) | |
post(blur) { | |
depthBuffer = blurTarget.depthBuffer!! | |
far = camera.far | |
focusPoint = 0.2 | |
focusScale = 0.16 | |
} | |
post(FrameBlur()) | |
} | |
if (!RECORDING) { | |
//extend(OrbitalControls(camera)) | |
extend(GUI()) { | |
add(dialateFilter) | |
add(blur) | |
} | |
} | |
extend { | |
time = ((frameCount - 1) % TOTAL_FRAMES) / TOTAL_FRAMES.toDouble() | |
update() | |
camera.update(deltaTime) | |
if (RECORDING) { | |
drawer.isolatedWithTarget(videoTarget) { | |
composite.draw(this) | |
} | |
drawer.image(videoTarget.colorBuffer(0)) | |
if (frameCount > DELAY_FRAMES) { | |
videoWriter.frame(videoTarget.colorBuffer(0)) | |
} | |
if (frameCount >= TOTAL_FRAMES * LOOPS + DELAY_FRAMES) { | |
videoWriter.stop() | |
application.exit() | |
} | |
} else { | |
if (frameCount % 1000 == 0) { | |
println(Vector3.fromSpherical(camera.spherical)) | |
println(camera.lookAt) | |
} | |
composite.draw(drawer) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment