Files
orx/orx-shader-phrases
Edwin Jakobs b74ad477c7 [orx-shader-phrases] Add common SDF utility phrases
Introduce several new shader utility phrases for Signed Distance Functions (SDF), including operations (e.g., union, intersection, XOR), primitive shapes (e.g., box, circle, heart, segment), and supporting functions like `dot2`.
2025-03-10 11:03:02 +01:00
..

orx-shader-phrases

A library that provides a #pragma import statement for shaders.

Usage

Work in progress.

We can use the preprocessShader() function to resolve #pragma import statements.

    val preprocessedSource = preprocessShader(originalSource)

Alternatively loading and preprocessing can be combined in a single function call.

    val preprocessedSource = preprocessShaderFromUrl(resourceUrl("/some-shader.frag"))

Example

import org.openrndr.application
import org.openrndr.draw.shadeStyle
import org.openrndr.extra.shaderphrases.ShaderPhrase
import org.openrndr.extra.shaderphrases.ShaderPhraseBook
import org.openrndr.extra.shaderphrases.preprocessShader

// 1. Define GLSL functions to reuse in multiple files or programs.
// Typically these will be larger blocks of code.
// Note that the Kotlin variable name do not matter, but the used GLSL function name must match the #pragma import.
class ColorShaderPhrases : ShaderPhraseBook("colors") {
    val fRed = ShaderPhrase(
        "vec3 red() { return vec3(1.0, 0.0, 0.0); }"
    )

    val fGreen = ShaderPhrase(
        "vec3 green() { return vec3(0.0, 1.0, 0.0); }"
    )

    val fBlue = ShaderPhrase(
        "vec3 blue() { return vec3(0.0, 0.0, 1.0); }"
    )
}

fun main() = application {
    program {
        // 2. Make defined GLSL functions available
        ColorShaderPhrases().register()

        extend {
            drawer.shadeStyle = shadeStyle {
                // 3. Import the GLSL functions needed in this program
                fragmentPreamble = preprocessShader("""
                    #pragma import colors.red
                    #pragma import colors.blue
                """.trimIndent())

                // 4. Make use of the available GLSL functions
                fragmentTransform = """
                    x_stroke.rgb = red();
                    x_fill.rgb = blue();
                """.trimIndent()
            }
            drawer.circle(drawer.bounds.center, 100.0)
        }
    }
}