aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2026-09-11 17:25:39 +0530
committerAkshay Nair <phenax5@gmail.com>2026-09-11 17:25:39 +0530
commitf4ec01f09dbe4b731bee44e12d2c1f93268ca705 (patch)
tree7ed1d824edb31ddcd45072920d4f9ee353844918 /src/main/kotlin
parent41246c0567f4c48ec5788a291386e377241e0eed (diff)
downloadart-playground-openrndr-f4ec01f09dbe4b731bee44e12d2c1f93268ca705.tar.gz
art-playground-openrndr-f4ec01f09dbe4b731bee44e12d2c1f93268ca705.zip
Overdid the raymarch experiment
Diffstat (limited to '')
-rw-r--r--src/main/kotlin/phenax/utils/program.kt36
-rw-r--r--src/main/kotlin/play/PlayProgram.kt84
2 files changed, 99 insertions, 21 deletions
diff --git a/src/main/kotlin/phenax/utils/program.kt b/src/main/kotlin/phenax/utils/program.kt
index a05307c..d3788ea 100644
--- a/src/main/kotlin/phenax/utils/program.kt
+++ b/src/main/kotlin/phenax/utils/program.kt
@@ -8,6 +8,11 @@ import org.openrndr.draw.*
import org.openrndr.extra.filewatcher.watchingFile
import org.openrndr.extra.olive.oliveProgram as oliveProgramOriginal
+enum class ActionType {
+ PAN,
+ LIGHT_MOVE
+}
+
fun Int.seconds(): Int = this * 1000
fun Int.minutes(): Int = this.seconds() * 60
@@ -37,11 +42,31 @@ fun ApplicationBuilder.oliveProgram(init: Program.() -> Unit) {
}
}
+fun preprocessShader(shaderCode: String): String {
+ val pattern = Regex("#include\\s+<([A-Za-z0-9-_/.]+)>")
+ val includes = pattern.findAll(shaderCode)
+ var parts: MutableList<String> = mutableListOf()
+ var lastIndex = 0
+ includes.forEach {
+ parts.add(shaderCode.slice(lastIndex..(it.range.first - 1)))
+ lastIndex = it.range.last + 1
+ val modulePath = it.groupValues[1]
+ val moduleContents = File("src/main/resources/shaders/modules/${modulePath}.cs").readText()
+ parts.add("/* Module ${modulePath} */")
+ parts.add(moduleContents)
+ parts.add("/* End module ${modulePath} */")
+ }
+ parts.add(shaderCode.slice(lastIndex..(shaderCode.length - 1)))
+ return parts.joinToString(separator = "\n")
+}
+
+val defaultShaderCode: String = ""
+
fun Program.liveComputeShader(name: String): ReadOnlyProperty<Any?, ComputeShader> {
val shaderCode by
watchingFile(File("src/main/resources/shaders/${name}.cs")) { file ->
println("change")
- file.readText()
+ preprocessShader(file.readText().toString())
}
var cs: ComputeShader = ComputeShader.fromCode(shaderCode, name)
@@ -55,9 +80,14 @@ fun Program.liveComputeShader(name: String): ReadOnlyProperty<Any?, ComputeShade
try {
cs = ComputeShader.fromCode(shaderCode, name)
} catch (e: Exception) {
- // Restore old shader if new code has error
- cs = ComputeShader.fromCode(lastCompiledCode, name)
println("Shader compile failed: ${e.message}")
+ // Restore old shader if new code has error or default shader
+ try {
+ cs = ComputeShader.fromCode(lastCompiledCode, name)
+ } catch (e: Exception) {
+ println("Falling back to default shader")
+ cs = ComputeShader.fromCode(defaultShaderCode, name)
+ }
}
lastCompiledCode = shaderCode
}
diff --git a/src/main/kotlin/play/PlayProgram.kt b/src/main/kotlin/play/PlayProgram.kt
index 0ec0133..4840a89 100644
--- a/src/main/kotlin/play/PlayProgram.kt
+++ b/src/main/kotlin/play/PlayProgram.kt
@@ -1,9 +1,11 @@
package play
-import java.io.File
-import org.openrndr.Program
+import org.openrndr.KeyModifier
import org.openrndr.application
+import org.openrndr.color.*
import org.openrndr.draw.*
+import org.openrndr.extra.noise.filters.SimplexNoise3D
+import org.openrndr.math.Vector3
import phenax.utils.*
fun main() = application {
@@ -16,32 +18,78 @@ fun main() = application {
}
oliveProgram {
+ val font = loadFont("data/fonts/default.otf", 16.0)
val cs by liveComputeShader("play_shader")
- // val image = loadImage("data/images/pm5544.png")
- // val font = loadFont("data/fonts/default.otf", 64.0)
- // val inputBuffer = colorBuffer(width, height, type = ColorType.UINT8)
val outputBuffer = colorBuffer(width, height, type = ColorType.UINT8)
- extend {
- // drawer.drawStyle.colorMatrix = tint(ColorRGBa.WHITE.shade(0.2))
- // drawer.image(image)
+ var cameraPosition: Vector3 = Vector3(0.0, 1.0, 0.0)
+ var lightPosition: Vector3 = Vector3(1.0, 2.0, 1.0)
+ var actionType: ActionType? = null
+ var lastPos: Vector3? = null
+
+ mouse.moved.listen {
+ val dimens = Vector3(program.width.toDouble(), program.height.toDouble(), 1.0)
+ val pos = (Vector3(it.position.x, -it.position.y, 0.0) - dimens / 2.0).div(dimens)
+ if (actionType == ActionType.PAN) {
+ lastPos?.let { prev -> cameraPosition -= (pos - prev) }
+ } else if (actionType == ActionType.LIGHT_MOVE) {
+ lastPos?.let { prev -> lightPosition += (pos - prev) }
+ }
+ actionType?.let { lastPos = pos }
+ }
+ mouse.buttonDown.listen {
+ val isCtrl = KeyModifier.CTRL in it.modifiers
+ actionType =
+ if (isCtrl) {
+ ActionType.LIGHT_MOVE
+ } else {
+ ActionType.PAN
+ }
+ }
+ mouse.scrolled.listen {
+ val isCtrl = KeyModifier.CTRL in it.modifiers
+ if (isCtrl) {
+ lightPosition += Vector3(0.0, 0.0, it.rotation.y * 0.1)
+ } else {
+ cameraPosition += Vector3(0.0, 0.0, it.rotation.y * 0.1)
+ }
+ }
+ mouse.buttonUp.listen {
+ actionType = null
+ lastPos = null
+ }
- // drawer.fill = ColorRGBa.PINK
- // drawer.circle(
- // cos(seconds) * width / 2.0 + width / 2.0,
- // sin(0.5 * seconds) * height / 2.0 + height / 2.0,
- // 140.0
- // )
+ val noiseBuffer = colorBuffer(width, height, type = ColorType.UINT8)
+ val noiseFilter = SimplexNoise3D()
- // drawer.fontMap = font
- // drawer.fill = ColorRGBa.WHITE
- // drawer.text("OPENRNDR", width / 2.0, height / 2.0)
+ extend {
+ noiseFilter.seed = Vector3(0.0, 0.0, 0.1)
+ noiseFilter.scale = Vector3.ONE * 1.0
+ noiseFilter.octaves = 8
+ noiseFilter.premultipliedAlpha = true
+ noiseFilter.apply(emptyArray(), noiseBuffer)
+ cs.image("noise_texture", 0, noiseBuffer.imageBinding(0, ImageAccess.READ))
cs.uniform("seconds", seconds)
- cs.image("outputImg", 1, outputBuffer.imageBinding(0, ImageAccess.WRITE))
+ cs.uniform("camera_position", cameraPosition)
+ cs.uniform("primary_light_position", lightPosition)
+ cs.image("output_image", 1, outputBuffer.imageBinding(0, ImageAccess.WRITE))
cs.execute(outputBuffer.width, outputBuffer.height, 1)
drawer.image(outputBuffer)
+
+ drawer.fontMap = font
+ drawer.fill = ColorRGBa.WHITE
+ drawer.text(
+ "C: %.2f %.2f %.2f".format(cameraPosition.x, cameraPosition.y, cameraPosition.z),
+ 8.0,
+ 16.0
+ )
+ drawer.text(
+ "L: %.2f %.2f %.2f".format(lightPosition.x, lightPosition.y, lightPosition.z),
+ 8.0,
+ 32.0
+ )
}
}
}