如何在GLSL中创建一个“均匀”的渐变?

huangapple go评论55阅读模式
英文:

How to make a " homogeneous" gradient in glsl?

问题

#version 450

layout(binding=0) uniform UniformBufferObject {
    mat4 model;
    mat4 view;
    mat4 proj;
} ubo;

layout(location=0) in vec2 inPosition;
layout(location=1) in vec3 inColor;
layout(location=2) in vec2 inUV;

layout(location=0) out vec3 fragColor;

vec3 azure = { 0.0f, 0.5f, 0.5f };
vec3 blue = { 0.0f, 0.0f, 1.0f };
vec3 green = { 0.0f, 1.0f, 0.0f };

float translate(float i) {
    return i + 0.5;
}

float helpfunc(int x) {
    return mix(mix(blue[x], green[x], translate(inPosition.y)), mix(azure[x], azure[x], translate(inPosition.y)), translate(inPosition.x));
}

void main() {
    gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0);
    fragColor = vec3(helpfunc(0), helpfunc(1), helpfunc(2));
}
英文:

I don't know what is the best description for my target effect, but I want it to look this:
如何在GLSL中创建一个“均匀”的渐变?

I made these two attempts, where the color around diagonal line is more close to the right-down vertex.

<img src="https://i.stack.imgur.com/CBuSQ.jpg" width="300" height="300">
<img src="https://i.stack.imgur.com/GbCAJ.jpg" width="300" height="300">

#version 450

layout(binding=0) uniform UniformBufferObject {
    mat4 model;
    mat4 view;
    mat4 proj;
} ubo;

layout(location=0) in vec2 inPosition;
layout(location=1) in vec3 inColor;
layout(location=2) in vec2 inUV;

layout(location=0) out vec3 fragColor;

vec3 azure = { 0.0f, 0.5f, 0.5f };
vec3 blue = { 0.0f, 0.0f, 1.0f };
vec3 green = { 0.0f, 1.0f, 0.0f };

float translate(float i) {
    return i + 0.5;
}

float helpfunc(int x) {
    return mix(mix(blue[x], green[x], translate(inPosition.y)), mix(azure[x], azure[x], translate(inPosition.y)), translate(inPosition.x));
}

void main() {
    gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0);
    fragColor = vec3(helpfunc(0), helpfunc(1), helpfunc(2));
} 

I thought that this might be leaded by the color space, yet since after exchanging the position of blue and green in function helpfunc, the color of the distinctive diagonal line changed, not the distinctive diagonal line postion. I guess the problem is in algorithm, but I can't figure out what it is or how to solve it.

答案1

得分: 1

感谢krOoze,我找到了解决我的问题的方法,因为我不知道它是否是最佳解决方案。
我所做的唯一一件事是将顶点着色器中的"计算颜色"步骤移到片段着色器中。
以下是我的代码:

// 顶点着色器
#version 450

layout(binding = 0) uniform UniformBufferObject {
    mat4 model;
    mat4 view;
    mat4 proj;
} ubo;

layout(location = 0) in vec2 inPosition;
layout(location = 1) in vec3 inColor;
layout(location = 2) in vec2 inUV;

layout(location = 0) out vec2 outPosition;

void main() {
    gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0., 1.);
    outPosition = inPosition;
}

// 片段着色器
#version 450

layout(location = 0) in vec2 outPosition;

layout(location = 0) out vec4 outColor;

vec3 red = {1.f, 0.f, 0.f};
vec3 blue = {0.f, 0.f, 1.f};
vec3 green = {0.f, 1.f, 0.f};
vec3 white = {1.f, 1.f, 1.f};

float translate(float i) {
    return i + .5;
}

float helpfunc(int x) {
    return mix(mix(blue[x], green[x], translate(outPosition.y)), mix(white[x], red[x], translate(outPosition.y)), translate(outPosition.x));
}

void main() {
    outColor = vec4(helpfunc(0), helpfunc(1), helpfunc(2), 1.);
}

这是我的结果:
如何在GLSL中创建一个“均匀”的渐变?

英文:

thks to krOoze, i find a way to solve my problem, since idk if it is the best solution.
the only thing i do is put the step "calculate the color" from vertex shader into frag shader.
here's my code:

//vertex shader
#version 450

layout(binding=0)uniform UniformBufferObject{
    mat4 model;
    mat4 view;
    mat4 proj;
    
}ubo;

layout(location=0)in vec2 inPosition;
layout(location=1)in vec3 inColor;
layout(location=2)in vec2 inUV;

layout(location=0)out vec2 outPosition;

void main(){
    gl_Position=ubo.proj*ubo.view*ubo.model*vec4(inPosition,0.,1.);
    outPosition=inPosition;
}
//frag shader
#version 450

layout(location=0)in vec2 outPosition;

layout(location=0)out vec4 outColor;

vec3 red={1.f,0.f,0.f};
vec3 blue={0.f,0.f,1.f};
vec3 green={0.f,1.f,0.f};
vec3 white={1.f,1.f,1.f};

float translate(float i){
    return i+.5;
}
float helpfunc(int x){
    return mix(mix(blue[x],green[x],translate(outPosition.y)),mix(white[x],red[x],translate(outPosition.y)),translate(outPosition.x));
}
void main(){
    outColor=vec4(helpfunc(0),helpfunc(1),helpfunc(2),1.);
}

and here is my outcome :
如何在GLSL中创建一个“均匀”的渐变?

huangapple
  • 本文由 发表于 2023年2月16日 16:58:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定