actual Godot compatibility changes

This commit is contained in:
2024-03-31 22:11:55 -06:00
parent 893be91d12
commit 3741e0eecf
3 changed files with 46 additions and 22 deletions

View File

@@ -102,6 +102,7 @@ class ShaderFrontend implements FrontendPlugin {
var colorOut = "";
var coordIn = "";
transformedCode += "const float PI = 3.1415926535897932384626433832795;\n";
function nextToken() {
glslStream.dropWhitespace();
@@ -132,36 +133,59 @@ class ShaderFrontend implements FrontendPlugin {
default:
}
// Shadertoy compatibility:
case Some("iResolution"):
transformedCode += "openfl_TextureSize";
case Some("mainImage"):
dropNext("(");
dropNext("out");
dropNext("vec4");
switch (nextToken()) {
case Some(symbol):
colorOut = symbol;
default:
error();
}
dropNext(",");
dropNext("in");
dropNext("vec2");
switch (nextToken()) {
case Some(symbol):
coordIn = symbol;
default:
error();
}
dropNext(")");
transformedCode += "main() ";
case Some("void"):
switch (nextToken()) {
case Some("mainImage"):
dropNext("(");
dropNext("out");
dropNext("vec4");
switch (nextToken()) {
case Some(symbol):
colorOut = symbol;
default:
error();
}
dropNext(",");
dropNext("in");
dropNext("vec2");
switch (nextToken()) {
case Some(symbol):
coordIn = symbol;
default:
error();
}
dropNext(")");
transformedCode += "void main() ";
case Some("fragment"):
dropNext("()");
transformedCode += "void main() ";
case Some(other):
transformedCode += 'void $other';
default:
throw "expected name of void function";
}
case Some(name) if (name == colorOut):
transformedCode += "gl_FragColor";
case Some(name) if (name == coordIn):
transformedCode += "openfl_TextureCoordv * openfl_TextureSize";
// Godot compatibility
case Some("TIME"):
transformedCode += "iTime";
case Some("COLOR"):
transformedCode += "gl_FragColor";
// Not totally sure this actually is a 1-to-1 equivalency:
case Some("SCREEN_UV"):
transformedCode += "openfl_TextureCoordv";
case Some("SCREEN_PIXEL_SIZE"):
transformedCode += "vec2(1 / openfl_TextureSize.x, 1 / openfl_TextureSize.y)";
case Some("uniform"):
var uType = expect("uniform type", nextToken);

View File

@@ -0,0 +1,45 @@
#pragma header
// Source: https://godotshaders.com/shader/animated-mirrored-ornament/
// Modified by Nat
uniform vec3 color_a = vec3(0.5);
uniform vec3 color_b = vec3(0.5);
uniform vec3 color_c = vec3(1.0);
uniform vec3 color_d = vec3(0.0, 0.33, 0.67);
uniform int iterations = 10; // : hint_range(1, 50, 1)
uniform float speed = 1.0; // : hint_range(0.1, 10.0)
uniform float zoom = 1.0; // : hint_range(0.1, 5.0)
uniform float subtract = 0.5; // : hint_range(0.1, 1.0)
uniform float multiply = 1.1; // : hint_range(1.0, 2.0)
vec3 palette(float t, vec3 a, vec3 b, vec3 c, vec3 d) {
return a + b * cos(2.0 * PI * (c * t + d));
}
vec2 rotate(vec2 uv, float angle) {
return uv * mat2(
vec2(cos(angle), -sin(angle)),
vec2(sin(angle), cos(angle))
);
}
vec3 invert_color(vec3 color, float intensity) {
return mix(color.rgb, 1.0 - color.rgb, intensity);
}
void fragment() {
float time = TIME;
float angle = time * speed * 0.1;
vec2 uv = (SCREEN_UV - 0.5) / vec2(SCREEN_PIXEL_SIZE.x / SCREEN_PIXEL_SIZE.y, 1.0);
vec3 color = vec3(0.0);
uv /= zoom + sin(time * 0.1) * 0.5 + 0.5;
for (int i = 0; i < iterations; i++) {
uv = rotate((abs(uv) - subtract) * multiply, angle);
}
vec3 p = palette(length(uv) + dot(uv, uv), color_a, color_b, color_c, color_d);
color = clamp(vec3(length(uv) * p), vec3(0.0), vec3(1.0));
float intensity = sin(time) * 0.25 - 0.2;
color = invert_color(color, intensity);
COLOR = vec4(color, 1.0);
}