OpenGL: Circle bevel with fragment shader?

倖福魔咒の 提交于 2019-12-10 18:17:55

问题


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:

And here's what it currently looks like:

Not very good.

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.


回答1:


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;


来源:https://stackoverflow.com/questions/40947092/opengl-circle-bevel-with-fragment-shader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!