gpt4 book ai didi

glsl - 我如何标准化在 glsl 中绘制的圆,这样分辨率就不会拉伸(stretch)?

转载 作者:行者123 更新时间:2023-12-05 02:01:56 26 4
gpt4 key购买 nike

我正在尝试使用 glsl 围绕特定 Sprite 创建一个圆圈。然而,分辨率是矩形 (800x600),这会导致一个略微拉伸(stretch)的圆圈:

screenshot of the stretched circle around a sprite

我试过答案here ,但我不知道如何将它们正确应用到我的特定着色器。我需要做什么

#define SHADER_NAME SPOTLIGHT_FS
precision highp float;

uniform sampler2D uMainSampler;
varying vec2 outTexCoord;
// coord is the non-normalized position if the sprite that the circle follows
uniform vec2 coord;
vec2 px = vec2(1.0/800.0, 1.0/600.0);

float getDistance(vec2 pixelCoord, vec2 playerCoord) {
// pixelCoord is normalized, but playerCoord is passed in as-is
return distance(pixelCoord, playerCoord*px);
}

void main( void )
{
if (getDistance(outTexCoord, coord) > 0.125) {
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
else {
gl_FragColor = texture2D(uMainSampler, outTexCoord);
}
}

最佳答案

您必须考虑视口(viewport)的纵横比。例如:

vec2 px = vec2(1.0/800.0, 1.0/600.0);
float aspect = 800.0 / 600.0;

float getDistance(vec2 pixelCoord, vec2 playerCoord) {
vec2 p = playerCoord * px * vec2(1.0, 1.0/aspect);
// pixelCoord is normalized, but playerCoord is passed in as-is
return distance(pixelCoord, p);
}

或者,您可以以像素为单位计算距离:

vec2 resolution = vec2(800.0, 600.0);
float radius = 100.0; // radius in pixel

float getDistance(vec2 pixelCoord, vec2 playerCoord) {
return distance(pixelCoord*resolution, playerCoord);
}

void main( void )
{
if (getDistance(outTexCoord, coord) > radius) {
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
else {
gl_FragColor = texture2D(uMainSampler, outTexCoord);
}
}

关于glsl - 我如何标准化在 glsl 中绘制的圆,这样分辨率就不会拉伸(stretch)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66191819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com