Created
September 14, 2020 01:22
-
-
Save ylegall/3e487b38bdd9926a635f9d61778e47cf to your computer and use it in GitHub Desktop.
against the flow, code for https://www.instagram.com/p/CFD3XoGHRyM/
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.fx.blur.FrameBlur | |
import org.openrndr.extra.fx.blur.GaussianBloom | |
import org.openrndr.extra.gui.GUI | |
import org.openrndr.extra.parameters.Description | |
import org.openrndr.extra.parameters.DoubleParameter | |
import org.openrndr.extras.camera.OrbitalCamera | |
import org.openrndr.extras.camera.OrbitalControls | |
import org.openrndr.extras.camera.isolated | |
import org.openrndr.extras.meshgenerators.sphereMesh | |
import org.openrndr.ffmpeg.VideoWriter | |
import org.openrndr.math.Spherical | |
import org.openrndr.math.Vector2 | |
import org.openrndr.math.Vector3 | |
import org.openrndr.math.mix | |
import org.openrndr.math.transforms.transform | |
import org.openrndr.shape.Circle | |
import org.openrndr.shape.Segment3D | |
import org.openrndr.shape.contour | |
import org.ygl.fastnoise.FastNoise | |
import org.ygl.openrndr.utils.isolatedWithTarget | |
import org.ygl.openrndr.utils.randomDirection3D | |
import kotlin.math.PI | |
import kotlin.math.pow | |
import kotlin.math.sin | |
import kotlin.random.Random | |
private const val WIDTH = 920 | |
private const val HEIGHT = 920 | |
private const val TOTAL_FRAMES = 360 * 2 | |
private const val DELAY_FRAMES = 60 | |
private const val LOOPS = 2 | |
private const val RECORDING = true | |
fun main() = application { | |
configure { | |
width = WIDTH | |
height = HEIGHT | |
} | |
program { | |
var time = 0.0 | |
val center = Circle(50.0, 1450.0, 80.0) | |
val flowSpeed = 1.0 | |
val numPaths = 800 | |
val rng = Random(4) | |
val numParticles = 40000 | |
val noise = FastNoise() | |
val particleLoops = 2 | |
val particleGeometry = sphereMesh(radius = 1.0) | |
val bloom = GaussianBloom() | |
val blur = FrameBlur() | |
val params = @Description("params") object { | |
@DoubleParameter("noise scale", 0.0, 1.0, precision = 3) | |
var noiseScale = 0.5 | |
@DoubleParameter("noise mag X", 0.0, 100.0) | |
var noiseMagX = 4.0 | |
@DoubleParameter("noise mag Z", 0.0, 100.0) | |
var noiseMagY = 4.0 | |
} | |
val sphericalEye = Spherical(theta=31.66, phi=38.74, radius=615.16) | |
val camera = OrbitalCamera( | |
eye = Vector3.fromSpherical(sphericalEye), | |
lookAt = Vector3(x=1447.6, y=-320.42, z=-63.8), | |
far = 1500.0 | |
) | |
class Particle( | |
val pathOffset: Double, | |
val timeOffset: Double, | |
val positionOffset: Vector3 | |
) | |
val particles = List(numParticles) { | |
Particle(rng.nextDouble(), rng.nextDouble(), randomDirection3D() * 10.0) | |
} | |
val transforms = vertexBuffer(vertexFormat { | |
attribute("transform", VertexElementType.MATRIX44_FLOAT32) | |
}, numParticles) | |
fun getFieldVector2(x: Double, y: Double): Vector2 { | |
val dx = (x - center.center.x) | |
val dy = (y - center.center.y) | |
val xx = dx * dx | |
val yy = dy * dy | |
val aa = center.radius * center.radius | |
val denom = (xx + yy).pow(2) | |
val u = flowSpeed * (1 - aa * (xx - yy) / denom) | |
val v = -2 * flowSpeed * flowSpeed * aa * dx * dy / denom | |
return Vector2(u, v) | |
} | |
val paths = List(numPaths) { i -> | |
var x = rng.nextDouble(0.0, 2.0 * width) | |
var y = height * -1.6 | |
val pathPoints = mutableListOf(Vector2(x, y)) | |
while (y <= height * 0.4) { | |
val dt = 0.9 | |
val (v, u) = getFieldVector2(y, x) | |
x += u * dt | |
y += v * dt | |
pathPoints.add(Vector2(x, y)) | |
} | |
pathPoints.zipWithNext().map { | |
val start = Vector3(it.first.x, 0.0, it.first.y) | |
val stop = Vector3(it.second.x, 0.0, it.second.y) | |
Segment3D(start, stop) | |
} | |
} | |
fun getParticlePosition(particle: Particle, t: Double): Vector3 { | |
val path = paths[(particle.pathOffset * paths.size).toInt()] | |
val segmentIndex = (path.size * t).toInt() | |
val segment = path[segmentIndex] | |
val pos = mix(segment.start, segment.end, t) | |
return pos + particle.positionOffset | |
} | |
fun computeParticlePositions() { | |
transforms.put { | |
for (i in 0 until numParticles) { | |
val particle = particles[i] | |
val timeOffset = (100 + particle.timeOffset + (1 - particleLoops * time)) % 1.0 | |
val pos = getParticlePosition(particle, timeOffset) | |
noise.seed = i % 2 | |
val offsetX = params.noiseMagX * noise.getSimplex(params.noiseScale * pos.x, params.noiseScale * pos.z) | |
val offsetY = params.noiseMagY * noise.getSimplex(params.noiseScale * pos.z, params.noiseScale * pos.x) | |
val tx = transform { | |
translate(pos + Vector3(offsetX, offsetY, 0.0)) | |
val sizeOffset = 0.5 + 0.5 * sin(8 * PI * (time + particle.pathOffset)) | |
scale(1.0 + 1.0 * sizeOffset) | |
} | |
write(tx) | |
} | |
} | |
} | |
val composite = compose { | |
draw { | |
camera.isolated(drawer) { | |
drawer.clear(ColorRGBa.BLACK) | |
drawer.shadeStyle = shadeStyle { | |
vertexTransform = """ | |
//x_viewMatrix *= i_transform; | |
x_modelMatrix *= i_transform; | |
""".trimIndent() | |
} | |
drawer.fill = ColorRGBa.WHITE.opacify(0.9) | |
drawer.vertexBufferInstances( | |
listOf(particleGeometry), | |
listOf(transforms), | |
DrawPrimitive.TRIANGLES, | |
transforms.vertexCount | |
) | |
} | |
// draw the half sphere | |
val halfSphere = contour { | |
moveTo(265.0, 280.0) | |
arcTo(1.0, 1.0, 180.0, false, true, 480.0, 280.0) | |
close() | |
} | |
drawer.fill = ColorRGBa.BLACK | |
drawer.stroke = null | |
drawer.contour(halfSphere) | |
// drawer line segments | |
//camera.applyTo(drawer) | |
//drawer.clear(ColorRGBa.BLACK) | |
//drawer.stroke = ColorRGBa.WHITE.opacify(0.7) | |
//drawer.segments(paths.flatten()) | |
} | |
post(bloom) { | |
sigma = 0.01 | |
shape = 0.1 | |
gain = 0.85 | |
} | |
post(blur) { | |
blend = 0.38 | |
} | |
} | |
val videoTarget = renderTarget(width, height) { colorBuffer() } | |
val videoWriter = VideoWriter.create() | |
.size(width, height) | |
.frameRate(60) | |
.output("video/CircleStream3D.mp4") | |
if (RECORDING) { videoWriter.start() } | |
if (!RECORDING) { | |
extend(OrbitalControls(camera)) | |
extend(GUI()) { | |
add(params) | |
add(bloom) | |
add(blur) | |
} | |
} | |
extend { | |
time = ((frameCount - 1) % TOTAL_FRAMES) / TOTAL_FRAMES.toDouble() | |
camera.update(deltaTime) | |
computeParticlePositions() | |
if (RECORDING) { | |
drawer.isolatedWithTarget(videoTarget) { | |
composite.draw(drawer) | |
} | |
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 { | |
composite.draw(drawer) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment