我尝试按照 The Book of Shaders 中的示例来学习 Flutter 中的着色器,但出现以下错误:
'exp':找不到匹配的重载函数
这是我的着色器:
#include <flutter/runtime_effect.glsl>
// Uniform variables passed from the application
uniform vec2 uResolution; // The resolution of the screen (width, height)
uniform float uTime; // The elapsed time in seconds
uniform vec2 uMouse; // The mouse position (x, y)
// Output color of the fragment
out vec4 fragColor;
// Function to plot a line on the Y-axis using a value between 0.0 and 1.0
float plot(vec2 normalizedCoords, float lineYPosition) {
// Create a smooth transition around the lineYPosition
return smoothstep(lineYPosition - 0.02, lineYPosition, normalizedCoords.y)
- smoothstep(lineYPosition, lineYPosition + 0.02, normalizedCoords.y);
}
void main() {
// Normalize the fragment coordinates to the range [0.0, 1.0]
vec2 normalizedCoords = FlutterFragCoord().xy / uResolution;
// Calculate the y position using a power function to create a curve
float y = exp(normalizedCoords.x, 5);
// Initialize the color vector with the calculated y value
vec3 color = vec3(y);
// Plot a line at the calculated y position
float lineYPosition = plot(normalizedCoords, y);
// Blend the line color (green) with the base color
color = (1.0 - lineYPosition) * color + lineYPosition * vec3(0.0, 1.0, 0.0);
// Output the final color with premultiplied alpha
fragColor = vec4(color, 1.0);
}
问题是 exp() 采用单个参数,因此您不能像文本所示那样在着色器中替换它。更新后的着色器可以正常工作:
#include <flutter/runtime_effect.glsl>
// Uniform variables passed from the application
uniform vec2 uResolution; // The resolution of the screen (width, height)
uniform float uTime; // The elapsed time in seconds
uniform vec2 uMouse; // The mouse position (x, y)
// Output color of the fragment
out vec4 fragColor;
// Function to plot a line on the Y-axis using a value between 0.0 and 1.0
float plot(vec2 normalizedCoords, float lineYPosition) {
// Create a smooth transition around the lineYPosition
return smoothstep(lineYPosition - 0.02, lineYPosition, normalizedCoords.y)
- smoothstep(lineYPosition, lineYPosition + 0.02, normalizedCoords.y);
}
void main() {
// Normalize the fragment coordinates to the range [0.0, 1.0]
vec2 normalizedCoords = FlutterFragCoord().xy / uResolution;
// Calculate the y position using a power function to create a curve
float y = exp(normalizedCoords.x * .5) * 0.5;
// Initialize the color vector with the calculated y value
vec3 color = vec3(y);
// Plot a line at the calculated y position
float lineYPosition = plot(normalizedCoords, y);
// Blend the line color (green) with the base color
color = (1.0 - lineYPosition) * color + lineYPosition * vec3(0.0, 1.0, 0.0);
// Output the final color with premultiplied alpha
fragColor = vec4(color, 1.0);
}