From f4ec01f09dbe4b731bee44e12d2c1f93268ca705 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Fri, 11 Sep 2026 17:25:39 +0530 Subject: Overdid the raymarch experiment --- src/main/kotlin/phenax/utils/program.kt | 36 ++++++++++++-- src/main/kotlin/play/PlayProgram.kt | 84 ++++++++++++++++++++++++++------- 2 files changed, 99 insertions(+), 21 deletions(-) (limited to 'src/main/kotlin') 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 = 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 { 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 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 + ) } } } -- cgit v1.3.1