本文介绍了具有固定级别数的渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我画了一组四边形.对于每个四边形,我在其顶点都有定义的颜色.

I drawing a set of quads. For each quad I have a defined color in a vertex of it.

例如现在我的四边形集如下:

我以相当原始的方式获得了这种结果,只是将其作为四边形每个顶点的属性颜色传递到顶点着色器中.

I achive such result in rather primitive way just passing into vertex shader as attribute color of each vertex of an quad.

我的着色器非常简单:

顶点着色器

#version 150 core

in vec3 in_Position;
in vec3 in_Color;
out vec3 pass_Color;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

void main(void) {
    gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(in_Position, 1.0);

    pass_Color = in_Color;
}

片段着色器

#version 150 core
in vec3 pass_Color;
out vec4 out_Color;

void main(void) {
    out_Color = vec4(pass_Color, 1.0);
}

现在,我的目标是使颜色从顶点到顶点的非连续分布.也可以称为级别分布".

Now my goal is to get color non continuous distribution of colors from vertex to vertex. It also can be called "level distribution".

我的四边形集应如下所示:

我如何获得这样的结果?

How can I achieve such result?

板看起来像这样. (不可接受的变体)

With vesan and Nico Schertler plate looks like this. (not acceptable variant)

推荐答案

我的猜测是您使用的色相颜色和顶点插值(例如跳过某些频段)会出现问题.相反,也许传入单个通道值并计算片段着色器中的色相和离散级别(如@vesan所做的那样).我自己使用这些功能...

My guess is there will be issues with the hue colours you're using and vertex interpolation (e.g. skipping some bands). Instead, maybe pass in a single channel value and calculate the hue and discrete levels (as @vesan does) within the fragment shader. I use these functions myself...

vec3 hueToRGB(float h)
{
    h = fract(h) * 6.0;
    vec3 rgb;
    rgb.r = clamp(abs(3.0 - h)-1.0, 0.0, 1.0);
    rgb.g = clamp(2.0 - abs(2.0 - h), 0.0, 1.0);
    rgb.b = clamp(2.0 - abs(4.0 - h), 0.0, 1.0);
    return rgb;
}

vec3 heat(float x)
{
    return hueToRGB(2.0/3.0-(2.0/3.0)*clamp(x,0.0,1.0));
}

然后

float discrete = floor(pass_Value * steps + 0.5) / steps; //0.5 to round
out_Color = vec4(heat(discrete), 1.0);

其中in float in_Value是0到1.

这篇关于具有固定级别数的渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 20:01