0

我正在尝试将圆形 SDF 绘制到渲染纹理,只是一个 64 半径的圆形 SDF,以鼠标位置为中心。我已经设置了代码来将渲染纹理的内容渲染到屏幕上,它是 1366x768,与渲染纹理大小相同。我像这样设置渲染纹理:lighting_buffer = LoadRenderTexture(screen_width, screen_height);

我没有从 Raylib 得到任何错误。据我所知,生成的圆圈比它应该的要大得多,并且扭曲了,它比它的高度更宽。

我录制了一段视频,展示了当我在鼠标周围移动时生成的渲染纹理的样子:https ://youtu.be/fXxpW-UwYJo

这是我得到的渲染纹理内容的输出(鼠标的坐标约为 511x466):

在此处输入图像描述

这是我的像素着色器代码(使用默认顶点着色器)

// Input vertex attributes (from vertex shader)
in vec2 fragTexCoord;
in vec4 fragColor;

// Output fragment color
out vec4 finalColor; 

uniform vec2 position;
uniform float radius;
uniform vec4 colour;
uniform float intensity;

void main()   
{
    vec2 circle_pos = position;
    circle_pos.y = 768 - position.y;

    vec2 sample_pos = (fragTexCoord * vec2(1366, 768)) - circle_pos;
    float sdf = length(sample_pos) - radius;
    finalColor = vec4(sdf,sdf,sdf,1);
}

这是我的渲染代码:

void render_light(Vector2 position, float radius, Vector4 colour, float intensity) {
    
    SetShaderValueV(shader_lighting, GetShaderLocation(shader_lighting, "position"), (const void *)&position, RL_SHADER_UNIFORM_VEC2, 1);
    SetShaderValue(shader_lighting, GetShaderLocation(shader_lighting, "radius"), (const void *)&radius, RL_SHADER_UNIFORM_FLOAT);
    SetShaderValueV(shader_lighting, GetShaderLocation(shader_lighting, "colour"), (const void *)&colour, RL_SHADER_UNIFORM_VEC4, 1);
    SetShaderValue(shader_lighting, GetShaderLocation(shader_lighting, "intensity"), (const void *)&intensity, RL_SHADER_UNIFORM_FLOAT);
    
    Rectangle rect = { 0, 0, screen_width, screen_height };
    DrawRectangleRec(rect, RAYWHITE);  
}

void process_lighting() {
    BeginShaderMode(shader_lighting);
    BeginTextureMode(lighting_buffer);
    ClearBackground(BLACK);
    
    render_light({mouse_pos.x, mouse_pos.y}, 64.0f, {1.0f, 0.0f, 0.0f, 1.0f}, 5.0f);
    
    EndTextureMode();
    EndShaderMode();
}
4

0 回答 0