From 3482b596df06e70f1d0d0a82084f5549a631e89a Mon Sep 17 00:00:00 2001 From: Abe Pazos Date: Mon, 19 Feb 2024 16:46:13 +0000 Subject: [PATCH] [orx-shader-phrases] Update README.md Add an example, since there are no demos and it's not part of the guide. --- orx-shader-phrases/README.md | 52 +++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/orx-shader-phrases/README.md b/orx-shader-phrases/README.md index e7b1a398..2bb94094 100644 --- a/orx-shader-phrases/README.md +++ b/orx-shader-phrases/README.md @@ -4,7 +4,7 @@ A library that provides a `#pragma import` statement for shaders. ## Usage -Work in progress. +*Work in progress.* We can use the `preprocessShader()` function to resolve `#pragma import` statements. @@ -19,3 +19,53 @@ Alternatively loading and preprocessing can be combined in a single function cal val preprocessedSource = preprocessShaderFromUrl(resourceUrl("/some-shader.frag")) ``` +## Example + +```kotlin +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) + } + } +} +```