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 = 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 = 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) } } }