在球体上绘制圆
我正在尝试使用着色器在一个球体上绘制很多圆.基本的用法是这样的:
I'm trying to draw lots of circles on a sphere using shaders. The basic alogrith is like this:
- 计算片段(使用其纹理坐标)到圆心位置(圆心也在纹理坐标中指定)的距离
- 计算从炸药到圆心的角度.
- 根据角度,访问纹理(其中有360个像素,红色通道指定半径距离),并获取给定角度的半径
- 如果片段到圆心的距离小于检索到的半径,则片段的颜色为红色,否则为蓝色.
我想在蓝色球体上绘制60个红色圆圈.我让y着色器工作了一圈,但是怎么做60个呢?到目前为止,这是我尝试过的....
I would like to draw ... say 60 red circles on a blue sphere. I got y shader to work for one circle, but how to do 60? Here's what I've tried so far....
-
我传入了一个数据纹理,该纹理指定了给定角度的半径,但是我注意到了伪影.我相信这是由于当我尝试使用以下方法从数据纹理中检索信息时进行的线性插值: >
I passed in a data texture that specifies the radius for a given angle, but I notice artifacts creep in. I believe this is due to linear interpolation when I try to retrieve information from the data texture using:
float returnV = texture2D(angles, vec2(x, y)).r;
其中angles是包含给定角度的半径的数据纹理(Sampler2D),x =角度/360.0(角度为0到360),y = 0到60(y是圆数)
where angles is the data texture (Sampler2D) that contains the radius for a given angle, and x = angle / 360.0 (angle is 0 to 360) and y = 0 to 60 (y is the circle number)
我尝试传递统一浮点半径[360],但无法使用动态索引访问半径.我什至尝试过这个烂摊子...
I tried passing in a Uniform float radii[360], but I cannot access radii with dynamic indexing. I even tried this mess ...
getArrayValue(int index) {
if (index == 0) {
return radii[0];
}
else if (index == 1) {
return radii[1];
}
以此类推...
如果我创建一个纹理并将所有圆放置在该纹理上,然后将蓝色球与包含该圆的蓝色球进行多重纹理化,但是正如您所期望的那样,我的别名确实很糟糕.我喜欢基于片段的位置和圆的位置以程序方式生成圆的想法,因为实际上没有混叠.但是,我要比一个做矿石吗?
If I create a texture and place all of the circles on that texture and then multi-texture the blue sphere with the one containing the circles it works, but as you would expect, I have really bad aliasing. I like the idea of proceduraly generating the circles based on the position of the fragment and that of the circle because of virtually no aliasing. However, I do I do ore than one?
谢谢!
〜螺栓
我有一个可以在地形上画圆的着色器.它通过鼠标移动. 也许您会得到灵感?
i have a shader that makes circle on the terrain. It moves by the mouse moves. maybe you get an inspiration?
这是一个片段程序.它不是主程序,但是您可以将其添加到程序中. 试试这个...
this is a fragment program. it is not the main program but you can add it to your program. try this...
现在,您可以使用硬编码提供一些统一的参数.
for now you can give some uniform parameters in hardcode.
uniform float showCircle;
uniform float radius;
uniform vec4 mousePosition;
varying vec3 vertexCoord;
void calculateTerrainCircle(inout vec4 pixelColor)
{
if(showCircle == 1)
{
float xDist = vertexCoord.x - mousePosition.x;
float yDist = vertexCoord.y - mousePosition.y;
float dist = xDist * xDist + yDist * yDist;
float radius2 = radius * radius;
if (dist < radius2 * 1.44f && dist > radius2 * 0.64f)
{
vec4 temp = pixelColor;
float diff;
if (dist < radius2)
diff = (radius2 - dist) / (0.36f * radius2);
else
diff = (dist - radius2) / (0.44f * radius2);
pixelColor = vec4(1, 0, 0, 1.0) * (1 - diff) + pixelColor * diff;
pixelColor = mix(pixelColor, temp, diff);
}
}
}
,然后在顶点着色器中添加:
and in vertex shader you add:
varying vec3 vertexCoord;
void main()
{
gl_Position = ftransform();
vec4 v = vec4(gl_ModelViewMatrix * gl_Vertex);
vertexCoord = vec3(gl_ModelViewMatrixInverse * v);
}