OpenGL:圆弧斜角与片段着色器? [英] OpenGL: Circle bevel with fragment shader?

查看:133
本文介绍了OpenGL:圆弧斜角与片段着色器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个圆形的粒子,看起来像是顶部有光.

I'm trying to make a circular particle that looks like it's got a light shining on the top of it.

这就是我想要使它看起来像的样子:

Here's what I'm trying to get it to look like:

这是当前的样子:

不是很好.

由于我使用的是GL_POINTS,因此我得到了gl_PointCoord变量,这会使事情变得更容易,但我不知道如何正确使用它,这会导致混乱:

Since I'm using GL_POINTS, I get the gl_PointCoord variable, which should make things easier, except I don't know how to use it properly, which led to this mess:

varying lowp vec4 DestinationColor;

void main(void) {
  lowp vec2 circCoord = 2.0 * gl_PointCoord - 1.0;
  if (dot(circCoord, circCoord) > 1.0) {
    discard;
  }
  gl_FragColor = mix(DestinationColor, vec4(1, 0.5, 0.2, 1), (1.0-gl_PointCoord.t)*(max(abs(gl_PointCoord.t-0.5),abs(gl_PointCoord.s-0.5)))); // the world's worst slowest math
}

如果有人可以给我一点帮助,我将非常感激,因为我自己的糟糕数学技能使我陷入困境.

I would highly appreciate if someone could give me a bit of help with this, as I'm stuck thanks to my own awful math skills.

推荐答案

您可以进行正确的全光照计算.例如,朗伯散射体的工作方式如下:

You can do a correct full lighting calculation. For example a lambertian diffuse would work like:

const vec3 lightDir = normalize(vec3(0, 1, -0.5));
const vec3 ambient = ...;
const vec3 lightDiffuse = ...;

vec3 normal = vec3(circCoord, sqrt(1 - dot(circCoord, circCoord)));
float c = max(dot(normal, lightDir), 0);
gl_FracColor = ambient + lightDiffuse*c;

这篇关于OpenGL:圆弧斜角与片段着色器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆