Flutter着色器报错:syntax error, unexpected VEC3, expecting COMMA or SEMICOLON
错误原因排查与解决方法
核心语法错误
报错提示的syntax error, unexpected VEC3, expecting COMMA or SEMICOLON来自两处问题:
- 缺失分号:
float y = pow(st.x, 5)这行末尾未添加分号,GLSL要求所有语句必须以分号结尾,编译器解析到下一行的vec3 line时无法识别前序语句结束,因此抛出语法错误。 - 变量重复定义:main函数中先后两次声明了
float y,GLSL不允许同一作用域内重复定义同名变量。
修复后的完整代码
#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() { vec3 white = vec3(1.0); // White vec3 black = vec3(0.0); // Black vec3 red = vec3(1.0, 0.0, 0.0); // Red vec3 green = vec3(0.0, 1.0, 0.0); // Green vec3 blue = vec3(0.0, 0.0, 1.0); // Blue // Get the rgba values of the color vec3 color = blue; // Multiply each color component by an alpha value float alpha = 0.5; vec3 colorWithAlpha = color * alpha; float y = FlutterFragCoord().y / uResolution.y; vec3 background = vec3(y); // Normalized coordinates vec2 st = FlutterFragCoord() / uResolution; // 修复:替换重复变量名并添加分号 float curveY = pow(st.x, 5); // Create a line at the center of the screen vec3 line = mix(background, colorWithAlpha, plot(st, curveY)); // Output the final color with premultiplied alpha fragColor = vec4(line, alpha); }
关键修复点
- 给
pow(st.x, 5)语句末尾添加分号,补全GLSL语法要求; - 将重复定义的
float y改为新变量名(如curveY),避免作用域内变量名冲突; - 优化
plot函数参数,直接使用已定义的st变量,减少重复计算。
内容的提问来源于stack exchange,提问作者Code on the Rocks
相关产品推荐
相关产品推荐

