aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2026-09-20 15:53:10 +0530
committerAkshay Nair <phenax5@gmail.com>2026-09-20 15:53:10 +0530
commit7bdd84e8ecade87ececdefe3cee614df8438e0a1 (patch)
tree5228218d87c08ca348a549d3444c12657439a694 /src/main/kotlin
parent70e53263b259c6b73a03ec16fe819bfce5f1ecb7 (diff)
downloadart-playground-openrndr-7bdd84e8ecade87ececdefe3cee614df8438e0a1.tar.gz
art-playground-openrndr-7bdd84e8ecade87ececdefe3cee614df8438e0a1.zip
Wood pattern flow field(wip)
Diffstat (limited to '')
-rw-r--r--src/main/kotlin/phenax/utils/program.kt22
-rw-r--r--src/main/kotlin/wood/WoodProgram.kt83
2 files changed, 105 insertions, 0 deletions
diff --git a/src/main/kotlin/phenax/utils/program.kt b/src/main/kotlin/phenax/utils/program.kt
index ced3e3a..fe48351 100644
--- a/src/main/kotlin/phenax/utils/program.kt
+++ b/src/main/kotlin/phenax/utils/program.kt
@@ -5,6 +5,7 @@ import kotlin.properties.ReadOnlyProperty
import org.openrndr.ApplicationBuilder
import org.openrndr.Program
import org.openrndr.draw.*
+import org.openrndr.color.*
import org.openrndr.extra.filewatcher.watchingFile
import org.openrndr.extra.olive.oliveProgram as oliveProgramOriginal
@@ -57,6 +58,27 @@ fun preprocessShader(shaderCode: String): String {
val defaultShaderCode: String = ""
+fun averageColor(cb: ColorBuffer): ColorRGBa {
+ val shadow = cb.shadow
+ shadow.download()
+
+ var r = 0.0
+ var g = 0.0
+ var b = 0.0
+ var a = 0.0
+ for (y in 0 until cb.height) {
+ for (x in 0 until cb.width) {
+ val c = shadow[x, y]
+ r += c.r
+ g += c.g
+ b += c.b
+ a += c.alpha
+ }
+ }
+ val n = (cb.width * cb.height).toDouble()
+ return ColorRGBa(r / n, g / n, b / n, a / n)
+}
+
fun Program.liveComputeShader(name: String): ReadOnlyProperty<Any?, ComputeShader> {
val shaderCode by
watchingFile(File("src/main/resources/shaders/${name}.glsl")) { file ->
diff --git a/src/main/kotlin/wood/WoodProgram.kt b/src/main/kotlin/wood/WoodProgram.kt
new file mode 100644
index 0000000..66ae071
--- /dev/null
+++ b/src/main/kotlin/wood/WoodProgram.kt
@@ -0,0 +1,83 @@
+package wood
+
+import org.openrndr.application
+import org.openrndr.color.*
+import org.openrndr.draw.*
+import org.openrndr.extra.noise.filters.SimplexNoise3D
+import org.openrndr.math.IntVector2
+import org.openrndr.math.Vector2
+import org.openrndr.math.Vector3
+import org.openrndr.shape.IntRectangle
+import phenax.utils.*
+
+fun main() = application {
+ recordDuration(5.seconds())
+
+ configure {
+ width = 640
+ height = 480
+ windowResizable = false
+ }
+
+ oliveProgram {
+ val cs by liveComputeShader("wood")
+
+ val outputBuffer = colorBuffer(width, height, type = ColorType.UINT8)
+
+ val size = Math.max(width, height) * 2
+ val noiseBuffer = colorBuffer(size, size, type = ColorType.UINT8)
+ val noiseFilter = SimplexNoise3D()
+ noiseFilter.scale = Vector3.ONE * 1.8
+ noiseFilter.octaves = 8
+ noiseFilter.premultipliedAlpha = false
+ noiseFilter.seed = Vector3(0.1, 2.6, 0.1)
+ noiseFilter.apply(emptyArray(), noiseBuffer)
+
+ var flowField: MutableList<Double> = mutableListOf()
+ val subdiv = IntVector2(20, 20)
+ var subBuffer = colorBuffer(size / subdiv.x, size / subdiv.y)
+ for (row in 1..subdiv.y) {
+ for (col in 1..subdiv.x) {
+ val pos = IntVector2((col - 1) * subBuffer.width, (row - 1) * subBuffer.height)
+ noiseBuffer.copyTo(
+ subBuffer,
+ sourceRectangle = IntRectangle(pos, subBuffer.width, subBuffer.height),
+ targetRectangle = IntRectangle(0, 0, subBuffer.width, subBuffer.height)
+ )
+ flowField.add(averageColor(subBuffer).r)
+ }
+ }
+
+ var particles: MutableList<Vector2> = mutableListOf()
+ val particles_count = 5
+ for (i in 0..(particles_count - 1)) {
+ particles.add(Vector2(0.5, 0.5))
+ }
+ var particle_velocities = particles.map { Vector2(0.001, 0.0) }.toMutableList()
+ println(particles)
+ println(particles_count)
+
+ extend {
+ for ((index, particle) in particles.withIndex()) {
+ val gpos = Vector2(particle.x * subdiv.x, particle.y * subdiv.y)
+ val fidx = gpos.x.toInt() + gpos.y.toInt() * subdiv.y
+ if (flowField.count() > fidx && fidx >= 0) {
+ val current_cell = flowField[fidx]
+ particle_velocities[index] += Vector2(0.0, current_cell/1000000.0)
+ }
+ particles[index] += particle_velocities[index]
+ }
+ }
+
+ extend {
+ cs.uniform("particles", particles.toTypedArray())
+ cs.uniform("particles_count", particles_count)
+ cs.uniform("flow_field", flowField.toTypedArray())
+ cs.image("noise_texture", 1, noiseBuffer.imageBinding(0, ImageAccess.READ))
+ cs.uniform("seconds", seconds)
+ cs.image("output_image", 0, outputBuffer.imageBinding(0, ImageAccess.WRITE))
+ cs.execute(outputBuffer.width, outputBuffer.height, 1)
+ drawer.image(outputBuffer)
+ }
+ }
+}