You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Shaders报错:‘exp’无匹配重载函数

Flutter着色器:‘exp’函数匹配错误解决办法

问题描述

跟随《The Book of Shaders》学习Flutter着色器时,遇到编译错误:‘exp’ : no matching overloaded function found,涉及的着色器代码如下:

#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);
}

问题根源

GLSL中的exp()函数仅用于计算自然指数(e的x次方),只接受1个参数。而你代码里想用两个参数实现幂运算(底数^指数),这是pow()函数的功能,exp()不支持这种用法,因此触发匹配错误。

修复方案

将代码中的exp(normalizedCoords.x, 5)替换为pow(normalizedCoords.x, 5.0)。注意指数参数最好用浮点型(5.0而非5),避免隐式类型转换可能带来的问题。

修复后的完整代码:

#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 = pow(normalizedCoords.x, 5.0);

    // 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);
}

内容的提问来源于stack exchange,提问作者Code on the Rocks

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 23:11:14