[orx-shader-phrases] Add shader phrases tweaks (#196)
This commit is contained in:
@@ -2,6 +2,7 @@ package org.openrndr.extra.shaderphrases
|
||||
|
||||
import mu.KotlinLogging
|
||||
import org.openrndr.draw.Shader
|
||||
import org.openrndr.extra.shaderphrases.ShaderPhraseRegistry.getGLSLFunctionName
|
||||
//import org.openrndr.extra.shaderphrases.phrases.phraseTbnMatrix
|
||||
import org.openrndr.utils.url.textFromURL
|
||||
|
||||
@@ -16,16 +17,9 @@ class ShaderPhrase(val phrase: String) {
|
||||
* This will likely be called by [ShaderPhraseBook]
|
||||
*/
|
||||
fun register(bookId: String? = null) {
|
||||
val functionRex =
|
||||
Regex("(float|int|[bi]?vec2|[bi]?vec3|[bi]?vec4|mat3|mat4)[ ]+([a-zA-Z0-9_]+)[ ]*\\(.*\\).*")
|
||||
val defs = phrase.split("\n").filter {
|
||||
functionRex.matches(it)
|
||||
}.take(1).mapNotNull {
|
||||
val m = functionRex.find(it)
|
||||
m?.groupValues?.getOrNull(2)
|
||||
}
|
||||
val id = defs.firstOrNull() ?: error("no function body found in phrase")
|
||||
ShaderPhraseRegistry.registerPhrase("${bookId?.let { "$it." } ?: ""}$id", this)
|
||||
val id = getGLSLFunctionName(phrase)
|
||||
val prefix = bookId?.let { "$it." } ?: ""
|
||||
ShaderPhraseRegistry.registerPhrase("$prefix$id", this)
|
||||
}
|
||||
}
|
||||
/**
|
||||
@@ -61,6 +55,22 @@ object ShaderPhraseRegistry {
|
||||
}
|
||||
return phrase
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the first GLSL function name out of GLSL source code
|
||||
*/
|
||||
fun getGLSLFunctionName(glsl: String): String {
|
||||
val functionRex =
|
||||
Regex("""\s*(float|int|[bi]?vec[234]|mat[34])\s+(\w+)\s*\(.*\).*""")
|
||||
val defs = glsl.split("\n").filter {
|
||||
functionRex.matches(it)
|
||||
}.take(1).mapNotNull {
|
||||
val m = functionRex.find(it)
|
||||
m?.groupValues?.getOrNull(2)
|
||||
}
|
||||
return defs.firstOrNull()
|
||||
?: error("no function body found in phrase")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,10 +84,10 @@ fun preprocessShader(source: String, symbols: Set<String> = emptySet()): String
|
||||
newSymbols.addAll(symbols)
|
||||
|
||||
val lines = source.split("\n")
|
||||
val processed = lines.mapIndexed { index, it ->
|
||||
if (it.startsWith("#pragma import")) {
|
||||
val tokens = it.split(" ")
|
||||
val symbol = tokens[2].trim().replace(";", "")
|
||||
val funcName = Regex("""^\s*#pragma\s+import\s+([a-zA-Z0-9_.]+)""")
|
||||
val processed = lines.map { line ->
|
||||
if (line.contains("#pragma")) {
|
||||
val symbol = funcName.find(line)?.groupValues?.get(1) ?: return@map line
|
||||
val fullTokens = symbol.split(".")
|
||||
val fieldName = fullTokens.last().replace(";", "").trim()
|
||||
val packageClassTokens = fullTokens.dropLast(1)
|
||||
@@ -90,7 +100,7 @@ fun preprocessShader(source: String, symbols: Set<String> = emptySet()): String
|
||||
""
|
||||
}
|
||||
} else {
|
||||
it
|
||||
line
|
||||
}
|
||||
}
|
||||
return processed.joinToString("\n")
|
||||
|
||||
Reference in New Issue
Block a user